﻿var hiberd;
var productsInfo;
var _imgs = { down: ['arr_d', '/images/arr_b_tnav.gif'], right: ['arr_r', '/images/arr_r.gif'] }
_hiberd = function () { }

_hiberd.setVar = function (key, value) {
    var v = this.getVars();
    var sVals = '';
    v[key] = value;
    var s;
    for (s in v)
        sVals += '~|~' + s + '~!~' + v[s];
    var p = 0;
    if (sVals.length > 0) p = 3;
    $('#ctl00_CPHContent___hiVars').val(sVals.substr(p));
}
_hiberd.getVar = function (key) {
    if (this.getVars()[key] != null) {
        return this.getVars()[key];
    }
}
_hiberd.getVars = function () {
    if ($('#ctl00_CPHContent___hiVars').length > 0) {
        var vars = new Array();
        temp = $('#ctl00_CPHContent___hiVars').val().split('~|~');
        for (i = 0; i < temp.length; i++) {
            aryItem = temp[i].split('~!~');
            if (aryItem.length == 2)
                vars[aryItem[0]] = aryItem[1];
        }
        return vars;
    }
}

showMessage = function (container, type, title, text) {
    o = $('#' + container);
    o.css('display', 'block');
    if (o.length > 0) {
        if (text != undefined) {
            o.find('.icon').attr('class', 'icon t' + type);
            o.find('.title .text').text(title);
            o.find('.message').html(text);
        }
        o.fadeIn(500);
    }
}
hideMessage = function (container) {
    o = $('#' + container);
    if (o.length > 0) {
        o.fadeOut();
    }
    o.fadeOut(500);
    o.css('display', 'none');
}

