var doNews = true;
var slidedin = true;

$(document).ready(function(){ 
	//Links with the class external will open in a new window, to get the code valid in xhtml-strict
	$(".external").attr("target","_blank");
		   
	//Mailme - Email Spam Protector
    var at = / at /;
    var dot = / dot /g;
    $('span.mailme').each( function() {
        var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
        $(this)
            .after('<a href="mailto:'+addr+'" class="mailme">'+ addr +'</a>')
            .remove();
    });
	$('span.mailicon').each( function() {
        var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
        $(this)
            .after('<a href="mailto:'+addr+'" class="mailicon"></a>')
            .remove();
    });
	
	$('object').wrap('<div id="object" />');
	
	//Creating a nice scrollbar
	$('#scroll').jScrollPane({showArrows:true, verticalArrowPositions: 'before', scrollbarWidth:16});
	$('#news_archive').jScrollPane({showArrows:true, verticalArrowPositions: 'before', scrollbarWidth:16});
	$("a.popup").fancybox();
	setTimeout(function() {
		$('#photo_gallery').jScrollPane({showArrows:true, verticalArrowPositions: 'before', scrollbarWidth:16});
	}, 600);
	
	
	//Searchbox: hiding and showing value
	$('input[type="text"]').focus(function (){
		clearText(this);
	});
	$('input[type="text"]').blur(function (){
		addText(this);
	});
	$('textarea').focus(function (){
		clearText(this);
	});
	$('textarea').blur(function (){
		addText(this);
	});
//	$('#submit').click(function() {
//			post_form();
//	});
	
	//Validate - Validation of forms
	$("#contactform").validate();
	
	$("#load_secure_newinfo_form").live("click", function(){
  		loadForm('secure_newinfo_form');
	});
	$("#load_secure_newpassword_form").live("click", function(){
  		loadForm('secure_newpassword_form');
	});

}); 

function printPage(path)
{
	var main_div = $('#scroll_print').parent().attr('id');
	//Get new height, strecht to full lenght
	if(main_div == 'news_block'){
		var plus = 265;
	}
	else
	{
		var plus = 215;
	}
	
	var outerheight = $('#scroll_print').outerHeight(true);
	if(outerheight){
		var new_height = outerheight+plus;
	}
	else
	{
		var new_height = 645;
	}
	
	$('#wrapper, #holder').css('height',new_height+'px');
	
	///Print Functie
	$('#wrapper').printElement({
		overrideElementCSS:[
			path+'style/style_print.css',
			{ href:path+'style/style_print.css',media:'print'}
		],
		leaveOpen:false,
        printMode:'popup'
	});
	
	//Originele hoogte wrapper terug zetten
	$('#wrapper').css('height','645px');
	$('#holder').css('height','619px');
}

function clearText(theField)
{
if (theField.defaultValue == theField.value)
theField.value = '';
}

function addText(theField)
{
if (theField.value == '')
theField.value = theField .defaultValue;
}

