/** 
 * @projectDescription	ANYVAN Javascript
 * @author 	Matt Hobbs
 * @version 	0.1 
 */

//Font Replacement
Cufon.replace('#mainNav li:not(".right") a, #s-footer h6, #aap_content .sf-container h1, .bespoke', {
	hover: true
});



jQuery(function($){

    
	/**
	 * Open in new windows
	 */
	$("#labelterms-agreed0 a").click(function(){
		window.open(this.href);
		return false;
	}).attr("title", "Opens in a new window");
	
	/**
	 * Login Button Dialog
	 */
	$("#logInButton a").click(function(){
		$("#loginForm").dialog({
			modal:true,
			resizable: false,
			title: 'ANYVAN Login',
			dialogClass: 'loginBox',
			height: 185
		}).dialog('open');
		return false;
	});
	
	//Check to see if an error message occured
	if($("#loginForm .error").length){
		$("#logInButton a").trigger('click');
	}
	
	//Hacky way to detect home at the moment
	if($("#listBox", "#side").length){
		var flashvars = {};
		var params = {wmode:"transparent"};
		var attributes = {wmode:"transparent"}; 
		swfobject.embedSWF("swf/homeFlash.swf", "flash", "548", "434", "9.0.0", "swf/expressInstall.swf", flashvars, params, attributes);
	
		//Random van image
		var vanArray = ["v1.png", "v2.png", "v3.png", "v4.png", "v5.png", "v6.png", "v7.png"];
		var randomVanNumber = Math.floor(Math.random()*7);
		var vanImage = "url(/images/van-images/" + vanArray[randomVanNumber] + ")";
		$("#searchBox .sideImage").css({"backgroundImage": vanImage });
		
		//Random category image
		//var categoryArray = ["c1.png", "c2.png", "c3.png", "c5.png", "c6.png", "c7.png", "c8.png", "c9.png", "c10.png", "c11.png"];
		var categoryArray = ["c1.png", "c2.png", "c3.png", "c5.png", "c10.png", "c11.png"];
		var randomCategoryNumber = Math.floor(Math.random()*categoryArray.length);
		var categoryImage = "url(/images/cat-images/" + categoryArray[randomCategoryNumber] + ")";
		$("#listBox .sideImage").css({"backgroundImage": categoryImage });
	
		/**
		 * Trigger login button if #login is in the url
		 */
		var query = window.location.href;
		var vars = query.split("#");
		if(vars[1] === "login"){
			$("#logInButton a", "#header").trigger("click");
		}
	}
	
	/**
	 * Random Testimonial
	 */
	if($("#med").length){
		var quotes = [
			{
				one: "<strong>'</strong>My courier was happy to deliver at the specific time I wanted, which saved me having to take a day off work to wait for couriers who only offer 'between the hours of 9 and 5'!<strong>,</strong> <i>Joanne - Leeds</i>",
				two: "<strong>'</strong>I have arranged for 3 big christmas gifts to arrive on Christmas eve for less than &pound;80! Another courier company quoted me &pound;270! I will definitely use Anyvan again.<strong>,</strong> <i>Graeme - Aberdeen</i>"
			},
			{
				two: "<strong>'</strong>Anyvan has made my move to France so much easier! I have spent half what I was originally quoted to move ALL of my furniture!<strong>,</strong> <i>Anna - Marseille</i>",
				one: "<strong>'</strong>I was able to get a better sale on Ebay for my car as I could offer my customer very cheap delivery via Anyvan. The transporter got it there bang on time!<strong>,</strong> <i>Mike - Portsmouth</i>"
			},
			{
				one: "<strong>'</strong>I am useless with computers but this website is very easy to use and keeps you updated constantly. I managed to list my furniture and was receiving quotes within minutes!<strong>,</strong> <i>Jean - Dorset</i>",
				two: "<strong>'</strong>I got my sofa moved nearly 200 miles for just over &pound;60 pounds. I couldn't believe how cheap the quotes I was getting were!!<strong>,</strong> <i>Emily - Carlisle</i>"
			},
			{
				one: "<strong>'</strong>I can't believe we have managed to move flats for under &pound;200 on Anyvan. I thought it would cost me an absolute fortune. We probably could've moved for even less if I'd let the auction run longer. Unbelievable!<strong>,</strong> <i>Allan and Kara - Manchester</i>",
				two: "<strong>'</strong>The 'save your search' feature is brilliant as I don't have much time to sit online all day so I get email updates on my mobile phone when new jobs come up.<strong>,</strong> <i>Gary - Harlow</i>"
			},
			{
				one: "<strong>'</strong>It's a shame something like this hasn't existed sooner, I have lost count of how many trips I've done with empty backloads. Anyvan is brilliant!!!<strong>,</strong> <i>Sandeep - London</i>",
				two: "<strong>'</strong>When I was told about Anyvan, I was a bit unsure of how dependable these drivers would be, but I got onto the website and could see by their ratings that I would be in safe hands.<strong>,</strong> <i>Rose - Twickenham</i>"
			},
			{
				one: "<strong>'</strong>It took me less than 3 mins to list my items and I must have received about 8 quotes in half a day. This website has saved me hours of trawling the internet and making phone calls, it is so helpful.<strong>,</strong> <i>Karen - North London</i>",
				two: "<strong>'</strong>Being a bit of an environmentalist, I think this website is fantastic. It is helping save the environment in a big way. Anyvan has my full support.<strong>,</strong> <i>Tanya - Southampton</i>"
			},
			{
				one: "<strong>'</strong>I have discovered Anyvan at the perfect time. I was having a nightmare finding couriers to get my Christmas gifts out on time. Now I am spoilt for choice!<strong>,</strong> <i>Maria - Stourbridge</i>",
				two: "<strong>'</strong>I love spending hours on Ebay finding bargains, Anyvan has now given me access to all the 'local pickup only' items too. Brilliant!!!<strong>,</strong> <i>Sophie - Kingston-upon-Thames</i>"
			},
			{
				one: "<strong>'</strong>This website is great. I have managed to find several jobs on routes im already taking up and down the country, helping me fill my van up.<strong>,</strong> <i>Steve - Croydon</i>",
				two: "<strong>'</strong>I am managing to take home more money now that I can find jobs along my weekly runs to Scotland. This website is a great help and I've recommended it to lots of other drivers.<strong>,</strong> <i>Karl - Portsmouth</i>"
			},
			{
				one: "<strong>'</strong>You saved me 2 days wages so I'm over the moon. Its great how you can view all bids so quickly and decide for yourself. Great website.<strong>,</strong> <i>Jill - Leeds</i>",
				two: "<strong>'</strong>&pound;90 to transport my motorbike 250 miles... absolutely amazing price and great communication the whole way.<strong>,</strong> <i>David - Tunbridge Wells</i>"
			} 
		];
		
		//Settings
		var testimonialSettings = {
			timing: 6000,
			fadeSpeed: 400
		};
		
		var $med = $("#med");
		var randomQuoteNumber;
		var displayTestimonials = function(){
			randomQuoteNumber = Math.floor(Math.random()*quotes.length);
			$med.find("blockquote").fadeOut(testimonialSettings.fadeSpeed, function(){
				$med.find("blockquote:first").html(quotes[randomQuoteNumber].one).end().find("blockquote:last").html(quotes[randomQuoteNumber].two);
				$med.find("blockquote").fadeIn(testimonialSettings.fadeSpeed);
			});
		};
		var testimonialCycle = setInterval(displayTestimonials, testimonialSettings.timing);
	}
	
	/**
	 * Accept Bid Page
	 */
	if($("#accept-bid").length){
		$(".radio-item", document.getElementById("payment-wrap-how")).each(function(i){
			$(this).attr("id", "payment"+1);
		});
	}
	
	/**
	 * Custome Dash: What happens next?
	 */
	if($("#whats-moving").length){
		var $steps = $(".nextStep", document.getElementById("whats-moving"));
		var $liNav = $("ul li", document.getElementById("whats-moving"));
		//Select first
		$steps.filter(":not(:first)").hide();
		$liNav.filter(":first").addClass("active");
		
		$liNav.find("a").click(function(){
			var $this = $(this);
			$liNav.removeClass("active");
			var thisID = $this.parent().attr("class");
			$this.parent().addClass("active");
			//Show relevent step
			$steps.hide().filter("#"+thisID).show();
		});
	}
	
	/**
	 * Formatting of Step Address
	 */
	if($("#stepAddresses").length || $("#edit-listing-address").length){
		$("label[for=p_address_line-0], label[for=d_address_line-0]").parent().addClass("addressContainer");
		$("#d_save0, #p_save0").parent().addClass("saveAddress");
	}
	
	/**
	 * View / Big page
	 */
	if($("#view-and-bid").length){
		$("#detail p:even").addClass("even");
		$("#detail p:last").addClass("last");
	}
	
	/**
	 * Step 5 datepicker
	 */
	if($("#stepAddresses").length){
		$(".calIcon").click(function(){
			$(this).siblings("input").trigger("focus");
		});
	}
		
	/**
	 * Make Widge Header Text Clickable
	 */
	$(".anyvan-widget-header span").click(function(e){
		$(this).parent().find(".anyvan-expander").click();
	});
	
	/**
	 * Add Length / Width / Depth to the dimension form
	 */
	if($(".move-item-dimension", "#job-details-form")){
		$("input[name='item_width[]']").val("W").focus(function(){
			clearDimensionPrompts('W');
		}).blur(function(){
			addDimensionPrompts('W');
		});
		$("input[name='item_length[]']").val("D").focus(function(){
			clearDimensionPrompts('D');
		}).blur(function(){
			addDimensionPrompts('D');
		});
		$("input[name='item_height[]']").val("H").focus(function(){
			clearDimensionPrompts('H');
		}).blur(function(){
			addDimensionPrompts('H');
		});
	}
	
	$('#job-details-form').submit(function(){
		clearDimensionPrompts('A');
	});
	
	/**
	 * Help Centre JS
	 */
	if($('#help-centre-static').length){
		//Hide the TP FAQ's on first load
		$("#faqsectionTP-header, #faqsectionTP-content").hide();
		
		$("#customerLI").click(function(){
			$("#faqsectionCust-header, #faqsectionCust-content").show();
			$("#faqsectionTP-header, #faqsectionTP-content").hide();
		});
		$("#tpLI").click(function(){
			$("#faqsectionTP-header, #faqsectionTP-content").show();
			$("#faqsectionCust-header, #faqsectionCust-content").hide();
		});
		
		//Hide all help
		$("#helpcentre-content .info").hide();
		
		$(".listContainer li a", "#helpcentre-content").click(function(e){
			$("#helpcentre-content .info").hide();
			var HREF = $(this).attr('href');
			$(HREF).show();
		});
		
		//Help section
		$('#helpcentre-content .helpCol').not(':first').hide();
		
		$('#helpCentTabs li').click(function(){
			$('#helpCentTabs li').removeClass('active').filter(this);
			$(this).addClass('active');
			var index = $('#helpCentTabs li').index(this);
			$('.helpCol', '#helpcentre-content').hide().eq(index).show();
		});
		
		//FAQ Section Customer
		var $allCustContent = $(".faqColCust div", "#faqsectionCust-content");
		$allCustContent.hide();
		
		$(".faqColCust h4", "#faqsectionCust-content").click(function(){
			$allCustContent.hide();
			$this = $(this);
			$this.parents(".faqColCust").find("div").show();
		});
		
		//FAQ Section TP
		var $allTPContent = $(".faqColTP div", "#faqsectionTP-content");
		$allTPContent.hide();
		
		$(".faqColTP h4", "#faqsectionTP-content").click(function(){
			$allTPContent.hide();
			$this = $(this);
			$this.parents(".faqColTP").find("div").show();
		});
		
		$("#tips li:even").addClass("stripe");
		
		/**
		 * Tooltips
		 */
	}
	$("#moreOne").qtip({
		show: 'mouseover', 
		hide: 'mouseout',
		content: "If your item/s need more than one person to assist, please tick the box.  However, quotes you receive will be higher than if you only need one person.  If you or a friend can help you will receive cheaper quotes.", 
		style: 'avan',
		position: {
			corner: {
				target: 'topLeft',
				tooltip: 'bottomRight'
			}
		}
	});
	
	$(".tM").qtip({
		show: 'mouseover', 
		hide: 'mouseout',
		content: "By ticking the box it will store your address making it easier and quicker for you next time round.", 
		style: 'avan',
		position: {
			corner: {
				target: 'topLeft',
				tooltip: 'bottomRight'
			}
		}
	});
	
	$(".sA").qtip({
		show: 'mouseover', 
		hide: 'mouseout',
		content: "By ticking the box it will store the address making it easier and quicker for you next time round.", 
		style: 'avan',
		position: {
			corner: {
				target: 'topLeft',
				tooltip: 'bottomRight'
			}
		}
	});
	
	$("#whatTextActive").qtip({
		show: 'mouseover', 
		hide: 'mouseout',
		content: "If you wish to accept one of the bids click the ACCEPT bid button and follow the simple deposit payment procedure to secure the job.  Once a deposit has been received you will receive your chosen Transport Provider's contact details enabling you to contact them directly.  Don't forget to leave feedback so others can see how you got on.", 
		style: 'avanSmall',
		position: {
			corner: {
				target: 'topLeft',
				tooltip: 'bottomRight'
			}
		}
	});
	
	
});

