var MenuItems = new Array();
var MenuTimeout = null;
var CLI = 'CoverLayer';
var ns = (document.layers);
var ie = (document.all);
var w3 = (document.getElementById && !ie);
var localeId;
var mouseX = 0;
var mouseY = 0;

function RegisterNotification(){
    var o = document.createElement('div');
    o.setAttribute('id', 'Notification');
    o.className = 'Notification';
    document.body.appendChild(o);
}

function SelectContent(o){
    o.focus();
    o.select();
}


//Disable right mouse click Script
//By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
//For full source code, visit http://www.dynamicdrive.com
function clickIE4() {
    if (event.button == 2) {
        return false;
    }
}

function clickNS4(e) {
    if (document.layers || document.getElementById && !document.all) {
        if (e.which == 2 || e.which == 3) {
            return false;
        }
    }
}

if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = clickNS4;
}else if (document.all && !document.getElementById) {
    document.onmousedown = clickIE4;
}
document.oncontextmenu = new Function("return false")


cntimeout = null;
function CheckNumeric(o,cartId){
    clearTimeout(cntimeout);
    if(o.value!=""){
        cntimeout = setTimeout(function(){
            o.value = parseInt(o.value);
        }, 500);
        if(o.value=='NaN') o.value = 1;
    setTimeout(function(){location.href='cart.aspx?cart_id='+cartId+'&amount='+o.value},500);
    }
}

CartPositionedTimer = null;
function HideCartPositioned(){
    clearTimeout(CartPositionedTimer);
    CartPositionedTimer = setTimeout(function(){new Effect.Fade('SCContent'),{duration:0};}, 500);
}
function ShowCartPositioned(){
    clearTimeout(CartPositionedTimer);
    o = $("SCContent");
    if(o.style.position=='relative'){
        Position.absolutize(o);
        o.style.top = '160px';
        o.style.left = (980-500)+'px';
    }
    o.innerHTML = $('ShopCartDisplayContent').innerHTML;
    new Effect.Appear(o,{duration:0.3});
}

function ShowCart(mode){}

function AddToCardFailed(error){
    alert(error.get_message());
}

function AddToCartSuccess(result, eventArgs){
    $('ShopCartDisplayContent').innerHTML = result;
    ShowCartPositioned();
    CartPositionedTimer = setTimeout(function(){
        new Effect.Fade('SCContent',{duration:0.3});
    },4000);
}

function AddToCart(amount, properties, productId, SessionID, UserId, LocaleId){
    px.ShopService.AddToCart(productId, amount, properties, SessionID, UserId, LocaleId, AddToCartSuccess, AddToCardFailed);
    window.scrollTo(0,0);
}

function GetCartSuccess(result, eventArgs){
    $('ShopCartDisplayContent').innerHTML = result;
}

function GetCartFailed(error){
    alert(error.get_message());
}

function PopulateShoppingCart(SessionId, LocaleId){
    px.ShopService.GetCart(SessionId, LocaleId, GetCartSuccess, GetCartFailed);
}

function ProductSearchSuccess(result, eventArgs) {
    switch(localeId){
        case "fr_fr":
            head = '<h2>R&eacute;sultats de votre recherche:</h2>';
            break;
        case "en_us":
            head = '<h2>Search results:</h2>';
            break;
    }
    alert(result);
    if (result.length < 10) {
        switch (localeId) {
            case "fr_fr":
                result = "D&eacute;sol&eacute;, aucun produit ne correspond &agrave; votre recherche.";
                break;
            case "en_us":
                result = "Sorry, your search did not match any products.";
                break;
        }
    }
    $('ProductList').innerHTML = head + result;
}

function ProductSearchFailed(error){
    alert(error.get_message());
}

