/* 
JS made for Web 2.0 Logo Creator by hmaster [ http://h-master.net ]
*/

function genSubmit(obj) {
	scrollTo(0,0);
	submitelement = obj.elements['submit'];
	submitelement.value = 'Generating image...';

	submitelement.style.border = '3px double #aaa';
	submitelement.style.background = '#ddd';
	submitelement.style.color = '#000';
	
	loadStatus = document.getElementById("loadingstatus");
	loadStatus.style.display = "block";
	
	closeAll = document.getElementById("closeall");
	closeAll.style.display = "block";
	closeAll.innerHTML = "<a href=\"javascript://\" onclick=\"window.location='index.php'\";\">Close</a>";
	
	return genImage(obj);
}
 var brwsWidth = 0, brwsHeight = 0;

function genImage(obj) {
 
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    brwsWidth = window.innerWidth;
    brwsHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    brwsWidth = document.documentElement.clientWidth;
    brwsHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    brwsWidth = document.body.clientWidth;
    brwsHeight = document.body.clientHeight;
  }
  
	pageOver = document.getElementById("pageoverlay");
	pageOver.style.width = brwsWidth+"px";
	pageOver.style.height = brwsHeight+"px";
	pageOver.style.background = "#4FA3DA";
	pageOver.style.position = "absolute";
	pageOver.style.left = 0;
	pageOver.style.top = 0;
	pageOver.style.opacity = 5/10;
	pageOver.style.filter = 'alpha(opacity=' + 5*10 + ')';
	
	dataid = "outputimage";
    text = obj.elements['text'].value;
	reflection = getCheckedValue(obj.elements['reflection']);
    betatag = getCheckedValue(obj.elements['beta']);
	
return getImage(dataid, text, reflection, betatag);
}

function getImage(dataid, text, reflection, betatag) {
	var http_request = false;
	var dataid;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() { showImage(http_request); };
        http_request.open('GET', "web2img.php?gen=image&text=" + text + "&reflection=" + reflection + "&betatag=" + betatag , true);
        http_request.send(null);
		return false;
}

function showImage(http_request) {

	  if (http_request.readyState == 0) {
		document.getElementById("loadingstatus").innerHTML = "Uninitialized... <img src=\"loading.gif\" alt=\"Loading...\" />";
	  }
	  if (http_request.readyState == 1) {
		document.getElementById("loadingstatus").innerHTML = "Loading... <img src=\"loading.gif\" alt=\"Loading...\" />";
	  }
	  if (http_request.readyState == 2) {
		document.getElementById("loadingstatus").innerHTML = "Loaded... <img src=\"loading.gif\" alt=\"Loading...\" />";
	  }
	  if (http_request.readyState == 3) {
		document.getElementById("loadingstatus").innerHTML = "Interactive... <img src=\"loading.gif\" alt=\"Loading...\" />";
	  }
          if (http_request.readyState == 4) {
			if (http_request.status == 200) {
		        document.getElementById("loadingstatus").innerHTML = "Generated Image <img src=\"http://msig.info/emoticons/1.png\" alt=\"Smiley :)\" />";
				
				document.getElementById("hiddenimage").innerHTML = '<img src="' + http_request.responseText + '" alt="Generated Image" />';
				Owidth = document.images[2].width;
				Oheight = document.images[2].height;
				
				gimg = new Image(Owidth, Oheight);
				gimg.src = http_request.responseText;
				
				if(Owidth < 500) { Owidth = 500; } else { Owidth = Owidth + 100; }
				Oheight = Oheight + 450;
				document.getElementById(dataid).style.width = Owidth+"px";
				document.getElementById(dataid).style.height = Oheight+"px";
				document.getElementById(dataid).style.border = "3px double #FF8400";
				document.getElementById(dataid).style.background = "#fff";
				
				document.getElementById(dataid).style.position = 'absolute';
				toppos = Math.round((brwsHeight - (Oheight + 15)) / 2);
				document.getElementById(dataid).style.top = (toppos+15)+'px';
				leftpos = Math.round((brwsWidth - Owidth) / 2);
				document.getElementById(dataid).style.left = leftpos+"px";
				
				document.getElementById("loadingstatus").style.left = leftpos+"px";
				document.getElementById("loadingstatus").style.top = toppos-10+'px';
				document.getElementById("closeall").style.top = toppos-7+'px';
				document.getElementById("closeall").style.left = (leftpos + Owidth)-40+"px";
				
				
		        document.getElementById(dataid).innerHTML = '<img src="' + http_request.responseText + '" alt="Generated Image" /><br /><br />\n';
		        document.getElementById(dataid).innerHTML += '<p style="text-align:center">Image Link</p>\n';
				document.getElementById(dataid).innerHTML += '<textarea onClick="this.select()">' + http_request.responseText + '</textarea><br /><br />\n';
		        document.getElementById(dataid).innerHTML += '<p style="text-align:center">HTML Code</p>\n';
				document.getElementById(dataid).innerHTML += '<textarea onClick="this.select()">&lt;img src="' + http_request.responseText + '" alt="Generated Image" /&gt;</textarea><br /><br />\n';
		        document.getElementById(dataid).innerHTML += '<p style="text-align:center">BB Code</p>\n';
				document.getElementById(dataid).innerHTML += '<textarea onClick="this.select()">[img]' + http_request.responseText + '[/img]</textarea><br /><br />\n';
				
            } else if (http_request.status == 404) {
				document.getElementById("loadingstatus").innerHTML = "404 Error could not complete request...";
			} else {
                alert('There was a problem with the request.');
            }
        }
	return false;
}

function loadContent(){
	var str = document.location.href;
	var strsp = str.split("#");
	if(typeof strsp[1] != "undefined") {
	getContents(strsp[1]);
	 } else {
	getContents('home');
	}
}

function getContents(page) {
	var http_request = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() { showContents(page, http_request); };
        http_request.open('GET', page+".php" , true);
        http_request.send(null);
		return false;
}

function showContents(page, http_request) {
	contentid = "container";
	document.getElementById("loadingstatus").style.display = "block";
	  if (http_request.readyState == 0) {
		document.getElementById("loadingstatus").innerHTML = "Uninitialized... <img src=\"loading.gif\" alt=\"Loading...\" />";
	  }
	  if (http_request.readyState == 1) {
		document.getElementById("loadingstatus").innerHTML = "Loading... <img src=\"loading.gif\" alt=\"Loading...\" />";
	  }
	  if (http_request.readyState == 2) {
		document.getElementById("loadingstatus").innerHTML = "Loaded... <img src=\"loading.gif\" alt=\"Loading...\" />";
	  }
	  if (http_request.readyState == 3) {
		document.getElementById("loadingstatus").innerHTML = "Interactive... <img src=\"loading.gif\" alt=\"Loading...\" />";
	  }
          if (http_request.readyState == 4) {
			if (http_request.status == 200) {
		        document.getElementById("loadingstatus").innerHTML = "Ready...";
		        document.getElementById(contentid).innerHTML =  http_request.responseText;
		        
            } else if (http_request.status == 404) {
				document.getElementById("loadingstatus").innerHTML = "404 Error could not complete request...";
			} else {
                alert('There was a problem with the request.');
            }
        }
	if(document.getElementById("loadingstatus").innerHTML == "Ready...")  {document.getElementById("loadingstatus").style.display = 'none'; }
	if(page == "clock"){ StartClock(); } else { KillClock(); }
	return false;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}