function hover(who, baseClass, what) {
	var current;
	
	eval('current = who.className.match(/(Sel)?'+baseClass+'(Hover)?/);');
	if(current) {
		current = current[0];
		
		if(what && current && current.search(/Hover$/) == -1) {
			eval('who.className = who.className.replace(/'+current+'/, "'+current+'Hover");');
		} else if(!what && current && current.search(/Hover$/) != -1) {
			eval('who.className = who.className.replace(/'+current+'/, "'+current.replace(/Hover$/, '')+'");');
		}
	}
}

function select(who, baseClass, what) {
	var current;
	
	eval('current = who.className.match(/(Sel)?'+baseClass+'(Hover)?/);');
	if(current) {
		current = current[0];
		
		if(what && current && current.search(/^Sel/) == -1) {
			eval('who.className = who.className.replace(/'+current+'/, "Sel'+current+'");');
		} else if(!what && current && current.search(/^Sel/) != -1) {
			eval('who.className = who.className.replace(/'+current+'/, "'+current.replace(/^Sel/, '')+'");');
		}
	}
}

function supportsCanvas() {
	return !!document.createElement('canvas').getContext;
}

function parseStr(str) {
	var first = str.split('&');
	var data = new Array();
	
	for(var i = 0; i < first.length; i++) {
		var last = first[i].split('=');
		
		data[last[0]] = last[1];
	}
	
	return data;
}

function fixPNG(who) {
	var isIE = (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf('Opera') == -1);
	if(isIE) {
		// filter for MSIE 6.x and 5.5
		var ie6xFilter = /^.*MSIE [6]\.[0-9].*$/;
		var ie55Filter = /^.*MSIE [5].*$/;
		
		if(ie6xFilter.test(navigator.userAgent) || ie55Filter.test(navigator.userAgent)) {
			who.onload = function(){}
			who.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+who.src+')';
			who.src = 'images/pixel.gif';
		}
	}
}

function preload(imagePathArray) {
	var imageArray = new Array();
	for (var i = 0; i < imagePathArray.length; i++) {
		var nextImage = new Image();
		nextImage.src = imagePathArray[i];
		imageArray.push(nextImage);
	}
	return imageArray;
}

function processLoadPage(channel) {	
	if(!channel)
		channel = 'default';
		
	var detailSlip = document.getElementById('detailSlipContainer');
	if(detailSlip && detailSlip.style.display != 'none') {
		if(detailSlip.getAttribute('keepShown') == 'yes') {
			detailSlip.removeAttribute('keepShown');
		} else {
			hideSlip('detail');
		}
	}

	var el = document.getElementById('pageLoad');
	var title = httpReq[channel].responseText.match(/!--title:(.+)-->/);

	if(title) {
		document.title = title[1];
	}
	if(httpReq[channel].responseText.indexOf('--skipInnerHtml-->') == -1) {
		el.innerHTML = httpReq[channel].responseText.replace(/\<script.+?\<\/script\>/g, '');
	}
	if(el.style.display != 'block') {
		el.style.display = 'block';
	}
	
	evalScripts(httpReq[channel].responseText);
	
	if(document.ajaxHistory[location.hash] && document.ajaxHistory[location.hash].scrollTop) {
		window.scrollTo(0,document.ajaxHistory[location.hash].scrollTop);
	} else {
		window.scrollTo(0,0);
	}
}

function processDiv(div, channel) {
	if(!channel)
		channel = 'default';
		
	if(httpReq[channel].responseText.indexOf('--skipInnerHtml-->') == -1) {
		var slideIndex = httpReq[channel].responseText.indexOf('--slideIn:');
		if(slideIndex != -1) {
			document.slideDiv(div, slideIndex, httpReq[channel].responseText);
		} else {
			var el = document.getElementById(div);
			if(el) {
				el.innerHTML = httpReq[channel].responseText.replace(/\<script.+?\<\/script\>/g, '');
			}
		}
	}
	
	evalScripts(httpReq[channel].responseText);
}

