/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition License
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magentocommerce.com/license/enterprise-edition
 * 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     enterprise_default
 * @copyright   Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license     http://www.magentocommerce.com/license/enterprise-edition
 */

if (!window.Enterprise) {
    window.Enterprise = {};
}

if (!Enterprise.CatalogEvent) {
    Enterprise.CatalogEvent = {};
}

Enterprise.CatalogEvent.Ticker = Class.create();

Object.extend(Enterprise.CatalogEvent.Ticker.prototype, {
    initialize: function (container, seconds) {
        this.container = $(container);
        this.seconds   = seconds;
        this.start     = new Date();
        this.interval = setInterval(this.applyTimer.bind(this), 1000);
        this.applyTimer();
    },
    getEstimate: function () {
        var now = new Date();

        var result = this.seconds - (now.getTime() - this.start.getTime())/1000;

        if (result < 0) {
            return 0;
        }

        return Math.round(result);
    },
    applyTimer: function () {
        var seconds = this.getEstimate();
        var daySec = Math.floor(seconds / (3600*24)) * (3600*24);
        var hourSec = Math.floor(seconds / 3600) * 3600;
        var minuteSec =  Math.floor(seconds / 60) * 60;
        var secondSec = seconds;
        this.container.down('.days').update(this.formatNumber(Math.floor(daySec/(3600*24))));
        this.container.down('.hour').update(this.formatNumber(Math.floor((hourSec - daySec)/3600)));
        this.container.down('.minute').update(this.formatNumber(Math.floor((minuteSec - hourSec)/60)));
        this.container.down('.second').update(this.formatNumber(seconds - minuteSec));
        if (daySec > 0) {
            this.container.down('.second').previous('.delimiter').hide();
            this.container.down('.second').hide();
            this.container.down('.days').show();
            this.container.down('.days').next('.delimiter').show();
        } else {
            this.container.down('.days').hide();
            this.container.down('.days').next('.delimiter').hide();
            this.container.down('.second').previous('.delimiter').show();
            this.container.down('.second').show();
        }
    },
    formatNumber: function (number) {
        if (number < 10) {
            return '0' + number.toString();
        }

        return number.toString();
    }
});

(function($){
   	$(function() {
       	$('#lb-overlay').css('height', getDocHeight())

		$('.launch-shipsmartPopup').click(function() {
                    /* Start Modification */
                    if(!productAddToCartForm.validator.validate()){
                        //If product includes an options dropdown that must be selected,
                        //return false to prevent popup from being displayed.
                        return false;
                    }
                    /* End Modification */
			$('#lb-overlay').css('display','block');
			if((document.documentElement.scrollTop > -1) && (!$.browser.safari)) {
				$('#lb-shipsmartPopup').css("top", (document.documentElement.scrollTop + 196) + "px");
			} else {
				$('#lb-shipsmartPopup').css("top", (self.pageYOffset + 196) + "px");
			}
			$('#lb-shipsmartPopup').css('left','50%');
		});

		$('.launch-shipsmartInfoPopup').click(function() {
			$('#lb-overlay').css('display','block');
			if((document.documentElement.scrollTop > -1) && (!$.browser.safari)) {
				$('#lb-shipsmartInfoPopup').css("top", (document.documentElement.scrollTop + 100) + "px");
			} else {
				$('#lb-shipsmartInfoPopup').css("top", (self.pageYOffset + 100) + "px");
			}
			$('#lb-shipsmartInfoPopup').css('left','50%');
		});

		$('.close-shipsmartPopup').click(function() {
			$('#lb-overlay').css('display','none');
			$('.shipsmartModalWindow').css("top", "196px");
			$('.shipsmartModalWindow').css('left','-2000px');
			return false;
		});
	})
})(jQuery);

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
};



