$(document).ready(function() {
    
    var c = readCookie('style');
    if (c){
        style_link = $('.style_switch').find('.style_unselect').find('a.'+c);
        if(style_link.length == 0){
            eraseCookie('style');
            style_link = $('.style_switch').find('.style_unselect:first').find('a');
        }
    }else{
        style_link = $('.style_switch').find('.style_unselect:first').find('a');
    }
    
    switchStylestyle(style_link);
    
    $('.styleswitch').click(function()
    {
        switchStylestyle($(this));
        return false;
    });
    
    
    $('.style_prev').click(function(){
        var link = $('.style_switch li.style_select').prev('li.style_unselect').find('a.styleswitch');
        if(link.length == 0){
            link = $('.style_switch li .styleswitch:last');    
        }
        switchStylestyle(link);
        return false;
    });
    
    $('.style_next').click(function(){
        var link = $('.style_switch li.style_select').next('li.style_unselect').find('a.styleswitch');
        if(link.length == 0){
            link = $('.style_switch li .styleswitch:first');    
        }
        switchStylestyle(link);
        return false;
    });
    
});

var t;
function interval(){
    if(t){
        clearInterval(t);
    }
    t = setInterval(function(){
        $('.style_next').trigger('click');    
    }, 5000);
        
}

function switchStylestyle(link){
    eval(link.attr('rel'));
    var 
        path = '/images/design/header_fon/',
        text = $('#styles').find('#s_'+id).html(),
        text_container = $('#header_middle_text'),
        img_container = $('#bg'),
        img_src = path+img;
        
    if(link.length == 0){
        eraseCookie(styleName);
        style_link = $('.style_switch').find('.style_unselect:first').find('a');
        switchStylestyle(style_link);
        return false;    
    }
        
    setBgImage(img_container, img_src);
    setBgText(text_container, text);
    $('.style_switch li').attr('class','style_unselect');
    link.parents('.style_unselect').attr('class','style_select');
    createCookie('style', id, 365);
    interval();
    
    
    
}

function setBgImage(container, img_src){
    var 
        img = new Image(),
        current_img = container.find('img.current');
        
    $(img).attr('src', img_src).load(function(){
        container.append($(this));
        $(this).hide();
        if(current_img.length > 0){
            current_img.fadeOut('slow').remove();    
        }
        $(this).fadeIn('slow').attr('class', 'current');
    });
}

function setBgText(container, html){
    var 
        text = $('<div>'+html+'</div>');
        current_text = container.find('.text');
        
    container.append(text);
    text.hide();
    if(current_text.length > 0){
        current_text.fadeOut('slow').remove();    
    }
    text.fadeIn('slow').attr('class','text');
    
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
function eraseCookie(name)
{
    createCookie(name,"",-1);
}
// /cookie functions

