﻿///<reference name="jquery-1.3.2.min.js"/>

function Cube(width, length, height) {
    this.Width = width;
    this.Length = length;
    this.Height = height;

    //this.toString() = function() { return this.Width.toString() + this.Length.toString() + this.Height.toString(); };
};
Cube.prototype.toString = function() { return this.Width.toString() + this.Length.toString() + this.Height.toString(); };

function Cover(fabric, color) {
    this.Fabric = fabric;
    this.Color = color; 
    //this.toString() = function() { return this.Fabric.toString() + this.Color.toString(); };
};
Cover.prototype.toString = function() { return this.Fabric.toString() + this.Color.toString(); };

function ProductOptions() {
    this.Dimensions = new Array();
    this.Covers = new Array();

    this.GetValidLengths = function(Width) {
        var returnValue = new Array();
        for (var cube in this.Dimensions) {
            if (this.Dimensions[cube].Width == Width) returnValue.push(this.Dimensions[cube]);
        }
        return returnValue;
    };
    this.GetValidHeights = function(Width, Length) {
        var returnValue = new Array();
        for (var cube in this.Dimensions) {
            if (this.Dimensions[cube].Width == Width && this.Dimensions[cube].Length == Length) returnValue.push(this.Dimensions[cube]);
        }
        return returnValue;
    };

    this.GetValidColors = function(Fabric) {
        var returnValue = new Array();
        for (var cover in this.Covers) {
            if (this.Covers[cover].Fabric == Fabric) returnValue.push(this.Covers[cover]);
        }
        return returnValue;
    };
};

function AccessoriesClass(productid, price, description) {
    this.productid = productid;
    this.quantity = 0;
    this.price = price;
    this.description = description;
    this.elementid = function() { "p" + this.productid.toString()};
}

function Product(style, width, length, height, fabric, color) {
    this.style = style;
    this.width = width;
    this.length = length;
    this.height = height;
    this.fabric = fabric;
    this.color = color;
    this.price = new Number();
    this.productid = '';

    this.Update = function() {
        var tPrice = Products.getItem(this.toString());
        if (typeof tPrice == 'undefined') { alert("Unknown Config:" + this.toString()); return; }
        this.price = tPrice.Price;
        this.productid = tPrice.ProductID;
        $("#productprice").text(this.price);
        $("#productid").text(this.productid);
        $("#shelterimage").attr("src", "/productimages/" + this.style.toString() + this.width.toString() + this.height.toString() + ".png");
        buildSide();
        updateLinks();
    };

    this.updatewidth = function(Width) {
        if (Width == null) return;
        //if (Width == this.width) return;
        this.width = Width;
        var testCollection = Products.getItem(this.toString());
        if (typeof testCollection == "undefined") {
            var ValidCube = ProductConfig.GetValidLengths(this.width);
            this.height = ValidCube[0].Height;
            HeightControl.changeValue(this.height);
            this.length = ValidCube[0].Length;
            LengthControl.changeValue(this.length);
        }
        this.Update();
        //update length and height options and all prices
        WidthControl.Update();
        HeightControl.Update();
        LengthControl.Update();
        FabricControl.Update();
        ColorControl.Update();
    };
    this.updatelength = function(Length) {
        if (Length == null) return;
        if (Length == this.length) return;
        this.length = parseInt(Length);
        var testCollection = Products.getItem(this.toString());
        if (typeof testCollection == "undefined") {
            this.height = ProductConfig.GetValidHeights(this.width, this.length)[0].Height;
            HeightControl.changeValue(this.height);
        }
        this.Update();
        //update height options & length and height prices
        LengthControl.Update();
        HeightControl.Update();
        FabricControl.Update();
        ColorControl.Update();
    };
    this.updateheight = function(Height) {
        if (Height == null) return;
        if (Height == this.height) return;
        this.height = Height;

        this.Update();
        //Update Height prices
        HeightControl.Update();
        FabricControl.Update();
        ColorControl.Update();

    };
    this.updatefabric = function(Fabric) {
        if (Fabric == null) return;
        if (Fabric == this.fabric) return;
        this.fabric = Fabric;
        var testCollection = Products.getItem(this.toString());
        if (typeof testCollection == "undefined")
            this.color = ProductConfig.GetValidColors(this.fabric)[0].Color;
        this.Update();
        // Update Colors & fabric prices
        FabricControl.Update();
        ColorControl.Update();

    };
    this.updatecolor = function(Color) {
        if (Color == null) return;
        if (Color == this.color) return;
        this.color = Color;
        this.Update();
        ColorControl.Update();
    };

    this.GetValidDimensions = function() {
        return ProductConfig.GetValidDimensions(this.width);
    };
    this.GetValidColors = function() {
        return ProductConfig.GetValidColors(this.fabric);
    };
    this.GetValidLengths = function() {
        return ProductConfig.GetValidLengths(this.width);
    };
    this.GetValidHeights = function() {
        return ProductConfig.GetValidHeights(this.width, this.length);
    };

};
Product.prototype.toString = function() { return this.style.toString() + this.width.toString() + this.length.toString() + this.height.toString() + this.fabric.toString() + this.color.toString(); };

