﻿function Variance(id, sp, cp, disc, stock, color, size) {
    this.id = id;
    this.StartPrice = sp;
    this.CalculatedPrice = cp;
    this.Discount = disc;
    this.Stock = stock;
    this.VarianceSize = size;
    this.VarianceColor = color;
}


function Color(id, name, hexcode) {
    this.id = id;
    this.name = name;
    this.hexcode = hexcode;
}

function Size(id, name) {
    this.id = id;
    this.name = name;
}

function Product() {
    this.variances = new Array(arguments.length);
    for (i = 0; i < arguments.length; i++) {
        this.variances[i] = arguments[i];
    }

    this.addVariance = function(variance) {
        this.variances.push(variance)
    };
    this.AvailabilityInSizeColor = function(sizeid, colorid) {
        for (icount = 0; icount < this.variances.length; ++icount) {
            if (this.variances[icount].VarianceColor.id == colorid && this.variances[icount].VarianceSize.id == sizeid && this.variances[icount].Stock > 0) {
                return true;
            }
        }
        return false;
    }
}


$(document).ready(function() {
    $(".colour").bind("click", colourClick);
    $(".colour").bind("mouseenter", colourMouseEnter);
    $(".colour").bind("mouseleave", colourMouseLeave);
    $(".plaincolour").bind("click", plainColourClick);
    $(".plaincolour").bind("mouseenter", plainColourMouseEnter);
    $(".plaincolour").bind("mouseleave", plainColourMouseLeave);
    $(".size").bind("click", sizeClick);
    $(".size").bind("mouseenter", sizeMouseEnter);
    $(".size").bind("mouseleave", sizeMouseLeave);
});

var sizeMouseLeave = function() {
    if ($("#selectedSize").text() != $(this).text()) {
        $(this).css({
            'background-color': '#FFFFFF'
        });
    }
}

var sizeMouseEnter = function() {
    $(this).css({
        'background-color': '#A7893F'
    });
}

var sizeClick = function() {
    var doSelect = false;
    if (document.forms[0].colorId.value == "") {
        doSelect = true;
    } else {
        if (product.AvailabilityInSizeColor($(this).attr("id"), document.forms[0].colorId.value)) {
            doSelect = true;
        } else {
            doSelect = false;
        }
    }

    if (doSelect) {
        for (i = 0; i < $(".colorSwatches span").length; ++i) {
            if (product.AvailabilityInSizeColor($(this).attr("id"), $(".colorSwatches span").eq(i).attr("id"))) {
                $(".colorSwatches span").eq(i).attr("class", "colour");
                $(".colorSwatches span").eq(i).bind("click", colourClick);
                $(".colorSwatches span").eq(i).bind("mouseenter", colourMouseEnter);
                $(".colorSwatches span").eq(i).bind("mouseleave", colourMouseLeave);
            } else {
                $(".colorSwatches span").eq(i).attr("class", "inactivecolour");
                $(".colorSwatches span").eq(i).unbind();
            }
        }
        $(".size").css({
            'background-color': '#FFFFFF'
        });
        $(this).css({
            'background-color': '#A7893F'
        });
        $("#selectedSize").text($(this).text());
        document.forms[0].sizeId.value = $(this).attr("id");
        setSizeColourSelectionText();
    }
}


var colourMouseLeave = function() {
    if ($("#selectedColour").text() != $(this).text()) {
        $(this).css({
            'border-color': '#CCCCCC'
        });
    }
}

var colourMouseEnter = function() {
    $(this).css({
        'border-color': 'red'
    });
}


var colourClick = function() {
    var doSelect = false;
    if (document.forms[0].sizeId.value == "") {
        doSelect = true;
    } else {
        if (product.AvailabilityInSizeColor(document.forms[0].sizeId.value, $(this).attr("id"))) {
            doSelect = true;
        } else {
            doSelect = false;
        }
    }
    if (doSelect) {
        for (i = 0; i < $(".sizeSwatches span").length; ++i) {
            if (product.AvailabilityInSizeColor($(".sizeSwatches span").eq(i).attr("id"), $(this).attr("id"))) {
                $(".sizeSwatches span").eq(i).attr("class", "size");
                $(".sizeSwatches span").eq(i).bind("click", sizeClick);
                $(".sizeSwatches span").eq(i).bind("mouseenter", sizeMouseEnter);
                $(".sizeSwatches span").eq(i).bind("mouseleave", sizeMouseLeave);
            } else {
                $(".sizeSwatches span").eq(i).attr("class", "inactivesize");
                $(".sizeSwatches span").eq(i).css("background-color", "");
                $(".sizeSwatches span").eq(i).unbind();
            }
        }
        $(".colour").css({
            'border-color': '#CCCCCC'
        });
        $(this).css({
            'border-color': 'red'
        });
        $("#selectedColour").text($(this).text());
        document.forms[0].colorId.value = $(this).attr("id");
        setSizeColourSelectionText();
    }
}


var plainColourMouseLeave = function() {
    if ($("#selectedColour").text() != $(this).text()) {
        $(this).css({
            'border-color': '#CCCCCC'
        });
    }
}

var plainColourMouseEnter = function() {
    $(this).css({
        'border-color': 'red'
    });
}

var plainColourClick = function() {
    var doSelect = false;
    $(".plaincolour").css({
        'border-color': '#CCCCCC'
    });
    $(this).css({
        'border-color': 'red'
    });
    $("#selectedColour").text($(this).text());
    document.forms[0].colorId.value = $(this).attr("id");
    setColourSelectionText();
}


