// JavaScript Document


$(document).ready(function(){
	initAll();
	if($("#map").length > 0){
		loadGmaps();
	}
  $('#video').click(function(e) {
    e.preventDefault();
    $('#media').html('<iframe src="movie.php?id=' + $(this).attr('href').replace('#', '') + '" width="495" height="325" style="margin:17px 0 0 13px; padding:0; overflow:hidden;"></iframe>');
  });
  var liW = Math.floor($('#logos').width() / $('#logos li').length);
  $('#logos li').each(function() {
    $(this).css({width:(liW-4) + 'px'});
  });
  
  	startticker();
	setInterval(checkTickerPosition,50);
	$("#ticker ul li").hover(function(){ 
					$("#ticker ul").stop(); 
					$(this).attr('id','activeLI');
					var currentItem = $(this);
					var stopped = false;
					var widthPerLI = 0;
					$("#ticker ul li").each(function(){	
						if($(this).attr('id') ==  'activeLI' && stopped == false){
							stopped = true;
							if(parseInt($("#ticker ul").css('left')) < (-widthPerLI)){
								var newpos = (parseInt(widthPerLI)-10);
								$("#ticker ul").animate({ 
									left: -newpos+"px"
								}, 200);
							}
						}
						widthPerLI += $(this).innerWidth(); 
					});
				  }, 
				  function(){ 
					$("#activeLI").attr('id','');
					startticker(); 
		}
	);
});

function initAll(){
	$("#blocks a:not(.active)").wbflip();
	$("#logos li a, .winkelscroll a, #plattegrondwinkels li a, #plattegrond a, #winkels li a").hover(function(){
		wbShopOver($(this).attr('href'));
	},function(){
		wbShopOff($(this).attr('href'));
	});
	$("#plattegrondwinkels [rel^=shop], #plattegrond [rel^=shop]").hover(function(){
		wbShopOver($(this).attr('rel'),'rel');
	},function(){
		wbShopOff($(this).attr('rel'),'rel');
	});	
	

	$("#legenda li").click(function(){
		var current = $("#legenda li.active");
		current.removeClass('active');
		legendaOff(current);
		$(this).addClass('active');
		legendaOver($("#legenda li.active"));
	});
									
	$("#legenda li").hover(function(){
		legendaOver($(this));
	},function(){
		legendaOff($(this));		
	});	
	 $('.cycle').cycle({
		fx: 			'fade',
		timeout: 		5000
	});
	 
	$('.scroll').jScrollPane();
	
	$("#plattegrondtoggle").click(function(e){
		if($(this).hasClass('active')){
			$(this).removeClass('active')
			$("#media .cycle").show();		
			$("#plattegrond").hide();	
		}else{
			$(this).addClass('active')
			$("#media .cycle").hide();
			$("#plattegrond").show();
		}
		e.preventDefault();
	});
	
	$("#actie .pictures").cycle({
		fx: 			'fade',
		timeout: 		2000
	});
	
	setTimeout(function(){ 
		$("#actie").show().animate({'marginLeft':'+=100'},0).css({'opacity':0}).animate({'marginLeft':'-=100','opacity':1},1500).find('.scrolleractie').jScrollPane(); 
	}, 1000);
	
	$("#actie .close").click(function(e){
	 $("#actie").fadeOut(500);
	  e.preventDefault();
	})
}

function legendaOver(el){
	var nr = el.attr('rel');
	$(".bol[nr="+el.attr('rel')+"]").css('background-position','0px '+(nr*-12)+'px').addClass('active');
}
function legendaOff(el){
	if(!el.hasClass('active')){
		$(".bol[nr="+el.attr('rel')+"]").css('background-position','0px 0px').removeClass('active');
	}
}

function wbShopOver(href,type){
	if(href){
		if(type == 'rel'){
			$("[rel="+href+"]:not(.bol)").addClass('active');
			$(".bol[nr][rel="+href+"]:not(.active)").css('background-position','0px '+($(".bol[rel="+href+"]").attr('nr')*-12)+'px');
		}else{
			$("#logos li:not(.active) a[href="+href+"]").find('img').stop().animate({'marginTop':'0px'},200);
			$("a[href="+href+"]:not(.bol)").addClass('active');
			$(".bol[nr][href="+href+"]:not(.active)").css('background-position','0px '+($(".bol[href="+href+"]").attr('nr')*-12)+'px');
		}
	}
}
function wbShopOff(href,type){
	if(href){
		if(type == 'rel'){
			$("[rel="+href+"]:not(.bol)").removeClass('active');
			$(".bol[nr][rel="+href+"]:not(.active)").css('background-position','0px 0px');	
		}else{
			$("#logos li:not(.active) a[href="+href+"]").find('img').stop().animate({'marginTop':'20px'},200);
			$("a:not(.bol)[href="+href+"]").removeClass('active');
			$("a.bol[nr][href="+href+"]:not(.active)").css('background-position','0px 0px');
		}
	}
}


	

function checkTickerPosition(){
	var newpos = 0;
	var widthPerLI = 0;
	var totalpos = parseInt($("#ticker ul").css('left'));
	$("#ticker ul li").each(function(){	
		widthPerLI += $(this).innerWidth(); 
		if(widthPerLI < -1 * totalpos){
			$("#ticker ul").stop();
			newpos = totalpos + $(this).innerWidth();
			$(this).appendTo("#ticker ul");
			$("#ticker ul").css('left', newpos+'px');
			totalpos = newpos;
			startticker();
		}
	});
}


function startticker(){
	$("#ticker ul").stop(); 
	var totalwidth = 0;
	$("#ticker ul li").each(function(){	totalwidth += $(this).innerWidth(); })
	$("#ticker ul").animate({ 
		left: "-"+totalwidth+"px"
	  }, totalwidth * 20, "linear");
}
