adobe.link("adobe/Modal.js");
adobe.link("adobe/Cookie.js");

/*
	Class: adobe.CartFascade
	$Id$
	
*/

adobe.CartFascade = Class.create({
/*
	@overlayElement
	@distributionMethod
	@categoryPath
	@defaultStore
	@defaultCountry
	@requiredFlashVersion
	@detectedFlashVersion
	@storeRegion
	@productName
	@emailTrackingId
	@storeType
	

	
	itemizeFlashVersion() : Array
	isOverlaySupported() : Boolean
	isFlashVersionSupported() : Boolean
	getStoreRegion() : String
	setDistributionMethod()
	setCategoryPath()
	setDefaultStore()
	setDefaultCountry()
	setRequiredFlashVersion()
	getDetectedFlashVersion()  : String
	setProductName()
	setEmailTrackingId()
	setStoreType()
	
	
	openCartOverlay()
	closeCartOverlay()
*/
	initialize: function() {
		this.overlayElement = null;
		this.distributionMethod = "";
		this.distributionMethodDisplayed = "";
		this.categoryPath = "";
		this.defaultStore = "";
		this.defaultCountry = "";
		this.requiredFlashVersion = "0.0.0";
		this.detectedFlashVersion = "";
		this.storeRegion = "";
		this.productName = "";
		this.emailTrackingId = "";
		this.storeType = "";
	},
	itemizeFlashVersion: function(version) {
		return String(version).split(".").collect(parseFloat);
	},
	isOverlaySupported: function() {
		return this.isFlashVersionSupported() &&
			!(/Firefox\/2/.test(window.navigator.userAgent));
	},
	isFlashVersionSupported: function() {
		var playerVersion = this.itemizeFlashVersion(this.getDetectedFlashVersion());
		var requiredVersion = this.itemizeFlashVersion(this.requiredFlashVersion);
		
		for(var i=0; i<playerVersion.length; i++) {
			var current = playerVersion[i] || 0, // make undefined zero
			required = requiredVersion[i] || 0; // make undefined zero
			
			if(current > required) {
				break;
			} else if(current == required) {
				continue;
			} else {
				return false;	
			}
		}
		
		return true;
	},
	getStoreRegion: function() {
		return this.storeRegion = this.storeRegion || adobe.Cookie.get( "storeregion" );
	},
	setStoreRegion: function(region) {
		this.storeRegion = String(region);
		adobe.Cookie.set( "storeregion", 
				 this.storeRegion,
				 10*365,
				 '/',
				 '.adobe.com');
	},
	setDistributionMethod: function(distmethod) {
		this.distributionMethod = String(distmethod);
	},
	setCategoryPath: function(catpath) {
		this.categoryPath = String(catpath);
	},
	setProductName: function(productname) {
		this.productName = String(productname);
	},
	setEmailTrackingId: function(prmid) {
		this.emailTrackingId = String(prmid);
	},
	setDefaultStore: function(storeid) {
		this.defaultStore = String(storeid);
	},
	setDefaultCountry: function(countryid) {
		this.defaultCountry = String(countryid);
	},
	setRequiredFlashVersion: function(integer) {
		this.requiredFlashVersion = String(integer);
	},
	setStoreType: function(typestr) {
		this.storeType = String(typestr);
	},
	getDetectedFlashVersion: function() {
		if(!this.detectedFlashVersion) {
			var plVersion = swfobject.getFlashPlayerVersion();
			var usersFPversion = plVersion.major + "." + plVersion.minor + "." + plVersion.release;
			this.detectedFlashVersion = usersFPversion;
		}
		return this.detectedFlashVersion;
	},
	openCartOverlay: function(event) {
		if((this.overlayElement == null) ||
		   (this.distributionMethod != this.distributionMethodDisplayed)) {
			
			var paneChromeHtml = '<div class="chrome-pane-body">hello world<\/div>';
			
			if(Prototype.Browser.Gecko) {
				var chromeHack = '<iframe class="chrome-ff2-frame"><\/iframe>';
				paneChromeHtml = chromeHack + paneChromeHtml;
			}
			
			var pane = new adobe.Pane(null, {
				chrome: new Template(paneChromeHtml),
				width: 850,
				height: 535,
				duration: 600,
				frameborder: 0,
				z: 1001
			});
			var shade = new adobe.Shade(document.getElementsByTagName("BODY")[0], {
				duration: 400,
				label: "",
				scrolling: true,
				z: 1000
			});
			
			this.overlayElement = new adobe.Modal(pane, shade, {
				observeShade: false
			});
			
			var vars = {
				categorypath: this.categoryPath,
				defaultstore: this.defaultStore,
				defaultcountry: this.defaultCountry,
				distmethod: this.distributionMethod,
				storeregion: this.getStoreRegion(),
				omnitureproductname: this.productName,
				flashversion: this.getDetectedFlashVersion(),
				promoid: this.emailTrackingId,
				storetype: this.storeType
				
			},
			params = {
				allowScriptAccess: "sameDomain",
				id: "ubiCartMovie"
			},
			attrs = {
				allowScriptAccess: "sameDomain",
				id: "ubiCartMovie"	
			};
			
			pane.observe("pane:startopen", (function() {
				var id = pane.content.identify();
				swfobject.embedSWF("/swf/store/flex/StatelessStore.swf", 
					   id, 
					   "850", 
					   "535", 
					   this.requiredFlashVersion,
					   "",
					   vars,
					   params,
					   attrs);
			}).bind(this));	
		}
		
		this.overlayElement.enable(event);
	},
	closeCartOverlay: function() {
		if(!this.overlayElement) { return; }
		this.overlayElement.disable();
	},
	resizeCartOverlay: function(w, h, state) {
		if(!this.overlayElement) { return; }
		if(window.opera) {
			if(window.innerHeight < h) {
				window.innerHeight = h;
			}
		}
		this.overlayElement.pane.resizeTo(w, h);
		var ubiCartSWF = $("ubiCartMovie");
		ubiCartSWF.height = h;
		ubiCartSWF.width = w;
		var y_position = (h-470) / 2;
		var x_position = (w-850) / 2;
		this.overlayElement.pane.top - y_position;
		this.overlayElement.pane.left - x_position;
		
		y_position = (y_position > 0) ? true : false;
		x_position = (x_position > 0) ? true : false;
		
		this.overlayElement.pane.center(y_position,x_position);
		ubiCartSWF.sizeChanged(1);
	}
});
