﻿$(document).ready(function() {
    $('.extra').click(function(e) {
        e.preventDefault();
        $("#pieces").val($(this).attr('accesskey'));
        $("#photo").attr("src", $(this).children('img').attr('src').replace('_sm', '_xl'));
        $.getJSON("/Json/GetColors", { s: $("#pieces").val() }, function(data) {
            $("#colors").fillSelect(data);
            GetItemPrice();
        });
    });

    $('#pieces').change(function() {
        $.getJSON("/Json/GetColors", { s: $("#pieces").val() }, function(data) {
            $("#colors").fillSelect(data);
            SetImage();
            GetItemPrice();
        });
    });

    $('#colors').change(function() {
        SetImage();
        GetItemPrice();
    });

    $('.enlarge').click(function(e) {
        e.preventDefault();
        $('#modalset').css('display', 'block');
        $('#modalset > div > img').attr('src', $('#photo').attr('src'));
    });

    $('.colorchart').click(function(e) {
        e.preventDefault();
        $('#colorchart').css('display', 'block');
        $('#colorchart > div > img').attr('src', '/Content/Images/Modal/chart.jpg');
    });

    SetImage();
    GetItemPrice();
});

function SetImage() {
    $.get('/Json/GetProductImage', { s: $("#colors").val() }, function(data) {
        $("#photo").attr("src", data);
    });
}

function GetItemPrice() {
    $.get("/Json/GetItemPrice", { s: $("#colors").val() }, function(data) {
        $('#price').text(data);
    });
}

$.fn.clearSelect = function() {
    return this.each(function() {
        if (this.tagName == 'SELECT')
            this.options.length = 0;
    });
}

$.fn.fillSelect = function(data) {
    return this.clearSelect().each(function() {
        if (this.tagName == 'SELECT') {
            var dropdownList = this;

            $.each(data, function(index, optionData) {
                var option = new Option(optionData.Text, optionData.Value);

                if ($.browser.msie) {
                    dropdownList.add(option);
                } else {
                    dropdownList.add(option, null);
                }
            });
        }
    });
}