/**

 */
var FS = (function() {
	var FS = {},
			document = window.document,
			location = window.location,
			cartHeight = -1,
			accountHeight = 110,
			isAccountDropdownAnimated = false,
			timeout,
			isDropdownAnimated = false;

	FS.langId = -1;
	FS.catalogId = 23502;
	FS.storeId = 34053;
	FS.hostName = location.hostname;
	FS.zipMinLength = 4;

	FS.isLocal = (location.hostname.match(/(fossil\.local|192\.168|172\.30|127\.|assetstore-stage.fossil.com)/));

	FS.miniCart = {
		bagEmpty: true,
		cartHeight: function(height) {
			return (typeof height === "undefined") ? cartHeight : cartHeight = height;
		},
		numCartImages: 1,
		imagesLoaded: 0
	};

	FS.scene7 = {
		hostAssigned: ("https:" == location.protocol) ? "https://a248.e.akamai.net/f/248/9086/1.ccd/s7diod-isorigin.scene7.com/" : "http://s7d2.scene7.com/",
		imgPath: 'FossilAsset',
		recipeMiniTh: "$fossil_recommended$"
	};

	/**
	  @param {Object} settings Object containing four keys buttonName, buttonClass, buttonId, and buttonLabel.
	 */
	FS.button = function(settings) {
		return '<button name="'+settings.buttonName+'" class="'+settings.buttonClass+'" id="'+settings.buttonId+'"><span class="buttonLeft"><span class="buttonRight"><span class="buttonContent">'+settings.buttonLabel+'</span></span></span></button>';
	};

	/**
	  @param {Object} params Object of key/value pairs to transform into a querystring for URL's.
	  @param {Boolean} includeDefaults Flag to include default parameters (storeId, langId, catalogId) defaults to true.
	 */
	FS.params = function(params, includeDefaults) {
		params = (typeof params === "undefined") ? {} : params;
		includeDefaults = (typeof includeDefaults === "undefined") ? true : includeDefaults;

		var defaults = {storeId: this.storeId, catalogId: this.catalogId, langId: this.langId};
		var data = includeDefaults ? $.extend(defaults, params) : params;

		return $.param(data);
	};

	FS.accountSlideDown = function() {
		var cartDropdown = $('#cartDropdown');
		var cartButton = $('#cartButton');
		var accountDropdown = $('#accountDropdown');
		var accountDropdownContent = $('#accountDropdownContent');
		var accountButton = $('#accountButton');

		if (isDropdownAnimated) {
			cartDropdown.css('visibility', 'hidden').hide();
			cartButton.removeClass('showBag');
			isDropdownAnimated = false;
			cartDropdown.stop().clearQueue();
		}

		if (!isAccountDropdownAnimated) {
			isAccountDropdownAnimated = true;

			//swap style
			accountDropdown.css('visibility', 'visible');
			accountButton.addClass('showBag');
			accountDropdownContent.css('height', (accountHeight-20));
			accountDropdown.show().animate({height: accountHeight},200);
		}
		else {
			var newTime = 200 * ((accountHeight-accountDropdown.height())/ accountHeight);
			accountDropdown.stop().clearQueue().animate({height: accountHeight},newTime);
		}
	};

	FS.accountSlideUp = function() {
		var accountDropdown = $('#accountDropdown');
		var accountButton = $('#accountButton');

		accountDropdown.animate({height: 0}, 300, function() {
			//swap style
			accountButton.removeClass('showBag');
			isAccountDropdownAnimated = false;
			accountDropdown.hide();
		});
	};

	FS.slideDown = function() {
		var cartDropdown = $('#cartDropdown');
		var cartButton = $('#cartButton');
		var accountDropdown = $('#accountDropdown');
		var accountButton = $('#accountButton');

		if (isAccountDropdownAnimated) {
			accountDropdown.css('visibility', 'hidden');
			accountButton.removeClass('showBag');
			isAccountDropdownAnimated = false;
		}

		if (!isDropdownAnimated) {
			isDropdownAnimated = true;
			//Only calculate height on first hover. Delayed calculation because Safari does not return proper image height immediately after ajax loads.
			//This is now used as a redundancy only! cart Height should be calculated on AJAX Success
			if (cartHeight == -1 ) {
				cartHeight = $('#cartDropdown').height();
			}

			//swap style
			cartDropdown.css('visibility', 'visible').show();

			cartButton.addClass('showBag');
			cartDropdown.height(0).animate({height: cartHeight},300);
		}
		else {
			var newTime = 300 * ((cartHeight-cartDropdown.height())/ cartHeight);
			$('#cartDropdown').stop().clearQueue().animate({height: cartHeight}, newTime);
		}
	};

	FS.slideUp = function() {
		var cartDropdown = $('#cartDropdown');
		var cartButton = $('#cartButton');

		cartDropdown.animate({height: 0},300,
			function() {
				cartDropdown.hide();
				//swap style
				cartButton.removeClass('showBag');
				isDropdownAnimated = false;
			}
		);
	};

	FS.htmlEncode = function(value) {
		return $('<div/>').text(value).html();
	};

	FS.htmlDecode = function(value) {
		return $('<div/>').html(value).text();
	};

	/**
		Localized validation rules. These are merged in and overwrite the default rules when FSvalidate runs.
	 */
	FS.validationRules = {
		shipAddressCheck: function(obj, message) {
			var shipAddress = $(obj).val();
			var shipMethod = $("#shippingMethod option:selected").text();
			if (shipAddress !== "") {
				var poBox = shipAddress.match(/p\.?o\.? box/i);
				var shipUSPS = shipMethod.match(/Standard Delivery/i);
				if (poBox && !shipUSPS) {
					$(obj).data('errorMessage', message);
					return false;
				}
				else {
					return true;
				}
			}
		}
	};

	return FS;
}());