function processSlip(slip, skipShowSlip, slideTo, channel) {
	if(!slip)
		slip = 'detail';
	if(!channel)
		channel = 'default';

	if(httpReq[channel].responseText.indexOf('--skipInnerHtml-->') == -1) {
		if(slideTo && httpReq[channel].responseText.indexOf('--skipSlide-->') == -1) {
			slideSlip(slip, slideTo, httpReq[channel].responseText);
		} else {
			var el = document.getElementById(slip+'SlipContent');
			
			el.innerHTML = httpReq[channel].responseText.replace(/\<script.+?\<\/script\>/g, '');
			
			if(skipShowSlip) {
				var theSlip = $(slip+'SlipContainer');
				setSlipScrollers(theSlip, 'auto');
			}
		}
	}
	
	evalScripts(httpReq[channel].responseText);
	
	if(!skipShowSlip) {
		showSlip(slip);
	}
}

function scrollFix(el, event) {
	if(el.scrollHeight <= el.offsetHeight)
		return true;
	
	if(window.event) {
		el.scrollTop -= (window.event.wheelDelta / 120 * 60);
		return false;
	} else if(this.scrollTop == 0 && event.wheelDelta > 0) {
		return false;
	} else if((el.scrollHeight - this.scrollTop) == el.offsetHeight && event.wheelDelta < 0) {
		return false;
	}
}

function submitForm(theForm, queryString, proccessFunction) {
	var data = '';
	
	if(theForm) {
		if(theForm.jt_submitted) {
			theForm.jt_submitted.value = 'isValid';
		}
		var formElements = theForm.elements;
	
		for(i = 0; i < formElements.length; i++) {
			if(formElements[i].disabled || ((formElements[i].type == 'radio' || formElements[i].type == 'checkbox') && !formElements[i].checked))
				continue;
				
			if(i)
				data += "&";
			
			if(formElements[i].type == 'checkbox') {
				data += escape(formElements[i].name) + '[]=' + escape(formElements[i].value);
			} else if(formElements[i].options) {
				var valueAppend = '';
			
				for(k = 0; k < formElements[i].length; k++) {
					if(formElements[i].options[k].selected) {
						if(valueAppend != "") {
							valueAppend += ",";
						}
						
						valueAppend += formElements[i].options[k].value;
					}
				}
				
				data += escape(formElements[i].name) + '=' + escape(valueAppend);
			} else {
				data += escape(formElements[i].name) + '=' + escape(formElements[i].value);
			}
		}
	}
	
	sendRequest('view_controller.php', queryString, data, (proccessFunction ? proccessFunction : 'processSlip("detail", true);'));
}

function setLoadStatus(on) {
	if(!document.loadStatus) {
		if(0 && supportsCanvas()) {
			document.loadStatus = document.createElement('canvas');
			document.loadStatus.id = 'loadStatus';
			document.loadStatus.className = 'loadStatus';
			document.loadStatus.setAttribute('width', '163');
			document.loadStatus.setAttribute('height', '35');
			
			var context = document.loadStatus.getContext('2d');
			if(context) {
				context.translate(17.5, 17.5);
			}
			
			document.loadStatus.img = new Image();
			document.loadStatus.img.addEventListener('load', function() {
				document.rotateLoadStatus();
			}, false);
			document.loadStatus.img.src = 'images/status/animation.png';
		} else {
			document.loadStatus = document.createElement('div');
			document.loadStatus.id = 'loadStatus';
			document.loadStatus.className = 'loadStatus';
			document.loadStatus.innerHTML = '<img src="images/status/animation.gif" width="35" height="35">';
		}
		
		if(document.loadStatus) {
			document.body.insertBefore(document.loadStatus, document.body.firstChild);
		}
	}
	
	if(document.loadStatus) {
		if(on) {
			var fixedNotSupported = document.loadStatus.currentStyle && document.loadStatus.currentStyle['position'] == 'absolute';

			var currentWidth = window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth);
			var currentHeight = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);

			var verticalPosition = fixedNotSupported ? (window.scrollY ? window.scrollY : document.documentElement.scrollTop) : 0;
			
			verticalPosition = (currentHeight/2)-40+verticalPosition;
			if(verticalPosition < 0) {
				verticalPosition = 0;
			}
			
			document.loadStatus.style.top = verticalPosition+'px';
			document.loadStatus.style.left = (currentWidth/2 - 91)+'px';
		
			document.loadStatus.style.display = 'block';
		} else {
			document.loadStatus.style.display = 'none';
		}
	}
}

