/***********************************************
***  BUTTON ROLLOVER & PRELOAD  ****************
************************************************/

$(function(){
	// button rollover
	if ($('.imgBtn') != null) {
		$('.imgBtn').hover(function(){
				if(!$(this).is('.disabled'))
					$(this).attr("src", $(this).attr("src").replace(/^(.*?)(?:-hover)?(\..*?)$/i,'$1-hover$2'));
			},
			function(){
					if(!$(this).is('.disabled'))
						$(this).attr("src", $(this).attr("src").replace(/^(.*)-hover(\..*?)$/i, '$1$2'));
				});
	}
	// preload image rollovers
	if ($("img.imgBtn, img[src^='images/buttons/btn'], input[src^='images/buttons/btn']") != null) {
	    $("img.imgBtn, img[src^='images/buttons/btn'], input[src^='images/buttons/btn']").each(function(){
			    $("<img>").attr("src", $(this).attr("src").replace(/(btn-.*)(\..*?)/i, '$1-hover$2'));
		    });
	}
});

/***********************************************
***  EXTERNAL LINKS  ***************************
************************************************/

$(document).ready(function() {
	$("a[rel=external]").each(function() {
		$(this).addClass('external');
		$(this).attr({
			title: "Open this page in a new window",
			target: "_blank"
		});
	})
});

/***********************************************
***  COMPACT FORM (LABEL OVER INPUT)  **********
************************************************/

$(function(){
	$('form.compact input, form.compact select, form.compact textarea').focus(function(){
			$('label[for=' + $(this).attr('id') + ']').hide();
		});
	$('form.compact input, form.compact select, form.compact textarea').each(function(ele){
			if($(this).val().length > 0)
				$('label[for=' + $(this).attr('id') + ']').css({ display: "none" });
		});
	$('form.compact input, form.compact select, form.compact textarea').blur(function(){
			if($(this).val().length == 0)
				$('label[for=' + $(this).attr('id') + ']').show();
		});
	$('form.compact').removeClass('compact');
});

/***********************************************
***  FOOTER SLIDING  ***************************
************************************************/

$(function(){
	$('#footerLinks ul a').hover(function(){
			$(this).stop().animate({
					paddingLeft: 8
				}, 200);
		},
		function(){
				$(this).stop().animate({
						paddingLeft: 0
					}, 200);
			});
});

/***********************************************
***  TAB BUTTON SLIDING  ***********************
************************************************/

$(function(){
	$('.infoTile .desc a, .blockList .viewMore, .featuredProducts .seeRange, .orderForm .addToCart').hover(function(){
			$(this).stop().animate({
					top: 0
				}, 200);
		},
		function(){
				$(this).stop().animate({
						top: -7
					}, 200);
			});
});

/***********************************************
***  TAB BUTTON SLIDING  ***********************
************************************************/

$(function(){
	$('.productSelection .viewMore').hover(function(){
			$(this).stop().animate({
					top: 173
				}, 200);
		},
		function(){
				$(this).stop().animate({
						top: 168
					}, 200);
			});
});

/***********************************************
***  BLOCK LIST BUTTON SLIDING  ****************
************************************************/
/*
$(function(){
	$('.blockList .viewMore').hover(function(){
			$(this).stop().animate({
					top: 0
				}, 200);
		},
		function(){
				$(this).stop().animate({
						top: 168
					}, 200);
			});
});*/

/***********************************************
***  HOME PAGE BANNER ROTATIONS  ***************
************************************************/

$(function(){
	var delay = 4000;
	var fadeTime = 500;

	var curr = 1;

	function intFunc(){
		var prev = curr;
		curr++;

		if (curr === 6){
			curr = 1;
		}
		$('#homeHero > li:nth-child(' + prev + ')').fadeOut(fadeTime);
		$('#homeHero > li:nth-child(' + curr + ')').fadeIn(fadeTime);

	}
	var interval = window.setInterval(intFunc, delay);
});

/***********************************************
***  FADE IN BOX  ******************************
************************************************/