function PriceDiffText(p) {
    var diff = p.Price - currentProduct.price;
    diff = Math.round(diff * 100) / 100;
    if (diff == 0) return "Included in Price"
    if (diff > 0) return "Add $" + diff.toString()
    else return "Subtract $" + Math.abs(diff).toString();

}

function Hash() {
    //http://www.mojavelinux.com/articles/javascript_hashes.html
    this.length = 0;
    this.items = new Array();
    for (var i = 0; i < arguments.length; i += 2) {
        if (typeof (arguments[i + 1]) != 'undefined') {
            this.items[arguments[i]] = arguments[i + 1];
            this.length++;
        }
    }

    this.removeItem = function(in_key) {
        var tmp_value;
        if (typeof (this.items[in_key]) != 'undefined') {
            this.length--;
            tmp_value = this.items[in_key];
            delete this.items[in_key];
        }

        return tmp_value;
    }

    this.getItem = function(in_key) {
        return this.items[in_key];
    }

    this.setItem = function(in_key, in_value) {
        if (typeof (in_value) != 'undefined') {
            if (typeof (this.items[in_key]) == 'undefined') {
                this.length++;
            }

            this.items[in_key] = in_value;
        }

        return in_value;
    }

    this.hasItem = function(in_key) {
        return typeof (this.items[in_key]) != 'undefined';
    }
}
//var myHash = new Hash('one', 1, 'two', 2, 'three', 3);

function ColorControlClass() {
    this.items = $(".Color1, .Color2, .Color3, .Color4, .Color5, .Color6");
    this.controlValues = [1, 2, 3, 4, 5, 6];
    this.selectedValue = this.items.find(":checked").val();
    this.ColorProduct = new Array(this.controlValues.length);
    this.changeValue = function(value) {
        $(document.getElementById(".Color" + value.toString())).find(":radio").attr("checked", "checked");
    }
    this.validcolors = new Array();
    this.Update = function() {
    if (this.items.length < 1) this.items = $(".Color1, .Color2, .Color3, .Color4, .Color5, .Color6");
        this.selectedValue = this.items.find(":checked").val();
        this.validcolors = currentProduct.GetValidColors();
        this.items.hide();
        for (idx in this.validcolors) {
            $(".Color" + this.validcolors[idx].Color.toString()).show();
            this.ColorProduct[idx] = Products.getItem(currentProduct.style.toString() +
                                                    currentProduct.width.toString() +
                                                    currentProduct.length.toString() +
                                                    currentProduct.height.toString() +
                                                    currentProduct.fabric.toString() +
                                                    this.controlValues[idx].toString());
                                                    
            var el = $(".Color" + this.controlValues[idx].toString()).get();
            if (this.controlValues[idx] == this.selectedValue) {
                $(el).find(".ColorPrice").text("[Included In Price]");
            }
            else {
                $(el).find(".ColorPrice").text("[" + PriceDiffText(this.ColorProduct[idx]) + "]");
            }                                        
        }

    };
}

