var path = "http://www4.bivet.nu/";
var path_to_images = "images/buttons/";
var default_text = "";
var farmer_text = "";
var current_phone_number_matches = "default";
var password_value = "";
var preloaded_images_array = new Array();

/** Preloading images **/
preloadThisImage(path+path_to_images+"down_gris.png");
preloadThisImage(path+path_to_images+"down_hest.png");
preloadThisImage(path+path_to_images+"down_hund_kat.png");
preloadThisImage(path+path_to_images+"down_ko.png");
preloadThisImage(path+path_to_images+"gris.png");
preloadThisImage(path+path_to_images+"hest.png");
preloadThisImage(path+path_to_images+"hund_kat.png");
preloadThisImage(path+path_to_images+"ko.png");
preloadThisImage(path+path_to_images+"over_gris.png");
preloadThisImage(path+path_to_images+"over_hest.png");
preloadThisImage(path+path_to_images+"over_hund_kat.png");
preloadThisImage(path+path_to_images+"over_ko.png");

function preloadThisImage(complete_image_path)
{
	var array_length = preloaded_images_array.length;
	preloaded_images_array[array_length] = new Image();
	preloaded_images_array[array_length].src = complete_image_path;
}

function imageToggle(image, toggle)
{
	if (toggle != '')
	{
		toggle = toggle+'_';
	}
	document.getElementById('button_'+image).style.backgroundImage = 'url(images/buttons/'+toggle+image+'.png)';
}

function myCreateOverlay(type, extra_string)
{
	ajaxRequest("GET", "ajax/login.php?action=getLoginForm&type="+type+(extra_string != undefined ? extra_string : ''), handleMyCreateOverlay);
}

function handleMyCreateOverlay()
{
	var data = JSON.parse(ajax_request_obj.responseText);
	
	var the_form = data.form;
	default_text = data.default_text;
	farmer_text = data.farmer_text;
	
	createVeil(75, true, "#000000", function(){createOverlay(the_form); checkUserAndPass();});
}

function checkPhoneNumber(obj)
{
	return false;
	var password_obj = getElement("password_entry");
	password_value = password_obj.value;
	
	ajaxRequest("GET", "ajax/login.php?phone_number="+obj.value, handleCheckPhoneNumber);
}

function handleCheckPhoneNumber()
{
	return false;
	var response = ajax_request_obj.responseText;
	
	var password_text			= getElement("password_text");
	var password_obj_container 	= getElement("password_container");
	var password_entry_obj		= getElement("password_entry");
	if(response == "practice")
	{
		/** Phone number matches a practice **/
		if(current_phone_number_matches != "default")
		{
			password_text.innerHTML = default_text;
			password_obj_container.innerHTML = '<input type="password" name="password" id="password_entry" value="'+password_value+'" style="width:'+password_entry_obj.style.width+';">';
		}
		
		current_phone_number_matches = "default";
		
	}
	else if(response == "farmer")
	{
		/** Phone number matches a farmer **/
		if(current_phone_number_matches != "farmer")
		{
			password_text.innerHTML = farmer_text;
			password_obj_container.innerHTML = '<input type="text" name="password" id="password_entry" value="'+password_value+'" style="width:'+password_entry_obj.style.width+';">';
		}		
		current_phone_number_matches = "farmer";
	}
	else
	{
		/** Phone number matches nothing **/
		if(current_phone_number_matches != "default")
		{
			password_text.innerHTML = default_text;
			password_obj_container.innerHTML = '<input type="password" name="password" id="password_entry" value="'+password_value+'" style="width:'+password_entry_obj.style.width+';">';
		}		
		
		current_phone_number_matches = "default";
	}
}


/** Detecting browser **/
if(navigator.userAgent.indexOf('Trident') != -1 || navigator.userAgent.indexOf('MSIE') != -1){var browser = "ie";}
else{var browser = "not ie";}
/** And browser version **/
if(navigator.appVersion.indexOf('MSIE 8.0') != -1){var browser_version = 8;}
else if(navigator.appVersion.indexOf('MSIE 7.0') != -1){var browser_version = 7;}
else if(navigator.appVersion.indexOf('MSIE 6.0') != -1){var browser_version = 6;}
else{var browser_version = 'not ie';}

