jQuery.noConflict();

//init
var base_url;
var site_url;
var scroller_current;
var scroller_interval;
var scroller_time = 10000; //5 sec
var flyer_current;
var flyer_interval;
var flyer_time = 5000; //5 sec
var photo_scroller_time = 10000; //5 sec
var pageY = 0;
var pageX = 0;

function init_javascript(site){
	site_url = site;

	if(document.all) {
		// Internet Explorer
		//jQuery.fx.off = true;
	}
	
	//auto scroll news
	photo_scroller_current = 1;
	photo_scroller_interval = setInterval(photo_scroller_next, photo_scroller_time);
	
	//auto scroll news
	scroller_current = 1;
	scroller_interval = setInterval(scroller_next, scroller_time);
	
	//auto scroll flyers
	flyer_current = 1;
	flyer_interval = setInterval(flyer_next, flyer_time);

	//update universal pageY variable
	document.onmousemove = update_mousepos;
	
	//jdiv_inner.css("top", 0);
	setInterval("scroll_content('agenda')", 50);
	setInterval("scroll_content('twitter')", 50);
	
	//menu_ini(); // controls the dropdown menu
	
	//testing
	//$.fx.off = true;
}

function submit_form(e){
	if(e.keyCode==13)
		jQuery("#login_box form").submit();
}

function hide_message(){
	jQuery('#post_message').fadeOut();
}
//hide message
setTimeout("hide_message()", 7000);


//General functions
function showMessage(messageText, isError){
	if(isError){
		boxClass = 'errorBox';
	}else{
		boxClass = 'messageBox';
	}	
	$("#messageTextDiv").html(messageText+" Klik hier om deze boodschap te sluiten");
	
	if(document.all) {
		$("#messageDiv").show(); // Internet Explorer
	}else{
		$("#messageDiv").attr('class', boxClass).slideDown('normal');
	}
}

function hideMessage(){
	if(document.all) {
		$("#messageDiv").hide(); // Internet Explorer
	}else{
		$("#messageDiv").slideUp('normal');
	}
}

function menu_ini()
{
	jQuery("#nav li").each(function()
	{	
		
		var $submenu = jQuery(this).find('ul:first');
		
		jQuery(this).hover(function()
		{	
			$submenu.stop()
			.css({overflow:"hidden", height:"auto", display:"none"})
			.slideDown(300, function()
			{
				jQuery(this).css({overflow:"visible", height:"auto"});
			});	
		},
		function()
		{	
			$submenu.stop()
			.slideUp(300, function()
			{	
				jQuery(this).css({overflow:"hidden", display:"none"});
			});
		});	
	});
	
	jQuery(" #nav ul ").css({display: "none"}); // Opera Fix
}

/*calendar*/
function slide_toggle(div){
	jQuery("#"+div).slideToggle();
}

/*infopane*/
function info_slider_over(div){
	jQuery('.header_text .slider').stop().animate({
		left: -500*(div-1)
	  }, 500);
	  
	jQuery('#header_infopane .buttons .bg_slider').stop().animate({
		left: 4+(83+8)*(div-1)
	  }, 500);
	
	//jQuery('.buttons_tooltip').stop().css("display", "inline");
	jQuery('.buttons_tooltip').stop().show().animate({
		opacity: 1
	  }, 500);
}

function info_slider_out(){
	jQuery('.buttons_tooltip').stop().animate({
		opacity: 0
	  }, 500);
}

function info_slider_down(url){
	window.location.href=url;
}

/*block content*/
function scroll_content(div){
	jdiv = jQuery("#"+div);
	jdiv_inner = jdiv.children(".inner_scroller");
	container_height = jdiv.height();
	container_width = jdiv.width();
	content_height = jdiv_inner.height();
	
	if(content_height > container_height){
		
		top_margin = 10;
		margin = 35;	
		mouseY = pageY-jdiv.offset().top;
		mouseX = pageX-jdiv.offset().left;
		
		//document.title = mouseX + "-" + mouseY;
		
		if(mouseX > -30 && mouseX < container_width+30){
			
			pos = parseInt ( jdiv_inner.css("top") );
			
			newpos = pos - Math.round( (mouseY - container_height/2) /2 );
			//jdiv_inner.css("top", newpos);
			
			if(newpos < -(content_height - container_height-margin) ){
				newpos = -(content_height - container_height-margin) ;
			}
			
			if(newpos > 0){
				newpos = 0;
			}			
			
			//animate
			jdiv_inner.stop().animate( {
				top: newpos+"px"
			  } , 400, "easeOutExpo");
			//animate
			//jdiv_inner.css("top", newpos);
			/*
			newpos = 12-Math.round( (content_height-container_height)*( (mouseY-margin)/ (container_height-2*margin) ) );
			
			//check if animation is desired
			//document.title = jdiv_inner.css("top");
			
				//animate
				jdiv_inner.stop().animate( {
					top: newpos
				  } , 400, "easeOutExpo");
			*/
		}
		
	}
}

