var fAry = new Array();
var hiAry = new Array();
var curFt = 0;
var d = document;
var prevobj = null;
var wto = null;
var whto = null;

function MM_swapImgRestore() { //v3.0
  var i,x,a=d.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
   if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;if (!d) d = document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; d.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){d.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function HomeImageLoad() {
    hiAry = HomeImageLoad.arguments;
    for (var i = 1; i < hiAry.length; i++) {
        MM_preloadImages(hiAry[i]);
    }
    d.getElementById("homecontwrap").style.backgroundImage = "url(" + hiAry[1] + ")";
    wto = window.setTimeout("RotateImage('1'," + (hiAry.length-1) + ",1);", 5000);
}

function ProductImageLoad() {
    hiAry = ProductImageLoad.arguments;
    for (var i = 1; i < hiAry.length; i++) {
        MM_preloadImages(hiAry[i]);
    }
    d.getElementById("productimage").style.backgroundImage = "url('/Thumbnail.ashx?width=350&height=350&ImgFilePath=" + hiAry[1] + "')";
    wto = window.setTimeout("RotateProductImage(" + (hiAry.length - 1) + ",1);", 6000);
}

function RotateProductImage(imgTtl, imgShow) {
    if (wto) window.clearTimeout(wto);
    d.getElementById("productimagefade").style.backgroundImage = "url('/Thumbnail.ashx?width=350&height=350&ImgFilePath=" + hiAry[imgShow] + "')";
    changeOpac(100, "productimagefade");
    imgShow++;
    if (imgShow > imgTtl) {
        imgShow = 1;
    }
    d.getElementById("productimage").style.backgroundImage = "url('/Thumbnail.ashx?width=350&height=350&ImgFilePath=" + hiAry[imgShow] + "')";
    opacity("productimagefade", 100, 0, 2000);
    wto = window.setTimeout("RotateProductImage(" + imgTtl + "," + imgShow + ");", 5000);
}

function RotateImage(imgid, imgTtl, imgShow) {
    if (wto) window.clearTimeout(wto);
    d.getElementById("homecontwrapfade").style.backgroundImage = "url("+hiAry[imgShow]+")";
	changeOpac(100, "homecontwrapfade");
	imgShow++;
	if (imgShow > imgTtl) {
	    imgShow = 1;
	}
	d.getElementById("homecontwrap").style.backgroundImage = "url("+hiAry[imgShow]+")";
	opacity("homecontwrapfade", 100, 0, 2000);
	wto = window.setTimeout("RotateImage(" + imgid + "," + imgTtl + "," + imgShow + ");", 4000);
}

function RotateMainImage(imgShow) {
		//if(imgShow==null || isNAN(imgShow))imgShow=1;
    if (whto) window.clearTimeout(whto);
    d.getElementById("mainimagefade").style.backgroundImage = "url('/images/main_" + imgShow + ".jpg')";
    changeOpac(100, "mainimagefade");
    imgShow++;
    if (imgShow > 2) {
        imgShow = 1;
    }
    d.getElementById("mainimage").style.backgroundImage = "url('/images/main_" + imgShow + ".jpg')";
    opacity("mainimagefade", 100, 0, 2000);
    whto = window.setTimeout("RotateMainImage(" + imgShow + ");", 4000);
}

function RotateMainImageStart() {
	MM_preloadImages('/images/main_1.jpg','/images/main_2.jpg');
	RotateMainImage(1);
}

addOnLoad(RotateMainImageStart);

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame 
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens 
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers 
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function addOnLoad(func) {
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        var oldonload = window.onload;
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function addFeature(title, content,url , imgsrc) {
    tmpImg = d.createElement("IMG");
    tmpImg.src = "Thumbnail.ashx?width=200&height=200&ImgFilePath=/images/featured/" + imgsrc;
    fAry[fAry.length] = { 'title': title, 'content': content, 'url' : url,'imgsrc': tmpImg };
}

function showFeature(nbr) {
    if (prevobj!=null) d.getElementById("featured" + (prevobj + 1)).src = "images/featured/featured_" + (prevobj + 1) + "off.png"
    nbr = nbr < 0 ? 0 : nbr > 9 ? 9 : nbr;
    d.getElementById("feattitle").innerHTML = fAry[nbr].title;
    d.getElementById("featcontent").innerHTML = fAry[nbr].content;
    d.getElementById("featimg").src = fAry[nbr].imgsrc.src;
    d.getElementById("featlnk").href = fAry[nbr].url;
    curFt = nbr;
    d.getElementById("featured" + (nbr + 1)).src = "images/featured/featured_" + (nbr + 1) + "on.png"
    prevobj = nbr;
}

function isNumber(e) {
    e = e || window.event;
    if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode == 8 || e.keyCode == 9) return true;
    return false;
}

function searchForm(e) {
    e = e || window.event;
    if (e.keyCode ==13){
        d.getElementById("cartForm").action = "/Search";
        d.getElementById("s_search").value = d.getElementById("searchtxt").value;
        d.getElementById("cartForm").submit();
        return false;
    }
    return true;
}
