var sitefunctions = {
	textresize : function(){
		// show text resizing links
		$(".FontSize").show();
		
		var $cookie_name = "CC-FontSize";
		var originalFontSize = $("html").css("font-size");
		// if exists load saved value, otherwise store it
		if($.cookie($cookie_name)) {
			var $getSize = $.cookie($cookie_name);
			$("html").css({fontSize : $getSize + ($getSize.indexOf("px")!=-1 ? "" : "px")}); // IE fix for double "pxpx" error
		} else {
			$.cookie($cookie_name, originalFontSize, { expires: 7, path: '/' });
		}
		// reset link
		$(".FontSizeReset").bind("click", function() {
			$("html").css("font-size", originalFontSize);
			$.cookie($cookie_name, originalFontSize, { expires: 7, path: '/' });
		});
		// text "+" link
		$(".FontSizeInc").bind("click", function() {
			var currentFontSize = $("html").css("font-size");
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*1.2;
			if (newFontSize < 25) {
				$("html").css("font-size", newFontSize);
				$.cookie($cookie_name, newFontSize, { expires: 7, path: '/' });
			}
			return false;	
		});

		$(".FontSizeDec").bind("click", function() {
  			var currentFontSize = $("html").css("font-size");
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*0.8;
		  	if (newFontSize > 10) {
				$("html").css("font-size", newFontSize);
				$.cookie($cookie_name, newFontSize, { expires: 7, path: '/' });
			}
			return false;
		});
	}
}


