
var Recipe = {
	Version: '1.0.0', 
	REQUIRED_PROTOTYPE: '1.6.0.3', 
	PageElemID: "WholePage", 
	RecipeIFrameElemID: "RecipeIFrame", 
	RecipeBGElemID: "RecipeBG", 
	RecipeCloseElemID: "RecipeClose", 
	CloseButtonPosition: 3, // 1=topleft, 2=topcenter, 3=topright
	load: function(){
		// verify the prototype is loaded and right version
		function convertVersionString(versionString) {
			var v = versionString.replace(/_.*|\./g, '');
			v = parseInt(v + '0'.times(4-v.length));
			return versionString.indexOf('_') > -1 ? v-1 : v;
		}
		if((typeof Prototype == 'undefined') || (typeof Element == 'undefined') || (typeof Element.Methods == 'undefined') || (convertVersionString(Prototype.Version) < convertVersionString(Recipe.REQUIRED_PROTOTYPE))){
			throw("CommerceCM requires the Prototype JavaScript framework >= " + Recipe.REQUIRED_PROTOTYPE);
		}
	}, 
	show: function(id){
		// grayout background
		$(this.RecipeBGElemID).setStyle({top: 0 + "px", left: 0 + "px"});
		// calculate where center is
		var popupCenterY = Math.round(this.getScrollAmount() + (this.getWindowHeight()/2) - (this.getAddedPopupHeight(id)/2));
		var popupCenterX = Math.round((this.getWindowWidth()/2) - (this.getAddedPopupWidth(id)/2));
		// position the added to cart message
		if(popupCenterY < 20){popupCenterY = 20;}
		if(popupCenterX < 20){popupCenterX = 20;}
		$(this.RecipeIFrameElemID).setStyle({top: popupCenterY + "px", left: popupCenterX + "px", height: this.getAddedPopupHeight(id) + "px"});
		$(id).setStyle({top: popupCenterY + "px", left: popupCenterX + "px"});
		if(this.CloseButtonPosition == 3){
			var closeX = popupCenterX + this.getAddedPopupWidth(id) - $(this.RecipeCloseElemID).getWidth() - 20;
			$(this.RecipeCloseElemID).setStyle({top: (popupCenterY + $(this.RecipeCloseElemID).getHeight()/4) + "px", left: closeX + "px"});
		}
		if($(this.PageElemID)){
			var pageHeight = $(this.PageElemID).getHeight();
			if(pageHeight > this.getWindowHeight()){
				$(this.RecipeBGElemID).setStyle({width: this.getWindowWidth() + "px", height: pageHeight + "px"});
			}
			$(this.PageElemID).addClassName("PrintRecipe");
		}
		return 0;
	}, 
	hide: function(id){
		$(id).setStyle({top: -2000 + "px", left: -2000 + "px"});
		$(this.RecipeCloseElemID).setStyle({top: -2000 + "px", left: -2000 + "px"});
		$(this.RecipeIFrameElemID).setStyle({top: -2000 + "px", left: -2000 + "px"});
		$(this.RecipeBGElemID).setStyle({top: -3000 + "px", left: -3000 + "px", height: 100 + "%"});
		if($(this.PageElemID)){
			$(this.PageElemID).removeClassName("PrintRecipe");
		}
		return 0;
	}, 
	getWindowWidth: function(win){
		var width;
		win = win ? win : window;
		width = win.innerWidth || (win.document.documentElement.clientWidth || win.document.body.clientWidth);
		return width; 
	}, 
	getWindowHeight: function(win){
		var height;
		win = win ? win : window;
		height = win.innerHeight || (win.document.documentElement.clientHeight || win.document.body.clientHeight);
		return height; 
	}, 
	getAddedPopupWidth: function(id){
		return $(id).getWidth();
	}, 
	getAddedPopupHeight: function(id){
		return $(id).getHeight();
	}, 
	getScrollAmount: function(){
		if(typeof window.pageYOffset != "undefined"){
			var scroll = window.pageYOffset;
		}else if(typeof document.documentElement.scrollTop != "undefined"){
			var scroll = document.documentElement.scrollTop;
		}else{
			var scroll = document.body.scrollTop;
		}
		return scroll;
	}
};

function loadRecipe(){
	Recipe.load();
}

addPageLoad_Handler(loadRecipe);

