/**********************************
 DIALOG BOX
**********************************/
setLocation = function(){
    var vso = document.viewport.getScrollOffsets();
    var top = 0, left = 0, styles, dimensions, windowHeight, newContentHeight;
    styles = {
        position: 'fixed'
    };
    
    if (Prototype.Browser.IE) {
      styles.position = 'absolute';
    }
    
    $('zoom').setStyle(styles);
    dimensions = document.viewport.getDimensions();    
    windowHeight = parseInt($('zoom').getStyle('height'), 10);
                                                          
    if (windowHeight >= dimensions.height) {
      newContentHeight = parseInt(dimensions.height * 0.5, 10);               
      windowHeight = parseInt($('zoom').getStyle('height'), 10);
    }
        
    top = vso.top + 50,
    left = String((document.viewport.getWidth()/2) - ($('zoom').getWidth()) + ($('zoom').getWidth()/2))
    
    if (Prototype.Browser.IE) {
        top += document.viewport.getScrollOffsets().top;
    }
    
    styles.top = top+'px';
    styles.left = left+'px';
    $('zoom').setStyle(styles);
}

showZoom = function(){
    $('zoom').show();
    draggable_handler = $('handle');  
    dragging = new Draggable('zoom', {
        'handle': draggable_handler,
        onEnd: function(draggable, event){
            if (parseInt($('zoom').getStyle('top'), 10) < 0) {
                setLocation();
            }
            if (parseInt($('zoom').getStyle('left'), 10) < -300){
                setLocation();
            }
        }.bind(this)
    });
}

closeZoom = function(){
    $('zoom').hide();
}

Event.observe(window, 'load', function(e){
    $$('.zoom').invoke('observe','click', function(e){ e.stop(); showZoom(); });
    $$('#handle .close').invoke('observe','click', function(e){ e.stop(); closeZoom(); });
    if ($('paymentMethodChkBox')) {
        $('paymentMethodChkBox').checked=false;
    }

    if($('payflowPro')) {
        Event.observe($('payflowPro'), 'click', setPayflowPro);
    }
    if($('payPalCheckout')) {
        Event.observe($('payPalCheckout'), 'click', setPayPalAsPayment);
    }
    if($('googleCheckout')) {
        Event.observe($('googleCheckout'), 'click', setGoolgeAsPayment);
    }
    if ($('updateprofileform')) {
        if ($('shipToCountryGeoId')) {
            checkShipFields();
            Event.observe($('shipToCountryGeoId'), 'change', checkShipFields);
        }
        if ($('billToCountryGeoId')) {
            checkBillFields();
            Event.observe($('billToCountryGeoId'), 'change', checkBillFields);
        }
    }
    if ($('paymentMethodCheckoutBtnDis')) {
        Event.observe($('paymentMethodCheckoutBtnDis'), 'mouseover', function() {
            if ($('paymentMethodChkBox').checked) {
                   new Effect.Fade($('advice-required-policy'), {duration: 0.2});
                   $('paymentMethodCheckoutBtnDis').hide();
                   $('paymentMethodCheckoutBtn').show();
            } else {
                new Effect.Appear($('advice-required-policy'), {duration: 0.2});
                $('paymentMethodCheckoutBtn').hide();
                $('paymentMethodCheckoutBtnDis').show();
            }
        });
    }
    if ($('saveShippingMethod')) {
        Event.observe($('saveShippingMethod'), 'change', function() {
            $('shipMethod').value = $F('saveShippingMethod');
        });
    }
    applyCoupan();
});

function setPayflowPro() {
    new Ajax.Request('setPaymentMethod', {
        asynchronous: false,
        parameters: {paymentMethodType: 'payflowPro'}
     });
}

function setPayPalAsPayment() {
     new Ajax.Request('setPaymentMethod', {
        asynchronous: false, 
        parameters: {paymentMethodType: 'payPal'}
     });
}

function setGoolgeAsPayment() {
    new Ajax.Request('setPaymentMethod', {
        asynchronous: false, 
        parameters: {paymentMethodType: 'googleCheckout'}
     });
}

