﻿$(document).ready(function() {
    $("a[rel^='prettyPhoto']").prettyPhoto({ animationSpeed: 'fast', theme: 'facebook', showTitle: true, slideshow: 5000 });

    PrepareCommonDialogs();
    $("a[rel^='popupRegulamin']").bind("click", function() { PokazRegulaminPopup(); });
    $("a[rel^='popupWysylka']").bind("click", function() { PokazWysylkaPopup(); });

    detectBrowser();
    AktTT();
    OdswiezKoszykMini();
});

function AktTT() {
    $(".ttt").ezpz_tooltip({
        contentPosition: 'myFollow',
        offset: 5,
        showContent: function(content) { content.fadeIn('fast'); }
        , hideContent: function(content) { /*content.stop(true, true).fadeOut('fast');*/content.stop(true, true).hide(); }
    });

    $(".tttd").ezpz_tooltip({
        contentPosition: 'belowFollow',
        showContent: function(content) { content.show(); }
        , hideContent: function(content) { /*content.stop(true, true).fadeOut('fast');*/content.stop(true, true).hide(); }
    });
}

$.fn.ezpz_tooltip.positions.myFollow = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
    contentInfo = $.fn.ezpz_tooltip.positions.belowRightFollow(contentInfo, mouseX, mouseY, offset, targetInfo);
    contentInfo['left'] += 20;
    contentInfo['top'] -= 25;
    return contentInfo;
};

function detectBrowser() {
    var browser = navigator.appName;
    var version = navigator.appVersion.split('MSIE');
    version = parseInt(version[1]);

    if ((browser == "Microsoft Internet Explorer") && (version <= 6)) {
        StopIE6();
    }
}

function StopIE6() {
    var StopBox = $('#StopIe6');
    StopBox.show();

    var paper = $('div.tlo');
    paper.css("margin-top", "80px");
}

function OdswiezKoszykMini() {
    CzyscKoszykMini();

    $.ajax({
        type: "POST",
        url: ws_url + "/CalculateMiniCart",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            CalculateMiniCartSucc(data);
        },
        error: CalculateMiniCartFail
    });

    $.ajax({
        type: "POST",
        url: ws_url + "/FillMiniCart",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            FillMiniCartSucc(data);
        },
        error: FillMiniCartFail
    });
}

function CzyscKoszykMini() {
    $("#koszykIlosc").empty();
    $("#koszykWartosc").empty();
    $("#innerMiniCart").empty();
    $("#innerMiniCartSummary").hide();
    $("#imcIlosc").empty();
    $("#imcWartosc").empty();
    $("#innerMiniCartFollow").text("Przejdź do koszyka");
}

function CalculateMiniCartSucc(result) {
    var array = result.d;
    var suma = array[0][1].toString();
    var ilosc = array[0][0].toString();

    if (ilosc != "0") {

        var prTekst = "produkt";
        if (ilosc == "2" || ilosc == "3" || ilosc == "4") prTekst = "produkty";
        else if (ilosc != "1") prTekst = "produktów";

        $("#imcIlosc").append(ilosc);
        $("#imcWartosc").append(suma);
        $("#innerMiniCartSummary").show();
        $("#innerMiniCartFollow").text("Zamawiam - przejdź do koszyka");
        $("#koszykIlosc").empty();
        $("#koszykWartosc").empty();
        $("#koszykIlosc").hide();
        $("#koszykWartosc").hide();
        $("#koszykIlosc").append("<span>" + ilosc + "</span> " + prTekst);
        $("#koszykWartosc").append("<span>" + suma + "</span>");
        $("#koszykIlosc").fadeIn(500);
        $("#koszykWartosc").fadeIn(500);
    }
    else {
        $("#koszykIlosc").append("Twój koszyk jest jeszcze pusty.");
        $("#koszykWartosc").append("");
    }
}
function CalculateMiniCartFail(result) {
    //
}

function FillMiniCartSucc(result) {
    var array = result.d;
    var html = "";

    $(array).each(function() {
        html += "<div class='imcRow'>";
        html += "<p>" + $(this)[0] + "</p>";
        html += "Ilość: <span class='imcRowIl'>" + $(this)[1] + "</span> Cena: <span class='imcRowWa'>" + $(this)[2] + "</span>";
        html += "</div>";
    });

    $("#innerMiniCart").append(html);
}
function FillMiniCartFail(result) {
    //
}

function PrepareCommonDialogs() {
    $('#dialogRegulamin').dialog({
        autoOpen: false,
        modal: true,
        width: 800,
        height: 500,
        disabled: true,
        position: 'center',
        resizable: false,
        closeOnEscape: true,
        open: function(event, ui) { $(".ui-dialog-titlebar-close").show(); }
    });

    $('#dialogWysylka').dialog({
        autoOpen: false,
        modal: true,
        width: 800,
        height: 400,
        disabled: true,
        position: 'center',
        resizable: false,
        closeOnEscape: true,
        open: function(event, ui) { $(".ui-dialog-titlebar-close").show(); }
    });

    $('#dialogDefaultWait').dialog({
        autoOpen: false,
        modal: true,
        width: 300,
        height: 120,
        disabled: true,
        position: 'center',
        resizable: false,
        closeOnEscape: false,
        open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
    });
}

function PokazRegulaminPopup() {
    PokazDefaultWaitPopup();
    $.ajax({
        type: "POST",
        url: ws_url + "/GetRegulaminText",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            PokazRegulaminPopupSucc(data);
        },
        error: PokazRegulaminPopupFail
    });
}
function PokazRegulaminPopupSucc(result) {
    var html = result.d;
    $("#dialogRegulamin").empty();
    $("#dialogRegulamin").append(html);
    UkryjDefaultWaitPopup();
    $("#dialogRegulamin").dialog("open");
}
function PokazRegulaminPopupFail(result) {
}

function PokazWysylkaPopup() {
    PokazDefaultWaitPopup();
    $.ajax({
        type: "POST",
        url: ws_url + "/GetWysylkaText",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            PokazWysylkaPopupSucc(data);
        },
        error: PokazWysylkaPopupFail
    });
}
function PokazWysylkaPopupSucc(result) {
    var html = result.d;
    $("#dialogWysylka").empty();
    $("#dialogWysylka").append(html);
    UkryjDefaultWaitPopup();
    $("#dialogWysylka").dialog("open");
}
function PokazWysylkaPopupFail(result) {
}

function PokazDefaultWaitPopup() {
    $("#dialogDefaultWait").dialog("open");
}
function UkryjDefaultWaitPopup() {
    $("#dialogDefaultWait").dialog("close");
}
