var collector = null;
var containerwidth = 0;
var imgs = [0];
var blockwidths = [];
var imgobj = [];
var last = 0;
var current = 0;
var counter = 0;
var replacer = 0;
var links = 0;
var slider = 0;
var isplayer = 0;
var playerVersion = null;
var containerheight = 360; // container-height

$(document).ready(function(){
	
	$('#video,#video2').each(function(){
		isplayer = 1;
		createPlayer(); // fw-player
		playerVersion = swfobject.getFlashPlayerVersion();
	});
	
	slider = $(document).width();
	$(window).resize(function(){
		slider = $(document).width();
		var tempp = parseInt((slider - blockwidths[current])/2)-(imgs[current]);
		collector.animate({ marginLeft: tempp},100,function(){
			collector.css('margin-left',tempp+'px');	
		});
	});

	collector = $($('div#slides')); // collector-container

	block = 0;
	collector.find('div.sliderblock').each(function(){
		
		var temp = parseInt($(this).css('width'));
				
		$(this).attr('id',counter);
		containerwidth += temp;
		counter++;
		blockwidths.push(temp);
		imgs.push(containerwidth);
		imgobj.push($(this));
		// fade all blocks except first
		if (block!=0) {		
			$(this).addClass("fader");
			if (block==1) {
				fader($(this));
			} else {
				$(this).fadeTo('fast',0.5);
			}
		}
		// fade arrows
		if (block>0) {
			$(this).children('span.arrowL').css('display','block');
			$(this).children('span.overarrow').css('display','block');
		}		
		block++;
		// click action
		$(this).click(function(me){
			imageclick($(this));
		});
		// next hover
		$(this).hover(
				function() {
					if( $(this).hasClass("fader") ) {
        				$(this).removeClass("fader");
      				}
					$(this).not('.active').stop().fadeTo('fast',0.95);
				},
				function() {				
					$(this).not('.active').fadeTo(600,0.5);
				}
			)		
	});
	
	imgobj[current].addClass('active');
	
	collector.css('width',containerwidth+'px');
	replacer = parseInt((slider - blockwidths[current])/2)
	
	/// get width of first image and position it centered in 1150
	collector.css('margin-left', replacer+'px');
	
	links = $($('div#pager a'));
	links.eq(current).addClass('active');
	
	links.click(function(me){ // linkclick
		me.preventDefault();
		links.eq(current).removeClass('active');
		imgobj[current].removeClass('active');
		current = parseInt($(this).html()) - 1;	
		moveImg();  
		  
    	$("#image"+current).animate({ // blendout-hover animation
    	    width: "0"
    	    }, 300, "linear", function(){
    	    	$("#wrapper"+current).removeClass("wrapperleft"),
    	    	$("#wrapper"+current).css({
    	    		display: "none"
    	    });
    	});
    	
	});
		
});

function imageclick(obj) {
	links.eq(current).removeClass('active');
    imgobj[current].removeClass('active');
    var tempimgc = parseInt(obj.attr('id'));
    if (tempimgc == current) {
    	moveImg();
    } else {
    	current = parseInt(obj.attr('id'));
    	moveImg();
    }	
    	    
    return false;
}

