/*jslint white: true, onevar: true, browser: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */

EONE.checkForObject('SMK');

SMK.perpCart = {
	autoClose: null,

	load: function () {
		var closeLink = document.getElementById('Mini_Cart');

		closeLink = EONE.utils.dom.getElementsByAttribute('class', 'closeLink', closeLink)[0];

		closeLink.onclick = function () {
				clearTimeout(SMK.perpCart.autoClose);
				return SMK.perpCart.close();
			};

		this.autoClose = setTimeout(SMK.perpCart.close, 6000);
	},

	close: function () {
		document.getElementById('Mini_Cart').style.display = 'none';
		document.getElementById('Mini_Cart').parentNode.removeChild(document.getElementById('Mini_Cart'));
	}
};

SMK.countChange = {
	initialize: function (itemSortForm, numdisp) {
		var lc1, lc2;

		for (lc1 = 0; lc1 < itemSortForm.length; lc1 += 1) {
			for (lc2 = 0; lc2 < itemSortForm[lc1].numdisp.options.length; lc2 += 1) {
				if (itemSortForm[lc1].numdisp.options[lc2].value === numdisp) {
					itemSortForm[lc1].numdisp.selectedIndex = lc2;
				}
			}
		}
	},

	submit: function (form) {
		if (form.numdisp.value === '0') {
			form.viewall.value = 'true';
		}

		form.submit();
	}
};

EONE.checkForObject('SMK.wishList');

SMK.wishList.updateItem = function (updateForm) {
	if (updateForm.comment.value === '') {
		updateForm.comment.value = ' ';
	}
};

SMK.skuPrice = {
	updateResponse: function (req) {
		var header = '';

		ajaxShopResponse(req);

		if(/-L/.test(req.request.parameters.itemSkuId)) {
			header += 'Large Bags<br \/>\n';
		} else if(/-S/.test(req.request.parameters.itemSkuId)) {
			header += 'Small Bags<br \/>\n';
		}

//		header +='500 per Unit\n';

		document.getElementById('Price_Header').innerHTML = header;
	},

	update: function (sku, quickbuy) {
		var skuForm = sku.form,
			parms = {},
			priceTable = document.getElementById('Item_Price_Table') || null,
			lc1;

		if (!priceTable) {
			priceTable = document.createElement('DIV');
			priceTable.id = 'Item_Price_Table';

			if (quickbuy) {
				document.getElementById('Item').insertBefore(priceTable, document.getElementById('Product_Form').nextSibling);
			} else {
				document.getElementById('Sku_Qty').parentNode.insertBefore(priceTable, document.getElementById('Sku_Qty').parentNode.firstChild);
			}

		}

		parms.itemSkuId = sku.id;
		parms.PCR = skuForm.PCR.value;
		parms.DSP = '700002';
		parms.IID = skuForm.IID.value;
		parms.dnXMLNodeName = 'Item_Price_Table';

		ajaxShop(parms, '', SMK.skuPrice.updateResponse);
		return true;
	}
};

SMK.priceMsg = {
	response: function (req) {
		if (req.responseXML) { // If successful response
			eoneCreateLightBox("<!--x-->", 'priceBreakMsg');
			ajaxShopResponse(req);
		}
	},

	request: function (msgXmlId) {
		var parms = {
				DSP: '700009',
				dnXMLNodeName: 'lbContent',
				updateDspNav: 'false'
			},
			response;

		(msgXmlId) ? msgXmlId = msgXmlId : msgXmlId = '700009';

//		parms.DSP = msgXmlId;

		ajaxShop(parms, '', SMK.priceMsg.response);
	}
};

SMK.searchFormat = {
	qryClear: function (e) {
		var that = (this === window ? document.solrsearchform.qry : this)
		if(that.value === that.title) {
			that.className = that.className.replace (/jsTitleValue/, '');
			that.value = '';
		}
	},

	init: function (qry) {
		if (qry.value === '' || qry.value === qry.title) {
			qry.className += ' jsTitleValue';

			addEvent(qry, 'focus', SMK.searchFormat.qryClear);
			addEvent(qry, 'keypress', SMK.searchFormat.qryClear);

			qry.value = qry.title;
		}
	}
};

