///////////////////////////////////////////////////////////////////////////////////////
// 
// (c) Copyright 2011 Crusader Ltd
// 
// Author:  Stephen Last
// Created: 18/05/2011
// 
///////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
// On load
/////////////////////////////////////////////////////////
$(document).ready(function(){
	
	if ($("#wrap").length) { emailInit(); crusaderPay(); }
	if ($("#desktop").length) { crusaderDesktop(); }
	//if ($("#mobile").length) { crusaderMobile(); }
	
});

/////////////////////////////////////////////////////////
// Pay requests
/////////////////////////////////////////////////////////
function crusaderPay() {
	
	var placeholder = 'Card Postcode or Zip Code';
	$("#form_errors").hide();
	
	$("#frm_pay").bind("submit",function(e){
		
		var form_ok = true;
		var msg = '';
		var radio_val = $('input:radio[name=option]:checked').val();
		var postcode_val = $('input:text[name=postcode]').val();
		
		if (postcode_val == placeholder) {
			form_ok = false;
			msg += 'Enter card postcode';
		}
		if (radio_val === undefined) {
			form_ok = false;
			if (msg != '') {
				msg += ' and select a payment';
			} else {
				msg += 'Select a payment';
			}
		}
		if (!form_ok) {
			msg += '.';
			$("#form_errors").hide().html('').append(msg).fadeIn(500);
			e.preventDefault();
		}
		
	});
	
	$("#postcode").CLPlaceHolder({
		'placeholder':placeholder,
		'col_light':'#aaa',
		'col_dark':'#c10808'
	});
	
}

/////////////////////////////////////////////////////////
// Mobile
/////////////////////////////////////////////////////////
//function crusaderMobile() {
//	$(".box_wrap").hide();
//	$("#box_home").show();
//	$(".box_lnk").bind('click',function(e){
//		var id = $(this).attr("id").replace("lnk_","");
//		$(".box_wrap").hide();
//		$("#box_"+id).show();
//		$(".box_lnk").removeClass("on").addClass("off");
//		$(this).removeClass("off").addClass("on");
//		e.preventDefault();
//	});
//}

