﻿
$(document).ready(function() {
	//Site search field - pre-fill value
	//	var textFieldVal = $("#site-search input").attr("value");

	//	$("#site-search input").focus(function() {
	//		textFieldVal = $(this).attr("value");
	//		if (textFieldVal == "Search entire site") {
	//			$(this).attr("value", "");
	//			textFieldVal = $(this).attr("value");
	//		}
	//	});

	//	$("#site-search input").blur(function() {
	//		textFieldVal = $("#site-search input").attr("value");
	//		if (textFieldVal == "" || textFieldVal == undefined) {
	//			$("#site-search input").attr("value", "Search entire site")
	//			textFieldVal = $("#site-search input").attr("value");
	//		}
	//	});

	//Popular Products
	$(".pet-products h3")
		.append('<a href="javascript:void(0);" id="slider-back">Back</a>')
		.append('<a href="javascript:void(0);" id="slider-forward">Forward</a>');

	sliderIntervalID = setInterval(function() {
		doMove(panelWidth, tooFar);
	}, delayLength);

	$("#slider-forward").click(function() {
		clearInterval(sliderIntervalID);
		doMove(panelWidth, tooFar);
		sliderIntervalID = setInterval(function() {
			doMove(panelWidth, tooFar);
		}, delayLength);
	});

	$("#slider-back").click(function() {
		clearInterval(sliderIntervalID);
		doMove(panelNegWidth, tooFar);
		sliderIntervalID = setInterval(function() {
			doMove(panelWidth, tooFar);
		}, delayLength);
	});

	//Pre-fill login fields 
	toggleLoginLabels();

	$(".login .field-email").focus(function() {
		$("#label-email").css("display", "none")
	});

	$(".login .field-password").focus(function() {
		$("#label-password").css("display", "none")
	});

	$("#label-email, #label-forgot-email").click(function() {
		$(this).css("display", "none")
		$(this).parent().find("input.field-email").focus();
	});

	$("#label-password").click(function() {
		$(this).css("display", "none")
		$(".field-password").focus();
	});

	$(".login .field-email").blur(function() {
		toggleLoginLabels();
	});

	$(".login .field-password").blur(function() {
		toggleLoginLabels();
	});

	//Expand login panel 
	$(".login h2").click(function() {
		if ($('#divLoginControls').css('display') == 'none') {
			$('#divForgotPassword').hide();
			$('#divLoginControls').show();
		}
		else {
			toggleLoginPanel($(".login").css("bottom"));
		}
	});

	$('#forgotPassLink').click(function() {
		$('#divForgotPassword').show();
		$('#divMessage').hide();
		$('#divLoginControls').hide();
		return false;
	});

	q = getUrlParams();
	if (q['showPanel'] != null && q['showPanel'] == 'true') {
		toggleLoginPanel($(".login").css("bottom"));
	}
});

function toggleLoginPanel(pos) {
	if (pos == "-30px") {
		$(".login").animate({
			"bottom": 0
		});
	}
	else {
		$(".login").animate({
			"bottom": -30
		});
	}
}


function doMove(panelWidth, tooFar) {
	var leftValue = $(".slide").css("left");
	// Fix for IE
	if (leftValue == "auto") { leftValue = 0; };

	var movement = parseFloat(leftValue, 10) - panelWidth;
	var finalPanel = parseFloat((-tooFar - movement));

	if (movement == tooFar) {
		$(".slide").animate({
			"left": 0
		});
	}
	else {
		if (movement > 0) {
			$(".slide").animate({
				"left": -finalPanel
			});
		}
		else {
			$(".slide").animate({
				"left": movement
			});
		}
	}
}

function toggleLoginLabels() {
	if ($(".field-email").attr("value") != "" && $(".field-email").attr("value") != undefined) {
		$("#label-email").css("display", "none")
	}
	else {
		if ($(".field-password").attr("value") != "" && $(".field-password").attr("value") != undefined) {
			$("#label-password").css("display", "none")
		}
		else {
			$(".login label").css("display", "block")
		}
	}
}

function ShowMessagePanel() {
	toggleLoginPanel($(".login").css("bottom"));
	$('#divForgotPassword').show();
	$('#divMessage').show();
	$('#divForgotPasswordFields').hide();
	$('#divLoginControls').hide();
}

function HideMessageOnError() {
	$('#divForgotPassword').show();
	$('#divMessage').hide();
	$('#divForgotPasswordFields').show();
	$('#divLoginControls').hide();
	toggleLoginPanel($(".login").css("bottom"));
}

function HideMessageOnSuccess() {
	$('#divLoginControls').show();
	$('#divForgotPassword').hide();
	$('#divMessage').hide();
	$('#divForgotPasswordFields').hide();
	toggleLoginPanel($(".login").css("bottom"));
}

/** getUrlParams
* This method will retrieve the parameters passed into the URL
* The parameters passed must follow url encoding and GET method standards
*/
function getUrlParams() {
	curURI = window.location.href;
	if (curURI.indexOf("?")) { //check if we have a ?
		//we need to take a substring - starting at the char AFTER the '?'
		qString = curURI.substring(curURI.indexOf('?') + 1);
		//if there are ampersands, split to multiple params
		if (qString.indexOf('&')) {
			qString = qString.split('&');
		}
		var qArray = Array();
		for (var i = 0; i < qString.length; i++) {
			var paramSet = qString[i].split('=');
			var k = paramSet[0];
			var v = '';
			if (paramSet[1] != null) {
				v = paramSet[1];
			}
			qArray[k] = v;
		}
		return qArray;
	}
}