function ajaxCallFailed(res, error) { }
/*******************************************/
function checkEnter(e, ctr) { var k; if (window.event) { k = e.keyCode; } else if (e.which) { k = e.which; }; if (k == '13') { $("#" + ctr).click(); return false; } }
jQuery.fn.rating = function (url, options) { if (url == null) return; var settings = { url: url, increment: 1, maxvalue: 5, curvalue: 0, disabled: 0 }; if (options) { jQuery.extend(settings, options); }; jQuery.extend(settings, { cancel: (settings.maxvalue > 1) ? true : false }); var container = jQuery(this); jQuery.extend(container, { averageRating: settings.curvalue, url: settings.url }); settings.increment = (settings.increment < .75) ? .5 : 1; var s = 0; for (var i = 0; i <= settings.maxvalue; i++) { if (i == 0) { if (settings.cancel == true) { var div = ''; container.empty().append(div); } } else { var $div = $('<div class="star"></div>').append('<a href="#' + i + '" ></a>').appendTo(container); if (settings.increment == .5) { if (s % 2) { $div.addClass('star-left'); } else { $div.addClass('star-right'); } } }; i = i - 1 + settings.increment; s++; } if (settings.disabled == 1) { $('div.rating').css('cursor', 'default'); $('.star').css('cursor', 'default'); $('.star a').css('cursor', 'default'); $('.star a').attr('href', ''); } var stars = jQuery(container).children('.star'); var cancel = jQuery(container).children('.cancel'); stars.mouseover(function () { if (settings.disabled == 1) { return false; } $(this).css('background-position', '0 -32px'); event.drain(); event.fill(this); }).mouseout(function () { $(this).css('background-position', '0 0'); if (settings.disabled == 1) { return false; } event.drain(); event.reset(); }).focus(function () { if (settings.disabled == 1) { return false; } event.drain(); event.fill(this); }).blur(function () { if (settings.disabled == 1) { return false; } event.drain(); event.reset(); }); stars.click(function () { if (settings.disabled == 1) { return false; } settings.disabled = 1; if (settings.cancel == true) { settings.curvalue = (stars.index(this) * settings.increment) + settings.increment; jQuery.post(container.url + "?rate=" + jQuery(this).children('a')[0].href.split('#')[1], {}); return false; } else if (settings.maxvalue == 1) { settings.curvalue = (settings.curvalue == 0) ? 1 : 0; $(this).toggleClass('on'); jQuery.post(container.url + "?rate=" + jQuery(this).children('a')[0].href.split('#')[1], {}); return false; } return true; }); if (cancel) { cancel.mouseover(function () { event.drain(); jQuery(this).addClass('on'); }).mouseout(function () { event.reset(); jQuery(this).removeClass('on'); }).focus(function () { event.drain(); jQuery(this).addClass('on'); }).blur(function () { event.reset(); jQuery(this).removeClass('on'); }); cancel.click(function () { event.drain(); settings.curvalue = 0; jQuery.post(container.url, { "rating": jQuery(this).children('a')[0].href.split('#')[1] }); return false; }); }; var event = { fill: function (el) { var index = stars.index(el) + 1; stars.children('a').css('width', '100%').end().slice(0, index).addClass('hover').end(); }, drain: function () { stars.filter('.on').removeClass('on').end().filter('.hover').removeClass('hover').end(); }, reset: function () { stars.slice(0, settings.curvalue / settings.increment).addClass('on').end(); } }; event.reset(); return (this); };
jQuery.fn.hExtender = function (o) { var s = { minh: 100, mode: 1, c0: "extendable", c1: "extendbox", c2: "extender", c3: "xpd", c4: "t" }; if (o) { jQuery.extend(s, o); }; var a = jQuery(this); var b = a.find("." + s.c2); var x = a.find("." + s.c1); if (s.mode == 1) { x.each(function () { $(this).css("height", $(this).scrollHeight + "px"); $(this).find("." + s.c2).addClass(s.c4); }); } b.click(function () { var x = $(this).parents().filter(".extendable").find("." + s.c1); var h = x[0].scrollHeight; if (x.attr("class").indexOf(s.c3) < 0) { x.animate({ height: h + "px" }, "slow"); $(this).addClass(s.c4); x.addClass(s.c3); } else { x.animate({ height: s.minh + "px" }, "slow"); $(this).removeClass(s.c4); x.removeClass(s.c3); } }); return (this); }
tooltip = function () { var id = 'tt'; var top = 3; var left = 3; var maxw = 300; var speed = 10; var timer = 20; var endalpha = 95; var alpha = 0; var tt, t, c, b, h; var ie = document.all ? true : false; return { show: function (title, help, w) { if (tt == null) { tt = document.createElement('div'); $(tt).attr('id', id); $(tt).css('visibility', 'hidden'); t = document.createElement('div'); $(t).attr('id', id + 'top'); c = document.createElement('div'); $(c).attr('id', id + 'cont'); if (title != '') $(tt).append(t); $(tt).append(c); $(document.body).append(tt); $(tt).css('opacity', '0'); $(tt).css('filter', 'alpha(opacity=0)'); document.onmousemove = this.pos; }; $(tt).css('display', 'block'); t.innerHTML = title; c.innerHTML = help; $(tt).css('width', w ? w + 'px' : 'auto'); if (!w && ie) { $(t).css('display', 'none'); $(tt).css('width', $(tt).innerWidth()); $(t).css('display', 'block'); }; if ($(tt).innerWidth() > maxw) { $(tt).css('width', maxw + 'px') }; h = parseInt($(tt).innerHeight()) + top; clearInterval(tt.timer); tt.timer = setInterval(function () { tooltip.fade(1) }, timer); }, showAlt: function (text) { this.show('', text); }, pos: function (e) { var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY; var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX; $(tt).css('top', (u - h) + 'px'); $(tt).css('left', (l + left) + 'px'); }, fade: function (d) { var a = alpha; if ((a != endalpha && d == 1) || (a != 0 && d == -1)) { var i = speed; if (endalpha - a < speed && d == 1) { i = endalpha - a; } else if (alpha < speed && d == -1) { i = a; }; alpha = a + (i * d); $(tt).css('visibility', 'visible'); $(tt).css('opacity', alpha * .01); $(tt).css('filter', 'alpha(opacity=' + alpha + ')'); } else { clearInterval(tt.timer); if (d == -1) { $(tt).css('display', 'none') } } }, hide: function () { if (tt != undefined) { clearInterval(tt.timer); tt.timer = setInterval(function () { tooltip.fade(-1) }, timer); } } }; } ();
jQuery.fn.grtabs = function () {
    var o = jQuery(this);
    var l = o.find(".tbbar li");
    o.each(function () {
        var li = $(this).find(".tbbar li");
        if (li.length > 0) {
            var li = $(li[0]);
            li.addClass("act");
            $(this).find(".slider").html(li.find(".box").html())
        };

    });
    l.click(function () {
        var p = $(this).parents(".grtabs");
        var cb = p.find(".tbbar li");
        if (cb.length > 0) {
            p.find(".tbbar li").removeClass("act");
            $(this).addClass("act");
            p.find(".slider").html($(this).find(".box").html());
            $(this).addClass("act");
        }
    });
    return (this);
}
jQuery.fn.combo = function () {
    var o = jQuery(this);
    var arr = o.find(".arrow");
    var op = o.find("li");
    $(document).unbind(o);
    var s = false;
    o.each(function () {
        var sl = $(this).find("select")[0];
        $(this).find(".text").text(sl.options[sl.selectedIndex].text);
    });
    arr.click(function () {
        var co = $(this).parent().parent();
        o.css("z-index", "2");
        co.css("z-index", "10");
        if (co.attr("open") == undefined) {
            co.attr("open", "0");
        }
        var ul = co.find("ul");
        var drp = co.find("select");
        o.find("ul").fadeOut();
        if (co.attr("open") == "0") { s = true; co.attr("open", "1"); ul.fadeIn(600, function () { s = false; }); }
        else { ul.fadeOut(400, function () { co.attr("open", "0"); s = false; }); }
    });
    op.click(function () {
        var co = $(this).parent().parent();
        var ul = co.find("ul");
        var drp = co.find("select");
        var t = co.find(".text");
        t.text($(this).text());
        drp.val($(this).attr("value"));
        ul.fadeOut();
    });
    $(document).click(function () { if (!s) { o.find("ul").fadeOut(); o.attr("open", "0"); } })
    return (this);
}

