/*
 * FILENAME: general.js
 * AUTHOR: Ashley Barrett (ashley.barrett@group.co.uk)
 * CREATED: 08/04/2010
 * COMMENTS: The general javascript file for www area of the Loanfinder website 
 */

$(function(){
	$('#switchergobutton').hide();

	changeInputType('passwordInput', 'text', $('#passwordInput').attr('value'));
	
	 $('#passwordInput').focus(function() {
	
		 if($(this).attr("type") != "password"){
			 changeInputType('passwordInput', 'password', $('#passwordInput').attr('value'));
			 setTimeout("$('#passwordInput').focus();", 10);
		 }	 
	 }); 
	
	$("#login form .formElement input[name=username]").focus(function(){
		var password = $("#login form .formElement input[name=password]");
		if(($(this).val()=='Username')&&(password.val()=='Password')){
			$(this).val(''); 
			password.val('');
		}
	});
	
	function changeInputType(id, type, value) {
		var name = $('#'+id).attr('name');
		var size = $('#'+id).attr('size');
		var html = '<input type="'+type+'" name="'+name+'" value="'+value+'" id="'+id+'" size="'+size+'" />';
		$('#'+id).after(html).remove();
	}
	
	
	if($(".showYoutubeVideoClick")){
		$(".showYoutubeVideoClick").show();
		$(".showYoutubeVideo").hide();
		
		$(".showYoutubeVideoClick").click(function(){
			$(this).parent().find(".showYoutubeVideo").toggle();
			
			if($(this).html() == "Show video"){
				$(this).html("Hide video");
			}else{
				$(this).html("Show video");
			}
		});
	}
	
	
	if($(".faqList")){
		$(".faqList li div.faqSubList").hide();
		
		$(".faqList li").click(function(){
			
			if($(this).find("div.faqSubList").is(':hidden')){
				$(".faqList li div.faqSubList").slideUp('fast');
				$(this).find("div.faqSubList").slideToggle("fast");
			}else{
				$(".faqList li div.faqSubList").hide();
			}
			
			return false;
		});
		
		
		$('input.star').rating();
		
	}
	
	if($(".loanHeading").length){
		$(".loanHeading .header a.hide").click(function(){

			if($(this).find("img").attr("src").indexOf("show") == -1){
				$(this).parent().next(".content").hide();
				var src = $(this).find("img").attr("src").replace("hide","show");
				$(this).find("img").attr("src",src);
			}else{
				$(this).parent().next(".content").show();
				var src = $(this).find("img").attr("src").replace("show","hide");
				$(this).find("img").attr("src",src);
			}
			
		});
	}
	
	$('a.new-window').click(function(){
		window.open(this.href);
		return false;
	});
	
	/* Hide the news feed, by default. */
	$('.loanHeading .header a.hide:first').click();
	
	/*$('#fetchAddress, #fetchAddressC').show();
	attachGeneral();
	$('.explanationClick').show();*/	


	
	$("select[name=App]").change(function(){
		if($(this).val() > 0) {
			$("#switcherPop").jqm();
			$("#switcherPop").jqmShow();
			setTimeout('closeSwitcher();',2000);
		} else {
			$('#multiAccountSwitch').submit();		
		}
	});

	/* Disable the forgotten password section on load */
	$(document).ready(function() {
		$('#forgottenPasswordSection').hide();
	});

	/* Toggle the forgotten password section on click of the forgotten link */ 
	$(".forgottenPassword").click(function(){	
		if($("#forgottenPasswordSection").is(":visible")){
			$('#forgottenPasswordSection').hide('fast');
		} else {
			$('#forgottenPasswordSection').show('fast');
		}
	});
	
	/* Empty input box when clicked */
	$(".loginPageInput").click(function(){
		this.value='';
	});

	/* If nothing entered default to original text */
	$(".loginPageInput").blur(function(){
		if(this.value == "") {
			if(this.id == "email") {
				this.value = "Enter your email address";
			} else if (this.id == "mobile") {
				this.value = "Enter your mobile number";
			}
		}
	});
});


function closeSwitcher() {
		$("#switcherPop").jqmHide();
		$('#multiAccountSwitch').submit();		
}



