/*
	Copyright (c) Zeros 2 Heroes All Rights Reserved. 
*/
 
dojo.require("dojo.parser");
dojo.require("dojo.io.iframe");
dojo.require("dijit.Dialog");

dojo.config.dojoBlankHtmlUrl = '/blank.html';

Z2h = {};

Z2h.link = {
	init: function() {
		dojo.query('a.delete').forEach(function(link) {
			dojo.connect(link, 'onclick', Z2h.link.doPostDelete);
		}); 
		
		var primer_shower = dojo.byId('primer_shower');
		if (primer_shower) {
			dojo.connect(primer_shower, 'onclick', Z2h.link.showPrimer); 
		}
	},
	
	doPostDelete: function(evt) {
		evt.preventDefault();
		
		if (confirm(evt.currentTarget.title)) { 
			var form = document.createElement('form');
			form.setAttribute("id","deleteForm");
			form.setAttribute("method","POST");
			form.setAttribute("action",evt.currentTarget.href);
			document.getElementsByTagName("body").item(0).appendChild(form);
			form.submit();
		}
	},
	
	showPrimer: function(evt) {
		evt.preventDefault();
		
		var hidden_primer = dojo.byId('primer_hide'); 
		if (hidden_primer) {
			hidden_primer.style.display = "";
			evt.currentTarget.style.display = "none";
		}
	}	
}		
	
Z2h.contact = {
	init: function() {
		dojo.query('a.contact_dialog').forEach(function(link) {
			dojo.connect(link, 'onclick', Z2h.contact.showDialog);
		}); 
	},
		 
	showDialog: function(evt) {
		evt.preventDefault();
		
		// close already opend overlay form.
		var opened_dialog = dijit.byId('contact_dialog');
		
		if (opened_dialog) {
			opened_dialog.hide(); 
			opened_dialog.destroy();
		} 
			
		var dialog = new dijit.Dialog({id:'contact_dialog'});
		dojo.style(dialog.titleBar, "display", "none");  

		dojo.xhrGet({
			url:evt.currentTarget.href+'/format/text',
			handleAs: "text", 
			load: function(html) { 
				dialog.attr('content', html);
				dialog.show();

				dojo.query('.dijitDialogCloseIcon, #cancelBtn').forEach(
					function(o) {
						var handle = dojo.connect(o, 'onclick', dojo.hitch(dialog, function(e) {
							e.preventDefault();
							dialog.attr('content', ''),
							dialog.hide();
							dialog.destroy();
							dialog.disconnect(handle);
						}));
					}
				);

				// override escape button to trigger the reason dialog close button
				dojo.connect(dialog.domNode, 'onkeypress', function(e) {
					var k = e.keyCode || e.keyChar;
					if (k == dojo.keys.ESCAPE) {
						dojo.byId('cancelBtn').click();
						dojo.stopEvent(e);
					}
				});
 
				// create a submit button to send the contact form.
				var submitButton = dojo.byId('submitBtn'); 
				if (submitButton) {  
				    dojo.connect(submitButton, 
						    	"onclick",
						    	function(e){ 
									e.preventDefault();  
									Z2h.contact.sendForm();
				    }); 
				} 
				    
				var postForm = dojo.byId('contactForm'); 
				if (postForm) {  
				    dojo.connect(postForm, 
						    	"onsubmit",
						    	function(e){ 
									e.preventDefault();
									Z2h.contact.sendForm();
				    }); 
				}   
			}
		});
	},
		
	sendForm: function() {
		// send ajax form. 
		dojo.byId("form_wrapper").style.display = "none";
		dojo.byId("contact_errors").style.display = "none";
		dojo.byId("submit_please_wait").style.display = ""; 
			
		dojo.xhrPost({
			url: "/contact-form/format/json", 
			form: "contactForm",  
			handleAs: "json", 
			headers: {"X-Requested-With": "XMLHttpRequest"},
			load: function(data,ioargs){  
				if(data.result == "1"){
					dojo.byId("sent_contact").style.display = "";  
					dojo.byId("submit_please_wait").style.display = "none";
				}else{ 
					var contact_errors = dojo.byId('contact_errors');
						
					if (contact_errors) {
						contact_errors.innerHTML = "";
							
						for(var k in data.errors){ 
							var errorItem = document.createElement('p');
							errorItem.innerHTML = data.errors[k];
							contact_errors.appendChild(errorItem);  
						}
						
						contact_errors.style.display = "";
					}
						 
					dojo.byId("form_wrapper").style.display = "";
					dojo.byId("submit_please_wait").style.display = "none"; 
				}
					
				return data; //always return the response back 
			},
			// if any error occurs, it goes here: 
			error : function(response, ioArgs) { 
		        console.log("failed xhrGet", response, ioArgs); 
		            
		        alert("ERROR: There was an error while submitting your request. Please try again!");
		            
		        /* handle the error... */ 
		        return response; //always return the response back 
		    }
		});
	}
}