SMK.marketSpecialty = {
	parms: {
			dnXMLNodeName: 'marketspecialty_response',
			dontUpdateDspNav: 'false',
			dsp: 'marketspecialty'
		},

	market: null,

	specialty: null,

	handleSpecialty: function (req) {
		var specialtySelect,
			lc1;

		ajaxShopResponse(req);

		if (SMK.marketSpecialty.specialty !== (null || '')) {
			specialtySelect = SMK.marketSpecialty.market.form.membervar01;

			for (lc1 = 0; lc1 < specialtySelect.options.length; lc1 += 1) {
				if (specialtySelect.options[lc1].value === SMK.marketSpecialty.specialty) {
					specialtySelect.selectedIndex = lc1;
				}
			}
		}
	},

	load: function (marketSegment, dsp) {
		SMK.marketSpecialty.parms.marketSegment = marketSegment[marketSegment.selectedIndex].value;
	
		// call to action processor
		ajaxShop(SMK.marketSpecialty.parms, '', 'SMK.marketSpecialty.handleSpecialty');
	},

	init: function () {
		if (SMK.marketSpecialty.market.selectedIndex !== 0) {
			SMK.marketSpecialty.load(SMK.marketSpecialty.market);
		}
	}
};

SMK.pageLoad = function () {
	if (document.solrsearchform.qry) {
		SMK.searchFormat.init(document.solrsearchform.qry);
	}
}


addEvent(window, 'load', SMK.pageLoad);


EONE.checkForObject('EONE.tracking');

EONE.tracking.google = (function () {
/**
	@description Constructs, initializes, and returns a tracking object to register various Google Analytics tracking events/data.
	@author John Arthur
	@since v11.5
	@param {STRING} trackerName The identifier (used by GA) for the tracking object (Allows Multiple track instances)
	@param {OBJECT} acctID The Google Analytics account ID for this tracking instance
	@param {OBJECT} [pageURL = ''] If a value is set (other than empty string), registers this particular page hit at that URL.
	@return {OBJECT} The Google Analytics tracking object.
*/
	var gaTracker = function (acctID, pageURL, debug) {
		var ga = document.createElement('SCRIPT'),
			s = document.getElementsByTagName('SCRIPT')[0],
			src = '.google-analytics.com/ga.js';

		if (debug) {
			src = '.google-analytics.com/u/ga_debug.js';
		}

		if (gaTracker.acctID.length === 0) {
			ga.type = 'text/javascript';
			ga.async = true;
			ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + src;
			s.parentNode.insertBefore(ga, s);
		}

		gaTracker.acctID.push(acctID);

		window._gaq = window._gaq || [];
		_gaq.push([acctID + '._setAccount', acctID]);
		if (pageURL !== '') {
			_gaq.push([acctID + '._trackPageview', pageURL]);
		} else {
			_gaq.push([acctID + '._trackPageview']);
		}
	};

	gaTracker.acctID = [];

	return gaTracker;
}());




/**
	@namespace Default debugging scripts to be used when not in active debug.
	@since 11.5
*/
EONE.DEBUG = (function () {
	return {
		/**
			@description Does nothing in normal mode.
			@author John Arthur
			@since v11.5
			@param {OBJECT} value The value to be logged in the console.
			@returns {BOOLEAN} Returns true regardless.
		*/
		log: function (value) {
			return true;
		},

		/**
			@description Does nothing in normal mode.
			@author John Arthur
			@since v11.5
			@param {OBJECT} value The value to be logged in the console.
			@returns {BOOLEAN} Returns true regardless.
		*/
		dir: function (value) {
			return true;
		},

		/**
			@description Does nothing in normal mode.
			@author John Arthur
			@since v11.5
			@param {OBJECT} value The value to be logged in the console.
		*/
		alert: function (value) {
			return true;
		}
	};
}());
