﻿var win;
function ShowCoupon(id) {
    win = window.open("coupon.aspx?cid=" + id, "Coupon", "location=0,status=0,scrollbars=0,width=500,height=500");
    if (!win) {
        alert("Please turn off your popup blocker to print the coupon");
        return;
    }
    win.focus();
}
function ShowSaleInfo(id) {
    win = window.open("popsale.aspx?cid=" + id, "Sale", "location=0,status=0,scrollbars=yes,width=600,height=600");
    if (!win) {
        alert("Please turn off your popup blocker to open the window");
        return;
    }
    win.focus();
}

function ShowPostCV(id) {
    window.location.href = "postcv.aspx?wid=" + id;
}

function PerformSearch() {
    var queryElement = document.getElementById('txtQuery');
    if (queryElement) {
        if (queryElement.value.length < 3) {
            alert('Search query must be longer than 2 characters'); return;
        }
        else {
            location.href = 'searchresults.aspx?q=' + escape(queryElement.value);
        }
    }
}
//
// Set the default button for the site
//
function ExecuteDefaultButton(event) {
    var queryElement = document.getElementById('txtQuery');
    if (!event) var event = window.event;
    if (event.keyCode == 13) {
        event.preventDefault ? event.preventDefault() : event.returnValue = false; event.cancel = true;
        if (document.getElementById('btnSearch'))
            document.getElementById('btnSearch').click();
    }
}
function setEnterKey() {
    document.onkeydown = ExecuteDefaultButton;
    if (window.addEventListener) {
        window.addEventListener('keydown', ExecuteDefaultButton, false);
    } else if (window.attachEvent) {
        window.attachEvent('onkeydown', ExecuteDefaultButton);
    }
}

function Toggle(elmId) {
    var subDiv = jPixeliT.$('#div_' + elmId);
    if (jPixeliT.cssValue(subDiv, "display") === 'none') {
        jPixeliT.slideDown(subDiv, 500);
        return false;
    }
    if (jPixeliT.cssValue(subDiv, "display") !== 'none' && jPixeliT.attr("#par_" + elmId, "class") === "menuTxt") {
        jPixeliT.slideUp(subDiv, 500);
        return false;
    }
}

function ChangePic(img, title) {
    var imgUrl = jPixeliT.querystringParam("src", img.src);
    var imgHolder = jPixeliT.$("#imgHolder");
    jPixeliT.setInnerHTML("#div_title", title);
    imgHolder.src = jPixeliT.String.Format("image.axd?src={0}&w=580&q=100", imgUrl);
}
//////// Site map functions

var currentOverShopId = -1;
var currentClickedShopId = -1;
var xy;

// The mouse is over an area
function areaOver(shopId, event) {
    xy = jPixeliT.mouseLocation(event);


    if (currentClickedShopId > 0)
        return;
    currentOverShopId = shopId;
    closeFloatingDiv();
    openOverDiv();
    var data = jPixeliT.Cache["shop_" + shopId];
    if (jPixeliT.isEmpty(data)) {
        beginGetData(shopId, endGetDataOver);
    }
    else { endGetDataOver(data, shopId); }
}

// On mouse out an area
function areaOut() {
    if (currentClickedShopId > 0)
        return;
    currentOverShopId = -1;
    closeFloatingDiv();
}

// The user clicked on an area
function areaClick(shopId) {
    currentClickedShopId = shopId;
    closeFloatingDiv();
    openClickDiv();
    var data = jPixeliT.Cache["shop_" + shopId];
    if (jPixeliT.isEmpty(data)) {
        beginGetData(shopId, endGetDataClick);
    }
    else { endGetDataClick(data, shopId); }
}

// Start getting the data
function beginGetData(shopId, callback) {
    var url = "mapshandler.ashx";
    jPixeliT.Ajax.executeRequest(url, true, "shopid=" + shopId, callback, shopId, false);
}



