/*  GET XMLHttpObject */

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

/* ---------------------- */

/* Tagging - Add tag weight */

function AddTagWeight(tag,news_id,id)
{

	xmlHttp = GetXmlHttpObject();
	if(xmlHttp==null)
	{
		return;
	}

	var url = "/s_scripts/tagweight.php";
	url = url + "?id=" + id;
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById("tagreturn_" + news_id).innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

/* ---------------------- */

/* Get opinions in a category - AJAX */

function opinionCatPicked(cat_id)
{

	xmlHttp = GetXmlHttpObject();
	if(xmlHttp==null)
	{
		return;
	}

	var value = document.getElementById(cat_id).value;

	var url = "/s_scripts/adminOpinionAjax.php";
	url = url + "?cat=" + value;
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			if (xmlHttp.responseText != "")
				document.getElementById("submitEdit").style.display = "block";
			else
				document.getElementById("submitEdit").style.display = "none";
			
			document.getElementById("catResponse").innerHTML=xmlHttp.responseText;
			
		}
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

/* ---------------------- */


/* Submit a shout AJAX style */
function SubmitShout(shoutbox_id,message_id, name_id, captcha_id, form_id)
{
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp==null)
	{
		return;
	}
		
	message = document.getElementById(message_id).value;
	name = document.getElementById(name_id).value;
	captcha = document.getElementById(captcha_id).value;
	
	document.getElementById(form_id).innerHTML="Sending data...";

//	var url = "s_content/newsShoutSidebar.php?action=shouted&shout=" + message + "&name=" + name + "&security_code=" + captcha;
	var url = "s_content/newsShoutSidebar.php"
	var params = "action=shouted&shout=" + message + "&name=" + name + "&security_code=" + captcha;

	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById(shoutbox_id).innerHTML=xmlHttp.responseText;
		}
	}
	//xmlHttp.open("GET", url, true);
	//xmlHttp.send(null);
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);

}
/* ---------------------- */


/* Toggles shoutbox visibility (yeah, not really ajax related) */ 

function ToggleShoutbox(div_id, shouts_id)
{

	if (document.getElementById(div_id).innerHTML == "")
	{
		rand = Math.round(Math.random()*1000);

		shoutboxContent = "<form method=\"post\" action=\"javascript:SubmitShout('" + shouts_id + "','shout_mess', 'shout_name', 'security_code', '" + div_id + "')\">";
		shoutboxContent+= "<input class=\"textForm\" type=\"text\" name=\"name\" value=\"Name\" maxlength=\"15\" id=\"shout_name\"  onFocus=\"clearDefault('shout_name', 'Name')\" onBlur=\"setDefault('shout_name', 'Name')\">";
		shoutboxContent+= "<div class=\"spacer2\"></div>";
		shoutboxContent+= "<textarea rows=\"3\" name=\"shout\" class=\"textArea\" cols=\"5\" id=\"shout_mess\" onFocus=\"clearDefault('shout_mess', 'Message')\" onBlur=\"setDefault('shout_mess', 'Message')\">Message</textarea>";
		shoutboxContent+= "<div class=\"spacer2\"></div>";
		shoutboxContent+= "<img src=\"/s_scripts/skull_captcha.php?characters=5&p=" + rand + " alt=\"spam protection, sorry, need images enabled to see it\">";
//		shoutbox+='<img src="skull_captcha.php?characters=5" alt="spam protection, sorry, need images enabled to see it">';

		shoutboxContent+= "	Security Code: ";
		shoutboxContent+= "<input id=\"security_code\" name=\"security_code\" type=\"text\" class=\"textForm\">";
		shoutboxContent+= "<div class=\"spacer2\"></div>";
		shoutboxContent+= "<input class=\"button\" type=\"submit\" name=\"submit\" value=\"- shout -\"></form>";
		document.getElementById(div_id).innerHTML=shoutboxContent;
	}
	else
		document.getElementById(div_id).innerHTML = "";
} 

/* ---------------------- */

/* Set default text if focus is lost (and empty) */ 

function setDefault(form_id, defaultText)
{
	if (document.getElementById(form_id).value == "")
		document.getElementById(form_id).value = defaultText;

} 

/* ---------------------- */

/* Remove default text if focus is aquired */ 

function clearDefault(form_id, defaultText)
{
	if (document.getElementById(form_id).value == defaultText)
		document.getElementById(form_id).value = "";

} 