function FabricControlClass() {
    this.items = $(".FabricControl");

    this.itemArray = this.items.get();
    this.controlValues = [1, 2, 3];
    this.selectedValue = this.items.find(":checked").val();
    this.FabricProduct = new Array(this.controlValues.length);
    this.changeValue = function(value) {
    $(document.getElementById("Fabric" + value.toString())).find(":radio").attr("checked", "checked");
    }

    this.Update = function() {
        if (this.items.length < 1) this.items = $(".FabricControl");
        this.selectedValue = this.items.find(":checked").val();
        for (idx in this.controlValues) {
            this.FabricProduct[idx] = Products.getItem(currentProduct.style.toString() +
                                                    currentProduct.width.toString() +
                                                    currentProduct.length.toString() +
                                                    currentProduct.height.toString() +
                                                    this.controlValues[idx].toString() +
                                                    ProductConfig.GetValidColors(this.controlValues[idx])[0].Color.toString());

            var el = document.getElementById("Fabric" + this.controlValues[idx].toString());
            if (this.controlValues[idx] == this.selectedValue) {
                $(el).find(".FabricPrice").text("[Included In Price]");
            }
            else {
                $(el).find(".FabricPrice").text("[" + PriceDiffText(this.FabricProduct[idx]) + "]");
            }
        }
        //this.Update();
    };
}
function HeightControlClass() {
    this.items = $(".HeightControl");
    this.itemArray = this.items.get();
    this.controlValues = [8, 10, 12];
    this.HeightProduct = new Array(this.controlValues.length);
    this.selectedValue = this.items.find(":checked").val();
    this.changeValue = function(value) {
        $(document.getElementById("Height" + value.toString())).find(":radio").attr("checked","checked");
    }
    this.Update = function() {
        if (this.items.length < 1) this.items = $(".HeightControl");
        this.selectedValue = this.items.find(":checked").val();
        this.items.hide();
        for (idx in this.controlValues) {
            this.HeightProduct[idx] = Products.getItem(currentProduct.style.toString() +
                                                    currentProduct.width.toString() +
                                                    currentProduct.length.toString() +
                                                    this.controlValues[idx].toString() +
                                                    currentProduct.fabric.toString() +
                                                    currentProduct.color.toString());

            //this.HeightProduct[idx] = prod;
            var el = document.getElementById("Height" + this.controlValues[idx].toString());
            if (this.controlValues[idx] == this.selectedValue) {
                //if ()
                $(el).find(".HeightPrice").text("[Included In Price]");
                $(el).show('normal');
            }
            else {
                if (typeof this.HeightProduct[idx] != "undefined") {
                    $(el).find(".HeightPrice").text("[" + PriceDiffText(this.HeightProduct[idx]) + "]");
                    $(el).show('normal');
                }
            }
        }

    };
}
function LengthControlClass() {
    this.items = $(".LengthControl");
    this.itemArray = this.items.get();
    this.controlValues = [8, 12, 16, 20, 24, 28, 32, 36];
    this.LengthProduct = new Array(this.controlValues.length);
    this.selectedValue = this.items.find(":checked").val();
    this.changeValue = function(value) {
        $(document.getElementById("Length" + value.toString())).find(":radio").attr("checked", "checked");        
    }
    this.Update = function() {
        if (this.items.length < 1) this.items = $(".LengthControl");
        this.selectedValue = this.items.find(":checked").val();
        this.items.hide();
        for (idx in this.controlValues) {
            var prod = Products.getItem(currentProduct.style.toString() +
                                                    currentProduct.width.toString() +
                                                    this.controlValues[idx].toString() +
                                                    currentProduct.height.toString() +
                                                    currentProduct.fabric.toString() +
                                                    currentProduct.color.toString());
            if (typeof prod == "undefined") {
                var ValidCube = ProductConfig.GetValidHeights(currentProduct.width, this.controlValues[idx]);
                if (ValidCube.length < 1)
                    prod = null
                else
                    prod = Products.getItem(currentProduct.style.toString() +
                                                    currentProduct.width.toString() +
                                                    this.controlValues[idx].toString() +
                                                    ValidCube[0].Height.toString() +
                                                    currentProduct.fabric.toString() +
                                                    currentProduct.color.toString());
            }
            this.LengthProduct[idx] = prod;
            if (prod != null) {
                var el = document.getElementById("Length" + this.controlValues[idx].toString());
                if (this.controlValues[idx] == this.selectedValue) {
                    $(el).find(".LengthPrice").text("[Included In Price]");
                    $(el).show('normal');
                }
                else {
                    if (typeof this.LengthProduct[idx] != "undefined") {
                        $(el).find(".LengthPrice").text("[" + PriceDiffText(this.LengthProduct[idx]) + "]");
                        $(el).show('normal');
                    }
                }
            }
        }

    };
}