Z2h.file = {
		init: function() {
			dojo.query('a.upload_file, a.upload_image').forEach(function(link) {
				dojo.connect(link, 'onclick', Z2h.file.showDialog);
			});   
		},
			 
		showDialog: function(evt) {
			evt.preventDefault();
		 
			var dialog = new dijit.Dialog({id:'upload_dialog'});
			dojo.style(dialog.titleBar, "display", "none");  

			dojo.xhrGet({
				url:evt.currentTarget.href+'/format/text',
				handleAs: "text", 
				load: function(html) { 
					dialog.attr('content', html);
					dialog.show();

					dojo.query('.dijitDialogCloseIcon, #cancelBtn').forEach(
						function(o) {
							var handle = dojo.connect(o, 'onclick', dojo.hitch(dialog, function(e) {
								e.preventDefault();
								dialog.attr('content', ''),
								dialog.hide();
								dialog.destroy();
								dialog.disconnect(handle);
							}));
						}
					);

					// override escape button to trigger the reason dialog close button
					dojo.connect(dialog.domNode, 'onkeypress', function(e) {
						var k = e.keyCode || e.keyChar;
						if (k == dojo.keys.ESCAPE) {
							dojo.byId('cancelBtn').click();
							dojo.stopEvent(e);
						}
					});
	 
					// create a submit button to upload file.
					var submitFileButton = dojo.byId('submitFileBtn'); 
					if (submitFileButton) {  
					    dojo.connect(submitFileButton, 
							    	"onclick",
							    	function(e){ 
										e.preventDefault();  
										Z2h.file.uploadFile();
					    }); 
					} 
					    
					var postFileForm = dojo.byId('uploadFileForm'); 
					if (postFileForm) {  
					    dojo.connect(postFileForm, 
							    	"onsubmit",
							    	function(e){ 
										e.preventDefault();
										Z2h.file.uploadFile();
					    }); 
					}   
					
					// create a submit button to upload file.
					var submitImageButton = dojo.byId('submitImageBtn'); 
					if (submitImageButton) {  
					    dojo.connect(submitImageButton, 
							    	"onclick",
							    	function(e){ 
										e.preventDefault();  
										Z2h.file.uploadImage();
					    }); 
					} 
					    
					var postImageForm = dojo.byId('uploadImageForm'); 
					if (postImageForm) {  
					    dojo.connect(postImageForm, 
							    	"onsubmit",
							    	function(e){ 
										e.preventDefault();
										Z2h.file.uploadImage();
					    }); 
					}   
				}
			});
		},
			
		uploadFile: function() {
			// check file is selected.
			var upload_file_name = dojo.byId('upload_file_name').value; 
			if (upload_file_name=='') {
				dojo.byId("no_file_error").style.display = "";
			}else{ 
				dojo.byId("no_file_error").style.display = "none";
				dojo.byId("file_upload_error").style.display = "none";
				
				// send ajax form. 
				dojo.byId("form_wrapper").style.display = "none";
				dojo.byId("submit_please_wait").style.display = ""; 
				
				dojo.io.iframe.send({
					method: "post",
					url: "/admin/upload-file/format/text", 
					form: "uploadFileForm",  
					handleAs: "text",  
					headers: {"X-Requested-With": "XMLHttpRequest"},
					load: function(data,ioargs){  
						var result = data.indexOf("SUCCESS::");
						
						if(result != -1){
							var filename = data.replace(/SUCCESS::/,"");
							 
							dojo.query('.pdf_file').forEach(function(node) {
								var tag = node.tagName;
								
								if (tag == 'SPAN') {
									node.innerHTML = filename;
								} else if (tag == 'INPUT') {
									node.value = filename;
								}  
							});
							
							dojo.byId("file_upload_success").style.display = ""; 
							dojo.byId("submit_please_wait").style.display = "none";
						}else{ 
							var upload_error = dojo.byId('file_upload_error');
								
							if (upload_error) {
								upload_error.innerHTML = "";
									
								var errorItem = document.createElement('p');
								errorItem.innerHTML = data;
								upload_error.appendChild(errorItem);  
								upload_error.style.display = "";
							}
								 
							dojo.byId("form_wrapper").style.display = "";
							dojo.byId("submit_please_wait").style.display = "none"; 
						}
							
						return data; //always return the response back 
					},
					// if any error occurs, it goes here: 
					error : function(response, ioArgs) { 
				        console.log("failed xhrGet", response, ioArgs); 
				            
				        alert("ERROR: There was an error while submitting your request. Please try again!");
				            
				        /* handle the error... */ 
				        return response; //always return the response back 
				    }
				}); 
			}  
		},
		
		uploadImage: function() {
			// check file is selected.
			var upload_file_name = dojo.byId('upload_file_name').value; 
			if (upload_file_name=='') {
				dojo.byId("no_file_error").style.display = "";
			}else{ 
				dojo.byId("no_file_error").style.display = "none";
				dojo.byId("file_upload_error").style.display = "none";
				
				// send ajax form. 
				dojo.byId("form_wrapper").style.display = "none";
				dojo.byId("submit_please_wait").style.display = ""; 
				
				dojo.io.iframe.send({
					method: "post",
					url: "/admin/upload-image/format/text", 
					form: "uploadImageForm",  
					handleAs: "text",  
					headers: {"X-Requested-With": "XMLHttpRequest"},
					load: function(data,ioargs){  
						var result = data.indexOf("SUCCESS::");
						
						if(result != -1){
							var filename = data.replace(/SUCCESS::/,"");
							 
							dojo.query('.image_name').forEach(function(node) {
								var tag = node.tagName;
								
								if (tag == 'SPAN') {
									node.innerHTML = filename;
								} else if (tag == 'INPUT') {
									node.value = filename;
								} else if (tag == 'IMG') {
									node.src = '/upload/images/'+filename;
								} 
							});
							
							dojo.byId("file_upload_success").style.display = ""; 
							dojo.byId("submit_please_wait").style.display = "none";
						}else{ 
							var upload_error = dojo.byId('file_upload_error');
								
							if (upload_error) {
								upload_error.innerHTML = "";
									
								var errorItem = document.createElement('p');
								errorItem.innerHTML = data;
								upload_error.appendChild(errorItem);  
								upload_error.style.display = "";
							}
								 
							dojo.byId("form_wrapper").style.display = "";
							dojo.byId("submit_please_wait").style.display = "none"; 
						}
							
						return data; //always return the response back 
					},
					// if any error occurs, it goes here: 
					error : function(response, ioArgs) { 
				        console.log("failed xhrGet", response, ioArgs); 
				        console.log(response); 
								            
				        alert("ERROR: There was an error while submitting your request. Please try again!");
				            
				        /* handle the error... */ 
				        return response; //always return the response back 
				    }
				}); 
			}  
		}
	}

dojo.addOnLoad(function() {  
	Z2h.link.init();
	Z2h.file.init();
	Z2h.contact.init(); 
});