﻿var hiberd;
var productsInfo;
var _imgs = { down: ['arr_d', '/images/arr_b_tnav.gif', 10], 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) { alert(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.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.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);
    }
    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();
}
function getAdsList(adsList) {
    $.ajax({
        type: "POST",
        url: "/Services/ShopServices.asmx/GetAdsPrice",
        data: "{AdsList:'" + adsList + "'}",
        contentType: "application/json; charset=utf-8",
        success: adsResponse,
        dataType: "json",
        error: ajaxCallFailed
    });
}
function adsResponse(res) {
    var r = res.hasOwnProperty("d") ? res.d : res;
    $("#adsTotal").text(r);
    $(".ads-chk").removeAttr("disabled");
}
$(document).ready(function () {
    $(".ads-chk").click(function () {
        var val = '';
        $(".ads-chk:checked").each(function () {
            val += "|" + $(this).attr("id");
        });
        $("#hidadsval input").val(val);
        getAdsList(val);
        $(".ads-chk").attr("disabled", true);
    })

    $(".r-price").each(function () {
        gp($(this).text(), $(this).attr("k"), $(this));
    });

    jmenu.buildmenu("tnav", _imgs);
    jmenu.buildmenu("tnav2", _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();

    $('#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
        });
    })

    $(".btn-more").click(function () { $(this).next("#main-popup").fadeIn(); });
    $(".btn-close").click(function () { $(this).parents("#main-popup").fadeOut(); });
    $(".expandable").hide();
    $(".expander").attr('class', 'expander');

    $(".expander").click(function () {
        var c = $(this).attr('class');
        if (c == "expander") {
            $(this).attr("class", "expander show");
            $(this).next(".expandable").slideDown();
        }
        else {
            $(this).attr("class", "expander");
            $(this).next(".expandable").slideUp();
        }
    });

    $('.box1.slideright').hover(function () {
        $(".cover", this).stop().animate({ left: '207px' }, { queue: false, duration: 300 });
    }, function () {
        $(".cover", this).stop().animate({ left: '0px' }, { queue: false, duration: 300 });
    });
    $('.box2.slideright').hover(function () {
        $(".cover", this).stop().animate({ left: '207px' }, { queue: false, duration: 300 });
    }, function () {
        $(".cover", this).stop().animate({ left: '0px' }, { queue: false, duration: 300 });
    });
    $('.adv-side.slideright').hover(function () {
        $(".cover", this).stop().animate({ left: '170px' }, { queue: false, duration: 300 });
    }, function () {
        $(".cover", this).stop().animate({ left: '0px' }, { queue: false, duration: 300 });
    });
    $('.wide-box.captionfull').hover(function () {
        $(".cover", this).stop().animate({ top: '0px' }, { queue: false, duration: 160 });
    }, function () {
        $(".cover", this).stop().animate({ top: '173px' }, { queue: false, duration: 160 });
    });

    $('.OpenComment').click(function () {
        if ($(this).next('div.comment-conteiner').is(':visible')) {
            $(this).next('div.comment-conteiner').slideUp('slow');
            $(this).attr('src', '/Images/toggle-expand-alt.png');
        }
        else {
            $(this).next('div.comment-conteiner').slideDown('slow');
            $(this).attr('src', '/Images/toggle-collapse-alt.png');
        }
    }).mouseenter(function () {
        $(this).css({ 'cursor': 'pointer' });
    }).mouseleave(function () {
        $(this).css({ 'cursor': 'default' });
    });

});


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);
}
function pageLoad() {
    var galImg = "<img src='" + $(".prod-gal-thumb:first").attr("img") + "' />";
    $(".prod-gal-img span").html(galImg);

    $(".prod-gal-thumb").click(function () {
        var pic = "<img src='" + $(this).attr("img") + "' />";
        $(".prod-gal-img span").hide();
        $(".prod-gal-img span").html(pic).fadeIn();
    })
}