document.rotateLoadStatus = function() {
	if(document.loadStatus) {
		var context = document.loadStatus.getContext('2d');
		if(context) {
			context.clearRect(-17.5, -17.5, 163, 35);
			context.rotate(0.104719755);
			context.drawImage(document.loadStatus.img, -17.5, -17.5, 35, 35);
			
			document.loadStatus.aniTimer = setTimeout(document.rotateLoadStatus, 33);
		}
	}
}

document.slideDiv = function(div, slideIndex, data) {
	var content = document.getElementById(div);
	
	if(content) {
		var scroller = content.parentNode;
		var container = scroller.parentNode;
		var newContent = document.createElement('div');
		
		if(scroller && container && newContent && !scroller.animation) {
			var direction = data.charAt(slideIndex+10);
			
			content.style.width = content.offsetWidth+'px';
			content.style.height = content.offsetHeight+'px';
			
			container.style.width = container.offsetWidth+'px';
			container.style.height = container.offsetHeight+'px';
			container.style.position = 'relative';
			container.style.overflow = 'hidden';
			
			content.style.top = '0px';
			content.style.left = '0px';
			content.style.position = 'absolute';
			
			scroller.style.top = '0px';
			scroller.style.left = '0px';
			scroller.style.position = 'absolute';
			
			newContent.style.position = 'absolute';
			newContent.style.left = content.offsetWidth+'px';
			
			newContent.innerHTML = data.replace(/\<script.+?\<\/script\>/g, '');
			
			scroller.appendChild(newContent);
			
			if(direction == 'l') {
				newContent.style.left = (-newContent.offsetWidth)+'px';
			} else if(direction == 't') {
				newContent.style.left = (-(content.offsetWidth - newContent.offsetWidth) / 2)+'px';
				newContent.style.top = newContent.offsetHeight+'px';
			} else if(direction == 'b') {
				newContent.style.left = (-(content.offsetWidth - newContent.offsetWidth) / 2)+'px';
				newContent.style.top = content.offsetHeight+'px';
			}

			setSlipScrollers(container, 'hidden');

			if(container.offsetHeight != newContent.offsetHeight || container.offsetWidth != newContent.offsetWidth) {
				if(container.animation) {
					container.animation.cancel();
				}
				
				container.animation = new Fx.Morph(container.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut, onComplete: function() {
						if(!scroller.animation) {
							container.style.overflow = 'visible';
							container.style.position = 'static';
							container.style.width = 'auto';
							container.style.height = 'auto';
							
							if(Browser.Engine.trident) {
								setTimeout('document.getElementById("'+scroller.id+'").style.display = "none"; document.getElementById("'+scroller.id+'").style.display = "block";', 1);
							}
						}
				
						container.animation = null;
					}
				});
				container.animation.start({
					'height': [container.offsetHeight, newContent.offsetHeight],
					'width': [container.offsetWidth, newContent.offsetWidth]
				});
			}

			scroller.animation = new Fx.Morph(scroller.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut, onComplete: function() {
					scroller.removeChild(content);
					newContent.id = div;
					setSlipScrollers(newContent, 'auto');
					if(data.indexOf('--evalScriptsOnSlide-->') != -1) {
						evalScripts(data);
					}
					
					scroller.style.position = 'static';
					newContent.style.position = 'static';
					
					if(!container.animation) {
						container.style.overflow = 'visible';
						container.style.position = 'static';
						container.style.width = 'auto';
						container.style.height = 'auto';
						
						if(Browser.Engine.trident) {
							setTimeout('document.getElementById("'+scroller.id+'").style.display = "none"; document.getElementById("'+scroller.id+'").style.display = "block";', 1);
						}
					}
					
					scroller.animation = null;
				}
			});
			
			scroller.animation.start({
				'left': [scroller.offsetLeft, -newContent.offsetLeft],
				'top': [scroller.offsetTop, -newContent.offsetTop]
			});
		}
	}
}