function getXmlHttpRequestObject() 
{
	if (window.XMLHttpRequest) 
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject) 
	{
		return new ActiveXObject('Microsoft.XMLHTTP');
	}
	else 
	{
		alert('Status: Cound not create XmlHttpRequest Object.' +	'Consider upgrading your browser.');
	}
}

function createOverlay(data)
{
	var body_dom = document.getElementsByTagName('body')[0];
	
	/** Creating an overlay **/		
	var overlay_div = document.createElement('div');
	overlay_div.id = 'overlay_div';
	
	/** A few styles **/
	writeOverlayHeader();
	if(browser != 'ie')
	{		
		overlay_div.style.position = "fixed";
	}
	overlay_div.style.zIndex = "999999";
	
	/** Now we append it to the body **/
	body_dom.appendChild(overlay_div);
	
	/** Inserting the data **/
	if(data != undefined)
	{
		overlay_div.innerHTML = data;	
	}

	/** Starting the onresize function **/
	if(browser != 'ie')
	{
		window.onresize = function(){centerOverlay();};
	}
	centerOverlay();
}

function centerOverlay()
{
	if(browser != 'ie')
	{
		var obj = getElement('overlay_div');
		
		/** Updating window dimensions **/
		updateWindowDimensions();
				
		/** Getting objects dimensions **/
		var width = obj.offsetWidth;
		var height = obj.offsetHeight;		
		
		obj.style.left 	= (window_width/2)-(width/2)+"px";
		obj.style.top 	= (window_height/2)-(height/2)+"px";
		
		if(parseInt(obj.style.top) < 0)
		{
			obj.style.top = '0px';
		}
		if(parseInt(obj.style.left) < 0)
		{
			obj.style.left = '0px';
		}
	}
}

var overlay_header_written = false;

function writeOverlayHeader()
{
	if(!overlay_header_written)
	{		
		/** If this browser is IE we write a special header that will make our protective layer work properly **/
		if(browser == "ie")
		{			
			var head_dom = document.getElementsByTagName('head')[0];
			var style_element = document.createElement('style');
			var style_rules = document.createTextNode("#overlay_div{position: absolute; top: expression( ieCssOverlay('top') ); left: expression( ieCssOverlay('left') ); }");
			style_element.type = 'text/css';
			if(style_element.styleSheet){style_element.styleSheet.cssText = style_rules.nodeValue;}
			else{style_element.appendChild(style_rules);}
			head_dom.appendChild(style_element);
		}	
		
		overlay_header_written = true;	
	}	
}

function ieCssOverlay(variable)
{
	var obj = getElement('overlay_div');
			
	/** Getting objects dimensions **/
	var width = obj.offsetWidth;
	var height = obj.offsetHeight;
	
	if(variable == 'top')
	{
		var return_variable = ((document.body.clientHeight/2) - (height/2)) + ( document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) + 'px';
	}
	else
	{
		var return_variable = ((document.body.clientWidth/2) - (width/2)) + ( document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) + 'px';
	}
	
	if(parseInt(return_variable) < 0)
	{
		return_variable = '0px';
	}
	return return_variable;	
}

function closeOverlay(fadeout)
{
	removeElementFromDom("overlay_div");
	removeElementFromDom("overlay_div_at_object");
	var protective_div = getElement('protective_div');
	if(fadeout != undefined && fadeout)
	{		
		var current_fade = 0;
		
		if(browser == 'ie')
		{
			current_fade = protective_div.filters[0].opacity;
		}
		else
		{
			current_fade = protective_div.style.opacity*100;
		}
		
		fadeElement(protective_div, current_fade, 0, 'out', function(){ removeElementFromDom("protective_div"); });
	}
	else
	{		
		removeElementFromDom("protective_div");
	}

	
	if(browser_version == 6)
	{
		showSelects();
	}
	
	
	if(browser != 'ie')
	{
		/** Stopping the onresize function **/
		window.onresize = "";
	}
	else
	{
		if(protective_div != undefined)
		{
			/** Stopping the onscroll function **/
			protective_div.onmousewheel = document.onmousewheel = '';
		}
	}
}