jQuery.fn.scroll = function (o) {
    var o = jQuery(this);
    o.attr("pno", 1);
    o.attr("hover", "0");
    o.find(".scroll").css({ left: "0px" });

    var opt = {
        ocss: "scrollshow",
        fbCss: "floatbox",
        nbCss: "next",
        pbCss: "pre",
        imgCss: "thumb",
        imgCount: 1,
        w: 114,
        dur: 800
    }

    $.extend(opt, o);

    o.each(function () {
        var noTemp = "";
        var maxw = $(this).find(".scroll-content")[0].offsetWidth;
        var pc = (maxw % opt.w == 0) ? Math.round(maxw / opt.w) : Math.round(maxw / opt.w) + 1;

    });


    o.find(".pre").click(function () {
        events.pre($(this).parents(".scroll-box"));
    });

    o.find(".next").click(function () {
        events.next($(this).parents(".scroll-box"));
    });
    o.hover(function () { $(this).attr("hover", "1") }, function () { $(this).attr("hover", "0") })

    var events = {
        move: function (co, pno) {
            var sc = co.find(".scroll-content");
            var maxNo = sc[0].offsetWidth;
            maxNo = (maxNo % opt.w == 0) ? Math.round(maxNo / opt.w) : Math.round(maxNo / opt.w) + 1;
            pno = (pno < 1 || pno + 3 >= maxNo) ? 1 : pno;
            co.find(".no").attr("class", "no");
            co.find(".no:eq(" + (pno - 1) + ")").attr("class", "no act");

            var cno = Number(co.attr("pno"));
            co.attr("pno", pno);
            var d = Math.abs((pno - cno) * opt.w);

            var inc = (pno > cno) ? "-=" + d + "px" : "+=" + d + "px";
            co.find(".scroll").animate({ left: inc }, { duration: 'slow', easing: 'easeInOutQuad', complete: function () { } });
        },
        next: function (co) {
            events.move(co, Number(Number(co.attr("pno")) + 1));
        },
        pre: function (co) {
            events.move(co, Number(Number(co.attr("pno")) - 1));
        }

    }
    o.each(function () {
        var obj = $(this);
        setInterval(function () { if (obj.attr("hover") == "0") { events.next(obj) } }, 5000);
    });

    return (this);
}

jQuery.fn.rotator = function () {
    var o = jQuery(this);
    var l = o.find(".selector li");
    var timer = null;
    o.each(function () {
        $(this).children("#" + $(this).find(".selector li.act").attr("bid")).addClass("act");
    });
    l.click(function () {
        events.act($(this));
    });
    l.hover(function () {
        var c = $(this);
        if (timer) { clearTimeout(timer); timer = null };
        timer = setTimeout(function () { events.act(c); }, 300);
    });
    var events = {
        autoRotate: function (c, b) {
            if (c.attr("lock") != "1") { if (b) { events.rotate(c); } }
            setTimeout(function () { events.autoRotate(c, true) }, 8000);
        },
        rotate: function (c) {
            var nn = c.find(".selector li.act").next(".selector li");
            nn.length > 0 ? events.act(nn) : events.act($(c.find(".selector li")[0]));
        },
        act: function (c) {
            var p = c.parents(".rotator");
            var cb = c.find(".box");
            var s = p.find(".slider");
            if (cb.length > 0) {
                s.html(cb.html()).css("opacity", "0");
                p.find(".selector li").removeClass("act");
                c.addClass("act");
                s.animate({ opacity: 1 }, 0);
            }
        },
        init: function () {
            o.each(function () {
                var l = $(this).find(".selector li");
                if (l.length > 0) events.act($(l[0]));
                events.autoRotate($(this), false);
            });
        }
    };
    o.hover(function () { $(this).attr("lock", "1"); }, function () { $(this).attr("lock", "0"); });
    events.init();
    return (this);
}