/*
function createDatePicker(minDates, maxDates, setDate) {
	$('#datepicker').datepicker({
		dateFormat: 'yy-mm-dd',
		firstDay: 1,
		minDate: minDates,
		maxDate: maxDates,
		defaultDate: setDate,
		onSelect: function(dateText, inst) {
			$(this).children('.calendar').val(dateText);
			document.location.href = $('#datepickerMove').val()+$('#FutureDate').val();
		}
	});
}

function attachGeneral() {

	var setDate = new Date();
	if($('#FutureDate').val()) {
		var futureDate = $('#FutureDate').val().split('-');
		setDate = new Date(futureDate[0], futureDate[1] - 1, futureDate[2]);
	}
		
		if($('#paymenttype').val() == 2) {
			createDatePicker('7', '+6w', setDate);
		} else {
			createDatePicker('14', '+8w', setDate);
		}
	
	$('.explanation').click(function() {
		$(this).hide();
		return false;
	});
	
	$('.explanation div div').click(function() {
		return false;
	});
	
	$('.explanationClick').click(function() {
		$(this).parent().children('.explanation').show();
		return false;
	});

	
	$('.paymentDetails').keyup(function() {
		var id = $(this).attr('id');
		var value = $(this).val();
		$.ajax({
			type:           "POST",
			url:            "/Assets/setPaymentDetails.php",
			dataType:       "json",
			cache:          false,
			data:           "id="+id+"&value="+value,
			success:        function(json){
							}
		});
	});
	
	$('#selectAddress').change(function() {
		var postCode = $('#PostCode1').val();
		var key = $(this).val();
		$.ajax({
			type:           "POST",
			url:            "/populateAddress.php",
			dataType:       "json",
			cache:          false,
			data:           "postCode="+postCode+"&key="+key,
			success:        function(json){
								var line1 = '';
								var property = json['Property']+',';
								if(property != ',') {
									line1 = property+' ';
								}
								if(json['Street'].length > 0) {
									line1 += json['Street'];
								} else {
									line1 = line1.slice(0, (line1.length - 2));
								}
								$('#Address1').val(line1);
								$('#Address2').val(json['Locality']);
								$('#Town').val(json['Town']);
								$('#PostCode1').val(json['PostCode']);
								$('#selectAddressesWrapper').slideUp();
							}
		});
	});
	
	if($('#fetchAddressC').length){
		$("input[name=Address1], input[name=Address2], input[name=Town], label[for=Address1], label[for=Address2], label[for=Town]").hide();
	}
	
	$('#fetchAddressC').click(function() {
		var postCode = $('#PostCode1').val();
		
		if($("#selectAddress")){
			$("#selectAddressesWrapperC").slideUp("slow");
			$("#selectAddress").remove();
		}
		
		$.ajax({
			type:           "POST",
			url:            "/fetchAddressList.php",
			dataType:       "json",
			cache:          false,
			data:           "postCode="+postCode,
			success:        function(json){
								if(json != '') {
									var select = '<a href=\"#\" id=\"cantFind\">My address isn\'t here</a>&nbsp;&nbsp;<select id="selectAddress"><option selected="selected" value="0">- Select -</option>';
									jQuery.each(json, function(i, value) {
										value = value.replace(postCode.toUpperCase(),"");
										select += '<option value="'+i+'">'+$.trim(value)+'</option>';
									});
									select += '</select>';
									$('#selectAddresses').html(select);
									
									$('#selectAddressesWrapperC').slideDown("slow");
									
									
									$("#cantFind").click(function(){
										$("input[name=Address1], input[name=Address2], input[name=Town], label[for=Address1], label[for=Address2], label[for=Town]").slideDown("slow");
										$("#selectAddressesWrapperC").slideUp("slow");
										$("#selectAddress").remove();
										return false;
									});
									
									$('#selectAddress').change(function() {
										
										var value = $("#selectAddress :selected").text();
										var address = value.split(",");
										
										if(address[3]){
											$("input[name=Address1]").val($.trim(address[0]+' '+address[1]));
											$("input[name=Address2]").val($.trim(address[2]));
											$("input[name=Town]").val($.trim(address[3]));
										}
										else if(address[2]){
											$("input[name=Address1]").val($.trim(address[0]));
											$("input[name=Address2]").val($.trim(address[1]));
											$("input[name=Town]").val($.trim(address[2]));
										}else{
											$("input[name=Address1]").val($.trim(address[0]));
											$("input[name=Address2]").val('');
											$("input[name=Town]").val($.trim(address[1]));
										}		
										
										$("input[name=Address1], input[name=Address2], input[name=Town], label[for=Address1], label[for=Address2], label[for=Town]").slideDown("slow");
									});
									
								} else {
									$('#selectAddressesWrapperC').slideUp('normal', function() {
										
										alert('No addresses found for the post code "'+postCode+'". Please revise or enter your address manually.');
										$("input[name=Address1], input[name=Address2], input[name=Town], label[for=Address1], label[for=Address2], label[for=Town]").slideDown("slow");
									});
								}
							}
		});
		return false;
	});
	
	$('#fetchAddress').click(function() {
		var postCode = $('#PostCode1').val();
		$.ajax({
			type:           "POST",
			url:            "/fetchAddressList.php",
			dataType:       "json",
			cache:          false,
			data:           "postCode="+postCode,
			success:        function(json){
								if(json != '') {
									var select = '<select id="selectAddress"><option selected="selected" value="0">- Select -</option>';
									jQuery.each(json, function(i, val) {
										select += '<option value="'+i+'">'+val+'</option>';
									});
									select += '</select>';
									$('#selectAddresses').html(select);
									$('*').unbind();
									attachGeneral();
									$('#selectAddressesWrapper').slideDown();
								} else {
									$('#selectAddressesWrapper').slideUp('normal', function() {
										alert('No addresses found for the post code "'+postCode+'"');
									});
								}
							}
		});
		return false;
	});
	
}*/