/////////////////////////////////////////////////////////
// Desktop
/////////////////////////////////////////////////////////
function crusaderDesktop() {
	
	// Setup
	$(".c_content").hide();
	$(".box2_shadow").css("opacity",".1");
	$(".box2_wrap").each(function(){
		$(this).append("<a href='#' id='close'>Close</a>");
		$("#close",this).bind("click",function(e){
			boxClose(true,true);
			e.preventDefault();
		});
	});
	
	boxLinks();
	
	// Setup links
	function boxLinks() {
		
		$(".box2_lnk").unbind("click").bind("click",function(e){
			
			var page = "";
			var id = $(this).attr("href").replace("#","").replace("http://www.crusader.co.uk/new/","");
			var rel = $(this).attr("rel");
			var backlinks = $("#backlinks").text();
			
			if (rel=="back") {
				boxpage = backlnkGetPrev();
			} else {
				boxpage = id;
			}
			
			boxClose(false,false);
			backlnkSet(boxpage);
			
			$(".box2_wrap").fadeIn(200,function(){
				setTimeout(function(){
					var content = $("#c_"+boxpage).html();                         // Get the content
					$(".box2_inner").hide().html(content).fadeIn(200);          // Put the content in
					$(".box2_inner h1:first").hide();                           // Hide the title
					if (boxpage == 'maps') { googleMap('google_map'); }
					if (boxpage == 'contact') {
						if (rel) { pencils(rel); } else { pencils("ltd"); }
						if (!backlinks) { $("#backlnk_contact").hide(); } else { $("#backlnk_contact").show(); }
					}
					boxLinks();
					backCloseLinks();
					top();
				},100);
			});
			
			e.preventDefault();
		});
		
	}
	
	// Close back button
	function backCloseLinks() {
		$(".backclose_lnk").unbind("click").bind("click",function(e){
			boxClose(true,true);
			e.preventDefault();
		});
	}
	
	// Close popup
	function boxClose(fade,clear) {
		if (fade) {
			$(".box2_wrap").fadeOut(100,function(){
				$(".box2_inner").html('');
			});
		} else {
			$(".box2_inner").html('');
		}
		if (clear) {
			backlnkClear();
		}
	}
	
	// Close pay successful box
	$("#pay_close").bind("click",function(e){
		$(".pay_success").fadeOut(200,function(){
			var root = $("#root").html();
			setTimeout(function(){
				window.location='http://www.crusader.co.uk'+root;
			},1000);
		});
		e.preventDefault();
	});
	// Print pay successful box
	$("#pay_print").bind("click",function(e){
		setTimeout(function(){ window.print(); },200);
		e.preventDefault();
	});
	
	// Titles
	$(".cart").each(function(){
		$(this).attr("title","Online Shop");
	});
	$(".fb").each(function(){
		$(this).attr("title","Facebook Page");
	});
	$(".b2b_clients li").each(function(){
		var title = $(this).text();
		$(this).attr("title",title);
	});
	$(".ps_clients li").each(function(){
		var title = $(this).text();
		$(this).attr("title",title);
	});
	
	// Pencils
	function pencils(id) {
		// Default
		$("ul li a").removeClass("pencilon").addClass("penciloff");
		$("#pencil_"+id).removeClass("penciloff").addClass("pencilon");
		var html = $("#contactinfo_"+id).html();
		$(".paper_content").fadeOut(50,function(){
			$(this).html(html).fadeIn(300);
		});
		// On click
		$("ul li a").bind("click",function(e){
			var id = $(this).attr("id").replace("pencil_","");
			$("ul li a").removeClass("pencilon").addClass("penciloff");
			$(this).removeClass("penciloff").addClass("pencilon");
			var html = $("#contactinfo_"+id).html();
			$(".paper_content").fadeOut(50,function(){
				$(this).html(html).fadeIn(300);
			});
			e.preventDefault();
		});
	}
	
	// Back links
	function backlnkSet(page) {
		var current = $("#backlinks").text();
		$("#backlinks").text(current + "|" + page);
	}
	function backlnkClear() {
		$("#backlinks").text("");
	}
	function backlnkGetPrev() {
		var current = $("#backlinks").text();
		var arr = current.split("|");
		var arrlen = arr.length;
		var lastpage = arr[arrlen-2];
		return lastpage;
	}
	
	// Top
	function top() {
		$(".top").bind("click",function(e){
			$(".box2_inner").scrollTop(0);
			e.preventDefault();
		});
	}
	
}

