//var base_url = 'http://www.planetppl.wsg/';
var base_url = 'http://www.planetppl.com/';

// From www.quirksmode.org
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function getCSS() {

	var browser = BrowserDetect.browser;
	var version = BrowserDetect.version;
	var OS = BrowserDetect.OS;

	if (OS == 'Linux') {
		if (browser == 'Firefox') 
			css = '';
		else if (browser == 'Opera') 
			css = 'op.css';
		else if (browser == 'Konqueror') 
			css = '';
		else
			css = '';
	}
	else if (OS == 'Windows') {
		if (browser == 'Explorer') {
			if (version < 7)	
				css = 'ie.css';
			else
				css = 'ie7.css';
		}
		else if (browser == 'Opera')
			css = 'op.css';
		else
			css = '';
	}
	else if (OS == 'Mac') {
		if (browser == 'Safari') 
			css = 'sf.css';
		else if (browser == 'Firefox') 
			css = '';
		else if (browser == 'Opera') 
			css = 'op.css';
		else
			css = '';
	}
	else	
		css = '';

	if (css != '') 
		document.write('<link rel="stylesheet" type="text/css" href="css/'+ css +'" />');
}

var g_id;
var g_section;
var g_vote;
var g_opacity = 1;
var g_opa = 100
var g_count = 0;	

function votesOpacityAdd()
{
	g_opacity = g_opacity + 0.1;
	g_opa = g_opa + 10;
	g_count--;

	document.getElementById(g_section).style.opacity = g_opacity;
	document.getElementById(g_section).style.filter = "alpha(opacity="+g_opa+");";

	if (g_count != 0)	
		setTimeout("votesOpacityAdd()", 100);
}
	
function voteAdd()
{
	if (window.XMLHttpRequest) 
	        request = new XMLHttpRequest();     // Firefox, Safari, ...
	else if (window.ActiveXObject) 
	       request = new ActiveXObject("Microsoft.XMLHTTP");    // Internet Explorer

	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			if (request.status == 200) {
				var res = request.responseText;
				document.getElementById(g_section).innerHTML = res;
				setTimeout("votesOpacityAdd()", 100);
			}
			else
				document.getElementById(g_section).innerHTML = 'Error: ' + request.status;
		}
	};
	
	var url = base_url + "vote_process.php?id=" + g_id + "&vote=" + g_vote;
	request.open('GET', url, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send(null); 
}

function votesOpacityRest()
{
	g_opacity = g_opacity - 0.1;
	g_opa = g_opa - 10;
	g_count++;

	document.getElementById(g_section).style.opacity = g_opacity;
	document.getElementById(g_section).style.filter = "alpha(opacity="+g_opa+");";

	if (g_count == 10) 
		voteAdd();
	else 
		setTimeout('votesOpacityRest()', 100);
}

function doVote(post_id, vote_way)
{
	g_vote = vote_way;
	g_id = post_id;
	g_section = 'votes_' + g_id;
	var do_section = 'do_vote_' + g_id;

	if (vote_way == 'positive')
		var new_vote = '<a class="star" href="#" onClick=\'return false;\'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>';
	else
		var new_vote = '<a class="gray" href="#" onClick=\'return false;\'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>';

	document.getElementById(do_section).innerHTML = new_vote;

	setTimeout('votesOpacityRest()', 100);
}

function logIn() {

	if (window.XMLHttpRequest) 
	        request = new XMLHttpRequest();     // Firefox, Safari, ...
	else if (window.ActiveXObject) 
	       request = new ActiveXObject("Microsoft.XMLHTTP");    // Internet Explorer

	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			if (request.status == 200) {
				var res = request.responseText;
				if (res == '0')
					document.getElementById("login_msg").innerHTML = 'Incorrect Username or Password.';
				else 
					window.location.reload();	
			}
			else
				document.getElementById("login_msg").innerHTML = 'Error: ' + request.status;
		}
	};
	
	var url = base_url + "login_process.php?username=" + document.getElementById("username").value + "&password=" + document.getElementById("password").value;
	request.open('GET', url, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send(null); 
}

function switchSection(section) {
	
	if (document.getElementById(section).style.display == "block") 
		document.getElementById(section).style.display = "none";
	else 
		document.getElementById(section).style.display = "block";
}

function doVoteComment(comment_id, vote, post_id) {

	if (window.XMLHttpRequest) 
	        request = new XMLHttpRequest();     // Firefox, Safari, ...
        else if (window.ActiveXObject) 
               request = new ActiveXObject("Microsoft.XMLHTTP");    // Internet Explorer
       
       	request.onreadystatechange = function() {
       		if (request.readyState == 4) {
       
       			if (request.status == 200) {
       				var res = request.responseText;
				var sec_id = "com_vote_" + comment_id;
       				document.getElementById(sec_id).innerHTML = res;
			}
       			else
       				document.getElementById("text_comment").value = 'Error: ' + request.status;
       		}
       	};
        		
       	var url = base_url + "vote_comment_process.php?id=" + comment_id + "&vote=" + vote + "&post_id=" + post_id;
       	request.open('GET', url, true);
       	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
       	request.send(null); 
}

