function startAds(portal,input){
	ads = new SapoAds(portal,'0',input);
}

// some stuff is in javascript.js and should be here

var SapoAds = Class.create({
	initialize: function(portal_str,ad_type_num,ad_data_json){
		// portal string to handle correct image paths
		this.portal = portal_str;
		/**
		 * JSON encoded ad data
		 * structure should be:
		 * {
		 * 	adv_id: <idOfAd>,
		 *	adv_line1: <line1OfTextAd>,
		 *	adv_line2: <line2OfAd>,
		 *	adv_image_path: <pathOfImage>,
		 *	adv_url_redirect: <redirectUrl>,
		 *	adv_url_display: <displayedUrl>
		 * }
		 **/
		if(ad_data_json != null && ad_data_json.evalJSON()){
			this.ad_data = ad_data_json.evalJSON();
		}else{
			// ivalid json input. it's probably empty
			this.ad_data = {};
		}

		this.loadAd();

	},
	loadAd: function(){
		
		if(this.ad_data.adv_image_path != undefined){
			//let's show the image
			$("img_path_preview").update(
				new Element("img",{"src":this.ad_data.adv_image_path,"style":"margin-left:0"})
			);
		}else{
			// no image to show yet. let's just show an empty space
			$("img_path_preview").update("");
		}
		// substitute line1 text input for image file input. also attach event
		$("img_path").observe("change",function(){
				this.uploadFile();
		}.bind(this));

		// ajaxify image deleter
		$("img_remover").observe("click",function(event){
			event.stop();
			var deleter = new Ajax.Request($("img_remover").href,{
				method: "post",
				onSuccess: function(transport){
					try{
					var is_deleted = Number(transport.responseText);
					if(is_deleted === 1)
						this.deleteImg();
					else
						writeError("img_remover","Erro ao remover imagem.");
						}catch(e){alert(e)}
				}.bind(this),
				onFailure: function(){
					writeError("img_remover","Erro ao remover imagem. Erro de conectividade.");
				}
			});
		}.bind(this));

	},
	deleteImg: function(){
		// a bit useless to separate this but I have a feeling smothing more might one day become necessary :D
		$("img_path_preview").update("");
	},
	isValidFile: function(){
		var filename = $F("img_path");
		return /.+\.(jpg|jpeg|png)$/i.test(filename);
	},
	uploadFile: function(){
		// only upload if input is valid
		if(this.isValidFile()){
			// disable file input and replace it with the waiting image
			$("img_path").style.display = "none";
			$("mainContent").insert(
				new Element("img",{"src":"http://imgs.sapo.pt/images/ANUNCIOSV3/ANUNCIOS/conteudo/img_loader.gif"})
			);
			// check if we've got our hidden upload iframe. If not, create it
			if($("uploadIframe") == null){
				var bodyElement = $$("body")[0];
				bodyElement.insert(
					new Element("iframe",{"id":"uploadIframe","name":"uploadIframe","frameborder":"0","width":"0","height":"0"})
				);
			}
			// start observing load event to check for upload result
			Event.observe($("uploadIframe"),"load",function(event){
				 var iframeDocument = $("uploadIframe").document || $("uploadIframe").contentWindow.document;
				 // check if there's a url there. if not show an error
                                 var uploadOutput = iframeDocument.body.innerHTML;

				 $("mainContent").update(
                                          new Element("input",{"type":"file","id":"img_path","name":"img_path"})
                                   );
				 if(isWizardUrl(uploadOutput,'')){
				 	// it's a url. let's show the image
					this.ad_data.adv_image_path=uploadOutput;
					
				 }else{
					 // upload didn't work so well. 
					 writeError("img_path","Erro ao carregar ficheiro: "+uploadOutput.substring(0,100));
				 }
				 this.loadAd();
				 $("uploadIframe").remove();
			 }.bind(this));
			// change form target and action so that it submits to the upload iframe
			var thisForm = $("img_path").up("form");
			var formAction = thisForm.action;
			thisForm.target = "uploadIframe";
			thisForm.action = "wizard/uploadFile.php";
			// submit it
			thisForm.submit();
			// reset form target and action
			thisForm.target = "";
			thisForm.action = formAction;

		}else{
			writeError("img_path","Ficheiro inv&aacute;lido. Tipos de ficheiro aceites: jpeg e png");
		}

	}
});

