AIM = {

	frame : function(c) {

		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:dnone" width="100%" height="300px" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);

		var i = Core.getElementU(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}

		return n;
	},

	form : function(f, name) {
		f.setAttribute('target', name);
	},

	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},

	loaded : function(id) {
		var i = Core.getElementU(id);
		try {
			if (i.contentDocument) {
				var d = i.contentDocument;
			} else if (i.contentWindow) {
				var d = i.contentWindow.document;
			} else {
				var d = Core.getElementU(id).document;
			}
//			if (d.location.href == "about:blank") {
//				return;
//			}

			if (typeof(i.onComplete) == 'function') {
//				i.onComplete(d.body.innerHTML);
				i.onComplete();
			}
		} catch(ex) {
			return;
		}
	}

}

// extra function
function highlight(field) {
	field.focus();
	field.select();
}

function _getEmailsList(str) {
	str = str.replace(/\s/g, "");

	var matches = str.split(",");
	var emails = Array();

	for (var i = 0; i < matches.length; i++) {
		if (matches[i] && Core.validateEmail(matches[i])) {
			emails.push(matches[i]);
		}
	}
	return emails;
}

function sendLinksByEmail () {
	var action = 'emaillinks';

	try {
		var e_from = Core.getElementU('emaillinks_from').value;
		var e_to = Core.getElementU('emaillinks_to').value;

		var from_emails = _getEmailsList(e_from);
		var to_emails = _getEmailsList(e_to);
		if(!from_emails[0]) { alert("Your email address is invalid."); return; }
		if(!to_emails[0]) { alert("Recipient's email address(es) is invalid."); return; }

		advAJAX.post({
			cmd  			: action,
			email_from	: from_emails[0],
			email_to  	: to_emails.join(),
			url_f			: links['url_f'] || '',
			url_th		: links['url_th'] || '',
			url_p			: links['url_p'] || '',
			onLoading: function(obj) {
				Core.setText('emaillinks_msgblock', progress_el+"&nbsp;&nbsp;Sending...");
			},
			onSuccess : function(obj) {
				var response = obj.responseText.split("|");

				if (response[0] == 'ok') {
					Core.setText('emaillinks_msgblock', "Links has been sent.");
				} else {
					Core.setText('emaillinks_msgblock', "Server error.");
				}
				Core.setText('emaillinks_msgblock', "<span style='color:#00D025'><b>Sent</b></span>");
      	},
			onError : function(obj) {
				Core.setText('emaillinks_msgblock', "<span style='color:#BA2B00'>System error.</span>");
			}
		});
	} catch(ex) {
		Core.setText('emaillinks_msgblock', "");
      return;
   }
}

function sendFeedback() {
	var action = 'hotfeedback';

	try {
		var body = Core.getElementU('fbform').value || "";
		if(!body) { alert("Your feedback is empty."); return; }

		advAJAX.post({
			cmd         : action,
			hotfeedback : body,

			onLoading: function(obj) {
				Core.getElementU('fbform').value = "Sending...";
				Core.getElementU('fbform').disabled = true;
				Core.getElementU('fbsendbtn').disabled = true;
			//	Core.getElementU('fbclearbtn').disabled = true;
			},

			onSuccess : function(obj) {
				Core.getElementU('fbform').disabled = false;
				Core.getElementU('fbsendbtn').disabled = false;
         // Core.getElementU('fbclearbtn').disabled = false;
				if (obj.responseText == 'ok') {
					Core.getElementU('fbform').value = "...feedback sent";
					setTimeout(function() {
						try {
							Core.hideElement("fbblock");
							Core.getElementU('fbform').value = "";
						} catch(ex) {
							return;
						}
					}, 1000);
					return;
				}
				Core.getElementU('fbform').value = "Server error. Try again later.";
			},

			onError : function(obj) {
				Core.getElementU('fbform').disabled = false;
				Core.getElementU('fbsendbtn').disabled = false;
			//	Core.getElementU('fbclearbtn').disabled = false;
				Core.getElementU('fbform').value = "System error. Try again later.";
			}
		});
	} catch(ex) {
		Core.getElementU('fbform').value = "";
		Core.hideElement('fbblock');
		return;
	}
}

