/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/LICENSE-L.txt
 *
 * @category   AW
 * @package    AW_Blog
 * @copyright  Copyright (c) 2009-2010 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/LICENSE-L.txt
 */

var AwpmPrice = Class.create({

    initialize: function(origPriceBox,pmPriceBox) {
        this.origPriceBox = origPriceBox;
        this.pmPriceBox = pmPriceBox;
    },
    
    reinitPrice: function() {
        
        if(!$$('.' + this.pmPriceBox).length) {
            this.setCookie('awpm_product_'+this.origPriceBox,this.getProductPrice(),'/','','');
            return;
        }
        try {
            this.deleteCookie('awpm_product_'+this.origPriceBox,'/','');
            $$('.' + this.pmPriceBox).invoke('writeAttribute','value',this.getProductPrice());
        } catch(e) { }
    },

    getCookiePrice: function() {
        var cookiePrice = this.getCookie('awpm_product_'+this.origPriceBox);        
        if(cookiePrice) {
            try {
                this.deleteCookie('awpm_product_'+ this.origPriceBox,'/','');
                $$('.' + this.pmPriceBox).invoke('writeAttribute','value',cookiePrice);
            } catch(e) { }
            
        }
    },

    getProductPrice: function() {

        var prices = new Array();

        prices[0] = 'price-including-tax-';
        prices[1] = 'price-excluding-tax-';
        prices[2] = 'product-price-';

        for(i=0;i<prices.length;i++) {
            try {
                var priceBox = $(prices[i]+this.origPriceBox);
                if(priceBox) {
                    i = 0;
                    while(Object.isElement(priceBox.down(i))) {
                        priceBox = priceBox.down(i);
                        i++;
                    }
                    return priceBox.innerHTML.replace(/\D*/,'');
                }
                continue;
            } catch(e) { }            
        }
        return false;
    },

    setCookie: function(name, value, path, domain, secure) {
 
        var cookie_string = name + "=" + escape ( value );
 
        if ( path ) {
            cookie_string += "; path=" + escape ( path );
        }
        if ( domain )
            cookie_string += "; domain=" + escape ( domain );
        if ( secure )
            cookie_string += "; secure";
        document.cookie = cookie_string;
    },

    deleteCookie: function(name,path,domain) {

        try {
            document.cookie = name + "=" +
            ( ( path ) ? ";path=" + path : "") +
            ( ( domain ) ? ";domain=" + domain : "" ) +
            ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
        } catch(e) { }
    },

    getCookie: function(cookie_name) {        
        var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
        if ( results )
            return ( unescape ( results[2] ) );
        else
            return null;
    }
});
;

