$(function(){
    // menu slider
    var selector = $('#menu-main-selector');
    selector.attr('style', '');
    startnav = $('#menu-main .active');

    function shiftnav(target, speed)
    {
        selector.clearQueue().animate({
            'width': target.width(),
            'left': target.position().left
        }, speed);
    }
    if(startnav.length != 0)
    {
        $('#menu-main li').mouseenter(function(){
            shiftnav($(this), 500);
        });
        $('#menu-main').mouseleave(function(){
            shiftnav(startnav, 500);
        });
        shiftnav(startnav, 0);
    }
    else
    {
        selector.hide();
    }

    // email suggestion
    $('input[name="email"]').each(function(){
    	var elem = $(this);
        var elemval = elem.val();
        if(elem.parent().children('.errors').size() == 0)
        {
        	elem.focus(function(){
                if(elem.val() == elemval) elem.val('');
            });
        	elem.blur(function(){
                if(elem.val() == '') elem.val(elemval);
            });
        }
    });

    // font resize
    var min = 11;
    var max = 20;
    var def = 25; // px
    var current_font;

    var fontreset = $('#fontreset');
    fontreset.css('display', 'none');
    fontreset.click(function(){
    	$.cookie('the_cookie2', def, { path: '/' });
        update_font(def);
        slider.slider('value', def);
        niceFade($(this), 'none');
    });

    function update_font(a)
    {
        range = (max-min) / 100;
        range = min + range * a;
        lh = range + 9;
        $.cookie('the_cookie', range, { path: '/' });
        $('#content').css('font-size', parseInt(range)+'px');
        
        if(a != def)
        {
        	niceFade(fontreset, 'block');
        }
    }
    var slider = $('#slider');
    slider.slider({
        range: 'min',
        slide: function(){
            $.cookie('the_cookie2', $(this).slider('value'), { path: '/' });
            update_font($(this).slider('value'));
        },
        change: function(){
            var a = $(this).slider('value');
            $.cookie('the_cookie2', a, { path: '/' });
            update_font(a);
        }
    });
    if($.cookie('the_cookie2')){
        slider.slider('value', $.cookie('the_cookie2'));
    }else{
        slider.slider('value', def);
    }
    if ($.cookie('the_cookie')){
       $('#content').css('font-size', $.cookie('the_cookie') + 'px'); 
    }
    
    // list of Customers: max height for columns
    $("div.list-customers ul:last").addClass("last");
    var tmp_heights = [];
    $("div.list-customers ul").each(function(i,e) { tmp_heights[i] = $(e).height() });
    var max_height = Math.max.apply(null, tmp_heights);
    $("div.list-customers ul").height(max_height);
    
    // list of Vacancies: max height for title
    var tmp_heights = [];
    $("div.list-vacancies h3").each(function(i,e) { tmp_heights[i] = $(e).height() });
    var max_height = Math.max.apply(null, tmp_heights);
    $("div.list-vacancies h3").height(max_height);
    
    // list of Category: columns width and title block height
    if ($.browser.msie && (parseInt($.browser.version) < 9)) {
        $("ul.list-category li:first").addClass("first");
    }
    // columns width
    var ul_width = $("ul.list-category").width();
    var li_qty = $("ul.list-category li").length;
    var li_width = (ul_width - li_qty*2 - (li_qty - 1)*9) / li_qty;
    $("ul.list-category li").width(li_width);
    // title block height
    var tmp_heights = [];
    $('ul.list-category .title-bg_').each(function(i,e) { tmp_heights[i] = $(e).height() });
    var max_height = Math.max.apply(null, tmp_heights);
    $('ul.list-category .title-bg_').height(max_height);
    
    // list of Box info: columns width/height
    if ($.browser.msie && (parseInt($.browser.version) < 9)) {
        $("ul.list-box-info li:first").addClass("first");
    }
    // columns width
    var block_width = $("div.block-info").width();
    var box_qty = $("li.box-info").length;
    var box_width = (block_width - box_qty*2 - (box_qty - 1)*20) / box_qty;
    $("li.box-info").width(box_width);
    // columns height
    var tmp_heights = [];
    $('div.block-info .box-info-content').each(function(i,e) { tmp_heights[i] = $(e).height() });
    var max_height = Math.max.apply(null, tmp_heights);
    $('div.block-info .box-info-content').height(max_height);
        
    // expand/collapse info
    $('.button-open-close').click(function(){
        $(this).toggleClass('open').next().slideToggle('300');
    });
    
    // rounded corners for images
    $('img.img-rounded').one('load',function(){
        var img = $(this);
        var img_width = img.width();
        var img_height = img.height();
        
        // build wrapper
        var wrapper = $('<div class="img-wrapper"></div>');
        wrapper.width(img_width);
        wrapper.height(img_height);
        
        // move CSS properties from img to wrapper
        wrapper.css('float', img.css('float'));
        img.css('float', 'none')
        
        wrapper.css('margin-right', img.css('margin-right'));
        img.css('margin-right', '0')
    
        wrapper.css('margin-left', img.css('margin-left'));
        img.css('margin-left', '0')
    
        wrapper.css('margin-bottom', img.css('margin-bottom'));
        img.css('margin-bottom', '0')
    
        wrapper.css('margin-top', img.css('margin-top'));
        img.css('margin-top', '0')
    
        // wrap image
        img.wrap(wrapper);
        
        // add rounded corners
        //img.after('<span class="tl"></span><span class="tr"></span><span class="bl"></span><span class="br"></span>');
    }).each(function(){
        if(this.complete) $(this).trigger('load');
    });
    
    // popup management
    $('#content a[target="_self"]').click(function(e){
        e.preventDefault();
        popupOpen($(this).attr('href') + '?scripted=true');
    });
    $('#popup-container').live('click', function(){
        popupClose();
    });
    $('.popup-window').live('click', function(e){
        // prevent popup inner-click to reach close function
        e.stopPropagation();
    });
    $('body').append('<div id="popup-container"><div class="popup-window"><a href="javascript: popupClose();" class="button-close" title="Close">Close</a><div id="popup-iframe"></div></div></div>');
    
    // fading replaces css hovers
    var speed = 100;
    function buttonHover(elem)
    {
    	elem.hover(function(){
            $(this).clearQueue().animate({ backgroundColor: '#8dc63f', borderColor: '#7aab36' }, speed);
        },function(){
            $(this).clearQueue().animate({ backgroundColor: '#ffffff', borderColor: '#bdbcbd' }, speed);
        });
    }
    $('.list-category li').hover(function(){
        $(this).children().first().clearQueue().animate({ backgroundColor: '#8ac842' }, speed);
    },function(){
        $(this).children().first().clearQueue().animate({ backgroundColor: '#eaeaea' }, speed);
    });
    $('.menu-addition li').hover(function(){
        $(this).clearQueue().animate({ backgroundColor: '#8dc834' }, speed);
    },function(){
    	var color = $(this).hasClass('active') ? '#e1eece' : '#ffffff';
        $(this).clearQueue().animate({ backgroundColor: color }, speed, function(){
        	$(this).attr('style', 'background-color:none;');
        });
    });
    buttonHover($('._save'));
    buttonHover($('#admin'));
    buttonHover($('#fontreset'));
    buttonHover($('.button-open-close'));
});

// popup window: open
function popupOpen(page)
{
    ($.browser.safari) ? (document.body.scrollTop = 0) : (document.documentElement.scrollTop = 0);
    $('#popup-iframe').load(page);

    var popupcontainer = $('#popup-container');
    
    var body_height = $('body').height() - 115;
    var popup_height = popupcontainer.height();
    var max_height = Math.max(body_height, popup_height);
    popupcontainer.height(max_height);
    
    niceFade(popupcontainer, 'block')
}

// popup window: close
function popupClose()
{
    var popupcontainer = $('#popup-container');
    niceFade(popupcontainer, 'none')
}

function niceFade(elem, display)
{
    if ($.browser.msie && (parseInt($.browser.version) < 9)) {
        elem.css('display', display);
    } else {
        if(display == 'block') elem.fadeIn(300, function(){
            $(this).css('display', display);
        });
        else elem.fadeOut(300, function(){
            $(this).css('display', display);
        });
    }
}

