var booLoadingProduct = false;
//Scrolling
var intClipWidth = null;
var intClipLeft = 0;
var intLeft = 0;
var intScrollBySlow = 2;
var intScrollByFast = 20;
var intScrollDelay = 50;
var intMaxLeft = 0;
var intMinLeft = 0;
var intMinClipLeft = 0;
var intMaxClipLeft = 0;
var intOrigClipWidth = 980;
var scrolling = false;
var strSpeed = 'slow';
function init() {
	initPopups();
	initSubNav();
	initNav();
	initProducts();
	initCarousel();
	stopLoading();	
}
function initProducts()
{
	if (!document.getElementById('divResultsPane')) return false;
	arrProds = document.getElementById('divResultsPane').getElementsByTagName('a');
	for (i=0, j = arrProds.length; i<j; i++)
	{
		addListener(arrProds[i],"click",function(e){getProduct(this);cancelEvent(e);});
		//addListener(arrProds[i].childNodes[0],"mouseover",function(){zoomIn(this);});
		//addListener(arrProds[i].childNodes[0],"mouseout",function(){zoomOut(this);});
	}
}
function getProduct(anchor)
{
	if (booLoadingProduct)
	{
		return false;
	}
	booLoadingProduct = true;
	startLoading();
	imgs = getElementsByClass('productImg',document,'img');
	for (var i = 0, j = imgs.length; i < j; i++) {
		imgs[i].className = 'productImg';
	}
	anchor.getElementsByTagName('img')[0].className = 'productImg active';
	dat = new Date();
	url = '/ajaxdetails.asp?typeid=' + anchor.id.split('_')[1] + '&jewelleryid=' + anchor.id.split('_')[2] + '&decacher=' + escape(dat.toUTCString());
	ajax(url,'',updateProduct);
	return false;
}
function updateProduct(xml) {
	document.getElementById('carouselItem').innerHTML = String(xml);
	booLoadingProduct = false;
	stopLoading();
}
function startLoading()
{
	document.getElementById('loading').style.display = 'block';
}
function stopLoading()
{
	if (!document.getElementById('loading')) return false;
	setTimeout(function(){document.getElementById('loading').style.display = 'none';},100);
}
function initCarousel()
{
	if (!document.getElementById('divResultsPane')) return false;
	intResultsWidth = parseInt(getStyle(document.getElementById('divResultsPane'),'width'),10);
	if (!intResultsWidth)
	{
		intResultsWidth = document.getElementById('divResultsPane').offsetWidth;
	}
	intOrigClipWidth = document.getElementById('divResults').parentNode.clientWidth;
	if (intResultsWidth < intOrigClipWidth)
	{
		return false;
	}
	document.getElementById('leftArrow').parentNode.style.display = 'block';
	document.getElementById('rightArrow').parentNode.style.display = 'block';
	anchor = document.getElementById('leftArrow').parentNode;
	addListener(anchor,"mouseover",function(){startScroll('Left');});
	addListener(anchor,"mouseout",stopScroll);
	addListener(anchor,"mousedown",function(e){fastScroll();cancelEvent(e);this.blur();});
	addListener(anchor,"mouseup",slowScroll);
	addListener(anchor,"click",function(e){cancelEvent(e);this.blur();});
	
	anchor = document.getElementById('rightArrow').parentNode;

	addListener(anchor,"mouseover",function(){startScroll('Right');});
	addListener(anchor,"mouseout",stopScroll);
	addListener(anchor,"mousedown",function(e){fastScroll();cancelEvent(e);this.blur();});
	addListener(anchor,"mouseup",slowScroll);
	addListener(anchor,"click",function(e){cancelEvent(e);this.blur();});
}
function startScroll(strDir)
{
	intScrollBy = intScrollBySlow;
	strSpeed = 'slow';
	intResultsWidth = parseInt(getStyle(document.getElementById('divResultsPane'),'width'),10);
	intAddOn = 52;
	if (!intResultsWidth)
	{
		intResultsWidth = document.getElementById('divResultsPane').offsetWidth;
		intAddOn -= 20;
	}
	intOrigClipWidth = document.getElementById('divResults').parentNode.clientWidth;
	if (intResultsWidth < intOrigClipWidth)
	{
		return false;
	}
	if (!intClipWidth) {intClipWidth = intOrigClipWidth;}
	intMinLeft = 0-intResultsWidth+intOrigClipWidth-intAddOn;
	intMaxClipLeft = intResultsWidth-intOrigClipWidth;
	intMaxClipWidth = intResultsWidth+intAddOn;
	intMinClipWidth = intOrigClipWidth;
	scrolling = true;
	if (strDir == 'Left')
	{
		scrollLeft();
	}
	else
	{
		scrollRight();
	}
}
function fastScroll()
{
	strSpeed = 'fast';
	intScrollBy = intScrollByFast;
}
function slowScroll()
{
	strSpeed = 'slow';
	intScrollBy = intScrollBySlow;
}
function stopScroll()
{
	scrolling = false;
	elLeftArrow = document.getElementById('leftArrow');
	elRightArrow = document.getElementById('rightArrow');
	if (elLeftArrow.src.indexOf('leftarrowactive.png') > -1) {elLeftArrow.src = '/images/site/leftarrow.png';}
	if (elRightArrow.src.indexOf('rightarrowactive.png') > -1) {elRightArrow.src = '/images/site/rightarrow.png';}
}
function scrollLeft()
{
	if (!scrolling) {return false;}
	elResults = document.getElementById('divResults');
	elLeftArrow = document.getElementById('leftArrow');
	elRightArrow = document.getElementById('rightArrow');
	if (strSpeed == 'slow') { elLeftArrow.src = '/images/site/leftarrowactive.png'; } else { elLeftArrow.src = '/images/site/leftarrowfast.png';}
	intLeft += intScrollBy;
	intClipLeft -= intScrollBy;
	intClipWidth -= intScrollBy;
	if (intClipLeft < intMaxClipWidth) {elRightArrow.src = '/images/site/rightarrow.png';}
	if (intLeft > intMaxLeft) {intLeft = intMaxLeft;}
	if (intClipLeft < intMinClipLeft) {intClipLeft = intMinClipLeft;}
	if (intClipWidth < intMinClipWidth) {intClipWidth = intMinClipWidth;}
	elResults.style.left = intLeft + 'px';
	strClip = 'rect(0px, '+intClipWidth+'px, 148px, ' + intClipLeft +'px)';
	elResults.style.clip = strClip;
	if (intLeft == intMaxLeft) {elLeftArrow.src = '/images/site/leftarrowinactive.png';scrolling=false;}
	setTimeout(scrollLeft,intScrollDelay);
}
function scrollRight()
{
	if (!scrolling) {return false;}
	elResults = document.getElementById('divResults');
	elLeftArrow = document.getElementById('leftArrow');
	elRightArrow = document.getElementById('rightArrow');
	if (strSpeed == 'slow') { elRightArrow.src = '/images/site/rightarrowactive.png'; } else { elRightArrow.src = '/images/site/rightarrowfast.png';}
	intLeft -= intScrollBy;
	intClipLeft += intScrollBy;
	intClipWidth += intScrollBy;
	if (intClipLeft > intMinClipWidth) {elLeftArrow.src = '/images/site/leftarrow.png';}
	if (intLeft < intMinLeft) {intLeft = intMinLeft;}
	if (intClipLeft > intMaxClipLeft) {intClipLeft = intMaxClipLeft;}
	if (intClipWidth > intMaxClipWidth) {intClipWidth = intMaxClipWidth;}
	elResults.style.left = intLeft + 'px';
	strClip = 'rect(0px, '+intClipWidth+'px, 148px, ' + intClipLeft +'px)';
	elResults.style.clip = strClip;
	if (intLeft == intMinLeft) {elRightArrow.src = '/images/site/rightarrowinactive.png';scrolling=false;}
	setTimeout(scrollRight,intScrollDelay);
}
function ajax(url,params,fn) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', url, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.setRequestHeader("Content-length", params.length);
    self.xmlHttpReq.setRequestHeader("Connection", "close");
    self.xmlHttpReq.onreadystatechange = function() {
    if (self.xmlHttpReq.readyState == 4) 
	{
             fn(self.xmlHttpReq.responseText);
        }
    };
    self.xmlHttpReq.send(params);
}
function populateCarousel(strResponse) {
	document.getElementById('carousel').innerHTML = strResponse;
}
function initPopups() 
{
	var popupLinks = getElementsByClass('popup',document,'a');
	for (var i=0, j = popupLinks.length; i<j; i++) 
	{
		addListener(popupLinks[i],"click", function(e){window.open(this.href);cancelEvent(e);});    
	}
}
function initNav()
{
	if (!window.XMLHttpRequest || navigator.userAgent.toLowerCase().indexOf("msie") != -1) 
	{
		// IE, older browsers
	    var elMenu = getElementsByClass('nav',null,'ul')[0];
	    if (getElementsByClass('nav',null,'ul')[0] && getElementsByClass('nav',null,'ul')[0].childNodes && getElementsByClass('nav',null,'ul')[0].childNodes[0])
	    {
			var elMenuNodes = elMenu.getElementsByTagName('li');
			for (var i=0, j=elMenuNodes.length; i<j; i++)
			{
				if (elMenuNodes[i].getElementsByTagName('ul').length>0)
				{
	               	addListener(elMenuNodes[i],"mouseover",function(){this.getElementsByTagName('ul')[0].style.display='inline';});
					addListener(elMenuNodes[i],"mouseout",function() {this.getElementsByTagName('ul')[0].style.display='none';});
				}
           	}
       	}
    }
}
function initSubNav() {
	if (!window.XMLHttpRequest || navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
		// IE, older browsers
	    var elMenu = getElementsByClass('subnav',null,'ul')[0];
	    if (getElementsByClass('subnav',null,'ul')[0] && getElementsByClass('subnav',null,'ul')[0].childNodes && getElementsByClass('subnav',null,'ul')[0].childNodes[0]) {
			var elMenuNodes = elMenu.getElementsByTagName('li');
			for (var i=0, j=elMenuNodes.length; i<j; i++) {
				if (elMenuNodes[i].getElementsByTagName && elMenuNodes[i].getElementsByTagName('ul').length>0) {
	               	addListener(elMenuNodes[i],"mouseover",function(){this.getElementsByTagName('ul')[0].style.display='inline';});
					addListener(elMenuNodes[i],"mouseout",function() {this.getElementsByTagName('ul')[0].style.display='none';});
				}
           	}
       	}
    }
}

//Basics
//global variables
var __eventListeners = [];
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/-(w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}
function getElementsByClass(searchClass,node,tag)
{
        var classElements = new Array();
        if (node == null) node = document;
        if (tag == null) tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++)
        {
        if ( pattern.test(els[i].className) )
                {
                classElements[j] = els[i];
                j++;
        }
        }
        return classElements;
}
function addListener( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
function cancelEvent(e) {
    var e = e || window.event;

    e.cancelBubble = true; // for IE
    if (typeof e.stopPropagation == 'function')
        e.stopPropagation();

    e.returnValue = false; // for IE
    if (typeof e.preventDefault == 'function')
        e.preventDefault();
}
function emptyElement(el)
{
	while (el.firstChild)
	{
		elRemove = el.firstChild;
		el.removeChild(elRemove);
	}
}
//Prototype extentions
String.prototype.trim = function() 
{
	return this.replace(/^\s+|\s+$/g,"");
}
