// setup the namespace
if (typeof onepica == "undefined" || !onepica) {
    var onepica = {};
}
if (typeof onepica.flash == "undefined" || !onepica.flash) {
    onepica.flash = {};
}

onepica.CONTAINER_SLIDE_OUT_DURATION = 0.5;
onepica.CONTAINER_SLIDE_IN_DURATION = 0.5;
onepica.flash.MINIMUM_VERSION = "9.0";
onepica.flash.URL_KEY = '_';
onepica.flash.DEFAULT_PAGE = "0";
onepica.flash.CUR_POPUP = 1;
onepica.flash.popup_array = new Array();

onepica.FlashManager = Class.create({
	initialize: function() {
		if(!swfobject.hasFlashPlayerVersion(onepica.flash.MINIMUM_VERSION)) return false;
		this.mainContainer = $$('#container .page');
		this.mainContainer = $(this.mainContainer[0]);
		Element.insert(document.body, "<div id='flashPopup_" + onepica.flash.CUR_POPUP +"'><div id='flashInner_" + onepica.flash.CUR_POPUP + "'></div></div>");
		this.flashContainer = $('flashPopup_' + onepica.flash.CUR_POPUP);
		
		flashStyles = {
			"padding": 0,
			"margin": 0,
			"border": 0,
			"display": "none",
			"position": "fixed",
			"top": 0,
			"left": 0,
			"zIndex": "1000",
			"height": "100%",
			"width": "100%"
		}
		this.flashContainer.setStyle(flashStyles);
		this.showingFlashPopup = 0;
		this.flashContainerContents = this.flashContainer.innerHTML;
		
		
		this.initializeCatalog();
		//onepica.flash.CUR_POPUP++;
	},
	
	increment_popup: function (){
		Element.insert(document.body, "<div id='flashPopup_" + onepica.flash.CUR_POPUP +"'><div id='flashInner_" + onepica.flash.CUR_POPUP + "'></div></div>");
		this.flashContainer = $('flashPopup_' + onepica.flash.CUR_POPUP);
		//this.flashContainer = $('flashPopup_' + onepica.flash.CUR_POPUP);
		
		var zindex = 1000; // + onepica.flash.CUR_POPUP;
		flashStyles = {
			"padding": 0,
			"margin": 0,
			"border": 0,
			"display": "none",
			"position": "fixed",
			"top": 0,
			"left": 0,
			"zIndex": zindex,
			"height": "100%",
			"width": "100%"
		}
		this.flashContainer.setStyle(flashStyles);
	},
	
	replace: function() {
		if(!swfobject.hasFlashPlayerVersion(onepica.flash.MINIMUM_VERSION)) return false;
		var links = $$('.flashreplace');
		this.replaceLinks(links);
	},
	
	replaceLinks: function(links) {
		links.each(function(link, index) {
			link.setAttribute('orighref', link.getAttribute('href'));
			link.setAttribute('href', 'javascript:;');
			link.observe('click', this.footerOpenPopup.bindAsEventListener(this));
		}, this);
	},
	
	footerOpenPopup: function(e) {
		var link = e.findElement('a');  // in case a span within the a tag is clicked
		var swfpath = link.getAttribute('swfpath');
		var loader = "popup_loader.swf";
		var flashVars = link.getAttribute('flashvars');
		this.createPopup(loader, swfpath, flashVars);
	},
	
	createPopup: function(loader, swfpath, addvars) {
		var flashManager = screenManager.getFlashManager();
		
		
		if(onepica.flash.CUR_POPUP > 1) {
			flashManager.increment_popup();
			//return;
		}
		onepica.flash.popup_array.push(onepica.flash.CUR_POPUP);
		//onepica.flash.CUR_POPUP++;
		
		
		var mainContainer = flashManager.mainContainer;
		var flashContainer = flashManager.flashContainer;
		if(flashContainer == undefined) return false;
		
		/**
		 * Bring on the flash
		 */
		flashContainer.setStyle({display: 'block'});
		flashContents = flashContainer.innerHTML;

		var flashvars = "template_path="+TEMPLATE_PATH+"&flash_path=flash/"+swfpath;
		if(addvars) {
			flashvars += "&"+addvars;
		}
		
		var params = {
			scale: "noscale",
			allowScriptAccess: "always",
			menu: "false",
			wmode: "transparent",
			flashvars: flashvars
		};

		var atts = { data:TEMPLATE_PATH+"flash/"+loader, width:"100%", height:"100%" };
		flashManager.flashObject = swfobject.createSWF(atts, params, "flashInner_" + onepica.flash.CUR_POPUP);
		onepica.flash.CUR_POPUP++;
		
	},
	
	closeNextPopup: function() {
		this.closePopup();
		onepica.flash.closeAllPopups();
	},
	
	closePopup: function() {
		/**
		 * remove the flash div
		 * Safari crashes if trying to destroy the Flash object before returning (see ticket 362)
		 */
		var i = onepica.flash.popup_array.pop();
		setTimeout("swfobject.removeSWF('flashInner_" + i + "');", 500);
		setTimeout("var flashManager = screenManager.getFlashManager(); $('flashPopup_" + i + "').update(flashManager.getOrigFlashContainerContent());", 200);
		$('flashPopup_' + i).hide();
		//document.fire('onepica:closedFlashPopup');
		
	},
	
	getOrigFlashContainerContent: function() {
		return this.flashContainerContents;
	},
	
	//History: http://www.prototypextensions.com/history
	initializeCatalog: function() {
		onepica.flash.initializeMovie();
	}
});

