﻿		(function(){
			var Event = YAHOO.util.Event;
			var Dom = YAHOO.util.Dom;
			
			YAHOO.namespace('hz');
			/** Module pattern
			* assign the result of the anonymous function to YAHOO.hz.quickcomp
			*/
			YAHOO.hz.quickcomp = function(){
				//Private variables and functions here
				var urlBase = ""; //Base url for php scripts
				var callback = {
					success:function(o){
						//First, hide the loading panel, then set the src property of the image
						var elemLoading = Dom.get('loadingpanel');
						var imgElem = Dom.get('img1');
						//Get the image id
						var strImageId = o.responseText;
						Dom.addClass(elemLoading,'invisible');			
						//alert(strImageId);
						imgElem.src = urlBase+"getimage.php?id="+strImageId;
						imgElem.style.display = "inline";
						
					},
					failure:function(o){
						alert('Something went wrong =(, Maybe the server is not working right now. \n Please try again or user Firefox to generate the image inmediately');
					},
					cache:false
				}
				return {
					/**
					* Initialize main application module
					*/
					init:function(){
						//Pass the language as flashvars, extract from query string
						var currentLocation = window.location.href;
						var parts = currentLocation.split("?");
						var langCode = "en";
						var flashvars = null;
						if(parts.length>1)
						{
							var parameters = parts[1].split("&");
							var parameterPair = null;
							do
							{
								parameterPair = parameters.shift();
								if((typeof parameterPair == "string") && parameterPair.length>0)
								{
									var keyvalue = parameterPair.split("=");
									if(keyvalue[0] == "lang")
										langCode = keyvalue[1];
								}
								else
									break;
							}while(1);
						}
						flashvars = {"lang":langCode};
						swfobject.embedSWF("loader.swf?version=1","contenedorSwf","940","600","9.0.0",false,flashvars);
					},
					/**
					* Save image. If user agent does not support inline base64 for image src, then
					* generate image on server
					*/
					saveImage:function(data)
					{	
						if(YAHOO.env.ua.ie > 0)
						{
							//IE: =( Use server proxy for image generation )
							var elemLoading = Dom.get('loadingpanel');
							Dom.removeClass(elemLoading,'invisible');
							var postData = "data="+encodeURIComponent(data);
							var sUrl = "createimage.php";
							var imgElem = Dom.get('img1');
							//Show loading image to avoid showing the last generated image
							imgElem.src = "assets/ajax-loader.gif";
							imgElem.style.display = "inline";
							var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
							
						}
						else
						{
							var imgElem = Dom.get('img1');
							imgElem.src = "data:image/png;base64,"+data;
							imgElem.style.display = "inline";
						}
						
						//Move scrollbar
						var elemImage = Dom.get('imageresult');
						var yPos = Dom.getY( elemImage );
						window.scrollTo(0,yPos);
					}
				};
				
			}();
			
			Event.onDOMReady(function(){
				YAHOO.hz.quickcomp.init();
			});
			

		})();