HotImage = {
	js_url : "http://hotimg1.fotki.com/servers.js",
	best_serv : null,
	timer : 0, // clear interval variable
	counter : 0,
	type : 'main',

	init : function() {
		if (document.location.hostname.indexOf("dev")>-1) {
			HotImage.js_url = "http://hotimg1.dev.fotki.com:"+document.location.port+"/servers.js";
		} else if (document.location.hostname.indexOf("staging")>-1) {
			HotImage.js_url = "http://hotimg1.staging.fotki.com:8080/servers.js";
		}
		script = document.createElement('script'),
		head = document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0];
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('src', HotImage.js_url);
		head.appendChild(script);
	},

	initHref : function(el) {
		var regexp = new RegExp("hotimg1\.fotki\.com");
		el.href=el.href.replace(regexp, HotImage.best_serv);
	},

	isServer : function() {
		HotImage.counter++;
		if (HotImage.best_serv != null) {
			window.clearInterval(HotImage.timer);
			HotImage.counter = 0;
			var form = Core.getElementU("hotuploadform");
			form.action = "http://"  + HotImage.best_serv;
			form.submit();
			switch(HotImage.type) {
			case 'main':
				Input.hide('hotlinkphoto');
				Input.hide('hotlinkfetch');
				Core.getElementU('typeFile').disabled = true;
				Core.getElementU('typeUrl').disabled = true;
				Input.show('uploadblock');
				break;

			case 'view':
				Input.hide('hotuploadform');
				Input.show('uploadblock');
				break;	
			}
			HotProgress.startUpload();
		} else if (HotImage.counter > 600) {
			window.clearInterval(HotImage.timer);
			HotImage.counter = 0;
			//    alert("Timeout error. Try again later.");
		}
	},

	upload : function(ref, type) {
		try {
			HotImage.init();
			HotImage.timer = self.setInterval("HotImage.isServer()", 40);
			HotImage.type = type;
			if(ref.type == 'button') ref.disabled = true;
		} catch(ex) {
			if(ref.type == 'button') ref.disabled = false;
			ref.disabled = false;
			return;
		}
	}
}

				
function setpx(ref) {
	try {
		var str = (ref.value == 'optimize' ? '' : 'pixels');
		Core.getElementU('hotpx').innerHTML = str;
	//	Core.setText('hotpx', str);
	} catch(ex) {
		return;
	}
}


Host = {
	el : null,
	counter : 0,
	timer : 0,
	uri : '',

	image : function(ref, uri) {
		try {
			Host.el = ref;
			Host.uri = uri;
			HotImage.init();
			Host.timer = self.setInterval("Host.isServer()", 40);
			return;  
		} catch(ex) {
			return;
		}
	},

	isServer : function () {
		try {
			Host.counter++;
			if (HotImage.best_serv != null) {
				window.clearInterval(Host.timer);
				Host.counter = 0;
				document.location = 'http://'+HotImage.best_serv+"/upload?hotfetch="+Host.uri;
				return;
			} else if (Host.counter > 600) {
				window.clearInterval(Host.timer);
				Host.counter = 0;
				return;
			}
		} catch(ex) {
			return;
		}
	},
   
	initHref : function() {
		try {
			var regexp = new RegExp("hotimg1\.fotki\.com");
			Host.el.href = Host.el.href.replace(regexp, HotImage.best_serv);
		} catch(ex) {
			return;
		}
	}  
}

HotImage.formDisable = function () {
	try {
		Core.getElementU('typeFile').disabled = true;
		Core.getElementU('typeUrl').disabled = true;
		Core.getElementU('hotphoto').disabled = true;
		Core.getElementU('fetch').disabled = true;
		Core.getElementU('uploadBtn').disabled = true;
	} catch(ex) {
		return;
	}
}

HotImage.formEnable = function () {
	try {
		Core.getElementU('typeFile').disabled = 'false';
		Core.getElementU('typeUrl').disabled = 'false';
		Core.getElementU('hotphoto').disabled = 'false';
		Core.getElementU('fetch').disabled = 'false';
		Core.getElementU('uploadBtn').disabled = 'false';
	} catch(ex) {
		return;
	}
}

var progress_el = "<img src=http://images.fotki.com/progress.gif>";

var HotProgress = {
	currentCounter : null,
	progressBlockId : "uploadblock",
	progressId : "progress",
	width : 260, // progress bar width

	startUpload : function() {
		try {
			Core.getElementU(HotProgress.progressBlockId).style.display = '';
			var progressBar = Core.getElementU(HotProgress.progressId);
			HotProgress.uploadProg(progressBar, 1);
		} catch(ex) {
			return;
		}
	},

	uploadProg : function(obj, totaltime) {
		try {
			totalcount = Math.ceil(HotProgress.width * (1.0 - (8 / (totaltime + 8))));
			if(totalcount > HotProgress.width) return;
			obj.style.width = (HotProgress.width - totalcount) + 'px';
			currentCounter = setTimeout(function() { HotProgress.uploadProg(obj, ++totaltime) }, 100);
		} catch(ex) {
			return;
		}
	},

	clearUpload : function() {
		try {
			Core.getElementU(HotProgress.progressBlockId).style.display = 'none';
		//    HotImage.formEnable();
		} catch(ex) {
			return;
		}
	}
}

var Input = {
	ref : null,
      
	showHide : function(e) {
		try {
			Input.ref = Core.getElementU(e);
      
			if (Input.ref != null) {
				if (Input.ref.style.display == "none") {
					Input.show();
				} else {
					Input.hide();
				}
			}
		} catch(ex) {
			return;
		}
	}, 
   
	show : function(e) {
		try {
			Input.ref = Core.getElementU(e);
			if (Input.ref != null) {
				Input.ref.style.display = ""; //inline, ""
			}
		} catch(ex) {
			return;
		}
	},

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

function showFile(btn) {
	Input.hide('hotlinkfetch');
	Input.show('hotlinkphoto');
//   Core.getElementU('hotfetch').disabled = true;
//   Core.getElementU('hotphoto').disabled = false;
	if(btn) Input.hide('uploadBtn');
}

function showUrl(btn) {
//   Core.getElementU('hotphoto').disabled = true;
//   Core.getElementU('hotfetch').disabled = false;
	Input.hide('hotlinkphoto');
	Input.show('hotlinkfetch');
	if(btn) Input.show('uploadBtn');
}


Core.addEvent(window, 'unload', HotProgress.clearUpload);