/**
 * The following functions cannot be in a class/instance because the Flash movies 
 * must be able to call them.
 * Namespaced to avoid collision.
 */
onepica.flash.closePopup = function() {
	var flashManager = screenManager.getFlashManager();
	return flashManager.closePopup();
}

onepica.flash.closeNextPopup = function() {
	var flashManager = screenManager.getFlashManager();
	return flashManager.closeNextPopup();
}

onepica.flash.closeAllPopups = function() {
	var flashManager = screenManager.getFlashManager();
	if (onepica.flash.popup_array.length) {
		var flashInner = 'flashInner_' + onepica.flash.popup_array[onepica.flash.popup_array.length - 1];
		$(flashInner).closeMovie();
		//flashManager.closePopup();
	}
}


onepica.flash.createPopup = function(swfpath, flashvars) {
	var flashManager = screenManager.getFlashManager();
	return flashManager.createPopup("popup_loader.swf", swfpath, flashvars);
}

onepica.flash.resizeMovie = function(id, height, animate) {
	if(!animate) {
		$(id).setStyle({'height':height+'px'});
	} else {
		new Effect.Morph(id, {duration:0.4, style:{ "height": height+"px"}});
		
		//Safari 3 does not expand OBJECT tag to 100% after Effect.Morph so we explicitly set it to compensate
		if(Prototype.X.Browser.Safari > 0 && Prototype.X.Browser.Safari < 4) {
			setTimeout("$('" + id + "').firstDescendant().setStyle({'height':'" + height + "px'});", 1000);
		}
	}
}

/**
 * Called by the Flash movie when the user browses somewhere that needs to
 * update the URL. This function will update the URL to what the Flash movie
 * tells it to.
 */
onepica.flash.setWindowLocation = function(url) {
	//console.log('setting window location to '+url);
	History.set(onepica.flash.URL_KEY, url);
	if ((url == onepica.flash.DEFAULT_PAGE) || (url == "home"))
		onepica.flash.hideAddThis();
	else
		onepica.flash.showAddThis();
}

onepica.flash.movieElementId = 'product_list_embed';
onepica.flash.movieId = 'product_list_embed_inner';
onepica.flash.isLoaded = false;

/**
 * Called by the History wrapper when the URL is changed by the user. This
 * function needs to communicate to the flash movie to go to a URL.
 * The Flash movie itself calls this function after it's loaded in order to
 * get the location it should go to initially.
 */
onepica.flash.setMovieLocation = function(url) {
        onepica.flash.isLoaded = true; //in case we need this at some point
        var realUrl = History.get(onepica.flash.URL_KEY);
        if(Object.isUndefined(realUrl)) realUrl = onepica.flash.DEFAULT_PAGE;
	//console.log('setting flash movie to '+realUrl);
        var movieElm;
        if (navigator.appName.indexOf("Microsoft") != -1) {
                movieElm = window[onepica.flash.movieId];
        } else {
                movieElm = document[onepica.flash.movieId];
        }
        if(typeof(movieElm.moveToUrl) == "function") {
                movieElm.moveToUrl(realUrl);
                if ((realUrl == onepica.flash.DEFAULT_PAGE) || (realUrl == "home"))
					onepica.flash.hideAddThis();
				else
					onepica.flash.showAddThis();
        } else {
                //console.log('flash function "moveToUrl" not found.');
        }
}

/**
 * Called by the FlashManager instance above
 * First line exits out of this function if you aren't at a catalog page.
 */
onepica.flash.initializeMovie = function() {
	if(!$(onepica.flash.movieElementId)) return;
	History.Observer.start();
        History.Observer.setRequired(onepica.flash.URL_KEY, onepica.flash.DEFAULT_PAGE);
	History.Registry.set({
		id: onepica.flash.URL_KEY,
		onStateChange: onepica.flash.setMovieLocation,
                defaultValue: onepica.flash.DEFAULT_PAGE
	});
}


onepica.flash.showAddThis = function() {
	$$('.addthis_button').invoke('show');
}
onepica.flash.hideAddThis = function() { 
	$$('.addthis_button').invoke('hide');
}

onepica.flash.redirectHtmlUsersViewingFlashContent =function() {
	if (window.location.hash) {
		if(!swfobject.hasFlashPlayerVersion(onepica.flash.MINIMUM_VERSION)) {
			if (window.location.hash.substring(0,10) == "#_=detail-") {
				window.location = STORE_PATH + "catalog/product/view?id=" + window.location.hash.substring(10);
			}
		}
	}
}

onepica.flash.initCookieJar = function() {
	onepica.cookieJar = new CookieJar({expires: 3600, path: '/'});
}
onepica.flash.checkFlash = function() {
	onepica.flash.redirectHtmlUsersViewingFlashContent();
	onepica.cookieJar = null;
	onepica.flash.initCookieJar();
	/** conditional commented out so to check for flash every page load. **/
	//if(onepica.cookieJar.get('flash_available') == undefined) {
		onepica.cookieJar.put('flash_available', swfobject.hasFlashPlayerVersion(onepica.flash.MINIMUM_VERSION));
		onepica.cookieJar.put('flash_version', swfobject.getFlashPlayerVersion());
	//}
	return;
}
onepica.flash.checkFlash();