jQuery.fn.scrollShow = function (o) {
    var o = jQuery(this);

    var opt = {
        ocss: "scrollshow",
        fbCss: "floatbox",
        nbCss: "next",
        pbCss: "pre",
        imgCss: "thumb",
        imgCount: 5,
        w: 650,
        dur: 800
    }

    $.extend(opt, o);

    var pb = o.find("." + opt.pbCss);
    var nb = o.find("." + opt.nbCss);
    pb.css("opacity", "0.5");
    pb.click(function () {
        var co = $(this).parents("." + opt.ocss);
        if (co.attr("lock") == "1") return;
        co.attr("lock", "1");
        var fb = co.find("." + opt.fbCss);
        var cnb = co.find("." + opt.nbCss);
        var lpos = fb.css("left");
        lpos = Math.abs(Number(lpos.substr(0, lpos.length - 2)));
        if (lpos > 0) {
            fb.animate({ "left": "+=" + opt.w + "px" }, opt.dur, function () { co.attr("lock", "0") });
            cnb.css('opacity', '1');
        }
        else { co.attr("lock", "0") };
        if (lpos <= opt.w) { $(this).css('opacity', '0.5'); }
        else $(this).css('opacity', '1');
    });

    nb.click(function () {
        var co = $(this).parents("." + opt.ocss);
        if (co.attr("lock") == "1") return;
        co.attr("lock", "1");
        var fb = co.find("." + opt.fbCss);
        var cpb = co.find("." + opt.pbCss);
        var lpos = fb.css("left");
        var maxw = co.find("." + opt.imgCss).length / opt.imgCount * opt.w;
        lpos = Math.abs(Number(lpos.substr(0, lpos.length - 2)));
        var d = maxw - lpos;
        if (d > opt.w) {
            fb.animate({ "left": "-=" + opt.w + "px" }, opt.dur, function () { co.attr("lock", "0") });
            cpb.css('opacity', '1');
        }
        else { co.attr("lock", "0") };
        if (d <= 2 * opt.w) { $(this).css('opacity', '0.5'); }
        else $(this).css('opacity', '1');
    });

    return (this);
}
jQuery.fn.advRotator = function (t) {
    var opt = {
        advCss: "adv",
        advObj: null,
        dur: 800,
        delay: 5000
    }

    var arrImg = new Array();
    var loaded = false;

    $.extend(opt, t);

    var a = jQuery(this);

    a.hover(function () {
        $(this).attr("stop", "1")
    },
    function () {
        $(this).attr("stop", "0")
    }
    );

    var events = {
        autoRotate: function (c, b) {
            var co = c.find("." + opt.advCss);
            if (b) { $(co[0]).show(); }
            if (co.length > 1) { setTimeout(function () { events.rotate(c) }, opt.delay) }
            else if (opt.advObj.length > 0 && !loaded) { setTimeout(function () { events.autoRotate(c, b) }, 1000); };
        },
        rotate: function (c) {
            if (c.attr("stop") != "1") {
                var pn = c.find("." + opt.advCss + ".act");
                var co = c.find("." + opt.advCss);
                var nn = pn.next("." + opt.advCss);
                nn.length > 0 ? events.act(nn, pn) : events.act($(co[0]), $(co[co.length - 1]));
            }
            setTimeout(function () { events.rotate(c) }, opt.delay);
        },
        act: function (c, p) {
            if (p != undefined) {
                p.fadeOut(opt.dur);
            }
            c.parents($(this).selector).children("." + opt.advCss).removeClass("act");
            c.addClass("act");
            c.fadeIn(opt.dur);
        },
        loadAdv: function (c, o) {
            for (var i = 0; i < o.length; i++) {
                var img = new Image();
                img.src = o[i].image;
                img.alt = o[i].title;
                img.link = o[i].link;
                if (i == 0) events.loadComplate(c, o, img);
                else img.onload = new function () { events.loadComplate(c, o, img) };
            }
        },
        loadComplate: function (c, o, img) {
            arrImg[arrImg.length] = img;
            c.append('<div class="' + opt.advCss + '"><a target="_blank" href="' + img.link + '"><img border=0 src="' + img.src + '"/></a></div>');
            if (arrImg.length == opt.advObj.length) { loaded = true; }
        },
        init: function () {
            a.each(function () {
                if (opt.advObj != null)
                    events.loadAdv($(this), opt.advObj);
            });
            a.each(function () {
                events.autoRotate($(this), true);
            });
        }
    };
    events.init();
    return (this);
}
function checkImage(O) {
    if (O.attr("src") == "") { O.css("display", "none"); }
    else {
        var img = new Image();
        img.src = O[0].src;
        img.onerror = function () { O[0].src = "/images/nopic.gif" };
    }
}