$(function() {

	if ($.cssHooks.opacity.set) {
		$.cssHooks.opacity.defaultSet = $.cssHooks.opacity.set
		$.cssHooks.opacity.set = function(elem, value) {
			$.cssHooks.opacity.defaultSet(elem, value)
			if (!elem.style.filter.replace(/\ *(alpha\(opacity=100\))?/, ''))
				elem.style.removeAttribute('filter')
			}
	}
	
	$('#main_menu li ul').css({ opacity: 0 })
	$('#main_menu li ul').css("width", "0px");
	$('#main_menu li ul li').css("display", "none");
	//$('#main_menu li ul li ul').css("height", "0px");
	
	$('#main_menu .active ul li').css("display", "block");
	$('#main_menu .active').children('ul').stop().animate({
	  width: '160px',
	  opacity: '1'
	});
	//$('#main_menu .active').children('a').css('color','#333333').css('background','url(/images/main_menu_arrow_active.png) no-repeat 0 4px');
	//var ul_height = 0;
//	$('#main_menu .active ul li').children('ul').find('li').each( function() {
//		ul_height = ul_height + $(this).outerHeight(true);
//	});
//	$('#main_menu .active ul li').children('ul').css('width','130px');
	$('#main_menu .active ul li').children('ul').stop().animate({
	  width: '130px',
	  opacity: '1'
	});
		
	$('#main_menu').children('li').hover(function(){
		$(this).children('a').css('color','#333333').css('background','url(/images/main_menu_arrow_active.png) no-repeat 0 4px');
		$('#main_menu .active ul').stop().animate({
		  width: '0px',
		  opacity: '0'
		});
		
		if($(this).hasClass('active')) {
			$(this).children('a').css('color','#333333').css('background','url(/images/main_menu_arrow_active.png) no-repeat 0 4px');
		} else {
			$('#main_menu .active').children('a').css('color','#ffffff').css('background','url(/images/main_menu_arrow.png) no-repeat 0 4px');
		}
		
		$(this).find('ul').find('li').css("display", "block");
		$(this).find('ul').stop().animate({
		  width: '160px',
		  opacity: '1'
		});
		$(this).find('ul').find('li').find('ul').stop().animate({
		  width: '130px',
		  opacity: '1'
		});
	},
	function () {
		
			$(this).children('a').css('color','#ffffff').css('background','url(/images/main_menu_arrow.png) no-repeat 0 4px');
			$(this).find('ul').stop().animate({
			  width: '0px',
			  opacity: '0'
			});
			$('#main_menu .active').children('ul').stop().animate({
			  width: '160px',
			  opacity: '1'
			});
			$('#main_menu .active ul li').children('ul').stop().animate({
			  width: '130px',
			  opacity: '1'
			});
			$('#main_menu .active').children('a').css('color','#333333').css('background','url(/images/main_menu_arrow_active.png) no-repeat 0 4px');
			$('#main_menu .active ul li.active').children('a').css('color','#333333').css('background','url(/images/main_menu_arrow_active.png) no-repeat 0 4px');
			
		
	});
});

//Googlemaps on hte contactpage
$(window).ready(function () {
    if($("#map").length != 0)
	{
	var path = $("#path").val();
	
	$.ajax({
		type: "GET",
		url: path+"exe/getaddress.php",
		dataType: "html",
		error: function(){
			alert(mi_ajax_error);
		},
		success: function(data){
			var address = $(data).filter('#address').html();
            var googlelink = $(data).filter('#googlelink').html();
			
			$("#map").gMap({
				markers: [
					{
						address: address,
						popup: false
					}
				],
				zoom: 14,
				onComplete: function() {
					var data = $('#map').data('gmap');
					google.maps.event.addListener(data.markers[0], 'click', function() {
						//window.open( 'http://maps.google.nl/maps?q=Maastricht+Unstruments&daddr=Biopartner+Center,+Oxfordlaan+70,+Maastricht&hl=nl&sll=52.469397,5.509644&sspn=4.699192,9.448242&view=map&geocode=CRGQTK47TRIaFbGlBwMdvDdXACHo_xn_YtneDw&t=h&z=16' );
        				window.open( googlelink );
						return false;
					});
				}
			});
		}
	});
	}
});

function newsCycle(position,path)
{
	if(doNews){
	var totalli = $("#news li").size();
	var li = position;
	var list_items = $('#news li');
	changeNews(list_items.eq(li).children().attr('id'),path);
	li++;
	
	if(li == totalli) li=0;
	
	setTimeout(function() {
		newsCycle(li,path);
	}, 8000);
	}
}

function changeNews(id,path)
{
	var news = $("#"+id);
	
	$.ajax({
		type: "POST",
		url: path+"exe/newshomepage.php?id="+id+"&path="+path,
		success: function (data) {
			var response=$(data);
			var oneval = response.filter('#image').html();
			var subval = response.filter('#text').html();

			//Change arrow position
			var position = news.position();
			$("#news").animate({backgroundPosition:'0 '+position.top+'px'});
			
			///Change class
			$("#news li a").removeClass('active');
			news.addClass('active');
			
			//Elementen hiden
			$('#news_foto').fadeOut( function()
				{
					$('#news_foto').attr('src',oneval);
					$('#news_foto').fadeIn();
				}
			);
			$('#news_item_text').slideUp( function() 
				{
					$('#news_item_text').html(subval);
					$('#news_item_text').slideDown();
				}
		    );			
		}
	});
}