// The data recivied from the server. show it in the floating over div
var overHtml = "<div class='shopOverImgDiv' align='center'><table width='100%' height='100%'><tr><td width='100%' height='100%' align='center' valign='middle'>{0}</td></tr></table></div><div class='shopOverTxtDiv'>{1}</div>";
function endGetDataOver(text, shopId) {
    jPixeliT.Cache["shop_" + shopId] = text;
    if (shopId != currentOverShopId)
        return;
    var parts = text.split('|');
    var img = jPixeliT.isEmpty(parts[3]) ? "" : "<img src='image.axd?src=~/" + parts[3] + "&w=200&h=100&q=90' />";
    var html = jPixeliT.String.format(overHtml, img, parts[0]);
    jPixeliT.setInnerHTML("#overDiv", html);
}


var clickHtml = "<div class='shopClickImgDiv' align='center'><table width='100%' height='100%'><tr><td width='100%' height='100%' align='center' valign='middle'>" +
 "{0}</td></tr></table></div>" +
 "<div class='shopClickTxtDiv'><b>{1}</b><br /><div style='text-align: right; direction: rtl; padding: 10px;'>{2}{3}</div></div>" +
 "<div align='right' style='padding-bottom: 2px;'><img src='images/close_pink.jpg' hspace='5' alt='Close' style='cursor:pointer;' onclick='closeClicked();' />" +
 "</div>";

// The data recivied from the server. show it in the floating click div
function endGetDataClick(text, shopId) {
    jPixeliT.Cache["shop_" + shopId] = text;

    var parts = text.split('|');

    var img = jPixeliT.isEmpty(parts[3]) ? "" : "<img src='image.axd?src=~/" + parts[3] + "&w=200&h=100&q=90' />";
    var html = jPixeliT.String.format(clickHtml, img, parts[0], parts[1], parts[2]);
    var clickDiv = jPixeliT.$("#clickDiv");
    jPixeliT.setInnerHTML("#clickDiv", html);
    clickDiv.style["top"] = (xy.top - clickDiv.offsetHeight - 30) + 'px';
}


//
// Open & show the floating div for the 'Over' action
//
function openOverDiv() {
    var isNew = true;
    var overDiv = document.getElementById("overDiv");
    if (!overDiv) overDiv = document.createElement("div");
    else isNew = false;

    overDiv.id = "overDiv";
    overDiv.style.position = 'absolute';
    overDiv.style.zIndex = 3;
    overDiv.style.display = 'block';
    overDiv.className = "shopOverDiv";

    overDiv.innerHTML = "טוען נתונים...";

    var h = jPixeliT.cssValue(overDiv, "height");
    if (jPixeliT.isEmpty(h) || h == 0) h = 200;

    overDiv.style.top = (xy.top - h - 30) + 'px';
    overDiv.style.right = (BrowserWidth() - xy.left) + 'px';

    if (isNew) document.body.appendChild(overDiv);
}

//
// Open & show the floating div for the 'Click' action
//
function openClickDiv() {
    var isNew = true;
    var clickDiv = document.getElementById("clickDiv");
    if (!clickDiv) clickDiv = document.createElement("div");
    else isNew = false;

    clickDiv.id = "clickDiv";
    clickDiv.style.position = 'absolute';
    clickDiv.style.zIndex = 3;
    clickDiv.style.display = 'block';
    clickDiv.className = "shopClickDiv";
    clickDiv.innerHTML = "טוען נתונים...";

    clickDiv.style.top = (xy.top - 30) + 'px';
    clickDiv.style.right = (BrowserWidth() - xy.left) + 'px';

    if (isNew) document.body.appendChild(clickDiv);
}

function closeClicked() {
    closeFloatingDiv();
    currentClickedShopId = -1;
}
// 
// Hide the floating divs
//
function closeFloatingDiv() {
    var overDiv = document.getElementById("overDiv");
    if (overDiv) {
        overDiv.innerHTML = '';
        overDiv.style.display = 'none';
    }
    var clickDiv = document.getElementById("clickDiv");
    if (clickDiv) {
        clickDiv.innerHTML = '';
        clickDiv.style.display = 'none';
    }
}

var defaultRotation = 5;
//Banners functions
function initBan(currIndex, w, h, arr, txtContainer, imgContainer) {
    var partsCurr = arr[currIndex].split('|');
    var rotation = jPixeliT.isEmpty(partsCurr[2]) ? defaultRotation : parseInt(partsCurr[2]);
    setTimeout(function() { swapBan(currIndex, w, h, arr, txtContainer, imgContainer); }, rotation * 1000);

    var partsNext = arr[1].split('|');
    preloadBan(partsNext[1], w, h);
}