$(document).ready(function () {
	jQuery.fn.exists = function () {
		return jQuery(this).length > 0;
	}
	
	/* font resize */
	
	sitefunctions.textresize();	
	
	/* --- */

	//Lightbox
	$(".colorbox_login").colorbox({
width: "600px",
height: "400px",
iframe: true,
onClosed: function () {
			location.reload(true);
		}
	});

	$(".colorbox_past_grad_statement").colorbox({
width: "600px",
height: "500px",
iframe: true
	});

	$(".colorbox_sickness_misadventure").colorbox({
width: "600px",
height: "550px",
iframe: true
	});

	//Default text for Search
	$(document).ready(function () {
		$(".search_input").focus(function (srcc) {
			if ($(this).val() == $(this)[0].title) {
				$(this).removeClass("defaultTextActive");
				$(this).val("");
			}
		});

		$(".search_input").blur(function () {
			if ($(this).val() == "") {
				$(this).addClass("defaultTextActive");
				$(this).val($(this)[0].title);
			}
		});

		$(".search_input").blur();
	});


	if ($(".applicationForm").exists()) {

		$(".applicationForm").validate({
ignore: ".ignore"
		});


		//show-hide section//	
		$("#optionalSection").css("display", "none");

		$(".detailsChanged").click(function () {

			// If checked
			if ($(this).val() === "yes") {

				//show the hidden div
				$("#optionalSection").show("fast");
				/*$("#titleNA").removeClass("ignore");
			$("#firstnameNA").removeClass("ignore");
			$("#surnameNA").removeClass("ignore");*/
				$("#address1NA").removeClass("ignore");
				$("#address2NA").removeClass("ignore");
				$("#suburbNA").removeClass("ignore");
				$("#stateNA").removeClass("ignore");
				$("#postcodeNA").removeClass("ignore");
				$("#countryNA").removeClass("ignore");
				$("#homephoneNA").removeClass("ignore");
				$("#mobileNA").removeClass("ignore");
				$("#emailNA").removeClass("ignore");
				$("#dobNA").removeClass("ignore");
			} else {
				//otherwise, hide it
				$("#optionalSection").hide("fast");
				/*$("#titleNA").addClass("ignore");
			$("#firstnameNA").addClass("ignore");
			$("#surnameNA").addClass("ignore");*/
				$("#address1NA").addClass("ignore");
				$("#address2NA").addClass("ignore");
				$("#suburbNA").addClass("ignore");
				$("#stateNA").addClass("ignore");
				$("#postcodeNA").addClass("ignore");
				$("#countryNA").addClass("ignore");
				$("#homephoneNA").addClass("ignore");
				$("#mobileNA").addClass("ignore");
				$("#emailNA").addClass("ignore");
				$("#dobNA").addClass("ignore");
			}
		});



		$(".WWC").click(function () {

			if ($(this).val() !== "I_have_a_current_WWC") {
				$("#noticeNo").addClass("ignore");
				$("#noticeExp").addClass("ignore");

			} else {
				$("#noticeNo").removeClass("ignore");
				$("#noticeExp").removeClass("ignore");
			}

		});
		$(".conflictOfInterest").click(function () {
			
			if ($(this).val() === "yes") {
				$("#Conflict1Name").addClass("required");
				$("#Conflict1Relationship").addClass("required");
				$("#Conflict1School").addClass("required");
				
			} else {
				$("#Conflict1Name").removeClass("required");
				$("#Conflict1Relationship").removeClass("required");
				$("#Conflict1School").removeClass("required");
				
			}
			
		});
		$(".tutored").click(function () {
			
			if ($(this).val() === "yes") {
				$("#tutor1CourseCode").addClass("required");
				$("#tutor1Name").addClass("required");
				$("#tutor1School").addClass("required");
				
			} else {
				$("#tutor1CourseCode").removeClass("required");
				$("#tutor1Name").removeClass("required");
				$("#tutor1School").removeClass("required");
				
			}
			
		});
		$("#unavailableTable").css("display", "none");
		$(".availableWhen").click(function () {

			// If checked
			if ($(this).val() === "not_available_the_following_dates") {

				//show the hidden div
				$("#unavailableTable").show("fast");
				$(".unavailableDates").removeClass("ignore");
			}else{
				$("#unavailableTable").hide("fast");
				$(".unavailableDates").addClass("ignore");
				
			}
		});


		if ($(".confirmation").exists()) {
			$(".applicationForm").attr("style", "display:none");
		}

	}
	//Feedback form
	if ($("#FeedbackForm").exists()) {
		$("#FeedbackForm").validate({
invalidHandler: function (form, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					var message = errors == 1 ? 'You missed 1 field. Please correct the error above and try again.' : 'You missed ' + errors + ' fields. Please correct the errors above and try again.';
					$("span.error").html(message);
					$("span.error").show();
				} else {
					$("span.error").hide();
				}
			}
		});

		if ($("#ComplaintType_0").exists()) {
			$("#ComplaintType_0").click(function () {
				$("#serviceDelivery").show();
				$("#productsDelivery").hide();
			});

			$("#ComplaintType_1").click(function () {
				$("#serviceDelivery").hide();
				$("#productsDelivery").show();
			});
		}
	}

	if ($("#reload_vet").exists()) {

		$('#reload_vet').click(function() {				
			CallVETServiceJSON();
		});

		CallVETServiceJSON();

	}

	function CallVETServiceJSON()
	{
		
		$.ajax({
type: "GET", 
url: "http://www.curriculum.wa.edu.au/Apps/CCWS/VETWCFService.svc/GetFiles", 
contentType: "application/json; charset=utf-8", 
dataType: "json", 
processdata: false,
cache: false,
beforeSend: function() {
				var tableHeight = $('#VETFiles').innerHeight();
				
				$("#VETLoadingOverlay").height(tableHeight);
				$("#VETLoadingOverlay").fadeTo('fast', 0.80);
			},
success: function(data) { //On Successfull service call				
				var jsonResult = jQuery.parseJSON(data);
				
				
				$("#VETFileQualificationsTXT").attr("href", jsonResult.Directory + jsonResult.QualificationsTXTFileName);
				$("#VETFileQualificationsCSV").attr("href", jsonResult.Directory + jsonResult.QualificationsCSVFileName);
				$("#VETFileCompetenciesTXT").attr("href", jsonResult.Directory + jsonResult.CompetencyTXTFileName);
				$("#VETFileCompetenciesCSV").attr("href", jsonResult.Directory + jsonResult.CompetencyCSVFileName);
				$("#VETFileLinksTXT").attr("href", jsonResult.Directory + jsonResult.LinksTXTFileName);
				$("#VETFileLinksCSV").attr("href", jsonResult.Directory + jsonResult.LinksCSVFileName);

				$(".VETFileLastUpdated").html(jsonResult.LastUpdate);
				
				$("#VETLoadingOverlay").fadeOut('fast', 0);

				$("#vet_status_message").html("");

			},
error: function() {

				$("#VETLoadingOverlay").fadeOut('fast', 0);

				$("#vet_status_message").html("An error occurred while attempting to retrieve VET files");
				
			}
		});
	}




});
