//EDIT THIS
var delaychange=4;
var delayautochange=7;


//DO NOT EDIT
var nbpic=0;
var timeout=0;
var currpic=0;
var imgs=new Array();


$(document).ready(function()
{
	/*HACKS JS*/
	$(".gallery .infos, .fb").pngFix(); 
	$('#main_menu .menu li').hover(
	function(){
	$(this).addClass('sfhover');
	},
	function(){
	$(this).removeClass('sfhover');
	});
		
	$(function() {
			var zIndexNumber = 1000;
			$('#top .menu li').each(function() {
				$(this).css('zIndex', zIndexNumber);
				zIndexNumber -= 10;
			});
		});
    
    $('#main_menu .menu li').hover(function(){
        $(this).children('dl').slideDown('fast');
    },
    function()
    {
        $(this).children('dl').slideUp('fast');
    });
    
    
    nbpic=$('#img_list li').length;
    $('.gallery .controller .left').click(function()
    {
        if(timeout>0)
                clearTimeout(timeout);
        currpic--;
        if(currpic<0)
            currpic=nbpic-1;
        loadPic(false);
        return false;
    });
    
    $('.gallery .controller .right').click(function()
    {
        if(timeout>0)
                clearTimeout(timeout);
        currpic++;
        if(currpic>nbpic-1)
            currpic=0;  
        loadPic(false);
        return false;
    });
    
    timeout=setTimeout('autoChange()',delaychange*1000);
    
    
    $('.close_popup').click(function(){
        $('.popup').fadeOut();
        return false;
    });
    
    $('.tabs ul li.first').addClass('current');
    $('.tabs div.tab.first').addClass('current');
    $('.tabs ul li').click(function(){
        if(!$(this).hasClass('current'))
        {
         $('.tabs li.current').removeClass('current');
         $('.tabs div.tab.current').removeClass('current');
         $(this).addClass('current');
         $('.tabs div'+$(this).children('a').attr('href')).addClass('current');           
        }
        return false;
    })
	
	if(!iPhone)
	{
		$('a.video').click(function()
		{
			var href=$(this).attr('href');
			$.fancybox({
				'autoScale':false,
				'type':'swf',
				'href': url+'/flash-video-player/mediaplayer/player.swf?file='+escape(href)+'&controlbar=over&height=380&width=676&skin='+url+'/flash-video-player/skins/stylish/stylish.swf&autostart=true&bufferlength=1&displayclick=play&icons=true&linktarget=_blank&mute=false&quality=true&repeat=allways&resizing=true&shuffle=false&stretching=uniform&volume=90&aboutlink=',
				'swf' : {
					 'wmode'		: 'opaque',
					'allowfullscreen'	: 'true'
				},
				'width': 676,
				'height': 380,
				'overlayOpacity':0.7,
				'overlayColor':'#000'
			});
			return false;
		});
	}
});

function autoChange(auto)
{
    if(nbpic>1)
    {
        currpic++;
        if(currpic>nbpic-1)
            currpic=0;
        loadPic(auto);
    }
}

function loadPic(auto)
{
    if(typeof(auto)=='undefined')
        auto=true;  
    var automation=auto;
    
    $('.gallery .back').html($('.gallery .front').html());
    $('.gallery .back img').attr('style','');
    $('.gallery .front img').stop().fadeTo(200,0,function()
    {
        var link=$($('.gallery #img_list a').get(currpic)).attr('href');
        
            if(typeof(imgs[currpic])=='undefined')
            {
                imgs[currpic]=new Image();
                imgs[currpic].src=link;
                imgs[currpic].style.display='none';
                imgs[currpic].onload=function()
                {
                    $('.gallery .front').html(imgs[currpic]);
                    onPicLoad(automation);
                }
            }
            else
            {
                $('.gallery .front').html(imgs[currpic]);
                onPicLoad(automation);
            }           
    });
}

function onPicLoad(automation)
{
    $('.gallery .front img').fadeTo(200,1);
    desc='';
    desc+='<h2>'+$($('.gallery #img_list a').get(currpic)).attr('title')+'</h2>';
    desc+=$($('.gallery #img_list li').get(currpic)).children('.desc').html();
    $('.gallery .overflow .desc').html(desc);
    if(timeout>0)
            clearTimeout(timeout);
    if(automation)
    {
        timeout = setTimeout('autoChange()',delaychange*1000);
    }
    else
    {
        timeout = setTimeout('autoChange()',delayautochange*1000);
    }

}