spt = null;
function SearchProduct(keyword, localeId) {
    if (keyword != "" && $('ctl00_cph_spotLightContainer')) {
        new Effect.BlindUp($('ctl00_cph_spotLightContainer'), { duration: .5 });
        if ($('col_latestAdded')) new Effect.Fade($('col_latestAdded'), { duration: .4 });
        if ($('col_mostPopular')) new Effect.Fade($('col_mostPopular'), { duration: .4 });
        if ($('col_topSave')) new Effect.Fade($('col_topSave'), { duration: .4 });
    }
    clearTimeout(spt);
    spt = setTimeout(function(){
        px.ShopService.SearchProducts(keyword, localeId, ProductSearchSuccess, ProductSearchFailed);
    },200);
}


function PageMessageSuccess(result, eventArgs){
    var cont = $('MessageBoxContent');
    cont.innerHTML = result;
    if(result != cont.innerHTML) alert(cont.innerHTML.length);
    $('MessageBoxContent').innerHTML = result;
    new Effect.Appear('MessageBox', {duration:0.5});
}

function PageMessageFiled(error){
    $('MessageBoxContent').innerHTML = "Service Error: " + error.get_message();
    new Effect.Appear('MessageBox', {duration:0.5});
}

function InitMessageTicker(){
    messageTickerInterval = setInterval(function(){
        px.PageMessages.GetMessages(PageMessageSuccess,PageMessageFiled);
        clearInterval(messageTickerInterval);
        // AUTOHIDE AFTER 50sec
        setTimeout(function(){
            clearInterval(messageTickerInterval);
            InitMessageTicker();
            Effect.Fade('MessageBox',{duration:0.5});
        },50000);
    },2000);
}
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();


var am_timer;
function am(KeyVal,oId){
    obj = document.getElementById(oId);
    var subs = document.getElementById('subMenu');
    MenuTimeout = setTimeout(function(){
        if(subs.firstChild){
            var child = subs.firstChild;
            while(child){
                if(child.nodeType==1){
                    child.style.display = 'none';
                }
                child = child.nextSibling;
            }
        }
        for(i=0;i<MenuItems.length;i++){
            MenuItems[i].src = ImageFolder + MenuItems[i]['srcOff'];
        }
        obj.src = ImageFolder + obj['srcOn'];
       
        $(KeyVal).style.display = 'block';
    },200);
}

function start_am(KeyVal, oId){
    setTimeout(function(){am(KeyVal, oId)}, 2500);
}

function SetupDyn(obj,srcOn,srcOff){
    if(obj['srcOn']==null){
        imgOn = new Image(); 
        imgOn.src = ImageFolder + srcOn;
        imgOff = new Image();
        imgOff = ImageFolder + srcOff;
        obj['srcOn'] = srcOn;
        obj['srcOff'] = srcOff;
        MenuItems[MenuItems.length] = obj;
    }
}

function dm(){ clearTimeout(MenuTimeout); }

function PreloadImages(images){
    a = images.split(',');
    for(i=0;i<a.length;i++){
        img = new Image(); 
        img.src = a[i];
    }
}


function tb(){ return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body }

var cddTimeout = null;
function ShowCategoryDropDown(to, from){
    clearTimeout(cddTimeout);

    ov = getMeasurements('parent_' + from);
    
    from = $(from);
    to = $(to);
    to.innerHTML = from.innerHTML;
    new Effect.Move(from,{x:0,y:0, duration:.0});

    to.style.top = '230px';
    to.style.left = (mouseX - 120) + 'px';
    to.innerHTML = from.innerHTML;
    if(to.innerHTML!=''){
        new Effect.Appear(to, {duration:0.4});
    }else{
        to.style.display = 'none';
    }
}

function HideCategoryDropDown(id){
    cddTimeout = setTimeout(function(){
        new Effect.Fade('ComboBox', {duration:0.4});
    }, 100);
}

function preloadPic(pic){
	preload = new Image();
	preload.src = pic;
}
function swapPic(iPic,picSrc){
	window.document.images[iPic].src = picSrc;
}