/* ---------------------- */



/* Changes current style, dynamically as well as setting it in the database */ 

function switchStyleSheet(styleName, id)
{
	curStyle = document.getElementById("currentStyleSheet");
	if (curStyle.href.match("styles_xmas.css"))
	{
		alert("Oh noes, its december, you can't change the theme then!");
		return;
	}

	curStyle.href = '/styles' + styleName + '.css';
	document.getElementById("theme").innerHTML = getThemeName(id);

	worker = GetXmlHttpObject();
	if(worker==null)
	{
		return;
	}

	var url = "http://www.skallen.net/s_scripts/switchStyle.php?style=" + id;
	
	worker.onreadystatechange=function()
	{
		if(worker.readyState==4)
		{
			if ( worker.responseText != "OK!" && worker.responseText != "")
			{
				//alert("error");
				alert(worker.responseText);
			}
		}
	}
	
	worker.open("GET", url, true);
	worker.send(null);
}

/* style sheet names */
function getThemeName(theme)
{	
	theme_month = new Date().getMonth();
	if( theme_month == 11)
		return("'Christmas Theme!'");

	switch (theme)
	{
		case 0: return("'Blue Sky'"); break;
		case 1: return("'Grassy Green'"); break;
		case 2: return("'Orange Sun'"); break;
		case 3: return("'Red Carpet'"); break;
		case 4: return("'<3 Skellefte&aring; AIK'"); break;
		case 5: return("Halloween, booo!"); break;
		case 6: return("'Grey Garden'"); break;
		case 7: return("'Purple Galaxy'"); break;
		default: return("'Some kind of haxx!!'"); break;
	}
}

/* ---------------------- */

/* Handles layout for opinions  */
function renderOpinions(pagenr, limit, sort)
{
	if (sort == "date")
	{
		opArray.sort(sortOpinionByTitle)
		opArray.sort(sortOpinionByDate)
	}
	else if (sort == "title")
		opArray.sort(sortOpinionByTitle)
	else
	{
		opArray.sort(sortOpinionByTitle)
		opArray.sort(sortOpinionByRating);
	}
	var output  = "<div class=\"dateFloatRight\">";
	output += "Sort by:";
	if (sort == "date")
	{
		output += "<div class=\"red\" style=\"margin: 5px; display: inline;\">Date</div>";
		output += "<a href=\"javascript:renderOpinions(" + pagenr + ", " + limit + ", 'rating')\" style=\"margin: 5px\">Rating</a>";
		output += "<a href=\"javascript:renderOpinions(" + pagenr + ", " + limit + ", 'title')\" style=\"margin: 5px\">Title</a>";
	}
	else if (sort == "title")
	{
		output += "<a href=\"javascript:renderOpinions(" + pagenr + ", " + limit + ", 'date')\" style=\"margin: 5px\">Date</a>";
		output += "<a href=\"javascript:renderOpinions(" + pagenr + ", " + limit + ", 'rating')\" style=\"margin: 5px\">Rating</a>";
		output += "<div class=\"red\" style=\"margin: 5px; display: inline;\">Title</div>";		
	}
	else
	{
		output += "<a href=\"javascript:renderOpinions(" + pagenr + ", " + limit + ", 'date')\" style=\"margin: 5px\">Date</a>";
		output += "<div class=\"red\" style=\"margin: 5px; display: inline;\">Rating</div>";
		output += "<a href=\"javascript:renderOpinions(" + pagenr + ", " + limit + ", 'title')\" style=\"margin: 5px\">Title</a>";
	}
	output += "</div>";
	output += "<br><br>";			
	output += getOpinionNavField(pagenr, opArray.length, limit, sort);
	output += "<div class=\"spacer3\"></div>";		
	for (var i = (pagenr-1)*limit; i < pagenr*limit; i++)
	{
		if (i >= opArray.length) // extra check to prevent overflow
			break;
		
		var opText = opArray[i][3];
		// truncate text if too long
		if (opText.length > 200)
		{
			var opBreak = opText.indexOf('.', 170);
			var opAdd = '.';
			if (opBreak < 0 || opBreak > 200)
			{
				opBreak = opText.indexOf(' ', 190);
				if (opText.charAt(opBreak - 1) == ',')
					opBreak--;
				opAdd = '...';
			}
			opText = opText.substr(0, opBreak) + opAdd + ' (<a href="javascript:showFullOpinion('  + i + ', 1)">read all</a>)';
			
		}
		output += "<div class=\"opinion-frame\">\n";
			output += "<div class=\"opinion-image\">\n";
				output += "<img src=\"/s_images/opinion/" + opArray[i][5] + "\">";
				output += "<div class=\"opinion-rating\">" + (opArray[i][4]/10).toFixed(1) + "/10</div>";
			output += "</div>\n";
			output += "<div class=\"opinion-data\">";
				output += "<div class=\"opinion-title\">'" + opArray[i][0] + "'";
				if (opArray[i][1] != "")
					output += " by " + opArray[i][1];
				output += "</div>\n";
				output += "<div class=\"dottedspacer2\"></div>\n";
				output += "<div style=\"height: 50px;\" id=\"" + opArray[i][0] + "_text\">" + opText + "</div>\n";
				output += "<div class=\"opinion-date\">Rated " + (opArray[i][4]/10).toFixed(1) + "/10 on " + opArray[i][2] + " [<a href=\"/opinion/" + opArray[i][6] + "/" + opArray[i][7] + "\">permalink</a>]</div>\n";
			output += "</div>\n";
		output += "</div>\n";
		output += "<div class=\"spacer3\"></div>\n";
	}
	
	output += getOpinionNavField(pagenr, opArray.length, limit, sort);
	
	output +="</div>";
	
	
	document.getElementById('opiniondata').innerHTML = output;
}

