
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 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=/";
}

document.writeln('<style type="text/css">');
document.writeln('body .navi li.active ul.menu {display: none;}');
var cookie = readCookie('dwtcMenu');
if (cookie) {
    document.writeln('body .navi li.active#' + cookie + ' ul.menu {display: block;}');
}
document.writeln('</style>');

jQuery(function($) {

    $('li.l2.inactive > h3').hover(
        function(event){
            $(this).stop().animate({backgroundColor: '#7aaaaa'}, {duration: 300, easing: 'linear', queue: false});           
        },  
        function(event){
            $(this).stop().animate({backgroundColor: '#aaae9e'}, {duration: 1500, easing: 'linear', queue: false});           
        }
    );
    $('li.l2.active > h3').hover(
        function(event){
            $(this).stop().animate({backgroundColor: '#03cce4'}, {duration: 300, easing: 'linear', queue: false});           
        },  
        function(event){
            $(this).stop().animate({backgroundColor: '#34a4bc'}, {duration: 1500, easing: 'linear', queue: false});           
        }
    );
    $('li.l3.inactive > h4').hover(
        function(event){
            if (this.fadeComplete)
                $(this).stop().animate({backgroundColor: '#417375'}, {duration: 300, easing: 'linear', queue: false});           
        },  
        function(event){
            if (this.fadeComplete)
                $(this).stop().animate({backgroundColor: '#4d4a3a'}, {duration: 1500, easing: 'linear', queue: false});           
        }
    );
    
    var els = $('li.l2.active');
    if (els && els.length > 0) {
        var id = $('li.l2.active')[0].id;
    }
    var cookie = readCookie('dwtcMenu');
    
    if (cookie && cookie == id) {
        $('li.l2.active > ul h4').show();
    }
    else {
        $('li.l2.active > ul h4').css('opacity','0');
        $('li.l2.active > ul').show();
        var els = $('li.l2.active > ul h4').each(function(index, el) {
            window.setTimeout(function(){$(el).animate({opacity: '1.0'}, 1500,'linear',function(){this.fadeComplete=true});}, 500 + index * 100);
        })
    }
    
    createCookie('dwtcMenu',id,0);
    
    $('li.l2.active > h3').css('backgroundColor','#03cce4');
    $('li.l2.active > h3').stop().animate({backgroundColor: '#34a4bc'}, 2000);
    
    $('li.l2 > h3').click(
        function(event){
            $('li.l2.active > h3').stop().animate({backgroundColor: '#aaae9e'}, 50);
            $('li.l2.active > ul').stop().fadeOut(50);
            //$('li.l2.active > h3').css('backgroundColor','#aaae9e');
            $(this).css('backgroundColor','#03cce4');
        } 
    );
    
    $('ul.grid li').hover(
        function(event){
            $(this).find('.title').slideDown(100);           
        },  
        function(event){
            $(this).find('.title').slideUp(100);           
        }
    );
    
    $('.slideshow img').load(function() {
        var show = $(this).parents('.slideshow')
        if (this.height > show.height()) {
            show.css('height',this.height+'px');
        }
    });
    
    
    $('.slideshow').cycle({
        fx:     'fade', 
        speed:  1600, 
        timeout:4500,
        containerResize: false,
		pager:              '.pager', 
		activePagerClass:   'active',
		pagerAnchorBuilder: function(idx, slide) { 
		    var img = $(slide).find('img');
            return '<a href="#">' + img.attr('alt') + '</a>'; 
        },
    });
    
    $('li.slide, .pager a').click(function(ev){
        $('.slideshow').cycle('pause');
    });
    
    $('.medias-grid a').fancybox({
        hideOnContentClick: true,
        zoomSpeedIn: 300,
        zoomSpeedOut: 300,
        overlayShow: true,
        overlayOpacity: 0.9       
    });

});