function fadeElement(obj, current_fade, final_fade, direction, function_to_execute_when_done)
{	
	obj = getElement(obj);
	if(direction == undefined)
	{
		direction = 'in';
	}
	
	/** Setting the fade to current_fade **/
	if(browser == 'ie')
	{
		try
		{
			obj.filters[0].opacity = current_fade;	
		}
		catch(e)
		{
			if(typeof function_to_execute_when_done == 'function')
			{
				function_to_execute_when_done();	
			}
			return false;
		}
	}
	else
	{
		obj.style.opacity = current_fade/100;
	}
	
	if(direction == 'in')
	{
		/** Increasing the current_fade by 10% **/
		current_fade += 10;
		
		if(current_fade <= final_fade)
		{
			/** We have some more fading to do **/
			setTimeout(function(){fadeElement(obj, current_fade, final_fade, direction, function_to_execute_when_done)}, 25);
		}
		else if(typeof function_to_execute_when_done == 'function')
		{
			function_to_execute_when_done();
		}
	}
	else
	{
		/** Decreasing the current_fade by 10% **/
		current_fade -= 10;
		
		if(current_fade >= final_fade)
		{
			/** We have some more fading to do **/
			setTimeout(function(){fadeElement(obj, current_fade, final_fade, direction, function_to_execute_when_done)}, 25);
		}
		else if(typeof function_to_execute_when_done == 'function')
		{
			function_to_execute_when_done();
		}
	}
}

function hideSelects()
{
	var select_objects = document.getElementsByTagName("select");	
	for(var i=0;i<select_objects.length;i++)
	{
		select_objects[i].style.display = 'none';
	}
}

function showSelects()
{
	var select_objects = document.getElementsByTagName("select");	
	for(var i=0;i<select_objects.length;i++)
	{
		select_objects[i].style.display = 'inline';
	}
}

function removeElementFromDom(id_or_dom_element)
{
	id_or_dom_element = getElement(id_or_dom_element);
	
	if(id_or_dom_element != undefined)
	{
		id_or_dom_element.parentNode.removeChild(id_or_dom_element);
	}
}

function getElement(id_or_dom_element)
{
	if(typeof(id_or_dom_element) != 'object')
	{
		id_or_dom_element = document.getElementById(id_or_dom_element);
	}
	
	if(typeof(id_or_dom_element) != 'object')
	{
		return false;
	}
	else
	{
		return id_or_dom_element;	
	}
}

var window_height = 0;
var window_width = 0;
var window_scroll_y = 0;
var window_scroll_x = 0;
function updateWindowDimensions()
{	
	if(browser == "ie")
	{
		window_scroll_y = document.body.scrollTop;
		window_scroll_x = document.body.scrollLeft;				
		window_height 	= document.body.clientHeight;
		window_width 	= document.body.clientWidth;
	}
	else
	{
		window_scroll_y = window.pageYOffset;
		window_scroll_x = window.pageXOffset;
		window_width 	= window.innerWidth;
		window_height 	= window.innerHeight;		
	}
}

var document_total_height = "";
var document_total_width = "";

function setDocumentHeightAndWidth()
{
	var body = document.body, html = document.documentElement;
	
	document_total_height = Math.max( body.scrollHeight, body.offsetHeight,  html.clientHeight, html.scrollHeight, html.offsetHeight );
	document_total_width = Math.max( body.scrollWidth, body.offsetWidth,  html.clientWidth, html.scrollWidth, html.offsetWidth );
}

function ajaxRequest(method, url, function_to_call_on_return, send_body)
{
	if(method != undefined)
	{		
		if(send_body == undefined)
		{
			send_body = null;
		}
		
		/** Creating a new object **/
		var ajax_request_object = getXmlHttpRequestObject();
		
		ajax_request_object.open(method, url, true);
		/** Setting the onreadystate function **/
		ajax_request_object.onreadystatechange = function(){ajaxRequestObjectReturn(ajax_request_object, function_to_call_on_return)};
		/** If we're dealing with a post we need to set the headers **/
		if(method.toLowerCase() == 'post')
		{
			ajax_request_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajax_request_object.setRequestHeader("Content-length", send_body.length);
			ajax_request_object.setRequestHeader("Connection", "close");
		}
		/** Submitting the request **/
		ajax_request_object.send(send_body);
	}
}

var ajax_request_data = "";
var ajax_request_obj = null;
function ajaxRequestObjectReturn(request_object, function_to_call)
{
	if (request_object.readyState == 4) 
	{		
		ajax_request_data = request_object.responseText;
		ajax_request_obj = request_object;
		if(typeof function_to_call == 'function')
		{
			function_to_call();
		}
		/** Destroying the object **/
		request_object = null;
	}
}

