var rollover_timer = null;

var full_path = "http://www.arabiamania.net";

/** Obtains a reference to an element on the current page **/
function get(field) {
	var element = element = document.getElementById(field);
	if(element) {
		return element;
	}
	
	return null;
}

/** Gets the X coordinate of an element **/
function get_x(id) {
	
	var obj = get(id);
	var x = 0;
	
	if (obj.offsetParent) {
		do {
			x += obj.offsetLeft;
		} while (obj = obj.offsetParent);
	}
	
	return x;
}

/** Gets the Y coordinate of an element **/
function get_y(id) {
	
	var obj = get(id);
	var y = 0;
	
	if (obj.offsetParent) {
		do {
			y += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	return y;
}

/** Gets the width of an element **/
function get_width(id) {

	var element = get(id);
	return element.clientWidth;
}

/** Gets the height of an element **/
function get_height(id) {

	var element = get(id);
	return element.clientHeight;
}


/** Reads a cookie from the client machine **/
function read_cookie(name) {
	
	var name_eq = name + "=";
	
	var cookies = document.cookie.split(";");
	
	for(i = 0; i < cookies.length; i++) {
		var cookie = cookies[i];
		while (cookie.charAt(0) == " ") {
			cookie = cookie.substring(1, cookie.length);
		}
		if (cookie.indexOf(name_eq) == 0) {
			return cookie.substring(name_eq.length, cookie.length);
		}
	}
	return null;
}

/** Creates a cookie with the specified value, valid for 12 hours **/
function create_cookie(name, value) {

	var cookie_lifetime = 1000 * 60 * 60 * 12;
	var date = new Date();
	
	date.setTime(date.getTime() + cookie_lifetime);
	var expires = "; expires=" + date.toGMTString();
	
	document.cookie = name + "=" + value + expires + "; path=/";
}

/** Shows an element **/
function show(id) {
	get(id).style.display = "block";
}

/** Hides an element **/
function hide(id) {
	get(id).style.display = "none";
}

/** Gets the height of an element **/
function get_height(id) {

	var element = get(id);
	return element.clientHeight;
}

/** Gets the width of an element **/
function get_width(id) {

	var element = get(id);
	return element.clientWidth;
}

/** Sets the width attribute to the actual width of an element (or its parent) **/
function set_exact_width(id, parent) {
	
	if(parent) {
		get(id).style.width = get_width(parent) + "px";
	} 
	else {
		get(id).style.width = get_width(id) + "px";
	}	
}

/** Sets the height attribute to the actual height of an element (or its parent) **/
function set_exact_height(id, parent) {
	
	if(parent) {
		get(id).style.height = get_height(parent) + "px";
	} 
	else { 
		get(id).style.height = get_height(id) + "px";
	}	
}

/** Gets the X coordinate of an element **/
function get_x(id) {
	
	var obj = get(id);
	var x = 0;
	
	if (obj.offsetParent) {
		do {
			x += obj.offsetLeft;
		} while (obj = obj.offsetParent);
	}
	
	return x;
}

/** Gets the Y coordinate of an element **/
function get_y(id) {
	
	var obj = get(id);
	var y = 0;
	
	if (obj.offsetParent) {
		do {
			y += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	return y;
}

function get_total_width() {
	
	var width = 0;
	var d = document; 
 	
 	if (typeof window.innerWidth != "undefined") { 
  		width = window.innerWidth;  
 	} else { 
  		if (d.documentElement && typeof d.documentElement.clientWidth != "undefined" && d.documentElement.clientWidth != 0) { 
   			width = d.documentElement.clientWidth; 
  		} else { 
   			if (d.body && typeof d.body.clientWidth != "undefined") { 
    			width = d.body.clientWidth; 
   			} 
  		} 
 	} 
 	
 	return width;
}

/** Returns the total height of the browser window **/
function get_total_height() {
	
	var height = 0;
	var d = document; 
 	
 	if (typeof window.innerHeight != "undefined") { 
  		height = window.innerHeight;  
 	} else { 
  		if (d.documentElement && typeof d.documentElement.clientHeight != "undefined" && d.documentElement.clientHeight != 0) { 
   			height = d.documentElement.clientHeight; 
  		} else { 
   			if (d.body && typeof d.body.clientHeight != "undefined") { 
    			height = d.body.clientHeight; 
   			} 
  		} 
 	} 
 	
 	return height;
}

/** Redirects an ifram in the current window to a new location **/
function redirect_iframe(iframe, url) {
	window.frames[iframe].window.location.href = url;
}

function change_parent_title(title) {
	window.parent.document.title = title;
}

function adjust_iframe_height(iframe) {
	get(iframe).style.height = get(iframe).contentWindow.document.body.scrollHeight + "px";
}

var base_path = "";

function start_rollover(id) {
	
	var current = get(id + "_thumb").src;
	var slash_index = current.lastIndexOf("/");
	
	var index = current.substring(slash_index + 1, current.length - 1).substring(0, 1); 
	index = parseInt(index);
	
	if(!isNaN(index)) {
		index++;
		
		if(index > 3) {
			index = 1;
		}
		
		get(id + "_thumb").src = current.substring(0, slash_index + 1) + index + ".jpg";
		rollover_timer = setTimeout("start_rollover(" + id + ");", 800);
	}
}

function stop_rollover() {
	clearTimeout(rollover_timer);
}

function is_logged_in() {
	
	var status = read_cookie("cookie_is_logged_in");
	
	return status == "true";
}

function duration_text(total) {

	var s = "";
	
	var minutes = Math.round(total / 60);
	if(minutes < 10) {
		s += "0";
	}
	s += minutes + ":";
	
	var seconds = total - (minutes * 60);
	if(seconds < 10) {
		s += "0";
	}
	s += seconds;
	window.alert("Total: "+total+" m: "+minutes+" s: "+seconds);	
	return s;
}

function mark_as_dead() {
	window.location.href = "report.php";
}

/** Returns the maximum height of the current window **/
function client_max_height() {
	
	var doc_element_client = document.documentElement ? document.documentElement.clientHeight : 0;
	var doc_element_scroll = document.documentElement ? document.documentElement.scrollHeight : 0;
	
	var doc_element = Math.max(doc_element_client, doc_element_scroll);
	
	var doc_body_client = document.body ? document.body.clientHeight : 0;
	var doc_body_scroll = document.body ? document.body.scrollHeight : 0;
	var doc_body_offset = document.body ? document.body.offsetHeight : 0;
	
	var doc_body = Math.max(doc_body_client, Math.max(doc_body_scroll, doc_body_offset));
	
	var window_inner_height = window.innerHeight ? window.innerHeight : 0;
	
	return Math.max(doc_element, Math.max(doc_body, window_inner_height));
}

/** Returns the maximum width of the current window **/
function client_max_width() {
	
	var doc_element = document.documentElement ? document.documentElement.clientWidth : 0;
	var doc_body = document.body ? document.body.clientWidth : 0;
	var doc_width = document.body ? document.body.offsetWidth : 0;
	var inner_width = window.innerHeight ? window.innerWidth : 0;
	
	return Math.max(doc_element, Math.max(doc_body, Math.max(doc_width, inner_width))) - 10;
}

/** Centers an element on the screen **/
function center(name) {
	
	var scrolledX, scrolledY;
 	
 	if( self.pageYOffset ) {
   		
   		scrolledX = self.pageXOffset;
   		scrolledY = self.pageYOffset;
   		
 	} else if( document.documentElement && document.documentElement.scrollTop ) {
   		
   		scrolledX = document.documentElement.scrollLeft;
   		scrolledY = document.documentElement.scrollTop;
   		
 	} else if( document.body ) {
   		
   		scrolledX = document.body.scrollLeft;
   		scrolledY = document.body.scrollTop;
 	}

 	var centerY, centerY;
 	
 	if( self.innerHeight ) {
   		
   		centerX = self.innerWidth;
   		centerY = self.innerHeight;
   		
 	} else if( document.documentElement && document.documentElement.clientHeight ) {
   		
   		centerX = document.documentElement.clientWidth;
   		centerY = document.documentElement.clientHeight;
   		
 	} else if( document.body ) {
 		
 		centerX = document.body.clientWidth;
   		centerY = document.body.clientHeight;   		
 	}

	var width = get_width(name);
	var height = get_height(name);
	
 	var leftOffset = scrolledX + (centerX - width) / 2;
 	var topOffset = scrolledY + (centerY - height) / 2;

	get(name).style.left = leftOffset + "px";
	get(name).style.top = topOffset + "px";
}

function display_centered_message(name, content, type, mode, timeout) {
		
	
	get(name).className = type;
		
	if(content != "") {
		get(name).innerHTML = "<table border='0' width='100%' height='100%'><tr><td style='vertical-align: middle;'>" + content + "</td></tr></table>";
	}
	
	show(name);
	center(name);
	
	if(mode == "inplay") {
		setTimeout("hide('" + name + "')", timeout);
	}
	
	if(mode == "normal" && timeout > 0) {
		setTimeout("hide_highlighted_message('" + name + "')", timeout);
	} 
}

/** Displays an element and sets the background to near opaque **/
function display_highlighted_message(name, content, type, mode, timeout) {	
	
	get("opaque_background").style.height = client_max_height();
	
	show("opaque_background");
	display_centered_message(name, content, type, mode, timeout);
}

/** Hides the element and sets the background back to normal **/
function hide_highlighted_message(name) {
	
	hide("opaque_background");
	hide(name);
}

/** Opens an inplay window **/
function open_inplay(url) {
	
	display_highlighted_message("inplay_div", "", "");
	redirect_iframe("inplay_iframe", full_path + "/" + url);
}

/** Closes the inplay window **/
function close_inplay() {
	
	hide_highlighted_message("inplay_div");
	redirect_iframe("inplay_iframe", full_path + "/blank.php");
}

function relocate_highlighted_items() {
	center("inplay_div");
	center("message");
}

function display_message(content, timeout, mode) {
	display_highlighted_message("message", content, "statusmessage", mode, timeout);
}

function display_error_message(content, timeout, mode) {
	display_highlighted_message("message", content, "errormessage", mode, timeout);
}