function moveImg() {
	if( imgobj[current].hasClass("fader") ) {
        imgobj[current].removeClass("fader");
    }
	links.eq(current).addClass('active');
	imgobj[current].addClass('active');	
	
	if( $(this).hasClass("fader") ) { // stop fading
        $(this).removeClass("fader");
    }
	
	// dis-display dark-overlayer
	imgobj[current].children('span.overarrow').css('borderLeft','0px');
	imgobj[current].children('span.overarrow').css('background-repeat','no-repeat');
	window.setTimeout(function(){
			imgobj[current].children('span.overarrow').css('display','none');
		}, 300);
	
	// fade arrows
	var i = 0;
	while (i < block) {
        if (i>=current) {
			imgobj[i].children('span.arrowL').css('display','block');
			imgobj[i].children('span.arrowR').css('display','none');
        } else {
        	imgobj[i].children('span.arrowR').css('display','block');
			imgobj[i].children('span.arrowL').css('display','none');
        }
        i++;
    }
	imgobj[current].children('span.arrowL').css('display','none');	
	imgobj[current].children('span.arrowR').css('display','none');		
	if(last!=current) {		
		imgobj[last].fadeTo(400, 0.5);		
		imgobj[last].children('span.overarrow').css('display','block');
		imgobj[current].children('span.overarrow').css('background-repeat','repeat');
		imgobj[last].children('span.overarrow').css('borderLeft','0px');		
	}
	
	var tempp = parseInt((slider - blockwidths[current])/2)-(imgs[current]);

	collector.animate({ marginLeft: tempp},300,'easeInOutQuad',function(){
		collector.css('margin-left',tempp+'px');						
		imgobj[current].fadeTo(1000, 1);
	});
	
	if(last!=current && isplayer == 1) { // video-player-stuff	
		if(imgobj[current].hasClass("player") || imgobj[current].children("div#video2").hasClass("player")) { // determine if flashplayer is working
    		$(".header").fadeOut();  
    		if(playerVersion.major >= 9) { // if flash-player blend controls
    			$(".mute").fadeIn();
    		}
    		$("body").addClass("nohover").addClass("ready");
    		if (navigator.userAgent.indexOf("Firefox")!=-1) { // Firefox gets some timeout
    			window.setTimeout(function(){
    				$("#screenshot").css("display", "none");
    				if($("body").hasClass("ready")) {
    					waitWhileLoading('true');
    				}
    			}, 700);
    		} else {
    			$("#screenshot").remove();
    			waitWhileLoading('true');
    		}
		}
		if(imgobj[last].hasClass("player") || imgobj[last].children("div#video2").hasClass("player")) {
			$(".header").show();
    		$("body").removeClass("ready");
    		if (navigator.userAgent.indexOf("Firefox")!=-1) { // Firefox again
    			waitWhileLoading('false');
    			window.setTimeout(function(){
    				$("body").removeClass("nohover");
    				$("#screenshot").css("display", "block");
    			}, 600);
    		} else {	
    			window.setTimeout(function(){
    				waitWhileLoading('false');
    				$("body").removeClass("nohover");
    			}, 600);			
    		}
		}	
	}
	
	if(last!=current) {  // go-back-link-action
		if(imgobj[current].attr('id') == 0) {
			$("span.goback").show();
		} else {
			$("span.goback").hide();
		}
		if(imgobj[current].attr('id') == block-1) {
			$("span.goback2").show();
		} else {
			$("span.goback2").hide();
		}
	}
	
	last = current;	
}

function waitWhileLoading(action) {
	if(playerVersion.major >= 9) { // if flash-player
		if (navigator.userAgent.indexOf("Firefox")!=-1 && action == "true") { // Firefox gets some timeout
			window.setTimeout(function(){
				if (player.sendEvent) { 
					player.sendEvent('PLAY',action);
				} else {
					waitWhileLoading(action);
				}
			}, 300);
		} else {
			if (typeof player.sendEvent == 'function') { 
					player.sendEvent('PLAY',action);
			} else {
					setTimeout("waitWhileLoading(\'" + action + "\')",300);
			}		
		
		}
	}
}
/* customisation du fader pour éviter après le chargement de la page le clignotement de la 2e image   */
function fader(obj) {
	if ( obj.is(".fader") ) { // fading-animation on start-up
	    obj.fadeTo(800,0.3,'easeInOutQuad');				
//		window.setTimeout(function(){
//			fader(obj);
//		},2000);
	}
//	window.setTimeout(function(){
//	    if ( obj.is(".fader") ) {
//	    	obj.fadeTo(1000,0.9,'easeInOutQuad');
//	    }
//	},1000);
	
}
