$(document).ready(function(){
	if ( $("#home").length ){
		var orgText = "jouw organisatie";
		$("#orgName").focus(function(){
			if ( $("#orgName").val() == orgText ){
				$("#orgName").val('');
			}
		}).blur(function(){
			if ( $("#orgName").val() == '' ){
				$("#orgName").val(orgText);
			}
		})
	}
	
	
	if ( $("#screenshots .screenshot a").length ){
		$("#screenshots .screenshot a").colorbox({current: ''});
	}
	
	var domainHelpText = "kies de gewenste (organisatie)naam";
	if ( $("#orderForm #orderForm_domain").length ){
		el = $("#orderForm #orderForm_domain");
		//Put in a grey help text
		if ( el.val() == "" ||  el.val() == domainHelpText ){
			el.css('color', '#aaa');
			el.css('fontStyle', 'italic');
			el.val(domainHelpText);
		}
		//Remove the help text on focus
		el.focus(function(){
			if ( el.val() == domainHelpText ){
				el.css('color', '#333');
				el.css('fontStyle', 'normal');
				el.val("");
			}
		})
		//Show the help text on blur when nothing is filled in
		el.blur(function(){
			if ( el.val() == "" ){
				el.css('color', '#aaa');
				el.css('fontStyle', 'italic');
				el.val(domainHelpText);
			}
		})
		
	}

	if ( $("#orderForm #orderForm_field_plan .radioButton").length ){
		//Hide the address fields if free plan was selected
		//Get the id of the chosen plan
		var planNr = $("#orderForm #orderForm_field_plan .radioButton:checked").attr('value');
		if ( planNr == '1' ) {
			$("#orderForm_field_street, #orderForm_field_number, #orderForm_field_zipcode, #orderForm_field_city, #orderForm_field_organisation_url").hide();
		}
		
		$("#orderForm #orderForm_field_plan .radioButton").click(function(){
			//Get the id of the chosen plan
			var id = $(this).attr('id').substring(6);
			//Show address fields if necessary
			if ( id > 1 ) {
				$("#orderForm_field_street, #orderForm_field_number, #orderForm_field_zipcode, #orderForm_field_city, #orderForm_field_organisation_url").show();
			}else{
				$("#orderForm_field_street, #orderForm_field_number, #orderForm_field_zipcode, #orderForm_field_city, #orderForm_field_organisation_url").hide();
			}
			
			// And save it in as a php session-variable
			$.post(
					'/php/AjaxController.php',
					{
						action: 'setPlanInSession',
						planId: id
					}
				);
			//Don't forget to change the active plan in the planpicker
			$("#planChoice ul li.plan").removeClass('active');
			$("#planChoice ul li#plan"+id).addClass('active').removeClass('hover');
		});
	}
	
	
	if ( $("#orderForm #orderForm_field_plan a.info, #orderForm a.info").length ){
		$("#orderForm #orderForm_field_plan a.info, #orderForm a.info").colorbox({inline: true, width: '750px', href: "#popup"});
	}
	
	
	if ( $("#popup a#btnContinue").length ){
		$("#popup a#btnContinue").click(function(){
			$.fn.colorbox.close();
		});
	}
	
	
	if ( $("#planChoice").length ){
		$("#planChoice ul li.plan").not('.shadow').hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
		);
		
		$("#planChoice ul li.plan").not('.shadow').click(
			function(){
				//Change the checked radiobutton
				$("#planChoice ul li.plan input").attr("checked", "");
				$("input", this).attr("checked", "checked");
				
				$("#planChoice ul li.plan").removeClass('active');
				$(this).addClass('active').removeClass('hover');
				//Send an Ajax request to save the chosen plan in a session variable
				//Get the id of the chosen plan
				var id = $(this).attr('id').substring(4);
				$.post(
					'/php/AjaxController.php',
					{
						action: 'setPlanInSession',
						planId: id
					}
				);

				if ( $("#orderForm").length ){
					//Change the choice in the orderForm
					$("#orderForm_field_plan #option"+id).attr('checked', 'checked');
				}else{
					//Goto to the orderform	
					window.location = $("a", this).attr('href');
				}
				
				return false;
			}
		);
	}
	
	$(".lightbox").colorbox({width:"650", height:"505", iframe:true});

	if ( $("#FAQ").length ){
		$("#faqHolder .faqs").hide();
		$("#faqHolder .faqGroupName").addClass("down");
		$("#faqHolder .faqGroupName").hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
		);
		$("#faqHolder .faqGroupName").click(function(){
			if ( $(this).parent().children('.faqs').is(":visible") ){
				$(this).parent().children('.faqs').slideUp(400);
				$(this).parent().children('.faqGroupName').removeClass('up').addClass('down');
			}else{
				//Others must slide up
				$("#faqHolder .faqs:visible").slideUp(400);
				$("#faqHolder .faqs:visible").parent().children('.faqGroupName').removeClass('up').addClass('down');
				//Clicked must slide down
				$(this).parent().children('.faqs').slideDown(400);
				$(this).parent().children('.faqGroupName').removeClass('down').addClass('up');
			}
		});
		
	}
});

