﻿var langId = 69;
var siteroot = "/";

function Variance(id, sp, cp, disc, stock, availability, color, size) {
    this.id = id;
    this.StartPrice = sp;
    this.CalculatedPrice = cp;
    this.Discount = disc;
    this.Stock = stock;
    this.VarianceSize = size;
    this.VarianceColor = color;
    this.Availability = availability;
}


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;
    }

    this.AvailabilityInColor = function(colorid) {
        for (icount = 0; icount < this.variances.length; ++icount) {
            if (this.variances[icount].VarianceColor.id == colorid  && 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();
                $(".colorSwatches span").eq(i).bind("click", inactiveColourClick);
            }
        }
        $(".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 inactiveColourClick = function() {
    var doSelect = false;
    document.forms[0].sizeId.value = "";
    $(this).attr("class", "colour");
    $(this).bind("click", colourClick);
    $(this).bind("mouseenter", colourMouseEnter);
    $(this).bind("mouseleave", colourMouseLeave);

    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();
    if (product.AvailabilityInColor($(this).attr("id"))) {
        $("#buttonCart").css({
            'color': '#990000'
        });
        $('#buttonCart').unbind();
		$('#buttonCart').removeAttr("onclick");
        $("#buttonCart").bind("click", function() { addProductWithColorToCart("Επιλέξτε παραλλαγή"); });
    }
    else {
        $("#buttonCart").css({
            'color': '#DDDDDD'
        });
        $('#buttonCart').unbind();
		$('#buttonCart').removeAttr("onclick");
        $("#buttonCart").bind("click", function() { alert("Μη διαθέσιμο") });
    }
}


function setSizeColourSelectionText() {
    strTitle = $("#productTitle").text();
    strSize = $("#selectedSize").text();
    strColor = $("#selectedColour").text();
    sizeId = document.forms[0].sizeId.value;
    colorId = document.forms[0].colorId.value;
    
    strPrice = "";
    strText = strTitle;
    var varianceIndex = 0;
    
    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;
                varianceIndex = vv;
                break;
            }
        }
    }
    if (strColor + "" != "")
        strText += ", " + strColor;
    if (strSize + "" != "")
        strText += ", " + strSize;
    if (strPrice + "" != "")
        strText += "<br/>" + strPrice + " &euro;";
    if (product.variances[varianceIndex].Availability + "" != "") {
        strText += "<br/>" + product.variances[varianceIndex].Availability;
    }
    $("#selectiontext").html(strText);
}


function setColourSelectionText() {
    strTitle = $("#productTitle").text();
    strColor = $("#selectedColour").text();
    colorId = document.forms[0].colorId.value;
    var varianceIndex = 0;
    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;
                varianceIndex = vv;
                break;
            }
        }
    }
    if (strColor + "" != "")
        strText += ", " + strColor;
    if (strPrice + "" != "")
        strText += "<br/>" + strPrice + " &euro;";
    if (product.variances[varianceIndex].Availability + "" != "") {
        strText += "<br/>" + product.variances[varianceIndex].Availability;
    }
    $("#selectiontext").html(strText);
}


function addProductWithSizeColorToCart(strMessage) {
    sizeId = document.forms[0].sizeId.value;
    colorId = document.forms[0].colorId.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 != "") {
            window.scroll(0, 0);
            var url = siteroot + "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(strMessage);
    }
}

function addProductWithColorToCart(strMessage) {
    colorId = document.forms[0].colorId.value;
    var stock = 0;
    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 != "") {
            window.scroll(0, 0);
            var url = siteroot + "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(strMessage);
    }
}


function addPlainProductToCart() {
    productId = document.forms[0].productId.value;
    var quantity = document.forms[0].quantity.options[document.forms[0].quantity.selectedIndex].value;
    if (productId != "") {
        window.scroll(0, 0);
        var url = siteroot + "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 != "") {
        window.scroll(0, 0);
        var url = siteroot + "Ajax/addToCart.ashx?productId=" + productId + "&quantity=1&lang=" + langId + "&salt=" + +Math.floor(Math.random() * 100000000);
        $("#basket").load(url, null, function() {
            cartLoadComplete();
        });
    }
}
function showCartLayer() {
    $("#basket").load(siteroot + "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();
}




