/* dispenserkeys.com */
/* jQuery 1.4.4 */

// define centerHorizontalNav()

$.fn.centerHorizontalNav = function () {
	var s = ['paddingLeft', 'paddingRight', 'marginLeft', 'marginRight', 'borderLeftWidth', 'borderRightWidth'];
	// each UL nav
	return this.each(function(){
		var jUl = $(this);
		var ulWidth = jUl.width();
		var liWidth = 0;
		// remove left and right margins for first and last LIs respectively
		var jLis = jUl.find('> li');
		var count = jLis.size();
		jLis.eq(0).css('margin-left','0');
		jLis.eq(count - 1).css('margin-right','0');
		// each LI
		jLis.each(function(){
			var x;
			var jLi = $(this);
			liWidth = liWidth + jLi.width();
			for (var k=0;k<s.length;k++) {
				x = parseInt(jLi.css(s[k]));
				if (!isNaN(x)) { liWidth = liWidth + x; }
			}
		});
		var x = ulWidth - liWidth;
		if (x > 0) {
			//$(this).find('li').eq(0).css('margin-left', parseInt(x / 2) + 'px');
			//$(this).css('padding-left', parseInt(x / 2) + 'px');
			var p = parseInt(jUl.css('padding-left'));
			p = isNaN(p) ? 0 : p;
			p = parseInt((x / 2) + p);
			$(this).css(	{	'padding-left'	: p + 'px',
								'width'			: 'auto'});
		}
	});
};