function post_form(form)
{
	var formdata = $("#"+form).serialize();
	$.ajax({
		type: "POST",
		url: "../exe/sendemail.php",
		data: formdata,
		timeout: 2000,
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			alert(mi_ajax_error);
		},
		success: function (data) {
			if(data == 'Verzonden'){
				$('#contactform_holder').html('<p class="success">'+mi_form_positive+'</p>');
			} else {
				$('#messages_p').addClass('error');
				$('#messages_p').html(data);
			}
			$('#scroll').jScrollPane({showArrows:true, verticalArrowPositions: 'before', scrollbarWidth:16});
		}
	});
};



function loadForm(form)
{
	//alert(form);
	//var formdata = $("#"+form).serialize();
	var lang = $("#post_lang").val();
	var path = $("#post_path").val();
	//alert(formdata);
	//alert(lang);
	//alert(path);
	$.ajax({
		type: "POST",
		url: "../exe/secure.php?t=load_"+form+"&lang="+lang+"&path="+path,
		//data: formdata,
		timeout: 2000,
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			alert(mi_ajax_error);
		},
		success: function (data) {
			$('#'+form).html(data);
		}
	});
};


function post_secure(form,path,action)
{
	var formdata = $("#"+form).serialize();
	//alert(formdata);
	$.ajax({
		type: "POST",
		url: path+"exe/secure.html?t="+action,
		data: formdata,
		timeout: 2000,
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			alert(mi_ajax_error);
		},
		success: function (data) {
			//alert(data);
			var response=$(data);
			var oneval = response.filter('#messagekind').html();
			var subval = response.filter('#messagetext').html();
			var linkval = response.filter('#backlink').html();
			
			if(action == 'secure_newpassword' || action == 'secure_newinfo'){
				$('#messages_p').removeClass();
				$('#messages_p').html('');
				$('#messages_p').addClass(oneval);
				$('#messages_p').html(subval);
				$('#messages_p').show();
				
				//$('#messages_p').fadeOut().delay(10000).fadeOut('slow'); 
				setTimeout(function(){ 
					$('#messages_p').fadeOut('slow');
			  	}, 5000 );
			}
			
			if(oneval == 'positive'){
				if(action == 'secure_newpassword'){
					$('#secure_newpassword_form').hide();
				}
				
				if(action != 'secure_forgotpassword')
				{
					$('#secure_login').hide();
					$('.required_fields').hide();
				}
				
				if(linkval){
					window.location=linkval;
				} else {
					$('#sec_messages_p').addClass(oneval);
					$('#sec_messages_p').html(subval);
					$('#scroll').jScrollPane({showArrows:true, verticalArrowPositions: 'before', scrollbarWidth:16});
				}
			} else {
				$('#sec_messages_p').addClass('error');
				$('#sec_messages_p').html(subval);
				$('#scroll').jScrollPane({showArrows:true, verticalArrowPositions: 'before', scrollbarWidth:16});
			}
		}
	});
};


function post_comp(form,path,action)
{
	var formdata = $("#"+form).serialize();
	$.ajax({
		type: "POST",
		url: path+"exe/competition.html?t="+action,
		data: formdata,
		timeout: 2000,
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			alert(mi_ajax_error);
		},
		success: function (data) {
			//alert(data);
			var response=$(data);
			var oneval = response.filter('#messagekind').html();
			var subval = response.filter('#messagetext').html();
			var linkval = response.filter('#backlink').html();
			
			if(oneval == 'positive'){
				if(linkval){
					window.location=linkval;
				} else {
					$('.fieldset_'+form).hide();
					$('#comp_messages_p').addClass(oneval);
					$('#comp_messages_p').html(subval);
					$('#scroll').jScrollPane({showArrows:true, verticalArrowPositions: 'before', scrollbarWidth:16});
				}
			} else {
				$('#comp_messages_p').addClass('error');
				$('#comp_messages_p').html(subval);
				$('#scroll').jScrollPane({showArrows:true, verticalArrowPositions: 'before', scrollbarWidth:16});
			}
		}
	});
};