function WidthControlClass() {
    this.items = $(".WidthControl");
    this.itemArray = this.items.get();
    this.controlValues = [8, 10, 12, 14];
    this.selectedValue = this.items.find(":checked").val();
    this.WidthProduct = new Array(this.controlValues.length);
    this.changeValue = function(value) {
        $(document.getElementById("Width" + value.toString())).find(":radio").attr("checked", "checked");
    }
    this.Update = function() {
        if (this.items.length < 1) this.items = $(".WidthControl");
        this.selectedValue = this.items.find(":checked").val();
        for (idx in this.controlValues) {
            var prod = Products.getItem(currentProduct.style.toString() +
                                                    this.controlValues[idx].toString() +
                                                    currentProduct.length.toString() +
                                                    currentProduct.height.toString() +
                                                    currentProduct.fabric.toString() +
                                                    currentProduct.color.toString());
            if (typeof prod == "undefined") {
                var ValidCube = ProductConfig.GetValidLengths(this.controlValues[idx]);
                prod = Products.getItem(currentProduct.style.toString() +
                                                    this.controlValues[idx].toString() +
                                                    ValidCube[0].Length.toString() +
                                                    ValidCube[0].Height.toString() +
                                                    currentProduct.fabric.toString() +
                                                    currentProduct.color.toString());
            }
            this.WidthProduct[idx] = prod;
            var el = document.getElementById("Width" + this.controlValues[idx].toString());
            if (this.controlValues[idx] == this.selectedValue) {
                $(el).find(".WidthPrice").text("[Included In Price]");
            }
            else {
                $(el).find(".WidthPrice").text("[" + PriceDiffText(this.WidthProduct[idx]) + "]");
            }
        }
    };
}


function buildSide() {
    $("#side_dim").text("Dimesions W" + currentProduct.width.toString() + "' X H" + currentProduct.height.toString() + "' X L" + currentProduct.length.toString() + "'");
    $("#side_fabric").text($("#Fabric" + currentProduct.fabric.toString()).text().replace("[Included In Price]", ""))
    $("#side_color").text($(".Color" + currentProduct.color.toString()).text().replace("[Included In Price]", ""))

}

function updateLinks() {
    var baseurl = "http://www.shelterlogic.com/custom_solutions/individual/";
    var link = baseurl + currentProduct.width.toString() + "Wide" + styleText(currentProduct.style) + ".asp";
    $(".more_info_details").attr("href", link);
}

function styleText(style) {
switch(style) {
    case 1:
        return "Round";
        break;
    case 2:
        return "Peak";
        break;
    case 3:
        return "Barn";
        break;
    case 4:
        return "TubePort";
        break;
    case 5:
        return "TubeGarage";
        break;
}
}