$('document').ready(function(){
	
	
	/*
	 * Determine wrapper height to place disclaimer at the bottom of document
	 */
	if ($(document).height() > $("div.wrapper").height()){
		
		var disclaimerHeight = $("div.disclaimer").height();
		
		if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion >= "4.0" && navigator.appVersion < "7.0")
		{
			disclaimerHeight = disclaimerHeight + 4;	
		} 
		
		$("div.wrapper").height($(document).height() - disclaimerHeight);
		
	}
	

	/*
	 * MAIN MENU
	 */
	$("#nav-one li").hover(
		function(){
			$("#nav-one ul").removeClass('sfHover');
			$(this).addClass('sfHover');
		},
		function(){
			$("#nav-one ul").removeClass('sfHover');
			$(this).removeClass('sfHover');
		}
	);
	
	
	/*
	 * DATEPICKERS
	 */
	$('.date-pick').datePicker({clickInput:true, startDate:'01-01-1900'});
	
		
	/*
	 * LIGHTBOX LISTENER
	 */	
	$('a.lightbox').lightBox({
			imageLoading: '/img/lightbox-ico-loading.gif',
			imageBtnClose: '/img/lightbox-btn-close.gif',
			imageBtnPrev: '/img/lightbox-btn-prev.gif',
			imageBtnNext: '/img/lightbox-btn-next.gif',
			imageBlank: '/img/lightbox-blank.gif'
		});
	 
	 
	 /*
	 * MIJN AANVRAGEN / MIJN WERKZAAMHEDEN
	 * ROW HOVER / CLICKS
	 */
	$('table.requests tr td')
		.css('cursor','pointer')
		.hover(	function(){
					$(this).parent('tr').addClass('trhover')
				},
				function(){
					$(this).parent('tr').removeClass('trhover')
				})
		.click(function(){
			
			var id = $(this).parent('tr').attr('id');
			var aSplitId = id.split('-');
			var iRecId  = aSplitId[1];
			
			if(aSplitId[0] == 'r') var page = 'mijn_aanvragen';
			else if(aSplitId[0] == 'a') var page = 'mijn_werkzaamheden';
			
			window.open('/voor_klanten/'+page+'/?id='+iRecId, '_self');
			
		});
		
	/*
	 * OVERRIDE ALERT BOX
	 */
	$('#alert').jqm({overlay: 20, modal: true, trigger: false});

	// trigger an alert whenever links of class alert are pressed.
	$('a.alert').click(function() { 
		alert('You Have triggered an alert!'); 
		return false;
	});
	
	
	/*
	 * SEARCHBAR FOCUS LISTENER
	 */
	$('.srch').focus(function(){
		$(this).select();
	}); 
	
	
	/*
	 * GOOGLEMAP ADD LIGHTBOX TO POPUP TRIGGER
	 * MORE FUNCTION DOWN THIS PAGE
	 */
	setTimeout(function(){
		$('[id^=mtgt_unnamed_]').click(function(){
			//alert($(this).attr('src'));
			setTimeout(function(){
				addLightBox();
			},500);
		})
	},500)
	
	/*
	 * HIDE GOOGLEMAP COPYRIGHT E.A.
	 */
	setTimeout(function(){
		$('div.gmnoprint').find('span:contains("Kaartgegevens)').css('display','none');
		$('div.gmnoprint').find('div:contains("100 km)').parent().css('display','none');
		
		
	},1000);
	
	/*
	 * HIDE 'LOADING' MSG WHEN GOOGLE MAP IS DISPLAYED
	 */
	$('div.wait').css('display','none');
	
	
	/*
	 * INFRA FORM FUNCTIONALITY
	 */
	$('select#Infratype').change(function(){
		switch($(this).val())
		{
			case 'Hoogspanningsmast':
				$('#Lijnbenaming, #Mastnummer').append('*');
				highlightFields(new Array('Lijnbenaming','Mastnummer'));
				break;
			default:
				$('#Lijnbenaming').html('Lijnbenaming');
				$('#Mastnummer').html('Mastnummer');
				$('#Lijnbenaming, #Mastnummer').siblings().removeClass('highlight');
		}
	})
	
	if($('select#Infratype').length > 0){
		if($('select#Infratype').val() == 'Hoogspanningsmast'){
			$('#Lijnbenaming, #Mastnummer').append('*');
		}
	}
	
});

