function ResizeImage(img, m_width, m_height)
{
    if (img.width < img.height) {
        width = m_width * (img.width / img.height);
        height = m_height;
    } else {
        width = m_width;
        height = m_height * (img.height / img.width);
    }
    
    if (!width || !height) {
        return;
    }
    img.width = width;
    img.height = height;
}

var RelatedProducts = Class.create();

RelatedProducts.prototype = {
    product: null,
    relatedproducts: null,
    total: 0,

    updateTotal: function () {
        this.total = 0;

        for (id in this.relatedproducts) {
            var rp = this.relatedproducts[id];
            
            this.total += rp.price * $('relatedproducts['+id+']').value;
        }
        
        this.product.updateTotal();
    },

    increaseProductQuantity: function (id) {
        $('relatedproducts['+id+']').value++;
        $('relatedproducts['+id+']_indicator').innerHTML = $('relatedproducts['+id+']').value;
           
        this.updateTotal();
    },

    decreaseProductQuantity: function (id) {
        if ($('relatedproducts['+id+']').value < 1) {
            return;
        }
        
        $('relatedproducts['+id+']').value--;
        $('relatedproducts['+id+']_indicator').innerHTML = $('relatedproducts['+id+']').value;
        
        this.updateTotal();
    },

    imageOnLoader: function() {
        ResizeImage(this, 50, 50);
        $('relatedproduct['+this.sysid+']_imgtd').appendChild(this);
    },

    initialize: function(product, relatedproducts) {
        this.product = product;
        this.relatedproducts = relatedproducts;

        for (id in this.relatedproducts) {
            var p = this.relatedproducts[id];

            img = p.imgobj = new Image();

            img.sysid = id;
            img.onload = this.imageOnLoader;
            img.src = p.smallimage_url;
        }
    }
}

var ProductFeatures = Class.create();

ProductFeatures.prototype = {
    product: null,
    features: null,
    tbody: null,
    total: 0,

    initialize: function (product, features, tbody) {
        this.product = product;
        this.features = features; 
        this.tbody = tbody;

        for (featureId in this.features) {
            var feature = this.features[featureId];
        
            feature.selected = 0;
        
            // ignore feature w/o items
            if (feature.items == '') {
                continue;
            }
        
            var f_tr = this.tbody.insertRow(this.tbody.rows.length);
        
            // label
            var f_name_th = document.createElement('th');
            f_name_th.appendChild(document.createTextNode(feature.name));
            f_tr.appendChild(f_name_th);
        
            // select
            f_select = document.createElement('select');
            f_select.name = 'features['+feature.id+']';
        
            var i = 0;
        
            for (itemId in feature.items) {
                var item = feature.items[itemId];
        
                if (i == 0 && item.price) {
                    f_select.options[0] = new Option('-', 0);
                    i++;
                }
                
                var str = item.name;
        
                if (item.price) {
                    str += ' ('+MoneyFormat(item.price)+')';
                }
        
                f_select.options[i] = new Option(str, itemId);
        
                if (i == 0) {
                    feature.selected = item.id;
                }
        
                i++;
            }
            
            // save numeric id so we can access it from the event handler
            f_select.sysid = feature.id;

            // save reference to feature manager
            f_select.manager = this;
        
            f_select.onchange = this.featureChanged;
        
            // append select to row
            var f_select_td = document.createElement('td');
            f_select_td.appendChild(f_select);
            f_tr.appendChild(f_select_td);
        }

        this.updateTotal();
    },

    featureChanged: function () {
        this.manager.features[this.sysid].selected = this.value;
        this.manager.updateTotal();
    },

    updateTotal: function () {
        this.total = 0;
        
        for (featureId in this.features) {
            var feature = this.features[featureId];
                
            if (feature.selected && feature.selected != 0) {
                this.total += feature.items[feature.selected].price;
            }
        }
    
        $('features_total').innerHTML = MoneyFormat(this.total);
    
        this.product.updateTotal();
    }
}

var Groupsale = Class.create();

Groupsale.prototype = {
    info: null,
    features: null,
    relatedproducts: null,

    itemPrice: null,
    subtotal: null,
    total: null,

    initialize: function (info) {
        this.info = info;
    },

    updateTotal: function () {
        this.itemPrice = this.info.price;

        if (this.features) {
            this.itemPrice += this.features.total;
        }
        
        this.subtotal = this.itemPrice * $('quantity').value;
        
        if (this.relatedproducts) {
             this.subtotal += this.relatedproducts.total;
        }
        
        $('item_price').innerHTML = MoneyFormat(this.itemPrice);
        $('subtotal').innerHTML = MoneyFormat(this.subtotal);
    },

    increaseQuantity: function () {
        if (parseInt($('quantity').value)+1 > this.info.quantity_left) {
            alert('ניתן להזמין '+this.info.quantity_left+' פריטים לכל היותר');
            return;
        }
        
        $('quantity').value++;
        $('quantity_indicator').innerHTML = $('quantity').value;
        
        this.updateTotal();
    },

    decreaseQuantity: function () {
        if ($('quantity').value <= 1) {
            return;
        }
        
        $('quantity').value--;
        $('quantity_indicator').innerHTML = $('quantity').value;
        
        this.updateTotal();
    }
}
