function gotopg(url_string, num_pages)
{
	var page = Math.round(document.getElementById('page_num').value);
	
	if(!page || page < 1)
	{
		alert("Please enter a valid page number");
	}
	else if(page > num_pages)
	{
		alert("You have to enter a number between 1 and " + num_pages);
	}
	else
	{
		url = new String(url_string)
		url = url.replace("{P}", page)

		window.location = url;
	}
}


function draw_page_jump(num_pages, url_string)
{
	var jumpdiv = document.getElementById('page_jump');

	if(jumpdiv)
	{
		jumpdiv.innerHTML = '<form action="" method="post" onsubmit="gotopg(\'' + url_string + '\', ' + num_pages + '); return false;">Go to page: <input type="text" name="page_num" id="page_num" class="text-field" /><input type="submit" value="Go" name="submit" class="submit-button negative" /></form>';
	}
}

function popup(popup_url, popup_name, width, height)
{
	var newwin = window.open(popup_url, popup_name, 'width=' + width + ',height=' + height + ',left=' + (screen.width-width)/2 + ',top=' + (screen.height-height)/2 + ',toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0');
	newwin.focus();
	
	void(0);
}


function field_add(field_id, add_value)
{
	if(window.opener)
	{
		var field = window.opener.document.getElementById(field_id);
	}
	else
	{
		var field = document.getElementById(field_id);
	}

	if(field)
	{
		field.value += add_value;
	}
}


function request_init()
{
	req = false;

	try
	{
		req = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			req = false;
		}
	}

	if(!req && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			req = new XMLHttpRequest();
		}
		catch(e)
		{
			req = false;
		}
	}
	
	if(!req && window.createRequest)
	{
		try
		{
			req = window.createRequest();
		}
		catch(e)
		{
			req = false;
		}
	}
}


function request_do(url, req_method, divid, req_value)
{
	if(divid == "tagboard-area")
	{
		document.getElementById(divid).innerHTML = '<img alt="Loading" src="/data/images/loader.gif" />';
	}
	
	request_init();
	
	req.open(req_method, url, true);
	
	if(req_method == "POST")
	{
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send("message=" + req_value);
	}
	
	req.onreadystatechange = function()
	{
		if(req.readyState == 4)
		{
			document.getElementById(divid).innerHTML = req.responseText;
		}
	}
	
	if(req_method != "POST")
	{
		req.send(null);	
	}
}


function image_popup()
{
	for(var i=0; i<document.links.length; i++)
	{
		if(document.links[i].className.indexOf("imgpopup") > -1)
		{
			document.links[i].onclick = function()
			{
				remove_viewer();
				
				previmg = this;
				previmghref = this.href;
				
				image_viewer(this.href);
				this.href = "javascript:void(0);";
			}
		}
	}
}


function image_viewer(imgurl)
{	
	var imgwidth = 1024;
	var imgheight = 768;
	var pagewidth = 970;
	var imgleft = 0;
	
	var ielement = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body
	
	yOffset = (document.all) ? ielement.scrollTop : pageYOffset;

	if(pagewidth > imgwidth)
	{
		imgleft = Math.floor((pagewidth-imgwidth)/2);
	}
	else
	{
		
	}
	
	var imgdiv = document.createElement('div');
	
	maindiv.appendChild(imgdiv);
	imgdiv.id = "imgpopupid";
	imgdiv.style.top = yOffset + "px";
	imgdiv.style.width = "968px";
	imgdiv.style.overflow = "hidden";
	imgdiv.style.margin = "0 0 0 " + imgleft;
	
	imgdiv.className = "imgviewer col3";
	
	imgdiv.innerHTML = '<div><a onclick="remove_viewer();" href="javascript:void(0);">Close image viewer</a></div><iframe id="imgframe" name="loadframe" style="width: 100%;" frameborder="0"></iframe><div id="imageinfo">&nbsp;</div>';
	frames[0].location = '/norule/image.php?img=' + imgurl;
}


function remove_viewer()
{
	if(previmg && previmghref)
	{
		previmg.href = previmghref;
	}
	
	if(document.getElementById('imgpopupid'))
	{
		maindiv.removeChild(document.getElementById('imgpopupid'));
	}
}


function preload_image(image, imgdiv)
{
	var tmpimage = new Image;
	
	imgdiv.innerHTML = '<div><a onclick="remove_viewer();" href="javascript:void(0);">Close image viewer</a></div><span id="loaded_content">Loading...</span><div>&nbsp;</div>';
	
	tmpimage.onload = function()
	{
		document.getElementById('loaded_content').innerHTML ='<iframe style="width: 100%;" frameborder="0" src="http://www.geekstinkbreath.net"></iframe>';
	}
	
	tmpimage.onerror = function()
	{
		document.getElementById('loaded_content').innerHTML = 'This image is deleted.';
	}
	
	tmpimage.src = image;
}