function swapBan(currIndex, w, h, arr, txtContainer, imgContainer) {
    currIndex = getNextIndex(currIndex, arr);
    var partsCurr = arr[currIndex].split('|');
    var rotation = jPixeliT.isEmpty(partsCurr[2]) ? defaultRotation : parseInt(partsCurr[2]);
    ChangeBann(partsCurr, w, h, arr, txtContainer, imgContainer);
    setTimeout(function() { swapBan(currIndex, w, h, arr, txtContainer, imgContainer); }, rotation * 1000);

    var partsNext = arr[getNextIndex(currIndex, arr)].split('|');
    preloadBan(partsNext[1], w, h);
}

function ChangeBann(partsCurr, w, h, arr, txtContainer, imgContainer) {
    jPixeliT.fadeOut("#" + txtContainer, 500);
    jPixeliT.fadeOut("#" + imgContainer, 500,
         function() {
             jPixeliT.setInnerHTML("#" + txtContainer, getTextHtml(partsCurr));
             jPixeliT.setInnerHTML("#" + imgContainer, getImgHtml(partsCurr, w, h));
             jPixeliT.fadeIn("#" + txtContainer, 500, function() { if (jPixeliT.browser.ie) jPixeliT.$("#" + txtContainer).style.removeAttribute('filter') });
             jPixeliT.fadeIn("#" + imgContainer, 500);
         });
}

function getNextIndex(curr, arr) {
    if (arr.length - 1 <= curr) return 0;
    return (curr + 1);
}

function getTextHtml(partsCurr) {
    var html;
    var href = partsCurr[4];
    var target = partsCurr[3];
    if (!jPixeliT.isEmpty(href)) {
        html = String.Format("<a href='{0}' target='{1}'>{2}</a>", href, target, partsCurr[5]);
    }
    else {
        html = partsCurr[5];
    }
    return html;
}
function getImgHtml(partsCurr, w, h) {
    var html;
    var filename = partsCurr[1];
    var href = partsCurr[4];
    var target = partsCurr[3];

    if (filename.endsWith(".swf", true)) {
        html = String.Format("<embed {0} {1} menu=\"true\" loop=\"true\" play=\"true\" src=\"{2}\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\"></embed>",
                    !jPixeliT.isEmpty(w) ? "width=\"" + w + "px\"" : "",
                    !jPixeliT.isEmpty(h) ? "height=\"" + h + "px\"" : "",
                    filename);
    }
    // Image
    else {
        html = String.Format("<img src='image.axd?src=~/{0}&amp;w={1}&amp;h={2}&amp;q=95' />", filename, w, h);
        if (!jPixeliT.isEmpty(href)) {
            html = String.Format("<a href='{0}' target='{1}'>{2}</a>", href, target, html);
        }
    }
    return html;
}

// Preload banner
function preloadBan(filename, w, h) {
    var html;
    // flash
    if (filename.endsWith(".swf", true)) {
        html = String.Format("<embed {0} {1} menu=\"true\" loop=\"true\" play=\"true\" src=\"{2}\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\"></embed>",
                    !jPixeliT.isEmpty(w) ? "width=\"" + w + "px\"" : "",
                    !jPixeliT.isEmpty(h) ? "height=\"" + h + "px\"" : "",
                    filename);
    }
    // Image
    else {
        html = String.Format("<img src='image.axd?src=~/{0}&amp;w={1}&amp;h={2}&amp;q=95' />", filename, w, h);
    }
    var isNew = jPixeliT.isEmpty(document.getElementById("preloaderDiv"));
    var preloaderDiv = !isNew ? document.getElementById("preloaderDiv") : document.createElement("div");
    preloaderDiv.innerHTML = html;
    preloaderDiv.id = "preloaderDiv";
    preloaderDiv.style.display = 'none';
    preloaderDiv.style.position = 'absolute';
    if (isNew) document.body.appendChild(preloaderDiv);
}
//
// Get browser window width
//
function BrowserWidth() {
    if (window.innerWidth)
        return window.innerWidth;
    return document.body.clientWidth;
}

