// The Wine Society JavaScript

$(document).ready(function() {

	//	$('#slider-stage').carousel('#previous', '#next');
	$('#sliderBloc a#previous').css('display','none');
	$('#sliderBloc a#next').css('display', 'none');

	$('#pnlSlider').hide();
	if ($('div.headerInner').hasClass("minimizedInnerHeader")) { $('#btnToggleHeaderSize').addClass('headerUpState').text("More Picture"); };

	$('#btnToggleHeaderSize').click(function() {
		if ($(this).hasClass('headerUpState')) {
			$('div.header').animate({ backgroundPosition: "(0 100px)" }, 1000, function() { $(this).removeClass("minimizedHeader"); });
			$('div.headerInner').animate({ height: 237 }, 1000);
			$(this).removeClass('headerUpState');
			$.cookie("tws_hdr_small", "0", { expires: 365 });
		} else {
			$('div.headerInner').animate({ height: 157 }, 1000);
			$('div.header').animate({ backgroundPosition: "(0 70px)" }, 1000);
			$(this).addClass('headerUpState');
			$.cookie("tws_hdr_small", "1", { expires: 365 });
		}
	});

	$('#btnToggleDecoration').click(function() {
		if ($(this).hasClass('decorationOn')) {
			$('#decoration1').show(); $('#decoration2').show(); $('#decoration3').show(); $('#decoration4').show();
			$(this).removeClass('decorationOn');
			$('#uctrlF').attr('value', '1');
		} else {
			$('#decoration1').hide(); $('#decoration2').hide(); $('#decoration3').hide(); $('#decoration4').hide();
			$(this).addClass('decorationOn');
			$('#uctrlF').attr('value', '0');
		}
	});

	if ($('#pnlSlider').text() != "") {
		var bInfo = $('#btnInformationPanel');
		bInfo.addClass('infoPanelCloseState');
		bInfo.click(function() {
			if ($(this).hasClass('infoPanelDownState')) {
				$('#pnlSlider').slideUp(500); $(this).removeClass('infoPanelDownState').addClass('infoPanelCloseState');
			} else {
				$('#pnlSlider').slideDown(500); $(this).removeClass('infoPanelCloseState').addClass('infoPanelDownState');
			}
		});
	}

	$('.panelQuickSearch .inputQuickSearch').attr('value', 'Enter search term').focus(function() {
		if ($(this).attr('value') == "Enter search term")
			$(this).attr('value', '');
	});

	//  if( $.browser.msie)
	//      $(".mainNav li").hover(function() {$(this).find("div").fadeIn('fast');}, function(){ $(this).parent().find("div").hide(); });

	setupPopupPanel();
//	setupValidationHilights();

});

// Below used to setup functionality that needs to be hookedup on partial postback (UpdatePanel) JRN 15/09/11
function pageLoad() {
	setupHelpToolTips(); 	
//	setupValidationHilights();
}

function setupHelpToolTips(){
	$(".helpButtonSideL").tooltip({ tipClass: "tooltipSideL", position: "center right", predelay: "500", effect: ($.browser.msie ? "toggle" : "fade") });
	$(".helpButtonSideM").tooltip({ tipClass: "tooltipSideM", position: "center right", predelay: "500", effect: ($.browser.msie ? "toggle" : "fade") });
	$(".helpButtonSideS").tooltip({ tipClass: "tooltipSideS", position: "center right", predelay: "500", effect: ($.browser.msie ? "toggle" : "fade") });
	$(".helpButtonTopL").tooltip({ tipClass: "tooltipTopL", position: "top center", predelay: "500", effect: ($.browser.msie ? "toggle" : "fade") });
	$(".helpButtonTopS").tooltip({ tipClass: "tooltipTopS", position: "top center", predelay: "500", effect: ($.browser.msie ? "toggle" : "fade") });
}

function twsValidate(vgroup) {
	var valid = Page_ClientValidate(vgroup);
	hilightInvalidItems();
	return valid;
}

