//Constants
var FadeSpeed = 1;
var ResizeSpeed = 1;
var SliderStep = 25;
var SliderSpeed = 1;

var WidgetSliderSpeed = 10;
var WidgetSliderStep = 5;

//Running values
var FadingDirection = 0;
var CurrentScroll = -1;
var storedScrollStyle = '';

var PlaceHolders;
var ScrollAfterAjaxPaging = true;

function ScrollToObject(id, setStyleOnScroll, runAfterScroll)
{
    if (!runAfterScroll) runAfterScroll = '';
    if (!setStyleOnScroll) setStyleOnScroll = '';
    var obj = document.getElementById(id);
    if (setStyleOnScroll != '')
    {
        storedScrollStyle = obj.style;
        obj.style = setStyleOnScroll;
    }    
    var target = findObjectTop(obj) - 5;
    setTimeout('doScroll(' + target + ', "' + id + '", "' + setStyleOnScroll + '", "' + runAfterScroll + '");', 5);
}

function findObjectTop(obj) 
{
	var curtop = 0;
    if (obj.offsetParent) {
        do {
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return curtop;
}

function doScroll(target, id, setStyleOnScroll, runAfterScroll)
{
    var obj = document.getElementById(id);
    var current = 0;//document.body.scrollTop;
    
    if (document.documentElement && !document.documentElement.scrollTop)
        current = document.documentElement.scrollTop;
    else if (document.documentElement && document.documentElement.scrollTop)
        current = document.documentElement.scrollTop;
    else if (document.body && document.body.scrollTop)
        current = document.body.scrollTop;
    
    var sc = (target-current)/7;
    var shouldScroll = !((sc<1) && (sc>-1))
    if ((!shouldScroll) || (current == CurrentScroll))
    {
        if (setStyleOnScroll != '')
            setTimeout('resetBoxAfterScroll(' + id + ')', 1500);

        if (runAfterScroll != '')
            eval(runAfterScroll);
           
    }
    else
    {
        scrollBy(0, sc);
        setTimeout('doScroll(' + target + ', "' + id + '", "' + setStyleOnScroll + '","' + runAfterScroll + '");', 5);
    } 
    CurrentScroll = current;
}

function resetBoxAfterScroll(id)
{
    var tab = document.getElementById(id);
    tab.style = storedScrollStyle;
    storedScrollStyle = '';
}

function DHTMLPlaceHolderValue(plh, val)
{
    this.PlaceHolder = plh;
    this.Value = val;
}


function mouseX(evt) 
{
    if (evt.pageX) 
        return evt.pageX;
    else if (evt.clientX)
        return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    else return null;
}

function mouseY(evt) 
{
    if (evt.pageY) 
        return evt.pageY;
    else if (evt.clientY)
        return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    else return null;
}

function showTip(e, id)
{
    moveTip(e, id);
    
    var T = document.getElementById('TT' + id);
    T.style.visibility = 'visible';
}

function moveTip(e, id)
{
    
    var T = document.getElementById('TT' + id);
    var top = mouseY(e) + 15;
    maxPosY = GetWndCliSiz('Height') + GetScrollY() - T.offsetHeight - 1;
    
    if (top > maxPosY) top = maxPosY;
    
    T.style.left = mouseX(e) + 15 + 'px';
    T.style.top = top + 'px';
    
}

function GetScrollY()
{
    db = document.documentElement;
	return(window.pageYOffset || (db ? (db.scrollTop || 0) : 0));
}

function GetWndCliSiz(s)
{
	var db, y = window["inner" + s], sC = "client" + s, sN = "number";
	if(typeof y == sN)
	{
		var y2;
		return(
			// Gecko or Opera with scrollbar
			// ... quirks mode
			((db = document.body) && typeof(y2 = db[sC]) == sN && y2 &&  y2 <= y) ? y2 
			// ... strict mode
			: ((db = document.documentElement) && typeof(y2 = db[sC]) == sN && y2 && y2 <= y) ? y2
			// No scrollbar, or clientarea size == 0, or other browser (KHTML etc.)
			: y
		);
	}
	// IE
	return(
		// document.documentElement.client+s functional, returns > 0
		((db = document.documentElement) && (y = db[sC])) ? y
		// ... not functional, in which case document.body.client+s 
		// is the clientarea size, fortunately
		: document.body[sC]
	);
}


function hideTip(id)
{
    //FadingDirection =-1;
    //eval('FadingDirection' + id + ' = -1');
    var T = document.getElementById('TT' + id);
    T.style.visibility = 'hidden';
    //FadeInOut(id, 100,0, '');
}

function SetOpacity(id, val)
{
    //alert(id + ' ' + val);
    var T = document.getElementById(id);        
    T.style.opacity = (val / 100); 
    T.style.MozOpacity = (val / 100); 
    T.style.KhtmlOpacity = (val / 100); 
    T.style.filter = 'alpha(opacity=' + val + ')'; 
    T.style.Msfilter = 'alpha(opacity=' + val + ')'; 
    //T.style.filters[0].opacity = val;
    //alert(T.innerHTML + ' ' + val);
}


function FadeInOut(id, val, target, showOnTarget)
{
    FadingDirection = eval('FadingDirection' + id);
    if (!showOnTarget) showOnTarget = '';
    document.getElementById(id).style.visibility = 'visible'
    if (val< 0) val = 0;
    SetOpacity(id, val)
    if ((val < target) && (FadingDirection == 1))
        setTimeout('FadeInOut("' + id + '", ' + (1*(val + 20)) + ', ' + target + ', "' + showOnTarget + '")', FadeSpeed);
    else if ((val > 0) && (FadingDirection == -1))
    {
        setTimeout('FadeInOut("' + id + '", ' + (1*(val - 20)) + ', ' + target + ', "' + showOnTarget + '")', FadeSpeed);
    }   
    else if ((val >= target) && (FadingDirection == 1))
    {
        FadingDirection = 0;
        if (document.getElementById(showOnTarget)) document.getElementById(showOnTarget).style.visibility = 'visible';
    }
    else if ((val == 0) && (FadingDirection == -1))
    {
        FadingDirection = 0;
        document.getElementById(id).style.visibility = 'hidden'
    }
}

function createSubLayer(id)
{
    var L = document.getElementById(id);
    SetOpacity(id, 50);
    //document.getElementById('divMain').style.filter = 'Blur(Add = 0, Direction = 180, Strength = 10) gray';
    //document.getElementById('divMain').style.filter = 'gray';
    L.style.width = 100;
    L.style.height = 100;
    centerObjectOnScreen(id);
    L.style.visibility = 'visible';
    //resizeLayer(id);
}

function resizeLayer(id)
{
    var sH = document.body.height;
    var sW = document.body.width;
    var obj = document.getElementById(id);
    var oW = parseInt(obj.style.width);
    var oH = parseInt(obj.style.height);
    var cont = false;
    if (oW < (sW - 100))
    {
        obj.style.width = oW + 10;
        cont = true;
    }
    
    if (oH < (sH - 100))
    {
        obj.style.height = oH + 10;
        cont = true;
    }
    
    if (cont) 
        setTimeout('resizeLayer("' + id + '")', ResizeSpeed);
        
    centerObjectOnScreen(id);
}

function centerObjectOnScreen(id)
{
    var sH = document.body.clientHeight;
    var sW = document.body.clientWidth;
    var obj = document.getElementById(id);
    var oW = parseInt(obj.style.width);
    var oH = parseInt(obj.style.height);
    //var oW = obj.offsetWidth;
    //var oH = obj.offsetHeight
    //alert(oW);
    
    //alert(oH);
    //alert(sH);
    
    obj.style.top = (sH - oH)/2;
    obj.style.left = (sW - oW)/2;
    
    
}


function PopupRelocate(id, firstload) 
{ 
    if (!firstload) firstload = false;
 	var centerX, centerY;
 	if( self.innerHeight ) {
   		centerX = self.innerWidth;
   		centerY = self.innerHeight;
 	} else if( document.documentElement && document.documentElement.clientHeight ) {
   		centerX = document.documentElement.clientWidth;
   		centerY = document.documentElement.clientHeight;
 	} else if( document.body ) {
   		centerX = document.body.clientWidth;
   		centerY = document.body.clientHeight;
 	}

    var obj = document.getElementById(id);
 	var oW = obj.offsetWidth;
    var oH = obj.offsetHeight;

    var scrolledX, scrolledY;
    if( self.pageYOffset ) {
	    scrolledX = self.pageXOffset;
	    scrolledY = self.pageYOffset;
    } else if( document.documentElement && document.documentElement.scrollTop ) {
	    scrolledX = document.documentElement.scrollLeft;
	    scrolledY = document.documentElement.scrollTop;
    } else if( document.body ) {
	    scrolledX = document.body.scrollLeft;
	    scrolledY = document.body.scrollTop;
    }
    
 	var leftOffset = scrolledX + (centerX) / 2;
    leftOffset -= (oW/2);
 	document.getElementById(id).style.left = leftOffset + "px";
 	    
    if ((oH <= centerY) || (firstload))
    {
        var topOffset = scrolledY + (centerY) / 2;
        if ((oH <= centerY))
            topOffset -= (oH/2); //center vertically
        else
            topOffset = (scrolledY*1) + 10; //set top to top of current screen plus a bit
 	
 	    document.getElementById(id).style.top = topOffset + "px";
 	}
}


function StartMask(finalLoad)
{
    eval('FadingDirectiondivPopupMask =1');
    setTimeout('FadeInOut("divPopupMask", 10, 70, "' + finalLoad + '")', FadeSpeed);
    PopupRelocate(finalLoad, true);
    document.body.onscroll = function () { PopupRelocate(finalLoad); };
 	window.onscroll = function () { PopupRelocate(finalLoad); };
 	document.body.onresize = function () { PopupRelocate(finalLoad); };
}

function confirmDHTML(text, yesaction, noaction)
{
    document.getElementById('aDHTMLConfirmYes').href = 'javascript:' + yesaction + ' CloseConfirm()';
    document.getElementById('aDHTMLConfirmNo').href = 'javascript:' + noaction + ' CloseConfirm()';
    sndReq('/XML/getDHTMLMessage.aspx?Type=confirm&MessageName=' + text)
    //alert('/XML/getDHTMLMessage.aspx?Type=confirm&MessageName=' + text)
}

function DisplayDHTMLAlert(content)
{
    initMask();
    
    var sp = content.split('|||');
    var title = sp[0];
    content = sp[1];
    var btn = sp[2];
    
    if (PlaceHolders)
    {
        for (i=0; i<PlaceHolders.length; i++)
        {
            content = doReplace(content, PlaceHolders[i].PlaceHolder, PlaceHolders[i].Value);
        }
    }
    document.getElementById('clAlertTitle').innerHTML = title;
    document.getElementById('clAlertMessage').innerHTML = content;
    document.getElementById('aDHTMLAlertOk').innerHTML = btn;
    
    StartMask('divAlertPopup') 
}

function initMask()
{
    div = document.getElementById('divPopupMask');
    SetOpacity('divPopupMask', 0);
    div.style.visibility = 'visible';
    div.style.top = 0;
    div.style.left = 0;
}

function DisplayDHTMLConfirm(content)
{
    initMask();
    var con = content.split('|||');
    document.getElementById('clConfirmTitle').innerHTML = con[0];
    document.getElementById('clConfirmMessage').innerHTML = con[1];
    document.getElementById('aDHTMLConfirmYes').innerHTML = con[2];
    document.getElementById('aDHTMLConfirmNo').innerHTML = con[3];
    
    StartMask('divConfirmPopup')
}

function alertDHTML(text)
{
    PlaceHolders = null;
    sndReq('/XML/getDHTMLMessage.aspx?Type=alert&MessageName=' + text)
}

function alertDHTMLPlaceHolders(text, plh)
{
    PlaceHolders = plh;
    sndReq('/XML/getDHTMLMessage.aspx?Type=alert&MessageName=' + text)
}

function ShowImage(title, img)
{
    document.getElementById('clMediaPopupTitle').innerHTML = '';
    document.getElementById('clMediaPopupContent').innerHTML = '';
    initMask();
    StartMask('divMediaPopup') 
    sndReq('/XML/GetMedia.aspx?Type=Image&title=' + title + '&img=' + img);
    //window.open('/ShowImage.aspx?Image=' + img, "", "resizable=1,HEIGHT=550,WIDTH=550");
}

function ShowGallery(title, gal)
{
    document.getElementById('clMediaPopupTitle').innerHTML = '';
    document.getElementById('clMediaPopupContent').innerHTML = '';
    initMask();
    StartMask('divMediaPopup')
    sndReq('/XML/GetMedia.aspx?Type=Gallery&title=' + title + '&gal=' + gal); 
    //window.open('/ShowGallery.aspx?Gallery=' + gal, "", "resizable=0,HEIGHT=500,WIDTH=910");
}

function SetMediaContent(doc)
{
    var xml = GetXMLObject(doc).childNodes[0];
    var action = xml.getAttribute('action');
    var title = getInnerText(xml.childNodes[0]);
    var content = getInnerText(xml.childNodes[1]);
    title = doReplace(title, '-', '&#45;');
    document.getElementById('clMediaPopupTitle').innerHTML = title;
    if (action == 'cell')
        document.getElementById('clMediaPopupContent').innerHTML = content;
    else
    {
        //alert(content);
        eval(content);
        PopupRelocate('divMediaPopup', true) 
    }
}

function CloseAlert()
{
    document.getElementById('divAlertPopup').style.visibility = 'hidden';
    eval('FadingDirectiondivPopupMask =-1');
    setTimeout('FadeInOut("divPopupMask", 60, 100, "")', FadeSpeed);    
}

function CancelReview()
{
    document.getElementById('divProductReviewPopup').style.visibility = 'hidden';
    FadingDirection =-1;
    setTimeout('FadeInOut("divPopupMask", 60, 100, "")', FadeSpeed);    
}

function CloseMedia()
{
    document.getElementById('divMediaPopup').style.visibility = 'hidden';
    document.getElementById('clMediaPopupTitle').innerHTML = '';
    document.getElementById('clMediaPopupContent').innerHTML = '';
    FadingDirection =-1;
    setTimeout('FadeInOut("divPopupMask", 60, 100, "")', FadeSpeed);    
}

function CloseConfirm()
{
    document.getElementById('divConfirmPopup').style.visibility = 'hidden';
    FadingDirection =-1;
    setTimeout('FadeInOut("divPopupMask", 60, 100, "")', FadeSpeed);    
}

function CancelReminder()
{
    document.getElementById('divPasswordReminder').style.visibility = 'hidden';
    FadingDirection =-1;
    setTimeout('FadeInOut("divPopupMask", 60, 100, "")', FadeSpeed);   
}

function CloseProductDHTML()
{
    document.getElementById('divProductDHTML').style.visibility = 'hidden';
    FadingDirection =-1;
    setTimeout('FadeInOut("divPopupMask", 60, 100, "")', FadeSpeed);   
}

function CancelTAF()
{
    document.getElementById('divTellAFriend').style.visibility = 'hidden';
    FadingDirection =-1;
    setTimeout('FadeInOut("divPopupMask", 60, 100, "")', FadeSpeed);   
}

function ShowProductDHTML(ap)
{
    sndReq('/XML/getProduct.aspx?AP=' + ap + '&Action=LoadProductDHTML');
}

function LoadProductDHTML(doc)
{
    var xml = GetXMLObject(doc).childNodes[0];
    var nme = xml.getAttribute('ShortName');
    var sku = xml.getAttribute('SKU');
    var img = xml.getAttribute('Image')
    var desc = getInnerText(xml.childNodes[0]);
    var Offers;
    Offers = GetOfferTable(xml.childNodes[1].childNodes, 'DHTML');
    if (Offers != '') desc = Offers + '<br>' + desc;
    
    if (!img) img = '';
    img = GetImageDisplay(img, null, nme, 250, '');
    
    document.getElementById('clProductDHTMLName').innerHTML = nme;
    document.getElementById('clProductDHTMLDescription').innerHTML = desc;
    document.getElementById('clProductDHTMLImage').innerHTML = img;
    document.getElementById('clProductDHTMLSKU').innerHTML = sku;
    
    var tbl = document.getElementById('tblProductDHTMLAdditional')
    var fields = xml.childNodes[2].childNodes;
    
    while (tbl.rows.length > 1)
        tbl.deleteRow(tbl.rows.length-1);
        
    for (f=0; f<fields.length; f++)
    {
        var val = getInnerText(fields[f]);
        if (val != 'none')
        {
            var row = tbl.insertRow(tbl.rows.length);
            var cell = row.insertCell(0);
            cell.className = 'ProductDetailAdditionalInfoFieldName';
            cell.innerHTML = fields[f].getAttribute('Name');
            cell = row.insertCell(1);
            cell.className = 'ProductDetailAdditionalInfoFieldValue';
            cell.innerHTML = val;
        }
    }
    
    initMask();    
    StartMask('divProductDHTML')
}

function OpenSlider(id, completedaction)
{
    if (!completedaction) completedaction = '';
    var Target = document.getElementById('divSliderContent' + id).offsetHeight;
    document.getElementById('clSliderHeader' + id).innerHTML = eval('SliderHeaderCloseImage' + id);
    Slide(id, Target, 1, completedaction);
}

function CloseSlider(id, completedaction)
{
    if (!completedaction) completedaction = '';
    document.getElementById('clSliderHeader' + id).innerHTML = eval('SliderHeaderOpenImage' + id);
    Slide(id, 0, -1, completedaction);
}

function Slide(id, target, direction, completedaction)
{
    //alert(document.getElementById('divSliderContent' + id).offsetHeight);
    var s = document.getElementById('divSlider' + id);
    var IsSet = false;
    
    var current = s.style.height.substr(0, s.style.height.length-2);

    if ((s.style.height=='0px') && (direction==1))
    {
        s.style.height = SliderStep + 'px';;
        IsSet = true;
    }
    
    if (direction==-1)
    {
        //need to check the height divisible by SliderStep
        var steps = (current/SliderStep) + '';
        var bits = steps.split('.');
        if (bits.length > 1)
        {
            var intbit = bits[0];
            s.style.height = (intbit * SliderStep) + 'px';
            IsSet = true;
        }
    }
    
    if ((!IsSet) && (((1*current)+(direction*SliderStep)) >=0)) 
    {
        var SetTo = ((1*current)+(direction*SliderStep))
        if ((SetTo > target) && (direction == 1))
            SetTo = target;
        s.style.height = SetTo + 'px';
    }
    
    if ((!IsSet) && (((1*current)+(direction*SliderStep)) <0)) //Set Height to zero
        s.style.height = '0px';;
    
    if (((current < target) && (direction==1)) || ((current > target) && (direction==-1)))
        setTimeout('Slide("' + id + '",' + target + ',' + direction + ',\'' + completedaction +  '\')', SliderSpeed);
    else
    {
        //Target met so turn links back on and run completed action
        if (completedaction != '') eval(completedaction);
        
        if (direction == 1)
            SetSliderLinks(id, 'open');
        else
            SetSliderLinks(id, 'closed');
    }
    
    
}

function SlideHolderToChildSize(Holder, Child, completedaction)
{
    if (completedaction) completedaction = '';
    var h = document.getElementById('divSlider' + Holder);
    var current = h.style.height.substr(0, h.style.height.length-2);
    
    var target = document.getElementById(Child).offsetHeight;
    var dir = 1;
    if (target < current) dir = -1;
    //alert('Target:' + target + ', Current:' + current);
    if (PageFirstLoad)
        h.style.height = target + 'px';
    else
        Slide(Holder, target, dir, completedaction);
    PageFirstLoad = false;
}

function SetSliderLinks(id, state)
{
    if (state == 'closed')
    {
        if (document.getElementById('clSliderHeader' + id))
            document.getElementById('clSliderHeader' + id).innerHTML = eval('SliderHeaderOpenLink' + id)  + eval('SliderHeaderOpenImage' + id) + '</a>';
    }
    else
    {
        if (document.getElementById('clSliderHeader' + id))
            document.getElementById('clSliderHeader' + id).innerHTML = eval('SliderHeaderCloseLink' + id)  + eval('SliderHeaderCloseImage' + id) + '</a>';
    }
}

function initSlider(id, state)
{
    SetSliderLinks(id, state);
    if (state == 'closed')
        document.getElementById('divSlider' + id).style.height = '0px';
    else
        document.getElementById('divSlider' + id).style.height = document.getElementById('divSliderContent' + id).offsetHeight + 'px';
}