/**
 * OVERRIDE ALERT BOX
 */
function alert(msg) {
	if (typeof(msg) == 'string') {
		msg = msg.replace(/([^>]?)\n/g, '$1<br />\n');
	}
	else {
		msg = toString(msg);
	}
	
	$('#alert').jqmShow().find('div.jqmAlertContent').html(msg);
}

/**
 * 
 * JQUERY EXTENSION FOR MAIN MENU
 * 
 */
jQuery.fn.replaceWith = function(replacement) {
  return this.each(function(){
    element = $(this);
    $(this)
      .after(replacement).next()
      .attr('class', element.attr('class')).attr('id',element.attr('id'))
      .html(element.html())
      .prev().remove();
  });
};


/**
 * 
 * HIGHLIGHTS FORM FIELDS AFTER VALIDATION
 * 
 */
function highlightFields(aFieldNames)
{
	if(typeof(aFieldNames) == "object")
	{
		for(i in aFieldNames)
		{
			$("input[name='"+aFieldNames[i]+"']").addClass('highlight');
			$("textarea[name='"+aFieldNames[i]+"']").addClass('highlight');
		}		
	}
}


/**
 * 
 * TOGGLE PASSWORD BOX WHEN CLICKING 'wachtwoord vergeten'
 * 
 */
function forgetPass(toggle){
	
	
	if(toggle == "on")
	{
		var title = "Wachtwoord vergeten";
		var formval = "forgetpassword";
		var buttonval = "Versturen"
		var hide = ".span1";
		var show = ".span2";
	}
	else
	{
		var title = "Inloggen";
		var formval = "login";
		var buttonval = "Inloggen"
		var show = ".span1";
		var hide = ".span2";
	}
	
	$('#loginbox h3 span').fadeOut('fast', function(){
		$('#loginbox h3 span').html(title);
		$('#loginbox h3 span').fadeIn('slow');
	});
	
	
	$('form.loginform input#form').attr('value',formval);
	
	
	if (toggle == "on") {
		$('form.loginform p:nth-child(2)').hide('slow');
		$('form.loginform p:nth-child(1) input').attr('name','email');
		
		
		$('form.loginform p:nth-child(1) label').fadeOut('slow',function(){
			$('form.loginform p:nth-child(1) label').html('E-mail adres:');
			$('form.loginform p:nth-child(1) label').fadeIn('slow')
		});
		
		
		$('form.loginform .span1').hide('fast', function(){
			$('form.loginform .span2').show('fast');
		});
		
		$('form.loginform .span3').hide('fast', function(){
			$('form.loginform .span4').show('fast');
		});
		
	}
	else {
		$('form.loginform p:nth-child(2)').show('slow');
		$('form.loginform p:nth-child(1) input').attr('name','username');
		
		
		$('form.loginform p:nth-child(1) label').fadeOut('slow',function(){
			$('form.loginform p:nth-child(1) label').html('Gebruikersnaam:');
			$('form.loginform p:nth-child(1) label').fadeIn('slow')
		});
		
		$('form.loginform .span2').hide('fast', function(){
			$('form.loginform .span1').show('fast');
		});
		
		$('form.loginform .span4').hide('fast', function(){
			$('form.loginform .span3').show('fast');
		});
	}
}

/**
 * 
 * GOOGLE MAP FUNCTIONS
 * 
 */
/* Add map popup trigger and attach lightbox when clicking in the map list */
function triggerPopup(i){
	GEvent.trigger(markers[i], "click");
	
	setTimeout(function(){
		addLightBox();
		
	},500);
}

/* Add lightbox trigger */
function addLightBox()
{
	$("a.enlarge").lightBox({
		imageLoading: '/img/lightbox-ico-loading.gif',
		imageBtnClose: '/img/lightbox-btn-close.gif',
		imageBtnPrev: '/img/lightbox-btn-prev.gif',
		imageBtnNext: '/img/lightbox-btn-next.gif',
		imageBlank: '/img/lightbox-blank.gif'
	}); 
}