/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-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.
 *
 * @category  Symmetrics
 * @package   Symmetrics_CalculateSquarePrice
 * @author    symmetrics gmbh <info@symmetrics.de>
 * @author    Siegfried Schmitz <ss@symmetrics.de>
 * @copyright 2009 Symmetrics Gmbh
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @link      http://www.symmetrics.de/
 */

function calculateSquarePriceFields() {
    if ($$('div.option-block-breitexhoehe input')[0]) {
        var hiddenFields = $$('div.option-block-breitexhoehe input');
    } else {
        var hiddenFields = $$('div.option-block-widthxheight input');
    }
    
    width = $('calcWidth').getValue();
    height = $('calcHeight').getValue();
    width = width.split(',').join('.');
    height = height.split(',').join('.');
    
    if (width == 0) {
        $('calcWidth').value = 100;
        width = 100;
    }
    if (height == 0) {
        $('calcHeight').value = 100;
        height = 100;
    }
    if (typeof tierPrice == "undefined" || !(tierPrice instanceof Array)) {
        sum = width * height;
        var myQty = 1;
    } else {
        sum = (width / 100) * (height/ 100);
        var myQty = $('qty').getValue();
    }

    if (sum > 0) {
        if (hiddenFields[0]) {
            $(hiddenFields[0].id).value = width + 'x' + height;
        }
        var upPrice = getUpPrice();
        $('width_height').value = sum * myQty;
        
        opConfig.reloadPrice(sum);
        
        var outputPrice = $$('.price-box .price')[1].innerHTML;
        var outNumber = outputPrice.split('&nbsp;')[0];
        upPrice = parseFloat(upPrice * (sum * myQty));
        outNumber = outNumber.split('.').join('');
        outNumber = parseFloat(outNumber.split(',').join('.'));
        var newPrice = parseFloat(upPrice + outNumber);
        newPrice = newPrice.toFixed(2);

        
        $('symmetrics_calc_price_value').update(newPrice + '&nbsp;€');
    }
}

function getUpPrice() {
    var upPrice = 0;
    var settings = $$('.super-attribute-select');
    
    for(var i=settings.length-1;i>=0;i--){
        var selected = settings[i].options[settings[i].selectedIndex];
        if(selected.config){
            upPrice += parseFloat(selected.config.price);
        }
    }

    if ($('up_price')) {
        $('up_price').value = upPrice;    
    }
    
    return upPrice;
}

document.observe('dom:loaded', function() {
    if ($('calcWidth') && $('calcHeight')) {
        calculateSquarePriceFields();
        Event.observe($('calcWidth'), 'change', calculateSquarePriceFields);
        Event.observe($('calcHeight'), 'change', calculateSquarePriceFields);
        $('symmetrics_calc_price_value').update($$('.price-box .price')[1].innerHTML);
    }
});