/***********************/
function bItem(id, name, count, price, image) {
    this.name = name;
    this.id = id;
    this.count = count;
    this.image = image;
    this.price = price;
}
function getBasketItems(b) {
    var items = new Array();

    str = b.d.split('<#>');
    if (str.length > 1) {
        for (i = 0; i < str.length; i += 5) {
            items.push(new bItem(str[i], str[i + 1], str[i + 2], str[i + 3], str[i + 4]));
        }
    }

    return items;
}
function addToBasket(pid, haveCover, cover, count) {
    $.ajax({
        type: "POST",
        url: "/Services/ShopServices.asmx/AddToBasket",
        data: "{ProductCode:'" + pid + "',Quantity:" + count + "}",
        contentType: "application/json; charset=utf-8",
        success: shopResponse,
        dataType: "json",
        error: ajaxCallFailed
    });
}
function removeFromBasket(id) {
    $.ajax({
        type: "POST",
        url: "Services/ShopServices.asmx/RemoveFromBasket",
        data: "{OrderID:" + id + "}",
        contentType: "application/json; charset=utf-8",
        success: shopResponse,
        dataType: "json",
        error: ajaxCallFailed
    });

}
function getBasket() {
    $.ajax({
        type: "POST",
        url: "Services/ShopServices.asmx/GetBasket",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        success: shopResponse,
        dataType: "json",
        error: ajaxCallFailed
    });
}
function shopResponse(response) {

    if (response == 'error') {
        //alert(clientErrorMessage);var data = response.d != undefined ? response.d : response;
    }
    buildBasket(response);
}
function gp(P, K, O) {
    var res = "";
    $.ajax({
        type: "POST",
        url: "Services/ShopServices.asmx/GP",
        data: "{P:'" + P + "',K:'" + K + "'}",
        contentType: "application/json; charset=utf-8",
        success: function rs(r) { O.text(r.d) },
        dataType: "json",
        error: ajaxCallFailed
    });
}
/*******************************************/
function Marquee() {
    $(".marquee .box").slideUp();
}
$(document).ready(function () {
    $(".r-price").each(function () {
        gp($(this).text(), $(this).attr("k"), $(this));
    });

    jmenu.buildmenu("tnav", _imgs);
    jmenu.buildmenu("rnav", _imgs);

    if ($("#advList1").length > 0) { $("#advList1").advRotator({ advObj: advList1 }) }
    if ($("#advList2").length > 0) { $("#advList2").advRotator({ advObj: advList2 }) }
    if ($('#gallery a').length > 0) { $('#gallery a').lightBox({}); }

    $(".grtabs").grtabs();

    if ($('.scroll-box').length > 0) {
        $(".scroll-box").scroll();
    }

    $('#hi_message .min').click(function () {
        hideMessage('hi_message');
    })

    if ($(".rotator").length > 0) {
        $(".rotator").rotator();
    }
    if ($('.rating').length > 0) {
        $('#pr_rating').rating('Rating.aspx', { maxvalue: 5, increment: 1, curvalue: _hiberd.getVar('cRate'), disabled: _hiberd.getVar('isRated') });
    }
    Marquee();
    $(".thumb img").each(function () {
        checkImage($(this));
    });

    $(".pr-menu-link").click(function () {
        if ($(".pr-menu").css("display") == "block") {
            $(".pr-menu").fadeOut();
        }
        else {
            $(".pr-menu").fadeIn();
        }
    })

    $(".ship-table input").click(function () {
        $.ajax({
            type: "POST",
            url: "Services/ShopServices.asmx/DoShipping",
            data: "{sc:'" + $(this).val() + "'}",
            contentType: "application/json; charset=utf-8",
            success: scResponse,
            dataType: "json",
            error: ajaxCallFailed
        });
    })
});
function scResponse(r) {
    var o;
    if (r.d != undefined) o = eval(r.d);
    else o = eval(r);

    $(".cost-label").text(o.tp + o.sc);
    $(".sc-cost").text(o.sc);
}
