/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
function ioShow(id, pos){
	$(id).style.left = pos[0]+48+"px";
	$(id).style.top = pos[1]+48+"px";
	$(id).style.position = "absolute";
	$(id).style.display = "block";

	
}
if(typeof Product=='undefined') {
    var Product = {};
}
/**************************** BUNDLE PRODUCT **************************/
Product.Bundle = Class.create();
Product.Bundle.prototype = {
    initialize: function(config){
        this.config = config;
        this.body  = '02';
        this.kugel = '02';
        this.seat  = '002';
        this.reloadPrice();
    },

       
    //
    //  toogle hover and large images
    //
    ioToogleImg: function(id, showLarge, optid){
    	var pos = $(id).positionedOffset();
    	if (!$(id).hasClassName('ioSelectedOpt')){
	    	var img = $(id).src;
	    	$(id).src = $(id+'H').src;
	    	$(id+'H').src = img;
    	}
    	var idL = 'L'+optid;
	    if (showLarge){
	       	$(idL).src = $(id+'L').src;
	    	$(idL).style.height = '300px'
	    	$(idL).style.width = '300px'
	    	$(idL).style.display = "none"; 
	    	this.timer = window.setTimeout(function(){ioShow(idL,pos);}, 1500);
	    } else {
	    	$(idL).src = "";
	    	$(idL).style.height = '1px'
		    	$(idL).style.width = '1px'
	    	$(idL).style.display = "none"; 
	    	window.clearTimeout(this.timer);
	    }
    	
    }, 
    
     
    changeSelection: function(selection){
    	
        var parts = selection.id.split('-');
          
        
        if (selection.className == 'ioSelImage'){
        	//alert('selection: '+parts[3]);
        	//alert (selection.className);
        	//alert (selection.name);
        	
        	if (parts[3] != '') {
                this.config.selected[parts[2]] = new Array(parts[3]);
            } else {
                this.config.selected[parts[2]] = new Array();
            }
            this.populateQty(parts[2], parts[3]);
            
            //alert ('.ioOption-'+parts[2]);
            // search all activated elements
            $$('.ioOption-'+parts[2]+' .options-list div').each ( function(el){
            	el.childElements().each (function (e){
            		if (e.hasClassName('ioSelectedOpt')){
            			e.removeClassName ('ioSelectedOpt');
            			// toogle image back to unselected
            			var img = e.src;
            			e.src = $(e.id+'H').src;
            			$(e.id+'H').src = img;
            			
            		}
            		
            	});
            	
            });
            // set active indicator img toogle is already done due to mouseOver
            $(selection.id).addClassName('ioSelectedOpt');
            var ioSkuParts = $(selection.id).parentNode.id.split('-');
            // SkuParts[0] = type (Seat,Body,Kugel) SkuParts[1] = artId part
            // alert(ioSkuParts[0] + '/'+ ioSkuParts[1] + '/'+ parts[3]);
            switch (ioSkuParts[0]){
            
            case 'B':
            	this.body = ioSkuParts[1];
            	break;
            case 'K':
            	this.kugel = ioSkuParts[1];
            	break;
            case 'S':
            	this.seat = ioSkuParts[1];
            	break;
            }
            
            $('ioSeat').src = (ioImages[this.seat]);
            $('ioBody').src = (ioImages[this.body+'-'+this.kugel]);
            
            // save selection in hidden input field
            //alert(parts[2]);
            //$('bundle_option[' + parts[2]+']').value = parts[3];
            $('bundle_option-' + parts[2]).value = parts[3];
            //alert (this.body + ' ' + this.kugel + ' ' + this.seat);
            //alert ($('bundle_option[' + parts[2]+']').value);
            //alert('parts[1] '+ parts[1]+'\n'+
            //		'parts[2] '+ parts[2]+'\n'+
            //		'parts[3] '+ parts[3]);        
          
            //fs change no price reload neccessary    caused error    zeile 153 by 2tem klick
            this.reloadPrice();
        } 
        
        

    },

    reloadPrice: function() {
    	// alert('reloadPrice');
        var calculatedPrice = 0;
        var dispositionPrice = 0;
        for (var option in this.config.selected) {
            if (this.config.options[option]) {
                for (var i=0; i < this.config.selected[option].length; i++) {
                    var prices = this.selectionPrice(option, this.config.selected[option][i]);
                    calculatedPrice += Number(prices[0]);
                    dispositionPrice += Number(prices[1]);
                }
            }
        }

        if (this.config.specialPrice) {
            var newPrice = (calculatedPrice*this.config.specialPrice)/100;
            calculatedPrice = Math.min(newPrice, calculatedPrice);
        }

        optionsPrice.changePrice('bundle', calculatedPrice);
        optionsPrice.changePrice('nontaxable', dispositionPrice);
        optionsPrice.reload();

        return calculatedPrice;
    },

    selectionPrice: function(optionId, selectionId) {
        if (selectionId == '' || selectionId == 'none') {
            return 0;
        }
        // alert(optionId+' '+selectionId);
        if (this.config.options[optionId].selections[selectionId].customQty == 1 && !this.config['options'][optionId].isMulti) {
            if ($('bundle-option-' + optionId + '-qty-input')) {
                qty = $('bundle-option-' + optionId + '-qty-input').value;
            } else {
                qty = 1;
            }
        } else {
            qty = this.config.options[optionId].selections[selectionId].qty;
        }

        if (this.config.priceType == '0') {
            price = this.config.options[optionId].selections[selectionId].price;
            tierPrice = this.config.options[optionId].selections[selectionId].tierPrice;

            for (var i=0; i < tierPrice.length; i++) {
                if (Number(tierPrice[i].price_qty) <= qty && Number(tierPrice[i].price) <= price) {
                    price = tierPrice[i].price;
                }
            }
        } else {
            selection = this.config.options[optionId].selections[selectionId];
            if (selection.priceType == '0') {
                price = selection.priceValue;
            } else {
                price = (this.config.basePrice*selection.priceValue)/100;
            }
        }
        //price += this.config.options[optionId].selections[selectionId].plusDisposition;
        //price -= this.config.options[optionId].selections[selectionId].minusDisposition;
        //return price*qty;
        var disposition = this.config.options[optionId].selections[selectionId].plusDisposition +
            this.config.options[optionId].selections[selectionId].minusDisposition;

        var result = new Array(price*qty, disposition*qty);
        return result;
    },

    populateQty: function(optionId, selectionId){
        if (selectionId == '' || selectionId == 'none') {
            this.showQtyInput(optionId, '0', false);
            return;
        }
        if (this.config.options[optionId].selections[selectionId].customQty == 1) {
            this.showQtyInput(optionId, this.config.options[optionId].selections[selectionId].qty, true);
        } else {
            this.showQtyInput(optionId, this.config.options[optionId].selections[selectionId].qty, false);
        }
    },

    showQtyInput: function(optionId, value, canEdit) {
        elem = $('bundle-option-' + optionId + '-qty-input');
        elem.value = value;
        elem.disabled = !canEdit;
        if (canEdit) {
            elem.removeClassName('qty-disabled');
        } else {
            elem.addClassName('qty-disabled');
        }
    },

    changeOptionQty: function (element, event) {
        var checkQty = true;
        if (typeof(event) != 'undefined') {
            if (event.keyCode == 8 || event.keyCode == 46) {
                checkQty = false;
            }
        }
        if (checkQty && (Number(element.value) == 0 || isNaN(Number(element.value)))) {
            element.value = 1;
        }
        parts = element.id.split('-');
        optionId = parts[2];
        if (!this.config['options'][optionId].isMulti) {
            selectionId = this.config.selected[optionId][0];
            this.config.options[optionId].selections[selectionId].qty = element.value*1;
            this.reloadPrice();
        }
    },

    validationCallback: function (elmId, result){
        if (typeof elmId == 'undefined') {
            return;
        }
        var container = $(elmId).up('ul.options-list');
        if (typeof container != 'undefined') {
            if (result == 'failed') {
                container.removeClassName('validation-passed');
                container.addClassName('validation-failed');
            } else {
                container.removeClassName('validation-failed');
                container.addClassName('validation-passed');
            }
        }
    }
}