function showFullOpinion(fieldID, mode)
{
	if( mode == 1)
	{
		var curField = document.getElementById(opArray[fieldID][0] + "_text");
		curField.innerHTML = opArray[fieldID][3] + " (<a href=\"javascript:showFullOpinion('"  + fieldID + "', '0')\">hide</a>)";
	
		curField.style.height = "auto";
	}
	else
	{
		var curField = document.getElementById(opArray[fieldID][0] + "_text");
		curField.style.height = "50px";
		
		var opText = opArray[fieldID][3];
		var opBreak = opText.indexOf('.', 170);
		var opAdd = '.';
		if (opBreak < 0 || opBreak > 200)
		{
			opBreak = opText.indexOf(' ', 190);
			if (opText.charAt(opBreak - 1) == ',')
				opBreak--;
			opAdd = '...';
		}
		opText = opText.substr(0, opBreak) + opAdd + ' (<a href="javascript:showFullOpinion('  + fieldID + ', 1)">read all</a>)';

		curField.innerHTML = opText;
	}
}
/* ---------------------- */

function getOpinionNavField(curPage, entries, limit, sort)
{
	var t = "<div class=\"opinion-navigation\">";
	
	t += "<div style=\"float: left\">";
	if (curPage <= 1)
		t += "« first";
	else
		t += "<a href=\"javascript:renderOpinions(" + (1) + ", " + limit + ", '" + sort + "')\">« first</a> ";
	t += "</div>";
	
	
	t += "<div style=\"float: right\">";
	if ((curPage)*limit >= entries)
		t += "last »";
	else	
		t += " <a href=\"javascript:renderOpinions(" + (Math.ceil(entries/limit)) + ", " + limit + ", '" + sort + "')\">last »</a>";
	t += "</div>";

	if (curPage <= 1)
		t += "« previous";
	else
		t += "<a href=\"javascript:renderOpinions(" + (curPage - 1) + ", " + limit + ", '" + sort + "')\">« previous</a> ";
	
	var maxPages = Math.ceil(entries/limit);

	t += "<div class=\"bold\"> - page " + curPage + " of " + (maxPages > 0? maxPages:1) + " - </div>";
	
	if ((curPage)*limit >= entries)
		t += "next »";
	else	
		t += " <a href=\"javascript:renderOpinions(" + (curPage + 1) + ", " + limit + ", '" + sort + "')\">next »</a>";
	t += "</div>";
	
	return t;
	
	
}

/* Sort data-array by date */
function sortOpinionByDate(a, b)
{
	return ((a[2] < b[2]) ? 1 : ((a[2] > b[2]) ? -1 : 0));
}
/* ---------------------- */

/* Sort data-array by rating */
function sortOpinionByRating(a, b)
{
	return parseFloat(b[4]) - parseFloat(a[4]);
}
/* ---------------------- */

/* Sort data-array by rating */
function sortOpinionByTitle(a, b)
{
	return ((a[0] < b[0]) ? -1 : ((a[0] > b[0]) ? 1 : 0));
}
/* ---------------------- */