function update_mousepos(e){
	e = e || window.event;
	
	pageY = e.clientY;
	pageX = e.clientX;
	
	if (e.pageX) pageX =  e.pageX;
	else if (e.clientX)
	   pageX =  e.clientX + (document.documentElement.scrollLeft ?
	   document.documentElement.scrollLeft :
	   document.body.scrollLeft);
	else pageX =  null;
	
	if (e.pageY) pageY =  e.pageY;
	else if (e.clientY)
	   pageY =  e.clientY + (document.documentElement.scrollTop ?
	   document.documentElement.scrollTop :
	   document.body.scrollTop);
	else pageY =  null;
}

function scroll_content_top(e, div){
	if(e.srcElement != div) return;
	
	//animate
	jdiv.children(".inner_scroller").stop().animate( {
		top: 12
	  } , 400, "easeOutExpo");
}

/*sidebar*/
function toggle_sidebar(div){
	jQuery('#'+div).slideToggle();
}

function toggle_sidebar_nav(div){
	if(jQuery('#'+div).is(":visible")){
		jQuery('#img'+div).removeClass("open");
	}else{
		jQuery('#img'+div).addClass("open");
	}
	
	jQuery('#'+div).slideToggle();
}

/*photo scroller*/
function photo_scroller_next(){
	
	photo_scroller_total = jQuery('.photo_container .image_container .image_slider .image').length;
	
	photo_scroller_current +=1;
	
	if(photo_scroller_current > photo_scroller_total){
		photo_scroller_current =1;
	}
	
	//animate
	jQuery(".photo_container .image_container .image_slider").stop().animate( {
		left: -496*(photo_scroller_current-1)
	  } , 400);
}

/*news scroller*/
function scroller_click(href){
	window.location.href=href;
}

function scroller_over(div){
	scroller_current = div;
	
	//animate image
	jQuery(".news_scroller#news .image_slider .headline_images").stop().animate( {
		top: -215*(div-1)
	  } , 400);
	
	jQuery(".news_scroller#news .news_items .slider").stop().animate( {
		left: -500*(div-1)
	  } , 400);
	  
	jQuery('.news_buttons .bg_slider').stop().animate({
		left: 0+(24+4)*(div-1)
	  }, 300);
}

function scroller_next(){
	
	scroller_total = jQuery('.news_scroller#news .news_items .headline').length;
	
	scroller_current +=1;
	
	if(scroller_current > scroller_total){
		scroller_current =1;
	}
	
	//animate
	scroller_over(scroller_current);
	
}

function scroller_hide(e){
	/*jdiv = jQuery('.news_scroller#news .news_items');
	mouseY = e.pageY-jdiv.offset().top;
	mouseX = e.pageX-jdiv.offset().left;
	if(mouseY < 0 || mouseX <0 || mouseX > 500){
		jdiv.stop().animate( {
			opacity: 0,
		  } , 200, "linear");
		
		//reset interval
		if(scroller_interval == undefined){
			scroller_interval = setInterval(scroller_next, scroller_time);
		}
		
	}else{
		//animate news items
		jdiv.stop().animate( {
			opacity: 1,
		  } , 200, "linear");
		  
		//clear interval
		clearInterval(scroller_interval);
		scroller_interval = undefined;
	}	*/
}

/*flyer scrollers*/
function init_flyer(div){
	//auto scroll flyers
	flyer_current = 1;
	flyer_interval = setInterval("flyer_next('"+div+"')", flyer_time);
}

function flyer_next(div){
	flyer_total = jQuery('#'+div+' .image_slider .image').length;
	
	flyer_current +=1;
	
	if(flyer_current > flyer_total){
		flyer_current =1;
	}
	
	//animate
	jQuery('#'+div+' .image_slider').stop().animate( {
		left: -217*(flyer_current-1)
	  } , 400);
	
	//reset timer
	clearInterval(flyer_interval);
	flyer_interval = setInterval("flyer_next('"+div+"')", flyer_time);
}

function flyer_previous(div){

	flyer_total = jQuery('#'+div+' .image_slider .image').length;
	
	flyer_current -=1;
	
	if(flyer_current < 1){
		flyer_current = flyer_total;
	}
	
	//animate
	jQuery('#'+div+' .image_slider').stop().animate( {
		left: -217*(flyer_current-1)
	  } , 400);
	
	//reset timer
	clearInterval(flyer_interval);
	flyer_interval = setInterval("flyer_next('"+div+"')", flyer_time);
}

/*comment*/
function submit_comment(){
	jQuery("#comment_form").submit();
}

/*login*/
function login_show(){
	jQuery("#login_box #login_overlay").css("height", jQuery(document).height());
	jQuery("#login_box").stop().fadeIn();
	jQuery(document).keydown( submit_form );
}
function login_hide(){
	jQuery("#login_box").stop().fadeOut();
}
function login_submit(){
	jQuery("#login_box form").submit();
}
function activation_submit(){
	jQuery("#activation_form").submit();
}