$(function(){
	// nav functionality
	$(".changeCountry").click(function(){
			if($(this).parent().hasClass('expanded')) {
				$(this).parent().removeClass('expanded');
				$(this).parent().addClass('collapsed');
				$(".loc > ul").fadeOut("fast");
			}
			else if($(this).parent().hasClass('collapsed')) {
				$(this).parent().addClass('expanded');
				$(this).parent().removeClass('collapsed');
				$(".loc > ul").fadeIn("fast").children().click(function(){
					var code = this.id.replace(/countryID-/g,'');	
					var newLocation = window.location.href.replace(/#.*/g,'');
					newLocation = ChangeQueryStringValue(newLocation,"code",code)
					//newLocation += 'code='+code + window.location.hash;
					window.location.href = newLocation;
					return false;
				});
			}
		});
});




/***********************************************
***  CUFON  ************************************
************************************************/

$(function() {
	Cufon.replace('#menu .navMain');
	Cufon.replace('#menu .megaFlyout .thumbText');
	Cufon.replace('.homeContent h1');
	Cufon.replace('#rightCol h1');
	Cufon.replace('.productSlides .price');
	Cufon.replace('.featuredProducts h1');
	Cufon.replace('.infoTile h1');
	Cufon.replace('.tabs li a', { hover: true });
	Cufon.replace('.productSelection h2');
	Cufon.replace('.productSelection .note');
	Cufon.replace('.productSelection .price');
	Cufon.replace('.leftCol h2');
	Cufon.replace('.rightCol h2');
	Cufon.replace('.leftCol h3');
	Cufon.replace('.rightCol h3');
	Cufon.replace('h2.cufon');
	Cufon.replace('.contentBlock h2');
	Cufon.replace('.blockList h2');
	Cufon.replace('.subMenuTitle');
	Cufon.replace('h2.usa');
	Cufon.replace('h2.gbr');
	Cufon.replace('h2.ireland');
	Cufon.replace('h2.canada');
	Cufon.replace('h2.australia');
	Cufon.replace('.contactForm h2');
	Cufon.replace('.addressDetails h2');
	Cufon.replace('form fieldset legend');
	Cufon.replace('h3.reviewTitle');
	Cufon.replace('#upsell h2');
    	Cufon.now();
});

/***********************************************
***  HOME PAGE TILES  **************************
************************************************/

$(document).ready(function() {

	$("#homeTiles li").hover(function() { 

		var thumbOver = $(this).find("img").attr("src");
		$(this).find("a").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
		$(this).find("span").stop().fadeTo('fast', 0 , function() {
			$(this).hide()
		});
	} , function() { 
		$(this).find("span").stop().fadeTo('fast', 1).show();
	});

});

/***********************************************
***  Main Nav Hover Intent *********************
************************************************/
$(document).ready(function(){
	$("#menu li").hoverIntent({
		sensitivity: 7, 
		interval: 100, 
		over: function(){$(this).children('.megaFlyout').show();}, 
		timeout: 350, 
		out: function(){$(this).children('.megaFlyout').hide();}
	});
}); // close document.ready


function ChangeQueryStringValue(action,qsname,qsvalue){
	if (action.indexOf('?') == -1 )	 //If no querystring present
		return (action+"?"+qsname+"="+UrlEncode(qsvalue)); //x.asp?Name=Peter+Paul
	else
	{		
		if ( (action.indexOf("?"+qsname+"=") == -1 ) && (action.indexOf("&"+qsname+"=") == -1 ) ) //Passed querystring not already present		


			{
				return (action+"&"+qsname+"="+UrlEncode(qsvalue)); //x.asp?Age=25&Name=Peter+Paul;
			}
			else //Passed Querystring already present, replace it no matter if it is ?Name= or &Name=


				{
					var replaceQSPrefix=(action.indexOf("?"+qsname+"=")==-1)?"&"+qsname+"=":"?"+qsname+"=";
					var replaceQSSuffix=""; //This will be John in case of ?Name=John or &Name=John 
					startpos=action.indexOf(replaceQSPrefix);
					
					for (a=(startpos+replaceQSPrefix.length);a<action.length;a++)


						{
							if ( action.charAt(a)=='&' ) //Next querystring beginning
								break;
							else
								replaceQSSuffix += action.charAt(a);
						}
						newaction=action.replace(replaceQSPrefix+replaceQSSuffix,replaceQSPrefix+UrlEncode(qsvalue));
						return newaction;
					}	
				}		
		}
function UrlEncode(text) {		

		//text=ReplaceAll(text, "/\", "%2F"); 
		text=ReplaceAll(text, "?", "%3F");
		text=ReplaceAll(text, "=", "%3D");
		text=ReplaceAll(text, "&", "%26");
		text=ReplaceAll(text, " ", "+");
		text=ReplaceAll(text, ",", "%2c");	
		
		return text;
}
function ReplaceAll(varb, replaceThis, replaceBy){	
	newvarbarray=varb.split(replaceThis);
	newvarb=newvarbarray.join(replaceBy);	
	return newvarb;
}


function checkThisBox(inID){
	if ($('input[name='+inID+']').is(':checked')){
		$('input[name='+inID+']').attr('checked', false);
	}else{
		$('input[name='+inID+']').attr('checked', true);
	}
}

/***********************************************
***  Review Forms *********************
************************************************/
$(function() {
	

	$('.reviewComments').click(function(){

		//alert($(this).parent('.blogContent').next('.reviewBlock').html());
		//alert($(this).parent().parent().next().html());
		$(this).parent().next().show();
	});


	/*SUBMIT REVIEW FORM*/
	$('.btnSubmitReview').click(function() {

		var inID			= $(this).parent().children('input[name=inID]').val();
		var inType			= $('#inType-'+inID).val();
		var reviewName		= $('#reviewName-'+inID).val();
		var reviewTitle		= $('#reviewTitle-'+inID).val();
		var reviewContent	= $('#reviewContent-'+inID).val();
		var verify			=  $('#verifyReview-'+inID).val();

		var errorMsgs = new Array();
		var x = 0;
		if (reviewTitle == '') {
			errorMsgs[x] = 'Please enter your review title.';
			x++;
		}
		if (reviewContent == '') {
			errorMsgs[x] = 'Please enter your review content.';
			x++;
		}
		if (verify.length != 4) {
			errorMsgs[x] = 'Please enter the verification code.';
			//$('#verify').addClass('error');
			x++;
		}
		
				
		if (errorMsgs.length > 0) {
			$('#reviewMsg-'+inID).html('<p class="error">' + errorMsgs.join("<br/>") + '</p>');
		} else {
			// Add items to cart
			$.ajax({type: "POST",
					url: $('#reviewForm-'+inID).attr('action'),
					data: { 'ajaxapp'		: 'AddReview',
							'inType'		: inType,
							'inID'			: inID,
							'reviewName'	: reviewName,
							'reviewTitle'	: reviewTitle,
							'reviewContent'	: reviewContent,
							'verify'		: verify
						},
					beforeSend: function() {
						$('#btnSubmitReview-'+inID).hide();
						$('#reviewProcessing-'+inID).show();
					},
					success: function(r) {
						var status = parseInt(r.root.status);
						var msgtxt= r.root.msgtxt;
						
						$('#reviewMsg-'+inID).html(msgtxt);
						if (status == 1){
							$('#reviewName-'+inID).val('');
							$('#reviewTitle-'+inID).val('');
							$('#reviewContent-'+inID).val('');
							$('#verifyReview-'+inID).val('');
						}
						if (r.root.verifyImage){
							$('#verificationImageReview-'+inID).replaceWith(r.root.verifyImage);
						}

						$('#reviewProcessing-'+inID).hide();
						$('#btnSubmitReview-'+inID).show();
						
					},
					error: function() {
						$('#reviewMsg-'+inID).html('<p class="error">An error has occurred. Please try again later.</p>');
						$('#reviewProcessing-'+inID).hide();
						$('#btnSubmitReview-'+inID).show();
					}
				});
		}
	});

	/*$('#addAnotherFriend').click(function(){
		var friendsCount = $('#friendsCount').val() + 1;
		$('#toName'+$('#friendsCount').val()).next().after('<label for="toEmail'+friendsCount+'">Friends email:<em>*</em></label><input type="text" class="txtInput" id="toEmail'+friendsCount+'" name="toEmail" value="" /><div class="clearFloat"></div><label for="toName'+friendsCount+'">Friends name:<em>*</em></label><input type="text" class="txtInput" id="toName'+friendsCount+'" name="toName" value="" /><div class="clearFloat"></div>');
		$('#friendsCount').val(friendsCount);

	})*/


	/*EMAIL A FRIEND FORM*/
	$('#btnSubmitEmailFriend').click(function(){

		var yourEmail	= $('#yourEmail').val();
		var yourName	= $('#yourName').val();
		var toEmail		= $('#toEmail').val();
		var toName		= $('#toName').val();
		var toSubject	= $('#toSubject').val();
		var toMessage	= $('#toMessage').val();
		var verify		=  $('#verify').val();


		var errorMsgs = new Array();
		var x = 0;
		if (yourEmail == '') {
			errorMsgs[x] = 'Please enter your email address.';
			//$('#yourEmail').addClass('error');
			x++;
		}
		if (yourName == '') {
			errorMsgs[x] = 'Please enter your name.';
			//$('#yourName').addClass('error');
			x++;
		}
		if (toEmail == '') {
			errorMsgs[x] = 'Please enter your firends email address.';
			//$('#toEmail').addClass('error');
			x++;
		}
		if (toName == '') {
			errorMsgs[x] = 'Please enter your firends name.';
			//$('#toEmail').addClass('error');
			x++;
		}
		//if (toSubject == '') {
		//	errorMsgs[x] = 'Please enter the subject.';
			//$('#toSubject').addClass('error');
		//	x++;
		//}
		//if (toMessage == '') {
		//	errorMsgs[x] = 'Please enter a message your friend will receive.';
			//$('#toMessage').addClass('error');
		//	x++;
		//}
		if (verify.length != 4) {
			errorMsgs[x] = 'Please enter the verification code.';
			//$('#verify').addClass('error');
			x++;
		}
				
		if (errorMsgs.length > 0) {
			$('#emailFriendMsg').html('<p class="error">' + errorMsgs.join("<br/>") + '</p>');
		} else {
			// Add items to cart
			$.ajax({type: "POST",
					url: $('#emailFriendForm').attr('action'),
					data: { 'ajaxapp'		: 'SendToFriend',
							'yourEmail'		: yourEmail,
							'yourName'	: yourName,
							'toEmail'	: toEmail,
							'toName'	: toName,
							'toSubject'	: toSubject,
							'toMessage'	: toMessage,
							'verify'	: verify
					},
					beforeSend: function() {
						//$('#yourEmail').val('');
						//$('#yourName').val('');
						$('#btnSubmitEmailFriend').hide();
						$('#emailFriendProcessing').show();
					},
					success: function(r) {
						//$('#emailFriendMsg').html(r.slice(9));
						var status = parseInt(r.root.status);
						var msgtxt= r.root.msgtxt;

						$('#emailFriendMsg').html(msgtxt);
						if (status == 1){
							$('#toEmail').val('');
							$('#toName').val('');
							$('#verify').val('');
						}
						if (r.root.verifyImage){
							$('#verificationImage').replaceWith(r.root.verifyImage);
						}
						$('#emailFriendProcessing').hide();
						$('#btnSubmitEmailFriend').show();
						
					},
					error: function() {
						$('#emailFriendMsg').html('<p class="error">An error has occurred. Please try again later.</p>');
						$('#emailFriendProcessing').hide();
						$('#btnSubmitEmailFriend').show();
					}
				});
		}
	});
});