function setSizeColourSelectionText() {
    strTitle = $("h2.title").text();
    strSize = $("#selectedSize").text();
    strColor = $("#selectedColour").text();
    sizeId = document.forms[0].sizeId.value;
    colorId = document.forms[0].colorId.value;
    
    strPrice = "";
    strText = strTitle;

    if (strColor + "" != "" && strSize + "" != "") {
        for (vv = 0; vv < product.variances.length; ++vv) {
            if (product.variances[vv].VarianceColor.id == colorId &&
            product.variances[vv].VarianceSize.id == sizeId) {
                strPrice = product.variances[vv].CalculatedPrice;
                break;
            }
        }
    }
    if (strColor + "" != "")
        strText += ", " + strColor;
    if (strSize + "" != "")
        strText += ", " + strSize;
    if (strPrice + "" != "")
        strText += "<br/>" + strPrice + " &euro;";
    $("#selectiontext").html(strText);
}


function setColourSelectionText() {
    strTitle = $("h2.title").text();
    strColor = $("#selectedColour").text();
    colorId = document.forms[0].colorId.value;

    strPrice = "";
    strText = strTitle;

    if (strColor + "" != "" ) {
        for (vv = 0; vv < product.variances.length; ++vv) {
            if (product.variances[vv].VarianceColor.id == colorId ) {
                strPrice = product.variances[vv].CalculatedPrice;
                break;
            }
        }
    }
    if (strColor + "" != "")
        strText += ", " + strColor;
    if (strPrice + "" != "")
        strText += "<br/>" + strPrice + " &euro;";
    $("#selectiontext").html(strText);
}


function addProductWithSizeColorToCart() {
    sizeId = document.forms[0].sizeId.value;
    colorId = document.forms[0].colorId.value;
    langId = document.forms[0].langId.value;
    varianceId = "";
    if (sizeId + "" != "" && colorId + "" != "") {
        for (vv = 0; vv < product.variances.length; ++vv) {
            if (product.variances[vv].VarianceColor.id == colorId &&
            product.variances[vv].VarianceSize.id == sizeId) {
                varianceId = product.variances[vv].id;
                break;
            }
        }
        var quantity = document.forms[0].quantity.options[document.forms[0].quantity.selectedIndex].value;
        if (varianceId != "") {
            var url = "http://www.magnesianews.gr/Ajax/addToCart.ashx?productId=" + varianceId + "&quantity=" + quantity + "&lang=" + langId + "&salt=" + +Math.floor(Math.random() * 100000000);
            $("#basket").load(url, null, function() {
                cartLoadComplete();
            });
        }
    } else {
    //TODO: internationalize string like http://msdn.microsoft.com/en-us/library/bb398935.aspx
        alert("Επιλέξτε χρώμα - μέγεθος");
    }
}

function addProductWithColorToCart() {
    colorId = document.forms[0].colorId.value;
    langId = document.forms[0].langId.value;
    varianceId = "";
    if (colorId + "" != "") {
        for (vv = 0; vv < product.variances.length; ++vv) {
            if (product.variances[vv].VarianceColor.id == colorId) {
                varianceId = product.variances[vv].id;
                break;
            }
        }
        var quantity = document.forms[0].quantity.options[document.forms[0].quantity.selectedIndex].value;
        if (varianceId != "") {
            var url = "http://www.magnesianews.gr/Ajax/addToCart.ashx?productId=" + varianceId + "&quantity=" + quantity + "&lang=" + langId + "&salt=" + +Math.floor(Math.random() * 100000000);
            $("#basket").load(url, null, function() {
                cartLoadComplete();
            });
        }
    } else {
        //TODO: internationalize string like http://msdn.microsoft.com/en-us/library/bb398935.aspx
        alert("Επιλέξτε χρώμα");
    }
}


function addPlainProductToCart() {
    productId = document.forms[0].productId.value;
    langId = document.forms[0].langId.value;
    var quantity = document.forms[0].quantity.options[document.forms[0].quantity.selectedIndex].value;
    if (productId != "") {
        var url = "http://www.magnesianews.gr/Ajax/addToCart.ashx?productId=" + productId + "&quantity=" + quantity + "&lang=" + langId + "&salt=" + +Math.floor(Math.random() * 100000000);
        $("#basket").load(url, null, function() {
            cartLoadComplete();
        });
    }
}

function addToCartFromProdcutList(productId, langId) {
    if (productId != "") {
        var url = "http://www.magnesianews.gr/Ajax/addToCart.ashx?productId=" + productId + "&quantity=1&lang=" + langId + "&salt=" + +Math.floor(Math.random() * 100000000);
        $("#basket").load(url, null, function() {
            cartLoadComplete();
        });
    }
}
function showCartLayer() {
    var langId = 69;
    var url = window.location + "";
    if (url.indexOf("/en/") > 0)
        langId = 50;
    $("#basket").load("http://www.magnesianews.gr/Ajax/addToCart.ashx?lang=" + langId + "&salt=" + Math.floor(Math.random() * 100000000), null, function() {
        cartLoadComplete();
    });
}

function cartLoadComplete() {
    if ($("#basket").is(":hidden")) {
        $("#basket").slideDown("slow", setTimerClose());
    }
    var totalItems = $("#cartTotalItems").html();
    if (totalItems != null) {
        if (totalItems == "1") {
            $("#spanDynamicCartTotalItems").html("1 προϊόν");
        }
        if (totalItems == "0") {
            $("#spanDynamicCartTotalItems").html("είναι άδειο");
        }
        if (totalItems > "1") {
            $("#spanDynamicCartTotalItems").html(totalItems + " προϊόντα");
        }
    }
    else {
        $("#spanDynamicCartTotalItems").html("είναι άδειο");
    }
}


function setTimerClose() {
    window.setTimeout("closeCartLayer()", 10000);
}

function closeCartLayer() {
    $("#basket").hide();
}