// WORKS INTERNET EXPLORER-only
//
// <SPAN ID="copytext" STYLE="height:150;width:162;background-color:pink">
// This text will be copied onto the clipboard when you click the button below. Try it!</SPAN>
//
//<TEXTAREA ID="holdtext" STYLE="display:none;"></TEXTAREA>
function clipBoard(id_text, id_hold) {
	try {
		id_hold = !id_hold ? 'holdtext' : id_hold;
		if(!id_text) return;
		if(document.body.createTextRange) {
			Core.setText(id_hold, Core.getText(id_text));
			var Copied = Core.getElementU(id_hold).createTextRange();
			Copied.execCommand("RemoveFormat");
			Copied.execCommand("Copy");
		} else {
			alert("Does\'t support for this browser.");
			return;
		}
	} catch(ex) {
		return;
	}
}

function clearHotCookie() {
	try {
		var hot_cookie_name = new Array("hotlink_uri","hotlink_th","hotlink_width","hotlink_height","hotlink_th_width","hotlink_th_height","hotlink_hostname");
		for(var i in hot_cookie_name) {
			deleteCookie(hot_cookie_name[i], "/", ".fotki.com");
		}
	} catch(ex) {
		return;
	}
}

function uploadMore(c) {
	try {
		var r = true;
		if(c) r = confirm("Please make sure you have saved the links created for future use.");
		if (r == true) {
			clearHotCookie();
			document.location.href = hot_url;
		}
	} catch(ex) {
		return;
	}
}

function showHideBtn(id) {
	Core.showHideElement(id);
}

var CopyBtn = {
	ref : null,

	showHide : function(e) {
		try {
			if (document.body.createTextRange && e != null) {
      		CopyBtn.ref = Core.getElementU(e);

				if (CopyBtn.ref != null) {
					if (CopyBtn.ref.style.display == "none") {
						CopyBtn.show();
					} else {
						CopyBtn.hide();
					}
				}
			}
		} catch(ex) {
			return;
		}
	},

	show : function() {
		try {
			if (CopyBtn.ref != null) {
				CopyBtn.ref.style.display = ""; //inline, ""
			}
		} catch(ex) {
			return;
		}
	},

	hide : function() {
		try {
			if (CopyBtn.ref != null) {
				CopyBtn.ref.style.display = "none"; //inline, ""
			}
		} catch(ex) {
			return;
		}
   }
}

function hotBookmark(){
	try {
		var title = document.title || "Fotki.com - Image Hosting";
		var url = self.location.href || "http://host.dev.fotki.com:"+document.location.port+"/";

		if(document.all &&  window.external) {
			window.external.AddFavorite(url, title);
		} else if(Core.detectBrowser().browser == 'firefox') {
			window.sidebar.addPanel(title, url, '');
		} else {
			alert('Press CTRL and D to add a bookmark:\n"'+url+'"');
		}
	} catch(ex) {
		return;
	}
}

function genEmbed(effect, size) {
	var dim = {'100x100':'100', '200x200':'200', '300x300':'300', '400x400':'400', '500x500':'500'};
	var src = hotimg_url+ '/flash/HotLinking_Loader.swf';
	var photo = escape('http://'+links['url_flash']);
	
	var embed = "<object width=\""+dim[size]+"\" height=\""+dim[size]+"\">"+
					"<param name=\"width\" value=\""+dim[size]+"\" />"+
					"<param name=\"height\" value=\""+dim[size]+"\" />"+
					"<param name=\"flashvars\" value=\"photo="+photo+"&effect="+effect+"\" />"+
					"<embed src=\""+src+"\" effect=\""+effect+"\" photo=\""+photo+"\" width=\""+dim[size]+"\" height=\""+dim[size]+"\" type=\"application/x-shockwave-flash\" pluginpage=\"http://www.macromedia.com/go/getflash/\" />"+
					"<object src=\""+photo+"\" />"+
					"</object>";

	prompt("embed", embed);
	Core.setText('testFlash', embed);	
}

var uploadParam = {
	host : '.host.fotki.com',
	opt_name : 'hotupload_param',
	type_name : 'hotupload_type',

	getPath : function() {
		try {
			if (document.location.hostname.indexOf("dev")>-1) {
				this['host'] = ".host.dev.fotki.com";
			} else if (document.location.hostname.indexOf("staging")>-1) {
				this['host'] = ".host.staging.fotki.com";
			}
		} catch(ex){
			return;
		}
	},
	
	setOpt : function() {
		try {
			var val1 = (Core.getElementU('resimg').checked ? 'true' : 'false');
			var val2 = Core.getElementU('imgsize').value || 'optimize';
			uploadParam.getPath();
			setCookie(this['opt_name'], val1+'_'+val2, '', '/', this['path']);	
		} catch(ex) {
			return;
		}		
	},

	setType : function(t) {
		try {
			uploadParam.getPath();
         setCookie(this['type_name'], t, '', '/', this['path']);
		} catch(ex) {
			return;
		}
	}
}