var HoverBoxActive = false;
function HoverBox(msg){
    HoverBoxActive = true;
    $('HoverBox').innerHTML = msg;
    Effect.Appear('HoverBox', {duration:0.4});
}
function HideHoverBox(){
    $('HoverBox').style.display = 'none'
    HoverBoxActive=false;
}
    
function getMouse(e) {
  if (ie){
    gs = getScroll();
    mouseX = event.clientX + gs[0];
    mouseY = event.clientY + gs[1];
  } else {
    mouseX = e.pageX
    mouseY = e.pageY
  }  
  if (mouseX < 0){mouseX = 0}
  if (mouseY < 0){mouseY = 0}
  
  if($('HoverBox')){
      if(HoverBoxActive){
        o = $('HoverBox');
        o.style.left = (mouseX+20) + "px";
        o.style.top = (mouseY+30) + "px";
      }else{
        $('HoverBox').style.display = 'none';
      }
  }
  
  return true
}
if (!ie) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouse;

function PositionLocaleBox(){
    var m = getMeasurements('ChooseLanguage');
    $('ChooseLanguage').style.left = (((m[0]/2)+490)-m[6]-10) + 'px';
    $('ChooseLanguage').show();
}

function getMeasurements(id){
    /*  0 = window width
        1 = window height
        2 = scroll X
        3 = scroll Y
        4 = object top
        5 = object left
        6 = object width
        7 = object height
    */
    if(ie){
        windowWidth = tb().offsetWidth;
        windowHeight = tb().offsetHeight;
    }else if(ns){
        windowWidth = window.innerWidth;
        windowHeight = window.innerHeight;
    }else if(w3){
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    scrollVals = getScroll();
    objectVals = getObjectMeasurements(id);

    
    return new Array(windowWidth,windowHeight,scrollVals[0],scrollVals[1],objectVals[0],objectVals[1],objectVals[2],objectVals[3]);
}

function getObjectMeasurements(id){
    /*
        0 = object top
        1 = object left
        2 = object width
        3 = object height
    */
    try{
        objectTop = parseInt($(id).offsetTop);
        objectLeft = parseInt($(id).offsetLeft);
        objectWidth = parseInt($(id).offsetWidth);
        objectHeight = parseInt($(id).offsetHeight);
    }catch(e){
        objectTop = null;
        objectLeft = null;
        objectWidth = null;
        objectHeight = null;
    }
    return new Array(objectTop,objectLeft,objectWidth,objectHeight);
}

function getScroll(){
    if(document.all){
        if (!document.documentElement.scrollTop){
            sX = document.body.scrollLeft;
            sY = document.body.scrollTop;
        }else{
            sX = document.documentElement.scrollLeft;
            sY = document.documentElement.scrollTop;
        }
    }else{
        sX = window.pageXOffset;
        sY = window.pageYOffset;
    }
    return new Array(sX,sY);
}

function CenterAndMiddle(id){

    if(ie){
        documentWidth = tb().offsetWidth;
        documentHeight = tb().offsetHeight;
    }else if(ns){
        documentWidth = window.innerWidth;
        documentHeight = window.innerHeight;
    }else if(w3){
        documentWidth = self.innerWidth;
        documentHeight = self.innerHeight;
    }
    objectVals = getObjectMeasurements(id);

    var o = $(id);
    o.style.top = parseInt((documentHeight/2)-(parseInt(o.style.height)/2)) + 'px';
    o.style.left = parseInt((documentWidth/2)-(parseInt(o.style.width)/2)) + 'px';
}

var LastActiveLayer = null;
function ShowFloatingLayer(id,ShowCover){
    if(ShowCover){
        document.body.style.overflow = 'hidden';
        var m = getMeasurements(null);

        var o = $('CoverLayer');
        o.style.height = m[1] + 'px';
        o.style.width = m[0] + 'px';
        o.style.left = '0px';
        o.style.top = '0px';
        Effect.Appear(o, {duration:1, from:0, to:0.5});
    }
    LastActiveLayer = id;
    CenterAndMiddle(id);
    setTimeout(function(){Effect.BlindDown(id, {duration:0.4})}, 500);
}

function ShowPhotoPreview(src){}

function RemoveFadingLayer(obj){
    Effect.Fade(LastActiveLayer, {duration:0.4})
    Effect.Fade(obj);
    document.body.style.overflow = 'auto';
    LastActiveLayer = null;
}

function DisplayAlert(StringToDisplay){
    $('CustomAlertContent').innerHTML = StringToDisplay;
    window.scrollTo(0,0);
    ShowFloatingLayer('CustomAlert',true);
}

function DisplayAlertWithTimeout(StringToDisplay, Timeout){
    DisplayAlert(StringToDisplay);
    setTimeout(function(){RemoveFadingLayer($('CoverLayer'))},Timeout);
}

function DisplayImage(src){
    window.open('zoom.aspx?src='+src,'zoom','location=0,toolbars=0,scrollbars=0,address=1,width=300,height=200');
}

function CheckPopup(){
    window.open('popupCheck.aspx?redirectUrl='+escape(paymentUrl));
    setTimeout("GetPopupResult();", 800);   //200
}

function GetPopupResult(){
    var acceptspopups = false;
    var RequestUrl = 'popupCheck.aspx';
    InitXmlHttp();

    if(XmlRequest!=null){
        XmlRequest.onreadystatechange = function(){
            if(XmlRequest.readyState==4){
                if(XmlRequest.responseText=="true"){
                    acceptspopups = true;
                }else{
                    acceptspopups = false;
                    $('PaymentLink').style.display = 'block';
                }
            }
        }
        XmlRequest.open("GET", RequestUrl, true);
        XmlRequest.send(null);
    }
}

var XmlRequest;
function InitXmlHttp(){
    try{
        XmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            XmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(ee){
            XmlRequest = null;
        }
    }
    
    if(!XmlRequest&&typeof XMLHttpRequest!="undefined"){
        XmlRequest = new XMLHttpRequest();
    }
}

var rY;
var r;
var moveint;



Event.observe(window, 'load', Page_Load, false);

function Page_Load(e) {
    if($('RightContainer')){
        r = $('RightContainer');
        r.absolutize(r);
        r.innerHTML = "";
    
        moveint = null;

        rY = parseInt(r.style.top);
    
        Event.observe(window, 'scroll', BackButtonPosition, false);
    };
    if ($('BackToTopShop')) {
        o = $('BackToTopShop');
        Event.observe(window, 'scroll', PositionBackToTopShop, false);
    }

};

function PositionBackToTopShop() {
    o = $('BackToTopShop');
    a = document.viewport.getScrollOffsets();
    o.absolutize();
    
    if (a[1] > 1000) {
        o.show();
        o.style.top = a[1] + "px";
    } else {
        o.hide();
    }
}

function BackButtonPosition(){
    o = $('BackToTop');
    a = document.viewport.getScrollOffsets();
    if(parseInt(a[1])>rY){
        if(r.innerHTML=="")r.innerHTML = $('BackToTop').innerHTML;
        r.style.top = parseInt(a[1])+"px";
    }else{
        r.innerHTML = "";
    };
};

var forumSearchTO = null;
function searchForum(s) {
    switch (localeId) {
        case "fr_fr":
            $('forumSearchResult').innerHTML = 'Recherche en cours...';
            break;
        case "en_us":
            $('forumSearchResult').innerHTML = 'Searching...';
            break;
    }
    
    
    clearInterval(forumSearchTO);
    forumSearchTO = setTimeout(function() {
    px.ForumService.searchThread(s, forumServiceSearchSuccess, forumServiceSearchFail);
    }, 200);
}

function forumServiceSearchFail(error) {
    alert(error.get_message());
}

function forumServiceSearchSuccess(result, eventArgs) {
    $('forumSearchResult').innerHTML = result;
}