﻿/**
    global variables
*/
var dl_locationselector_loaded = false;
var dl_locationselector_loaded_footer = false;
/**
    mootools extended
*/
Element.extend(
{
    hide: function() 
    {
        return this.setStyle('display', 'none');
    },
    
    show: function() 
    {
        return this.setStyle('display', '');
    }
});
/**
    overlay bugfix ie6
*/
function rl_registerFixIE6Bug(id) {
    if(window.ie6){ 
        var iframe = new Element ( 'iframe' , { 'src' : 'about:blank', 'frameborder' : '0', 'class' : 'rl_dl_iframe' }).injectInside($(id));
    }
}
/**
    context menu (e.g. share)
*/
function rl_registerContextMenu(id) { 
    if ($(id)) {    
        
        var dl_con = id + '_dialog';
        var dl_menu = dl_con + '_menu';
        $(dl_con).hide();
        
        $(id).addEvent('mouseenter', function(e){
            $(id).toggleClass('active');
            $(dl_con).show();
            
            var saveHeight = $(dl_menu).getSize().scrollSize.y;
            var saveWidth = $(dl_menu).getSize().scrollSize.x;
            
            $(dl_menu).setStyle('top', '-' + saveHeight + 'px');
            $(dl_menu).setStyle('margin-top', '6px');
            rl_registerFixIE6Bug(dl_menu);
            $(dl_menu).show();
        
        });
        $(id).addEvent('mouseleave', function() {
            $(dl_con).hide();
            $(id).toggleClass('active');
        });
        
    }
}
/**
    location selector
*/
var myLSEffects = Fx.Style();
var showingParent;
var showingParentFooter;
function rl_registerLocationSelectorFooter() {
    if ($('rl_header_label_location_footer')) {
        $('rl_header_label_location_footer').addEvent('click', function(e){
                $('rl_dialog_locationselect_container_footer').toggleClass('active');
                var dl_status_footer = $('rl_dialog_locationselect_container_footer').getProperty('class');
                
                if (dl_locationselector_loaded_footer == false) {
                    $('rl_dialog_locationselect_container_footer').hide();
                    var myAjax = new Ajax(dl_locationselector, {
                        method: 'get', 
                        update:$('rl_dialog_locationselect_container_footer'),
                        onComplete: function() {
                            dl_locationselector_loaded_footer = true;
                            $('rl_dialog_locationselect_container_footer').show();
                            rl_registerLocationSelectorClickOutsideFooter();
                        }
                    }).request();
                } else {
                    $('rl_dialog_locationselect_container_footer').show();
                }
                if (dl_status_footer != "active") {
                     $('rl_dialog_locationselect_container_footer').hide();
                }
                $('rl_header_label_location_footer').toggleClass('active');
                
                showingParentFooter = this;
        });
    }
}
var myLSEffects = Fx.Style();
var showingParent;
function rl_registerLocationSelector() {
    if ($('rl_header_label_location')) {
        $('rl_header_label_location').addEvent('click', function(e){
                $('rl_dialog_locationselect_container').toggleClass('active');
                var dl_status = $('rl_dialog_locationselect_container').getProperty('class');
                
                if (dl_locationselector_loaded == false) {
                    $('rl_dialog_locationselect_container').hide();
                    var myAjax = new Ajax(dl_locationselector, {
                        method: 'get', 
                        update:$('rl_dialog_locationselect_container'),
                        onComplete: function() {
                            dl_locationselector_loaded = true;
                            $('rl_dialog_locationselect_container').show();
                            rl_registerFixIE6Bug('rl_dialog_locationselect');
                            myLSEffects = new Fx.Style($('rl_dialog_locationselect'), 'height', {duration: 500, transition: Fx.Transitions.Expo.easeOut});
                            myLSEffects.hide();
                            var saveHeight = $('rl_dialog_locationselect').getSize().scrollSize.y;
                            $('rl_dialog_locationselect').setProperty('rev', $('rl_dialog_locationselect').getStyle('height').toInt());
                            myLSEffects.start(0, saveHeight);
                            rl_registerLocationSelectorClickOutside();
                        }
                    }).request();
                } else {
                    // Save height
                    var saveHeight = $('rl_dialog_locationselect').getSize().scrollSize.y;
                    $('rl_dialog_locationselect').setProperty('rev', $('rl_dialog_locationselect').getStyle('height').toInt());
                    myLSEffects.start(0, saveHeight);
                }
                if (dl_status != "active") {
                    myLSEffects.stop();
                    myLSEffects.hide();
                }
                $('rl_header_label_location').toggleClass('active');
                
                showingParent = this;
        });
    }
}
/** 
    location selector: hide when clicked outside 
*/
function rl_registerLocationSelectorClickOutside() {
    $("html").addEvent('click', function(e) {
        var event = new Event(e);
    
        var parent = showingParent;
        check = event.target;
        while (check && check!= parent) {
            check = check.parentNode;
        }
        
        if (showingParent && !event.target || !check) {
            var dl_status = $('rl_dialog_locationselect_container').getProperty('class');
            if (dl_status == "active") {
                $('rl_header_label_location').fireEvent('click');
            }
        }
    });
}
function rl_registerLocationSelectorClickOutsideFooter() {
    $("html").addEvent('click', function(e) {
        var event = new Event(e);
        var parent = showingParentFooter;
        check = event.target;
        while (check && check!= parent) {
            check = check.parentNode;
        }
        if (showingParentFooter && !event.target || !check) {
            var dl_status_footer = $('rl_dialog_locationselect_container_footer').getProperty('class');
            if (dl_status_footer == "active") {
                $('rl_header_label_location_footer').fireEvent('click');
            }
        }
    });
}
/**
    dropdown menu
*/
var myEffects = Fx.Style();
var DropdownMenu = new Class({    
    initialize: function(element)
    {
        $A($(element).childNodes).each(function(el)
        {
            if(el.nodeName.toLowerCase() == 'li')
            {
                $A($(el).childNodes).each(function(el2)
                {
                    if(el2.nodeName.toLowerCase() == 'span')
                    {
                        $(el2).hide();
                        
                        el.addEvent('mouseenter', function(e)
                        {
                            // Remove all active classes and menu layers if they are still shown to prevent 
                            // double layers in menu structure since event handling in some browsers is buggy
                            $A($(element).getChildren()).each(function(otherel){
                                if(otherel.nodeName.toLowerCase() == 'li')
                                {
                                    if (otherel.hasClass('active')) {
                                        otherel.removeClass('active');
                                    }
                                    $A($(otherel).childNodes).each(function(otherel2)
                                    {
                                        if(otherel2.nodeName.toLowerCase() == 'span')
                                        {
                                            dl_child_id = $(otherel2.id + '_menu');
                                            if ($(dl_child_id)) {
                                                $(dl_child_id).setStyle('height', 0);
                                            }
                                        }
                                    });
                                }
                            });
                            $('rl_nav_dropdown_frame').removeClass('active');
                            dl_loaded = $(el2.id).getProperty('rel');
                                
                            if (!dl_loaded) {
                                var dl = dl_menu[el2.id];
                                if (dl != undefined) {
                                var myAjax = new Ajax(dl, {
                                    method: 'get', 
                                    update:$(el2.id),
                                    onComplete: function() {
                                        $(el2.id).setProperty('rel',1);
                                        dl_child_id = $(el2.id + '_menu');
                                        rl_registerFixIE6Bug(dl_child_id);
                                        myEffects = new Fx.Style(dl_child_id, 'height', {duration: 500, transition: Fx.Transitions.Expo.easeOut});
                                        
                                        // Only display dialog when tab is active after loading the dislog asynchonously
                                        if (el.hasClass('active')) {
                                            myEffects.hide();
                                            el2.show();
                                        }
                                            
                                        // Save height
                                        var saveHeight = dl_child_id.getSize().scrollSize.y;
                                        if (el.hasClass('active')) {
                                            myEffects.start(0, saveHeight);
                                        }
                                    }
                                }).request();
                                }
                            } else {
                                el2.show();
                                dl_child_id = $(el2.id + '_menu');
                                saveHeight = dl_child_id.getSize().scrollSize.y;
                                myEffects = new Fx.Style(dl_child_id, 'height', {duration: 500, transition: Fx.Transitions.Expo.easeOut});
                                myEffects.start(0, saveHeight);
                            }
                            
                            $(el2.id).getParent().toggleClass('active');
                            $('rl_nav_dropdown_frame').toggleClass('active');
                            return false;
                        });
                        el.addEvent('mouseleave', function()
                        {
                            // Only stop running effect, if it has already been registered on the asynchonously loaded dialog.
                            if ($(el2.id).getProperty('rel') == '1') {
                                myEffects.stop();
                                myEffects.hide();
                            }
                            $(el2.id).getParent().toggleClass('active');
                            $('rl_nav_dropdown_frame').toggleClass('active');
                            // Remove all active classes and menu layers if they are still shown to prevent 
                            // double layers in menu structure since event handling in some browsers is buggy
                            $A($(element).getChildren()).each(function(otherel){
                                if(otherel.nodeName.toLowerCase() == 'li')
                                {
                                    if (otherel.hasClass('active')) {
                                        otherel.removeClass('active');
                                    }
                                    $A($(otherel).childNodes).each(function(otherel2)
                                    {
                                        if(otherel2.nodeName.toLowerCase() == 'span')
                                        {
                                            dl_child_id = $(otherel2.id + '_menu');
                                            if ($(dl_child_id)) {
                                                $(dl_child_id).setStyle('height', 0);
                                            }
                                        }
                                    });
                                }
                            });
                            $('rl_nav_dropdown_frame').removeClass('active');
                        });
                        new DropdownMenu(el2);
                    }
                });
            }
        });
        return this;
    }
});
/**
    dropdown menu
*/
var qaEffects = Fx.Style();
function rl_loadQALayer(element) {
    $A($(element).getChildren()).each(function(el)
    {
        if(el.nodeName.toLowerCase() == 'li' && el.hasClass('rl_layer'))
        {
            $A($(el).childNodes).each(function(el2)
            {
                if(el2.nodeName.toLowerCase() == 'span')
                {
                    $(el2).hide();
                        
                    var dl = dl_qa_menu[el2.id];
                    var myAjax = new Ajax(dl, {
                        method: 'get', 
                        update: $(el2.id),
                        autoCancel: false,
                        onComplete: function() {
                            $(el2.id).setProperty('rel',1);
                            dl_child_id = $(el2.id + '_menu');
                            rl_registerFixIE6Bug(dl_child_id);
                            qaEffects = new Fx.Style(dl_child_id, 'height', {duration: 500, transition: Fx.Transitions.Expo.easeOut});
                            // Only display dialog when tab is active after loading the dialog asynchonously
                            if (el.hasClass('active')) {
                                el2.show();
                                qaEffects.hide();
                            }
                                            
                            // Save height
                            var saveHeight = dl_child_id.getSize().scrollSize.y;
                            if (el.hasClass('active')) {
                                qaEffects.start(0, saveHeight);
                            }
        
                            el.addEvent('mouseenter', function(e)
                            {
                                // Remove all active classes and menu layers if they are still shown to prevent 
                                // double layers in menu structure since event handling in some browsers is buggy
                                $A($(element).getChildren()).each(function(otherel){
                                    if(otherel.nodeName.toLowerCase() == 'li')
                                    {
                                        if (otherel.hasClass('active')) {
                                            otherel.removeClass('active');
                                        }
                                        $A($(otherel).childNodes).each(function(otherel2)
                                        {
                                            if(otherel2.nodeName.toLowerCase() == 'span')
                                            {
                                                dl_child_id = $(otherel2.id + '_menu');
                                                if ($(dl_child_id)) {
                                                    $(dl_child_id).setStyle('height', 0);
                                                }
                                            }
                                        });
                                    }
                                });
                                el2.show();
                                dl_child_id = $(el2.id + '_menu');
                                saveHeight = dl_child_id.getSize().scrollSize.y;
                                qaEffects = new Fx.Style(dl_child_id, 'height', {duration: 500, transition: Fx.Transitions.Expo.easeOut});
                                qaEffects.start(0, saveHeight);
                                $(el2.id).getParent().toggleClass('active');
                                return false;
                            });
                            el.addEvent('mouseleave', function()
                            {
                                // Only stop running effect, if it has already been registered on the asynchonously loaded dialog.
                                if ($(el2.id).getProperty('rel') == '1') {
                                    qaEffects.stop();
                                    qaEffects.hide();
                                }
                            
                                $(el2.id).getParent().toggleClass('active');
                                // Remove all active classes and menu layers if they are still shown to prevent 
                                // double layers in menu structure since event handling in some browsers is buggy
                                $A($(element).getChildren()).each(function(otherel){
                                    if(otherel.nodeName.toLowerCase() == 'li')
                                    {
                                        if (otherel.hasClass('active')) {
                                            otherel.removeClass('active');
                                        }
                                        $A($(otherel).childNodes).each(function(otherel2)
                                        {
                                            if(otherel2.nodeName.toLowerCase() == 'span')
                                            {
                                                dl_child_id = $(otherel2.id + '_menu');
                                                if ($(dl_child_id)) {
                                                    $(dl_child_id).setStyle('height', 0);
                                                }
                                            }
                                        });
                                    }
                                });
                            });
                            //new QAMenu(el2);
                        }
                    }).request();
                }
            });
        }
    });
    return this;
}

