/**********************************************************************\
|**                                                                  **|
|*                ImageShack Uploader in Posting Area                 *|
|*  Coded by california, special thanks to Alex Levin of ImageShack®  *|
|*          © 2006 SoCal Codes, http://socal.proboards26.com          *|
|*    This file may not be modified or rehosted without permission    *|
|**                                                                  **|
\**********************************************************************/

// declare global variables
var iRow = document.getElementsByTagName("tr");
var progressImage = "http://devster.monkeeh.com/pics/forums/progress.gif"; // created by The Infinite at Socal
var formcontent = '<form name="uploadform" action="http://www.imageshack.us/index.php" method="post" enctype="multipart/form-data" target="upwin"><input name="boardname" type="hidden" /><input name="servernum" type="hidden" />\
<nobr><input name="fileupload" type="file" size="40" />&nbsp;<input name="submit" type="submit" value="Host It" disabled="true" style="margin-left: 5px;" /></nobr></form>';

// returns the value of the selected radio input
function getRadioValue(rname){
	for(i=0; i<rname.length; i++){
		if(rname[i].checked == true){
			return rname[i].value;
		}
	}
	return false;
}

// returns x-position of an element
function getX(obj){
	var xpos = 0;
	while(obj.offsetParent){
		xpos += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return (obj.x) ? obj.x : xpos;
}

// returns y-position of an element
function getY(obj){
	var ypos = 0;
	while(obj.offsetParent){
		ypos += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return (obj.y) ? obj.y : ypos;
}

// resets the upload form so it can be reused
function resetForm(){
	document.getElementById("updiv").innerHTML = formcontent;
	var imgfile = document.uploadform.fileupload;
	if(!document.body.createTextRange){
		imgfile.style.bottom = "0px";
	}
	if(location.href.match(/:\/\/((www\.)?\w+)\.proboards(\d+)\.com/i)){
		document.uploadform.boardname.value = RegExp.$1;
		document.uploadform.servernum.value = RegExp.$3;
		imgfile.onchange = imgfile.onkeyup = function(){
			var vRe = /\.(gif|jpe?g|png|bmp)$/i;
			document.uploadform.submit.disabled = (this.value.match(vRe)) ? false : true;
		}
	}
	document.uploadform.onsubmit = function(){
		document.uploadform.submit.disabled = true;
		var progcontent = '<center><img src="'+progressImage+'" /><br /><br />Please be patient while your image is uploaded to ImageShack<sup>®</sup>.<br />Closing this window will terminate the upload.</center><br />';
		var winprop = 'width=780,height=500,resizable=yes,scrollbars=yes,toolbars=no';
		var upwin = window.open('', 'upwin', winprop);
		upwin.document.write('<html><head><title>Your Image is Uploading...</title><style type="text/css">'+document.getElementsByTagName("style")[0].innerHTML+'</style></head><body bgcolor="'+document.body.bgColor+'">'+
		buildPage(150, 400, 'Your Image is Uploading', progcontent, 'windowbg2', false)+'</body></html>');
		setTimeout('resetForm();', 500);
	}
}

// builds the content for the popup pages
function buildPage(topmargin, divwidth, pagetitle, pagecontent, lowerclass, relativity){
	var bColor = document.getElementsByTagName("table")[0].bgColor;
	var relcss = (relativity) ? " position: relative; bottom: 28px;" : '';
	return '<center><div class="titlebg" style="border: 1px solid '+bColor+'; border-width: 1px 1px 0px 1px; padding: 5px; margin: auto; margin-top: '+topmargin+'px; text-align: center; width: '+divwidth+'px; vertical-align: middle;'+relcss+'">'+pagetitle+'</div><div class="'+
	lowerclass+'" style="border: 1px solid '+bColor+'; margin: auto; padding: 5px; text-align: left; width: '+divwidth+'px;'+relcss+'"><div style="width: 100%; text-align: center; margin-top: 10px; margin-bottom: 10px;"><a href="http://imageshack.us" target="_blank">\
	<img src="http://imageshack.us/img/imageshack.png" class="nohideimg" border="0" alt="Powered by ImageShack®" /></a></div><br />'+pagecontent+'</div></div></div></center>';
}

// validates the image results page form
function processIt(){
	document.getElementById("naverror").style.display = "none";
	document.getElementById("imgerror").style.display = "none";
	var checkIt = getRadioValue(document.imgform.imgselect);
	if(checkIt && window.opener && window.opener.document.postForm){
		window.opener.document.postForm.message.value += document.getElementById(checkIt).value;
		window.opener.document.postForm.message.focus();
		window.close();
	}else{
		if(!window.opener.add){
			document.getElementById("naverror").style.display = '';
		}
		if(!checkIt){
			document.getElementById("imgerror").style.display = '';
		}
	}
}

// builds the image results page
if(location.href.match(/\?action=uploaded/) && window.opener && window.opener.add){
	document.write('<style type="text/css">table, img, .hidecontent{display: none;} .nohideimg{display: inline;}</style>');
	var imgAr = ['error', 'error'];
	var failed = false;
	document.title = document.title.split(' - ')[0] + ' - Image Uploaded Successfully!';
	if(location.href.match(/&file=(.+)$/i)){
		imgAr = decodeURIComponent(RegExp.$1).split('|');
	}else if(window.name.match(/http.+\|\d/i)){
		imgAr = window.name.split(/\|/);
	}else{
		document.title = document.title.split(' - ')[0] + ' - Image Upload Failed!';
		var failcontent = '<span style="color: #ff0000; font-size: 13px;">Error: No data was received from the upload server</span><br /><div style="float: right; position: relative; top: 24px;"><a href="#closewindow" onclick="window.close(); return false;">[Close Window]</a></div>';
		document.write(buildPage(0, 700, 'Upload Failed!', failcontent, 'windowbg', true));
		failed = true;
	}
	if(!failed){
		var directurl = imgAr[0];
		var showurl = (directurl.match(/p:\/\/(.+?)\/.+\/(\w+\.\w{3,4})$/)) ? 'http://'+RegExp.$1+'/my.php?image='+RegExp.$2 : '';
		var tnhtml = '';
		if(imgAr[1] == "1"){
			var tnurl = directurl.replace(/(\.\w+)$/i, '.th$1');
			tnhtml = '<input type="radio" name="imgselect" value="tnimg" /> <input type="text" id="tnimg" onclick="this.select();" style="width: 470px;" size="70" value="[url='+showurl+'][img]'+tnurl+'[/img][/url]" /> Clickable thumbnail image:<br />\
			<table style="margin-top: 20px; margin-bottom: 10px; display: block;"><tr><td valign="middle"></td>\
			<td valign="middle"><a href="'+showurl+'" target="_blank"><img src="'+tnurl+'" class="nohideimg" border="0" style="position: relative; left: 35px;" /></a></td></tr></table>';
		}
		var pcontent = '<form name="imgform"><br /><br />'+
		tnhtml+'<input type="radio" name="imgselect" value="imglink" /> <input type="text" id="imglink" onclick="this.select();" style="width: 470px;" size="70" value="'+showurl+'" /> <a href="'+showurl+'" target="_blank">Link to image</a></form>\
		<span id="naverror" style="color: #ff0000; font-size: 13px; display: none;">Error: You appear to have navigated away from the posting page<br /></span><span id="imgerror" style="color: #ff0000; font-size: 13px; display: none;">Error: You did not select anthing</span><br /><br />\
		<div style="height: 27px;"><div style="float: left;"><button onclick="processIt();">Add to Post</button></div><div style="float: right; position: relative; top: 12px;"><a href="#closewindow" onclick="window.close(); return false;">[Close Window]</a></div>';
        	document.write(buildPage(0, 700, 'Upload Successful!', pcontent, 'windowbg', true));
	}
}

// builds the upload form and positions it inside the posting table, and builds progress popup
if(document.postForm && !location.href.match(/&accept=/) && document.postForm.message){
	document.write('<div id="updiv" style="display: none; position: absolute; top: 0px; left: 0px;" nowrap="nowrap"></div>');
	for(r=0; r<iRow.length; r++){
		if(iRow[r].cells[0].width == "30%" && iRow[r].cells[0].innerHTML.match(/Add Smilies/)){
			var imgRow = iRow[r].cloneNode(true);
			iRow[r].parentNode.insertBefore(imgRow, iRow[r+1]);
			imgRow.cells[0].firstChild.innerHTML = "Upload an Image:";
			imgRow.cells[1].innerHTML = '';
			var tempdiv = imgRow.cells[1].appendChild(document.createElement("div"));
			tempdiv.id = "tempdiv";
			tempdiv.style.height = "20px";
			tempdiv.style.width = "20px";
			var updiv = document.getElementById("updiv");
			updiv.style.top = getY(tempdiv);
			updiv.style.left = getX(tempdiv);
			updiv.style.display = '';
			resetForm();
			var resizer = setInterval('var tempdiv = document.getElementById("tempdiv"); updiv.style.top = getY(tempdiv); updiv.style.left = getX(tempdiv);', 10);
			setTimeout('clearInterval(resizer);', 10000);
			if(window.onresize){
				window.onresize = function(){
					var tempdiv = document.getElementById("tempdiv");
					updiv.style.top = getY(tempdiv);
					updiv.style.left = getX(tempdiv);
				}
			}
			break;
		}
	}
}