/*
 * 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 
 * UPDATE: Ashley Barrett (09/11/2010) Added in new logic for the new home page.
 * UPDATE: Dirk Lewis 	- 11/02/11	- Added in logic for new cancellation login page.
 * 
 */

$(function(){
	if ($('#logo').attr('href').indexOf('loanfinder') == -1)
	{
		$('#logo').click(function() {
			window.open($(this).attr('href'));
			return false;
		});
	}
	
	$('.fileDownloadButton a').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
	
	$('#showForgottenCancelBox').show();
	
	if((($('#loginMobile').val()== 'Enter your mobile number'))&&($('#loginEmail').val()== 'Enter your email address')){
			$('#forgottenPasswordSection,.forgottenPasswordSection').hide();
			$('#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);
		 }	 
	 }); 
	
	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();		
		}
	});




	/* Toggle the forgotten password section on click of the forgotten link */ 
	$("a.forgottenPassword").click(function(){	
		if($('#forgottenPasswordSection, .forgottenPasswordSection').is(":visible")){
			$('#forgottenPasswordSection, .forgottenPasswordSection').hide('fast');
		} else {
			$('#forgottenPasswordSection, .forgottenPasswordSection').show('fast');
		}
	});
	$(".forgottenPassword1").click(function(){	
		if($(".forgottenPasswordSection1").is(":visible")){
			$(".forgottenPasswordSection1").hide('fast');
		} else {
			$(".forgottenPasswordSection1").show('fast');
		}
	});
	
	
	/* Empty input box when clicked */
	/*$(".loginPageInput").click(function(){
		this.value='';
	});*/
	
	//If the forgotten details input boxes are the default values, then empty on click
	$(".forgottenDetsInput").click(function(){
		if((this.value == "Enter your email address") || (this.value == "Enter your mobile number"))
		{
		this.value='';
		}
	});
	
	//If there is nothing entered on blur default to original text
	$(".loginPageInput").blur(function(){
		if(this.value == "") {
			if(this.id == "loginEmail") {
				this.value = "Enter your email address";
			} else if (this.id == "loginMobile") {
				this.value = "Enter your mobile number";
			}
		}
	}); 
	
	$("form .uploadSubmit").click(function(){
		var allowedExt = "jpg,jpeg,gif,png,doc,docx,pdf";
		var filename = $("#fileUpload").val();
		
		var splitted = filename.split('.');

		var len = (splitted.length) - 1;

		if(allowedExt.indexOf(splitted[len].toLowerCase())==-1){
			//ERROR HERE
			alert("Your photo was not recognised as a valid image (.jpg, .gif or .png)");
			$(".fileUpload").val("");
			return false;
		}
		

	});	
	
	
	if($('#homePageContainer').length > 0){
		$('#whatMakesUsDifferent .different a img').hover(
			function(){
				$(this).parent().parent().find('a.heading h2').css('text-decoration','underline');
			},function(){
				//Remove all.
				$('#whatMakesUsDifferent .different:next a.heading h2').css('text-decoration','none');	
			}
		);
	}
	
});


function closeSwitcher() {
		$("#switcherPop").jqmHide();
		$('#multiAccountSwitch').submit();		
}

//append google analytics event tracking to the footer links
$(document).ready(function()
{
  $('#lenderPageFooterWrapper a').each(function()
  {
  	$(this).bind('click', function()
  	{
 		if (pageTracker)
  		{
  			pageTracker._trackEvent('LF_Footer_LenderPage_Links', $(this).attr('rel'), $(this).attr('title'));
  		}
  	});
  });
});


function roundToDecimal(num,dec){
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	result = result+"";
		if (result.indexOf(".")==-1) {
			result += ".0";
		}
		while(result.length-result.indexOf(".")<(dec+1)) {
			result += "0";
		}
		return result;

};