document.loginError = function(markUsername, markPassword, errorMessage) {
	var usernameLabelEl = document.getElementById('loginFormUsernameLabel');
	var passwordLabelEl = document.getElementById('loginFormPasswordLabel');
	var errorMessageEl = document.getElementById('loginFormErrorMessage');
	
	if(usernameLabelEl && passwordLabelEl && errorMessageEl) {
		if(markUsername) {
			usernameLabelEl.className = 'error';
		} else {
			usernameLabelEl.className = '';
		}
		if(markPassword) {
			passwordLabelEl.className = 'error';
		} else {
			passwordLabelEl.className = '';
		}
		errorMessageEl.innerHTML = errorMessage;
	}
}

document.loginFinish = function(redirect) {
	hideSlip('detail');
	if(redirect) {
		eval(redirect);
	} else {
		processCurrentState();
	}
}

document.updateQuantity = function(who, id, price) {
	var priceEl = document.getElementById('jt_product_price_'+id);
	
	if(priceEl && who) {
		who.value = who.value.replace(/[^\d]/g, '');
		
		if(who.value == '' || price == 0) {
			priceEl.innerHTML = '-';
		} else {
			priceEl.innerHTML = '$'+number_format(who.value*price, 2, '.', ',');
		}
	}
}

document.toggleDivFrom = function(sourceEl, toDiv, value, reverse) {
	var el = document.getElementById(toDiv);
	
	if(!el || !sourceEl)
		return;
	
	if(sourceEl.nodeName.toLowerCase() == 'select') {
		el.style.display = sourceEl.options[sourceEl.selectedIndex].value == value ? 'block' : 'none';
	} else if(sourceEl.nodeName.toLowerCase() == 'input') {
		if(sourceEl.type.toLowerCase() == 'radio') {
			el.style.display = sourceEl.value == value ? 'block' : 'none';
		} else {			
			if(sourceEl.value == value) {
				if(reverse) {
					el.style.display = !sourceEl.checked ? 'block' : 'none';
				} else {
					el.style.display = sourceEl.checked ? 'block' : 'none';
				}
			}
		}
	}
}

/* number_format Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

document.aniMiniCart = function() {
	var el = $('cartLink');
	
	if(el) {
		if(!el.ani) {
			el.ani = new Fx.Tween(el.id, {duration: 2500, property: 'color', link: 'chain'});
		}
		
		el.ani.start('#ff0000', '#6c9d31');
	}
}

function createExpirationDate(dateFieldID, monthFieldID, yearFieldID) {
	var dateField = document.getElementById(dateFieldID);
	var monthField = document.getElementById(monthFieldID);
	var yearField = document.getElementById(yearFieldID);

	if (dateField && monthField && yearField) {
		if (monthField.value != "" || yearField.value != "") {
			dateField.value = monthField.value + "/" + yearField.value;
		}
		else {
			dateField.value = "";
		}
		
	}

}

document.handleShippingZip = function(el, e) {
	if(!e)
		e = window.event;
	
	if(el.value.search(/[^\d]/) != -1) {
		el.value = el.value.replace(/[^\d]/g, '');
	}
	
	var serviceEl = document.getElementById('shippingService');
	
	if(serviceEl && el.value != el.savedValue) {
		serviceEl.selectedIndex = 0;
		while(serviceEl.options.length > 1) {
			serviceEl.options.remove(1);
		}
	}
}

document.openDictionary = function() {
	var newWindow = open('call.getDictionary', "DictionaryPopup", "width=533,height=495,scrollbars=no,resizable=no,toolbar=no,menubar=yes,titlebar=no,status=no");
}

document.openCalculator = function() {
	var newWindow = open('call.getCalculator', "CalculatorPopup", "width=533,height=495,scrollbars=no,resizable=no,toolbar=no,menubar=yes,titlebar=no,status=no");
}