function newComment(post_id) {

	if (document.getElementById("insert_comment").style.display == "none")
		document.getElementById("insert_comment").style.display = "block";
	document.getElementById("add_comment_legend").innerHTML = 'Add New Comment <a style="font-size: 10px;" href="#" onClick=\'showInsertComment(); return false;\'>show/hide</a>';
	document.getElementById("insert_comment").innerHTML = '<form class="extended"><textarea id="text_comment"></textarea><br /><br /><br /><br /><input type="button" class="submit" alt="Submit" onClick=\'postComment('+ post_id +', 0); return false;\' /><br /><br /></form>';
	document.getElementById("text_comment").value = '';
}

function showInsertComment()
{
	if (document.getElementById("insert_comment").style.display == "none") {
		document.getElementById("insert_comment").style.display = "block";
		document.getElementById("text_comment").value = '';
	}
	else 
		document.getElementById("insert_comment").style.display = "none";
}

function replyComment(post_id, comment_parent_id, username)
{
	document.getElementById("insert_comment").style.display = "block";
	document.getElementById("text_comment").value = '';
	document.getElementById("insert_comment").innerHTML = '<form class="extended"><textarea id="text_comment"></textarea><br /><br /><br /><br /><input type="button" class="submit" alt="Submit" onClick=\'postComment('+post_id+', '+comment_parent_id+'); return false;\' /><br /><br /></form>';
	document.getElementById("add_comment_legend").innerHTML = 'Reply Comment to '+ username +' <a style="font-size: 10px;" href="#" onClick=\'showInsertComment(); return false;\'>show/hide</a>&nbsp;&nbsp;&nbsp;<a style="font-size: 10px;" href="#" onClick=\'newComment('+ post_id + '); return false;\'>new comment</a>';
	document.getElementById("text_comment").focus();	
}

function getComments(post_id)
{
	document.getElementById("add_comment").style.display = "block";		
	document.getElementById("view_comments").style.display = "block";
	
	if (window.XMLHttpRequest) 
	        request = new XMLHttpRequest();     // Firefox, Safari, ...
        else if (window.ActiveXObject) 
               request = new ActiveXObject("Microsoft.XMLHTTP");    // Internet Explorer
       
       	request.onreadystatechange = function() {
       		if (request.readyState == 4) {
       
       			if (request.status == 200) {
       				var res = request.responseText;
       				document.getElementById("view_comments").innerHTML = res;
			}
       			else
       				document.getElementById("view_comments").innerHTML = 'Error: ' + request.status;
       		}
       	};
        		
       	var url = base_url + "get_comments.php?id=" + post_id;
       	request.open('GET', url, true);
       	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
       	request.send(null); 
}

function postComment(post_id, parent_id)
{
	request = false;
      	if (window.XMLHttpRequest) { // Mozilla, Safari,...
        	request = new XMLHttpRequest();
        	if (request.overrideMimeType) {
        		// set type accordingly to anticipated content type
	        	//request.overrideMimeType('text/xml');
        		request.overrideMimeType('text/html');
        	}
        } else if (window.ActiveXObject) { // IE
        	try {
        		request = new ActiveXObject("Msxml2.XMLHTTP");
        	} catch (e) {
        		try {
        			request = new ActiveXObject("Microsoft.XMLHTTP");
        		} catch (e) {}
        	}
        }
      	if (!request) {
       		alert('Cannot create XMLHTTP instance');
       		return false;
       	}
        	
       	request.onreadystatechange = function() {
       		if (request.readyState == 4) {
       			if (request.status == 200) {
       				result = request.responseText;
				getComments(post_id);
				document.getElementById("text_comment").value = '';
       			} else 
       				alert('There was a problem with the request.');
       		}
       	};
        	
       	var parameters = "id=" + post_id + "&parent_id=" + parent_id + "&comment=" + encodeURIComponent(document.getElementById("text_comment").value);
       	var url = base_url + 'insert_comment.php';
       	request.open('POST', url, true);
       	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       	request.setRequestHeader("Content-length", parameters.length);
       	request.setRequestHeader("Connection", "close");
       	request.send(parameters);
}

function reqLogin() {
	document.getElementById("login").style.display = "block";
	document.getElementById("username").focus();
}

function doSearch() {
	var search_term = document.getElementById("search_input").value;
	window.location.href = base_url + 'search/' + search_term;
}
