YAHOO.namespace("example.container");

function init() {
	

	// Initialize the temporary Panel to display while waiting for external content to load
	YAHOO.example.container.wait = 
		new YAHOO.widget.Panel("wait",  
			{ width:"240px", 
			  fixedcenter:true, 
			  close:false, 
			  draggable:false, 
			  zindex:4,
			  modal:true,
			  visible:false
			} 
		);

	YAHOO.example.container.wait.setHeader("Učitavam, molim pričekajte...");
	YAHOO.example.container.wait.setBody('<img src="/slike/rel_interstitial_loading.gif" />');
	YAHOO.example.container.wait.render(document.body);


	// Define various event handlers for Dialog
	var handleSubmitURL = function() {
		div.value="";
		vrijemeZahtjeva=(new Date()).getTime();
		myTabs.set('activeIndex',  0);
		this.submit();
	};
	var handleCancelURL = function() {
		this.cancel();
	};
	var handleSuccessURL = function(o) {
		YAHOO.example.container.wait.hide(); 
		handleSuccess(o);
	};
	var handleFailureURL = function(o) {
		YAHOO.example.container.wait.hide(); 
		alert("Submission failed: " + o.status);
	};

	var handleCancelDebug = function() {
		this.cancel();
	};

	var handleFileUpload = function(o) {
		YAHOO.example.container.wait.hide(); 
//		if (o.status == 200) {
			handleSuccess(o);
//		}
//		else {
//			alert ("Došlo je do problema pri prijenosu datoteke.");
//		}
	};

	var handleSubmitFile = function() {
		div.value="";
		vrijemeZahtjeva=(new Date()).getTime();
		myTabs.set('activeIndex',  0);
		this.submit();
	};

	var handleCancelFile = function() {
		YAHOO.example.container.wait.hide(); 
		this.cancel();
	};

	var downloadGadget = function() {
		window.location = "http://gallery.live.com/liveItemDetail.aspx?li=96f79622-1e19-468f-a5b3-c355495076e5&bt=1&pl=1";
		this.hide();
	};
	var handleCancelGadget = function() {
		this.hide();
	}

	// Instantiate the Dialog


	YAHOO.example.container.dialogURL = new YAHOO.widget.Dialog("URLinput", 
							{ width : "40em",
							  height: "20em",
							  fixedcenter : true,
							  visible : false, 
							  underlay : "shadow",
							  modal : true,
							  constraintoviewport : true,
						      effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25},
							  buttons : [ { text:"Pronađi pogreške", handler:handleSubmitURL, isDefault:true },
								      { text:"Odustani", handler:handleCancelURL } ]
							});

	YAHOO.example.container.dialogFile = new YAHOO.widget.Dialog("Fileinput",
							{ width : "40em",
							  height: "25em",
							  fixedcenter : true,
							  visible : false, 
							  underlay : "shadow",
							  modal : true,
							  constraintoviewport : true,
						          effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25},
							  buttons : [ { text:"Pošalji datoteku", handler:handleSubmitFile, isDefault:true },
								      { text:"Odustani", handler:handleCancelFile } ]
							});
	
	YAHOO.example.container.panelDebug = new YAHOO.widget.Panel("debug", 
							{ width : "60em",
							  height : "40em",
							  fixedcenter : true,
							  visible : false, 
							  underlay : "shadow",
							  modal : false,
							  constraintoviewport : true
							});


	YAHOO.example.container.dialogVista = new YAHOO.widget.SimpleDialog("vista", 
							{ width : "30em",
							  height : "11em",
							  fixedcenter : true,
							  visible : false, 
							  underlay : "shadow",
							  modal : true,
						          effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25},
							  constraintoviewport : true,
							  buttons : [ { text:"Download", type: "link", href:"http://gallery.live.com/liveItemDetail.aspx?li=96f79622-1e19-468f-a5b3-c355495076e5&bt=1&pl=1", handler: downloadGadget, isDefault:true },
                                                                      { text:"Odustani", handler:handleCancelGadget } ]

							});

	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.example.container.dialogURL.validate = function() {
		var data = this.getData();
		if (data.url == "" || data.url == "http://" || !(data.url.match("^http://"))) {
			YAHOO.example.container.wait.hide(); 
			alert("Molimo, unesite ispravnu adresu stranica!");
			return false;
		} else {
			YAHOO.example.container.wait.show(); 
			return true;
		}
	};

	YAHOO.example.container.dialogFile.validate = function() {
		var data = this.getData();
		if (data.file == "") {
			alert("Molimo, izaberite odgovarajuću datoteku!");
			YAHOO.example.container.wait.hide(); 
			return false;
		} else {
			if ( data.file.match( /(PDF|DOC|TXT)$/i ) ) {
				YAHOO.example.container.wait.show(); 
				return true;
			}	
			else {
				alert("Trenutno je podržano samo slanje datoteka u formatu PDF!");
				YAHOO.example.container.wait.hide(); 
				return false;
			}
		}
	};

	// Wire up the success and failure handlers
	YAHOO.example.container.dialogURL.callback = { success: handleSuccessURL,
						     failure: handleFailureURL };
	
	YAHOO.example.container.dialogFile.callback = { upload: handleFileUpload, 
							cache: false
							};
	
	// Render the Dialog
	YAHOO.example.container.dialogURL.render();
	YAHOO.example.container.dialogFile.render();
	YAHOO.example.container.panelDebug.render();
	YAHOO.example.container.dialogVista.render();

	var kl = new YAHOO.util.KeyListener(document, { ctrl: true, alt: true, keys: 68 },
	                                              { fn:YAHOO.example.container.panelDebug.show, 
	                                                scope:YAHOO.example.container.panelDebug, 
	                                                correctScope:true } ); 

	var ke = new YAHOO.util.KeyListener(document, { keys: 27 },
	                                              { fn:YAHOO.example.container.panelDebug.hide, 
	                                                scope:YAHOO.example.container.panelDebug, 
	                                                correctScope:true } ); 
	 
	 
	YAHOO.example.container.panelDebug.cfg.queueProperty("keylisteners", kl); 
	YAHOO.example.container.panelDebug.cfg.queueProperty("keylisteners", ke); 
	kl.enable();	
	ke.enable();	

	YAHOO.util.Event.addListener("adresa", "click", YAHOO.example.container.dialogURL.show, YAHOO.example.container.dialogURL, true);
	YAHOO.util.Event.addListener("upload", "click", YAHOO.example.container.dialogFile.show, YAHOO.example.container.dialogFile, true);
	YAHOO.util.Event.addListener("gadget", "click", YAHOO.example.container.dialogVista.show, YAHOO.example.container.dialogVista, true);
}

YAHOO.util.Event.onDOMReady(init);