function rl_registerMainSlider(id, mode) {
    if ($(id)) {
        var slider_height = $(id).getProperty('rel');
        var handle_height = $(id).getProperty('rev');
        var es = mode;
        var previous_item;
        var current_item = $ES('div.rl_pane',id)[0];
        var all_items = $ES('div.rl_pane',id);
        if (all_items.length > 1) {
            all_items.setStyle('opacity', '0');
            current_item.setStyle('opacity', '1');
            
            var ns1 = new noobSlide({
                    box: $(id),
                    mode: 'vertical',
                    autoPlay: es,
                    interval: 6500,
                    items: all_items,
                    size: slider_height,
                    handles: $ES('div','rl_slider_handles'),
                    handle_event: 'click',
                    buttons: {
                        previous: $('rl_slider_prev'),
                        next: $('rl_slider_next')
                    },
                    button_event: 'click',
                    fxOptions: {
                        duration: 20,
                        transition: Fx.Transitions.Expo.easeOut,
                        wait: false,
                        fps: 50
                    },
                    onWalk: function(currentItem,currentHandle){
                        var previousHandle = currentHandle.getPrevious();
                        var allHandles = this.handles;
                                            
                        allHandles.removeClass('rl_active');
                        currentHandle.addClass('rl_active');
                        
                        current_item = currentItem;
                        if (!previous_item) {
                            previous_item = currentItem.getPrevious();
                        }
                        
                        var mySliderEffectPrevious = new Fx.Styles(previous_item, {duration: 200, transition: Fx.Transitions.linear, onComplete: function(){ 
                            var mySliderEffectCurrent = new Fx.Styles(current_item, {duration: 1300, transition: Fx.Transitions.linear});
                            mySliderEffectCurrent.start({
                                'opacity': 1
                            });
                        }});
                        mySliderEffectPrevious.start({
                            'opacity': 0
                        });
                        
                        previous_item = current_item;
                    }
                });
                ns1.walk(0);
                
                //add mousein/out behaviors for all slides
                $$('#' + id + ' div.rl_pane').addEvents({
                    'mouseenter':function(){
                        ns1.stop();
                    },
                    'mouseleave':function(){
                        ns1.play(6500,"next",true);
                    }
                });
        }
    }
}
function rl_registerFlatSlider(id, handleid) {
    if ($(id)) {
        var slider_height = $(id).getProperty('rel');
        var ns2 = new noobSlide({
                box: $(id),
                mode: 'horizontal',
                autoPlay: false,
                interval: 10000,
                items: $ES('div.rl_flat_pane_block',id),
                size: slider_height,
                buttons: {
                    previous: $('rl_slider_flat_prev_' + handleid),
                    next: $('rl_slider_flat_next_' + handleid)                
                },
                button_event: 'click',
                fxOptions: {
                    duration: 1000,
                    transition: Fx.Transitions.Expo.easeOut,
                    wait: false
                }
            });
            ns2.walk(0);
    }
}
function rl_updateSearchField(id) {
    $(id).setStyle('color', '#1a171b');
}
function bookmark(url,title) {
    var url = url;
    var title = title;
    if (document.all)
        window.external.AddFavorite(url, title);
    else if (window.sidebar)
        window.sidebar.addPanel(title, url, "");
}
function rl_functionEditSlider(id) {
    $$('#' + id + ' div.rl_thumb').addEvents({
        'click':function(){
            // Reset panes and thumbs
            $$('#' + id + ' div.rl_thumb').removeClass('rl_active');
            $$('#' + id + ' div.rl_pane').hide();
        
            var thumb_id = $(this).id;
            var thumb_rel = $(this).getProperty('rel');
            var pane_id = "rl_pane_" + thumb_rel;
            
            $(thumb_id).toggleClass('rl_active');
            $(pane_id).show();
        }
    });
}
function rl_openSlideToEdit(id, paneid) {
                        
    // Reset panes and thumbs
    $$('#' + id + ' div.rl_thumb').removeClass('rl_active');
    $$('#' + id + ' div.rl_pane').hide();
                        
    var thumb_id = 'rl_thumb_' + paneid;
    var pane_id = 'rl_pane_' + paneid;
                            
    $(thumb_id).toggleClass('rl_active');
    $(pane_id).show();
                                
}
/** 
    Cookie handling for location  selector
*/
function rl_openConfirmationDialog(title, url) {
    var cookie_confirmed = confirm(title);
    if (cookie_confirmed == true) {
        window.location = url;
    } else {
        window.location = url + '&nocookie=true';
    }
};
/**
    register functions
*/
window.addEvent('domready', function() {
    rl_registerLocationSelector();
    rl_registerLocationSelectorFooter();
    if ($('rl_nav_dropdown')) {
        new DropdownMenu($('rl_nav_dropdown'));
    }
    rl_registerContextMenu('rl_share');
});