function checkShipFields() {
    if ($('shipToCountryGeoId').value =="USA") {
        $('asteriskPostalCodeShip').show();
        $('asteriskStateShip').show();
    } else {
        $('asteriskPostalCodeShip').hide();
        $('asteriskStateShip').hide();
    }
}
function checkBillFields() {
    if ($('billToCountryGeoId').value =="USA") {
         $('asteriskPostalCodeBill').show();
         $('asteriskStateBill').show();
    } else {
        $('asteriskPostalCodeBill').hide();
        $('asteriskStateBill').hide();
    }
}

function checkPaymentMethod() {
    if ($('paymentMethodChkBox').checked) {
        new Effect.Fade($('advice-required-policy'), {duration: 0.2});
        $('paymentMethodCheckoutBtnDis').hide();
        $('paymentMethodCheckoutBtn').show();
    } else {
        new Effect.Appear($('advice-required-policy'), {duration: 0.2});
        $('paymentMethodCheckoutBtn').hide();
        $('paymentMethodCheckoutBtnDis').show();
    }
}

function setPayPalCheckout() {
    if ($('paymentMethodChkBox').checked) {
        $('setPayPalCheckout').submit();
    }
}

function changeUpperCase() {
    $('productPromoCodeId').value = $F('productPromoCodeId').toUpperCase();
}
function applyCoupan() {
    if($('shipping_method') && $('cartShippingMethod')) {
        if($('shipping_method').value != "" && $('cartShippingMethod').value ==""){
            $('setPromoShipOptionInCart').submit();
        } else {
            Event.observe($('productPromoCodeSubmit'), 'click', function() {
                $('checkShipMethodReqForPromo').submit();
            });
        }
    }
}

/**********************************
 FLASH
**********************************/
var Swiff = Class.create({
	options: {
		id: null,
		height: '100%',
		width: '100%',
		container: null,
		properties: {},
		params: {
			quality: 'high',
			allowScriptAccess: 'always',
			wMode: 'transparent',
			swLiveConnect: true
		},
		callBacks: {},
		vars: {}
	},
	toElement: function(){
		return this.object;
	},
	initialize: function(path, options){
		this.instance = 'Swiff_' + new Date().getTime();
		this.setOptions(options);
		options = this.options;
		var id = this.id = options.id || this.instance;
		var container = $(options.container);
		Swiff.CallBacks[this.instance] = {};
		var params = options.params, vars = options.vars, callBacks = options.callBacks;
		var properties = Object.extend({height: options.height, width: options.width}, options.properties);
		var self = this;
		for (var callBack in callBacks){
			Swiff.CallBacks[this.instance][callBack] = (function(option){
				return function(){
					return option.apply(self.object, arguments);
				};
			})(callBacks[callBack]);
			vars[callBack] = 'Swiff.CallBacks.' + this.instance + '.' + callBack;
		}
		params.flashVars = Hash.toQueryString(vars);
		if (Prototype.Browser.IE){
			properties.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
			params.movie = path;
		} else {
			properties.type = 'application/x-shockwave-flash';
			properties.data = path;
		}
		var build = '<object id="' + id + '"';
		for (var property in properties) build += ' ' + property + '="' + properties[property] + '"';
		build += '>';
		for (var param in params){
			if (params[param]) build += '<param name="' + param + '" value="' + params[param] + '" />';
		}
		build += '</object>';
		this.object =  ((container) ? container.update() : new Element('div')).update(build).firstChild;
	},
	setOptions: function(options) {
	  this.options = Object.extend(this.options, options);
	},
	replaces: function(element){
		element = $(element, true);
		element.parentNode.replaceChild(this.toElement(), element);
		return this;
	},
	inject: function(element){
		$(element, true).appendChild(this.toElement());
		return this;
	},
	remote: function(){
		return Swiff.remote.apply(Swiff, [this.toElement()].extend(arguments));
	}
});

Swiff.CallBacks = {};
Swiff.remote = function(obj, fn){
	var rs = obj.CallFunction('<invoke name="' + fn + '" returntype="javascript">' + __flash__argumentsToXML(arguments, 2) + '</invoke>');
	return eval(rs);
};