function createVeil(final_fade, do_fade, color, function_to_execute_when_done)
{
	var body_dom = document.getElementsByTagName('body')[0];

	/** We make sure that the correct styles are set **/
	writeVeilHeader();
	
	/** Updating window dimensions **/
	updateWindowDimensions();
	
	/** We create the protective layer that will make sure that the user cannot click on anything except our overlay **/
	var protective_div = document.createElement('div');
	protective_div.id = 'protective_div';				
	protective_div.className = 'opaque';
	
	/** Styling the protective div **/
	if(browser != "ie")
	{
		protective_div.style.top = "0px";
		protective_div.style.left = "0px";
		protective_div.style.position = "fixed";
		protective_div.style.width = "100%";
	}
				
	protective_div.style.height = "100%";
	
	if(color == undefined)
	{
		color = "#000000";
	}
	protective_div.style.backgroundColor = color;
	protective_div.style.zIndex = "999998";
	protective_div.style.opacity = 0;
	protective_div.style.display = 'inline';
	
	/** Prepending it to the body **/
	body_dom.insertBefore(protective_div, document.body.childNodes[0]);	
	
	if(browser_version == 6)
	{
		hideSelects();
	}
	
	if(final_fade != undefined)
	{
		if(do_fade != undefined && do_fade)
		{
			fadeElement(protective_div, 0, final_fade, 'in', function_to_execute_when_done);
		}
		else
		{
			if(browser == 'ie')
			{
				protective_div.filters[0].opacity = final_fade;
			}
			else
			{
				protective_div.style.opacity = final_fade/100;
			}
			
			if(typeof function_to_execute_when_done == 'function')
			{
				function_to_execute_when_done();
			}
		}
	}
	else
	{
		if(browser == 'ie')
		{
			protective_div.filters[0].opacity = 50;
		}
		else
		{
			protective_div.style.opacity = 0.5;
		}
		
		if(typeof function_to_execute_when_done == 'function')
		{
			function_to_execute_when_done();
		}		
	}
	
	if(browser == 'ie')
	{
		setDocumentHeightAndWidth();
		protective_div.onmousewheel = document.onmousewheel = function(){limitIeScroll(event)};
	}
		
}

function limitIeScroll(event)
{	
	if(event.wheelDelta < 0)
	{
		if(document_total_height < (document.body.scrollTop*1+document.body.clientHeight*1))
		{
			event.returnValue = false;
		}		
	}
}

var veil_header_written = false;

function writeVeilHeader()
{
	if(!veil_header_written)
	{		
		/** If this browser is IE we write a special header that will make our protective layer work properly **/
		if(browser == "ie")
		{			
			var head_dom = document.getElementsByTagName('head')[0];
			var style_element = document.createElement('style');
			var style_rules = document.createTextNode("#protective_div{position: absolute; width: expression( ieCssVeilWidth() ); top: expression( ieCssVeilPositions('top') ); left: expression( ieCssVeilPositions('left') ); } .opaque{filter: alpha(opacity=0);}");
			style_element.type = 'text/css';
			if(style_element.styleSheet){style_element.styleSheet.cssText = style_rules.nodeValue;}
			else{style_element.appendChild(style_rules);}
			head_dom.appendChild(style_element);
		}	
		
		veil_header_written = true;	
	}
}

function ieCssVeilPositions(variable)
{
	if(variable == 'top')
	{
		return document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop + 'px';
	}
	else
	{
		return document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft + 'px';
	}
}

function ieCssVeilWidth()
{
	return document.body.clientWidth+'px';
}

function checkUserAndPass()
{
	if(getElement('username_entry').value != '' || getElement('password_entry').value != '')
	{
		ajaxRequest('GET', 'ajax/login.php?action=checkUserAndPass&user='+getElement('username_entry').value+'&pass='+getElement('password_entry').value, handleCheckUserAndPass);
	}
}

function handleCheckUserAndPass()
{
	if(ajax_request_obj.responseText == 'true')
	{
		getElement('login_box_container').style.top = '85px';
		getElement('initials_container').style.display = '';
	}
	else
	{
		getElement('login_box_container').style.top = '100px';
		getElement('initials_container').style.display = 'none';
	}
}