var mysite = {

	'settings' : {
		'inEditor'					: false,
		'defaultInputTextSel'		: '.default-input-text'
	},

	'isTrue' : function (name) {
		return 	(typeof mysite[name] != 'undefined')
				&& (	mysite[name] === 'T'
						|| mysite[name] === 't'
						|| mysite[name] === 'true'
						|| mysite[name] === true);
	},

	
	'start' : function () {

		var me = this;

		// EXTEND SETTINGS

		if (typeof mysiteSettings == 'object' && mysiteSettings !== null) {
			$.extend(me.settings, mysiteSettings);
		}

		// DO STUFF
		
		$(function(){ // doc ready

			// CENTER HORIZONTAL NAV UL
			$('ul#top-nav-2').centerHorizontalNav();

			/*// EXPANDABLE LEF NAV
			$('#left-nav-brands').awpExpandableMenu();
			// remove urls from top level links
			$('#left-nav-brands > li').each(function(){
				$(this).find('a').attr('href', '#');
			});*/

			// EXPANDABLE HOVER LEFT NAV
			$('#left-nav-brands').hoverAccordion({
				'keepHeight'		: true,
				'activateItem'		: 3,
				'speed'				: 500
			});
			
			/* IMAGEVIEWER */
			$('.imageviewer').imageviewer({
				'transitionTime'	: 400,
				'closeText'			: 'close',
				'overlayCss'		: {'backgroundColor':'#000'}
			})
			
			/* CUSTOM KEY RING PAGE */

			/*
				[name=vwattr0-Key_Codes]
				#custom-key-ring-keys-selected-count
				#custom-key-ring-message
				#custom-key-ring-message span
				#custom-key-ring-keycodes
				#custom-key-ring-keys
			*/

			/* is custom key ring page */
			if ($('body.custom-key-ring').size() > 0) {
			
				/* each key set row */
				$('#custom-key-ring-items-table tr').each(function(){
					var jRow = $(this);
					/* radio button */
					jRow.find('td.control input').bind('click', function(){
						me.hideMessage();
						/* if checked then set keys heading text */
						if ($(this).is(':checked')) {
							if (!(me.getNumKeysRequired() > 0)) {
								/* hide keys */
								$('#custom-key-ring-keys,#custom-key-ring-keys-selected')
									.css('visibility','hidden');
							} else {
								/* show keys */
								$('#custom-key-ring-keys,#custom-key-ring-keys-selected')
									.css('visibility','visible');
								/* set keys heading text */
								$('#custom-key-ring-pick-keys-text')
									.text(jRow.find('td.name').text());
							}
						}
						me.doKeysSelection();
					});
				});

				// each key row
				$('#custom-key-ring-keys tr').each(function(){
					var jQty,
						jRow = $(this);
					// get quantity box
					var jQty = jRow.find('td.control input.quantity');
					// remove name attr of all inputs in control cell
					jRow.find('td.control input').removeAttr('name');
					// change
					jQty.bind('keyup', function(){
						// kill message
						me.hideMessage();
						// if too many selected then set this to zero
						if (me.getNumKeysSelected() > me.getNumKeysRequired()) {
							$(this).val('0');
							me.showMessage();
						}
						me.doKeysSelection();
					});
				});

				/* on blur, set quantity to zero if NaN */
				$('#custom-key-ring-keys td.control input.quantity').blur(function(){
					adjustKeySelected($(this));
					var qty = parseInt($(this).val(), 10);
					if (isNaN(qty)) {
						$(this).val('0');
					}
				});
				
				/* form submit */
				$('#contentarea form').submit(function(){
					var	keycodes,
						keynames,
						ok = me.doKeysSelection().ok;
					/* ok, so make Keys value */
					if (ok) {
						keycodes = '';
						keynames = '';
						/* each key row */
						$('#custom-key-ring-keys tr').each(function(){
							var	keycode,
								keyname,
								jRow = $(this),
								jQty = jRow.find('td.control input.quantity'),
								qty = me.getKeyQuantity(jQty);
							if (qty > 0) {
								/* key code */
								keycode = jRow.find('td.control input.code').val();
								if (keycodes) { keycodes += ', '; }
								keycodes += keycode + ' (' + qty + ')';
								/* key name */
								keyname = jRow.find('td.name').text();
								if (keynames) { keynames += ', '; }
								keynames += keyname;
							}
						});
						/* set Keys field value */
						$('#custom-key-ring-keycodes').val(keycodes + ' (' + keynames + ')');
						return true;
					}
					/* else not ok */
					me.showMessage();
					return false; /* don't submit */
				});
	
				/* initialize by triggering radio click */
				$('#custom-key-ring-items-table tr td.control input').each(function(){
					if ($(this).is(':checked')) {
						$(this).trigger('click');
						return;
					}
				});
					
				/* hover enlarge for key images */
				$('#custom-key-ring-keys td.image a')
					.awpImageHoverEnlarge({
						'offsetX'				: 10,
						'offsetY'				: 30,
						'horizontalPosition'	: 'left'
					});
				
			} /* end custom key ring page */

		}); // end doc ready
	
	}, // end start()

	'showMessage' : function () {
		var me = this;
		$('#custom-key-ring-message')
			.css({'visibility':'visible','opacity':'1'})
			.find('span')
				.text(me.getNumKeysRequired())
				.end()
			.animate({
				'opacity': '0'
				}, 4000, function() {
					// Animation complete.
					me.hideMessage();
				}
			);		
	},
	
	'hideMessage' : function () {
		$('#custom-key-ring-message')
			.stop()
			.css('visibility','hidden');
	},
	
	'getNumKeysRequired' : function () {
		return $('#custom-key-ring-items-table td.control')
					.find('input:checked')
						.metadata().numkeys;
	},

	'getNumKeysSelected' : function () {
		var	me = this,
			qty = 0;
		$('#custom-key-ring-keys tr').each(function(i){
			var jQty = $(this).find('td.control input.quantity');
			qty += me.getKeyQuantity(jQty);
		});
		return qty;
	},
	
	'doKeysSelection' : function () {
		var	me = this,
			ok,
			numKeysSelected = me.getNumKeysSelected();
		ok = (numKeysSelected == me.getNumKeysRequired());
		$('#custom-key-ring-keys-selected-count').text(numKeysSelected);
		return {'ok' : ok, 'numKeysSelected' : numKeysSelected};
	},

	'getKeyQuantity' : function (jQty) {
		var qty = parseInt(jQty.val(), 10);
		if (isNaN(qty)) {qty = 0;}
		return qty;
	}

};
mysite.start();