/*var previmg;
var previmghref;
var maindiv = document.getElementById('wrapper');

image_popup()*/;


var screen_width = 0;
var screen_height = 0;

var ielement = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body

function mibba(showcase_data)
{
	var offset_y = (document.all) ? ielement.scrollTop : pageYOffset;

	set_showcase();
	
	var showcase = document.createElement('div');
	
	showcase.setAttribute("id", "showcase");
	
	var offset_y_fix = (screen_height > 20) ? Math.round((screen_height - 220)/2) : 100;

	showcase.style.top	= offset_y + offset_y_fix + "px";
	showcase.style.left	= Math.round((document.body.offsetWidth - 420)/2) + "px";
	
	document.body.appendChild(showcase);
	
	var work_data = new Array();
	
	work_data = showcase_data.split("|");
	
	var tmp_txt = document.createTextNode("Mibba.com");
	var tmp_ele = document.createElement('h2');
	
	tmp_ele.appendChild(tmp_txt);
	showcase.appendChild(tmp_ele);
	
	
	var holder = document.createElement('div');
	
	showcase.appendChild(holder);
	
	
	var title_stories	= false;
	var title_poems		= false;
	
	for(var i=0; i<work_data.length; i+=3)
	{
		if(work_data[i] == 1)
		{
			if(!title_stories)
			{
				title_stories = true;
				
				var tmp_txt = document.createTextNode("Stories");
				var tmp_ele = document.createElement('h3');
				
				tmp_ele.appendChild(tmp_txt);
				holder.appendChild(tmp_ele);
			}
			
			var tmp_txt = document.createTextNode(work_data[i+2]);
			var tmp_ele = document.createElement('a');
			
			tmp_ele.setAttribute("href", "http://www.mibba.com/stories/summary/" + work_data[i+1] + "/");
			
			tmp_ele.appendChild(tmp_txt);
			holder.appendChild(tmp_ele);
		}
		else if(work_data[i] == 2)
		{
			if(!title_poems)
			{
				title_poems = true;
				
				var tmp_txt = document.createTextNode("Poems");
				var tmp_ele = document.createElement('h3');
				
				tmp_ele.appendChild(tmp_txt);
				holder.appendChild(tmp_ele);
			}
			
			var tmp_txt = document.createTextNode(work_data[i+2]);
			var tmp_ele = document.createElement('a');
			
			tmp_ele.setAttribute("href", "http://www.mibba.com/poems/read/" + work_data[i+1] + "/");
			
			tmp_ele.appendChild(tmp_txt);
			holder.appendChild(tmp_ele);
		}
	}
	
	
	var holder_close = document.createElement('div');
	
	holder_close.setAttribute("id", "showcase-close");
	
	var tmp_txt = document.createTextNode("Close");
	var close_link = document.createElement('a');
	
	close_link.setAttribute("href", "javascript:void(0);");
	close_link.setAttribute("onclick", "remove_showcase();");
	
	close_link.appendChild(tmp_txt);
	holder_close.appendChild(close_link);
	
	showcase.appendChild(holder_close);
	
	close_link.onclick = function(){remove_showcase();}
}


function set_showcase()
{
	var offset_y = (document.all) ? ielement.scrollTop : pageYOffset;
	
	var showcase_background = document.createElement('div');
	
	showcase_background.setAttribute("id", "showcase-background");
	showcase_background.setAttribute("onclick", "remove_showcase();");

	document.body.appendChild(showcase_background);
	
	screen_height = showcase_background.offsetHeight;

	showcase_background.style.height = maindiv.offsetHeight + "px";
}


function remove_showcase()
{
	document.body.removeChild(document.getElementById('showcase'));
	document.body.removeChild(document.getElementById('showcase-background'));
}


var llink_current;

function preview_layout(layout)
{
	var llink = document.getElementById('layout-preview-' + layout);
	
	llink.style.paddingRight = "20px";
	
	llink.style.backgroundImage = "url('/data/images/loader.gif')";
	llink.style.backgroundPosition = "center right";
	llink.style.backgroundRepeat = "no-repeat";

	var css_link = document.createElement('link');
	css_link.setAttribute('rel','stylesheet');
	css_link.setAttribute('type','text/css');
	css_link.setAttribute('href','/norule/template.php?tid=' + layout);

	document.getElementsByTagName('head')[0].appendChild(css_link);
	
	setTimeout("remove_loader(" + layout + ")", 1000);
}

function remove_loader(layout)
{
	document.getElementById('layout-preview-' + layout).style.backgroundImage = "none";
}