/////////////////////////////////////////////////////////
// Google maps API v3
/////////////////////////////////////////////////////////
function googleMap(id) {
	
	var root = $("#root").html();
	var fldr = $("#fldr").html();
	
    var latlng_oh = new google.maps.LatLng(52.892850,-1.147320); // Oxford House
	//var latlng_n2 = new google.maps.LatLng(52.892550,-1.149341); // No.2 Easthorpe Street
	var latlng_n4 = new google.maps.LatLng(52.892640,-1.149200); // No.4 Easthorpe Street
	//var latlng_cp = new google.maps.LatLng(52.893600,-1.145600); // Car park
	
    var map_opts = {
		zoom:16, //10
		center:latlng_oh,
		mapTypeId:google.maps.MapTypeId.ROADMAP
	};
	
    var map = new google.maps.Map(document.getElementById(id),map_opts);
	
	// Oxford House
	var info_oh = new google.maps.InfoWindow({ content:googleMapHTML('oh') });
	var marker_oh = new google.maps.Marker({
		position: latlng_oh,
		map: map,
		title:"Crusader Head Office"
	});
	googleMapInfo(info_oh,marker_oh,'oxfordhouse');
	
	// No.4 Easthorpe Street
	var info_n4 = new google.maps.InfoWindow({ content:googleMapHTML('n4') });
	var marker_n4 = new google.maps.Marker({
		position: latlng_n4,
		map: map,
		title:"No.4 Easthorpe Street"
	});
	googleMapInfo(info_n4,marker_n4,'no4');
	
		// Info HTML
	function googleMapHTML(which) {
		var str = '<div style="line-height:14px;height:100px;">';
		if (which=='oh') {
			str += '<strong>Crusader Head Office</strong><br />';
			str += '<img src="'+root+fldr+'/_imgs/oxford_house.jpg" width="85" height="67" alt="Oxford House" style="float:left;margin:10px 5px 0 0;" />';
			str += '<div style="float:left;margin:9px 5px 0 0;">Oxford House,<br />Easthorpe Street,<br />Ruddington,<br />Nottingham,<br />NG11 6LA</div>';
		}
		if (which=='n4') {
			str += '<strong>Costume Hire Retail Shop</strong><br />';
			str += '<img src="'+root+fldr+'/_imgs/no4.jpg" width="85" height="67" alt="No.4 Easthorpe Street" style="float:left;margin:10px 5px 0 0;" />';
			str += '<div style="float:left;margin:9px 5px 0 0;">No.4,<br />Easthorpe Street,<br />Ruddington,<br />Nottingham,<br />NG11 6LA</div>';
		}
		str += '</div>';
		return str;
	}
	// Info click events
	function googleMapInfo(info,mkr,cl){
		$("."+cl).bind("click",function(){
			googleMapInfoShow(info,mkr,cl);
		});
		google.maps.event.addListener(mkr,'click',function(){
			googleMapInfoShow(info,mkr,cl);
		});
	}
	// Open info and highlight
	function googleMapInfoShow(info,mkr,cl) {
		info_oh.close();
		info_n4.close();
		info.open(map,mkr);
		$(".loc_on").removeClass("loc_on").addClass("loc_off");
		$("."+cl).removeClass("loc_off").addClass("loc_on");
	}

	  
}

///////////////////////////////////////////////////////////
// Convert e-mail address (anti-spam)
///////////////////////////////////////////////////////////
function emailInit() {
	$("a[href]").each(function(){
		if ($(this).attr("href").indexOf("mailto:")!=-1) {
			var new_mail = $(this).attr("href").replace("-at-","@").replace("-dot-",".").replace("-dot-",".");
			$(this).attr("href",new_mail);
			$(this).html(new_mail.replace("mailto:",""));
		}
	});
}

///////////////////////////////////////////////////////////
// INPUT PLACE HOLDER
// Crusader jQuery plugin
// CLPlaceHolder
// 14/04/2011 - SL
///////////////////////////////////////////////////////////
(function($){
    
	var methods = {
		init:function(opts){
			
			// Settings defaults
			var settings = {
				'placeholder' : 'Place Holder',
				'col_light'   : '#eee',
				'col_dark'    : '#444'
			};
			return this.each(function(){
				// If options exist, lets merge them with our default settings
				if (opts) { $.extend(settings,opts); }
				// On load
				var val = $(this).val();
				if (val=='') { $(this).val(settings.placeholder).css("color",settings.col_light); }
				// On focus
				$(this).bind("focus",function(){
					var val = $(this).val();
					$(this).css("color",settings.col_dark);
					if (val==settings.placeholder) { $(this).val(""); }
				});
				// On blur
				$(this).bind("blur",function(){
					var val = $(this).val();
					if (val=='') { $(this).val(settings.placeholder).css("color",settings.col_light); }
				});
			});
			
		}
	};
	
	$.fn.CLPlaceHolder = function(method){
		if (methods[method]) {
			return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
		} else if ( typeof method === 'object' || !method ) {
			return methods.init.apply(this,arguments);
		} else {
			$.error('Method '+method+' does not exist on jQuery.tooltip');
		}
    };
	
})(jQuery);

	  

