//Preload hover images
image1 = new Image(220,136);
image1.src= '/images/wtf_hover.png';
image2 = new Image(218,180);
image2.src= '/images/ftw_hover.png';
image2 = new Image(132,130);
image2.src= '/images/suggest_hover.png';

//My ajax functions
function ajax_start()
{
		var XMLHttpRequestObject = false; 
		if (window.XMLHttpRequest)
		{
			XMLHttpRequestObject = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			XMLHttpRequestObject = new ActiveXObject('Microsoft.XMLHTTP');
		}	
		return XMLHttpRequestObject;
}
//First arg is the ?do= for ajax.php, 2nd are the posted args, 3rd is the div it updates, 4th meh, 5th is the callback function
//Not insanely pretty but it works
function ajax_yo(page, args, div_id, XMLHttpRequestObject, callback, callback_args, args2)
{
	if(XMLHttpRequestObject)
	{
		var params = args;

		XMLHttpRequestObject.open('POST', '/ajax.php?do='+page, true); 
		XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		XMLHttpRequestObject.onreadystatechange = function() 
			{ 
				if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
				{ 
					if(callback)
						callback(XMLHttpRequestObject.responseText, callback_args, args2);
					else
						document.getElementById(div_id).innerHTML = XMLHttpRequestObject.responseText;

				} 
			} 
		XMLHttpRequestObject.send(params); 
	}
}
//Hide the upper bar
function hide_info()
{
	if(document.getElementById('info_box'))
	{
		//Hide after 6 seconds
		setTimeout("$('#info_box').hide('blind', {}, 1000);", 6000);
	}
}
function show_page(page, title)
{
	XMLHttpRequestObject = ajax_start();
	ajax_yo(page, '', 'contactArea', XMLHttpRequestObject);
	
	//Center based on the page id
	centerPopup(false, page);
	var content = "<img src='/images/small_load.gif' alt='' /> Loading...";
	loadPopup(title, content);	
}

function suggest_pic()
{
	XMLHttpRequestObject = ajax_start();
	ajax_yo('suggest_pic', '&url='+encodeURIComponent(document.getElementById('pic').value), 'contactArea', XMLHttpRequestObject);	
}

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup without jQuery magic!
function loadPopup(title, content, count){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$('#backgroundPopup').fadeIn("slow");
		$('#popupContact').fadeIn("slow");
		document.getElementById('popup_title').style.display = 'block';
		document.getElementById('contactArea').style.display = 'block';
		replace_html(document.getElementById('popup_title'), title);
		replace_html(document.getElementById('contactArea'), content);
		popupStatus = 1;
	}
}
//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$('#backgroundPopup').fadeOut("slow");
		$('#popupContact').fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(id, id2){
	//request data for centering

	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	
	if(id)
		var pos = findPos(document.getElementById(id+'_assign'))
	else if(id2)
		var pos = findPos(document.getElementById(id2))
	else
		var pos = 170;
	pos = pos-150;
	if(pos<0)
		pos = windowHeight/2-popupHeight/2;
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": pos,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});
function findPos(obj) 
{
	var curtop = 0;
	if(obj)
	{
		if (obj.offsetParent) 
		{
			do {
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
	}
return curtop;
}
function findPosX(obj) 
{
	var curtop = 0;
	if (obj.offsetParent) 
	{
		do {
			curtop += obj.offsetLeft;
		} while (obj = obj.offsetParent);
	}
return curtop;
}
function replace_html(el, html) {
	if( el ) {
                var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
                //var oldEl = el;
                var newEl = document.createElement(oldEl.nodeName);

                // Preserve any properties we care about (id and class in this example)
                newEl.id = oldEl.id;
                newEl.className = oldEl.className;

                //set the new HTML and insert back into the DOM
                newEl.innerHTML = html;
                if(oldEl.parentNode)
        	        oldEl.parentNode.replaceChild(newEl, oldEl);
                else
		        oldEl.innerHTML = html;

                //return a reference to the new element in case we need it
                return newEl;
	}
};