$.fn.qtip.styles.avanSmall = { // Last part is the name of the style
	width: 170,
	fontSize: '0.69em',
	padding: 5,
	background: '#D71C65',
	color: '#fff',
	textAlign: 'left',
	tip: 'bottomRight',
	border: {
	     width: 1,
	     radius: 4,
	     color: '#D0D0D0'
	}
};

$.fn.qtip.styles.avan = { // Last part is the name of the style
	width: 200,
	fontSize: '0.69em',
	padding: 5,
	background: '#D71C65',
	color: '#fff',
	textAlign: 'left',
	tip: 'bottomRight',
	border: {
	     width: 1,
	     radius: 4,
	     color: '#D0D0D0'
	}
};

function clearDimensionPrompts(dimension) {
	if ($("input[name='item_width[]']").val() == 'W' && (dimension == 'W' || dimension == 'A')) $("input[name='item_width[]']").val('');
	if ($("input[name='item_length[]']").val() == 'D' && (dimension == 'D' || dimension == 'A')) $("input[name='item_length[]']").val('');
	if ($("input[name='item_height[]']").val() == 'H' && (dimension == 'H' || dimension == 'A')) $("input[name='item_height[]']").val('');
}

function addDimensionPrompts(dimension) {
	if ($("input[name='item_width[]']").val() == '' && dimension == 'W') $("input[name='item_width[]']").val('W');
	if ($("input[name='item_length[]']").val() == '' && dimension == 'D') $("input[name='item_length[]']").val('D');
	if ($("input[name='item_height[]']").val() == '' && dimension == 'H') $("input[name='item_height[]']").val('H');
}