// JRN: 08/12/2011 following function is used to hilight invalid controls items on the client side
function hilightInvalidItems() {
	// Add 'validError' class to invalid fields within a form.
	var vals = $("span.valid-ctrl");
	$.each(vals, function() {
		var controlToValidate = $("#" + this.controltovalidate);
		var validators = controlToValidate[0].Validators;
		if (validators == null) return;

		var isValid = true;
		$(validators).each(function() {
			if (this.isvalid !== true) {
				isValid = false;
			}
		});

		if (isValid) {
			controlToValidate.removeClass("validError");
			controlToValidate.addClass("validOk");
		} else {
			controlToValidate.removeClass("validOk");
			controlToValidate.addClass("validError");
		}
	});
}

// JRN 15/09/11: Setup infrastructure for popup-over modal dialog, handles screen scroll and initial positioning
function setupPopupPanel() {
	/* Popup code */
	$("a#showPopupPanel").click(function(event) {
		$("div#twsPopupPanel").css("height", $("body").height());
		$("div#twsPopup").css("marginTop", ($(window).scrollTop() + 30) + "px");
		$("div#twsPopupPanel").fadeIn("fast");
	});
	$("a#twsPopupCloser").click(function(event) { $("div#twsPopupPanel").fadeOut("fast"); });

	var $scrollingDiv = $("div#twsPopup");
	$(window).scroll(function() { $scrollingDiv.stop().animate({ "marginTop": ($(window).scrollTop() + 30) + "px" }, "slow"); });
}

(function($) {
	if (!document.defaultView || !document.defaultView.getComputedStyle) { // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force) {
			if (name === 'background-position') {
				name = 'backgroundPosition';
			}
			if (name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[name]) {
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if (!force && style && style[name]) {
				return style[name];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) + ' ' + oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}

	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop) {
		if ('background-position' in prop) {
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if ('backgroundPosition' in prop) {
			prop.backgroundPosition = '(' + prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};

	function toArray(strg) {
		strg = strg.replace(/left|top/g, '0px');
		strg = strg.replace(/right|bottom/g, '100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g, "$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1], 10), res[2], parseFloat(res[3], 10), res[4]];
	}

	$.fx.step.backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem, 'backgroundPosition');

			if (!start) {//FF2 no inline-style fallback
				start = '0px 0px';
			}

			start = toArray(start);

			fx.start = [start[0], start[2]];

			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0], end[2]];

			fx.unit = [end[1], end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
		fx.elem.style.backgroundPosition = nowPosX[0] + ' ' + nowPosX[1];

	};
})(jQuery);


jQuery.cookie = function(name, value, options) {
	if (typeof value != 'undefined' || (name && typeof name != 'string')) { // name and value given, set cookie
		if (typeof name == 'string') {
			options = options || {};
			if (value === null) {
				value = '';
				options.expires = -1;
			}
			var expires = '';
			if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
				var date;
				if (typeof options.expires == 'number') {
					date = new Date();
					date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
				} else {
					date = options.expires;
				}
				expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
			}
			// CAUTION: Needed to parenthesize options.path and options.domain
			// in the following expressions, otherwise they evaluate to undefined
			// in the packed version for some reason...
			var path = options.path ? '; path=' + (options.path) : '';
			var domain = options.domain ? '; domain=' + (options.domain) : '';
			var secure = options.secure ? '; secure' : '';
			document.cookie = name + '=' + encodeURIComponent(value) + expires + path + domain + secure;
		} else { // `name` is really an object of multiple cookies to be set.
			for (var n in name) { jQuery.cookie(n, name[n], value || options); }
		}
	} else { // get cookie (or all cookies if name is not provided)
		var returnValue = {};
		if (document.cookie) {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (!name) {
					var nameLength = cookie.indexOf('=');
					returnValue[cookie.substr(0, nameLength)] = decodeURIComponent(cookie.substr(nameLength + 1));
				} else if (cookie.substr(0, name.length + 1) == (name + '=')) {
					returnValue = decodeURIComponent(cookie.substr(name.length + 1));
					break;
				}
			}
		}
		return returnValue;
	}
};




