/* CONFIG FOR JS */
var _path = '/';

function isset(varname)
{
	if(typeof( window[ varname ] ) != "undefined") return true;
	else return false;
}

function sure(msg)
{
	if (msg == undefined)
	{
		var msg = 'Are you sure?';
	}

	var ask = confirm(msg);
	if (ask == true)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function cp_imadd()
{
	var count = $(".ims").length;

	if ((count+1) > 5)
	{
		alert('You can only add up to 5 different type of IM Screen Names');
		return false;
	}

	var height = $("#im_opt").children("strong").css("height");
	var height2 = height.replace(/px/,'');

	var newheight = parseInt(height2) + 20 +"px";

	$("#im_opt").children("strong").css("height",newheight);

	var next = count+1;

	$(".ims:last").after("<span class=\"ims\">\n<input type=\"text\" name=\"im["+next+"]\" class=\"inputtext\" style=\"width:120px;\" />\n<select name=\"im_prog["+next+"]\" id=\"im_prog_"+next+"\"><option value=\"aim\">AIM</option><option value=\"skype\">Skype</option><option value=\"msn\">MSN</option><option value=\"ym\">Yahoo!</option><option value=\"gtalk\">gTalk</option></select>\n</span>");

	$(".ims:last").hide();
	$(".ims:last").slideDown('fast');

	return false;
}

function useropt()
{
	$("#useropt").toggle();
}

function changebg(what,to)
{
	switch (to)
	{
		case 'yes' :	var color = '#F2FFE4 url(\'/images/sign_yes.png\') no-repeat 95% 50%'; break;
		case 'no':		var color = '#FFF2F2 url(\'/images/sign_no.png\') no-repeat 95% 50%'; break;
		case 'def':		var color = '#F8FAFD'; break;
	}
	
	//$('#'+what).parent().css("background-color",color);
	$('#'+what).parent().css("background",color);
	$('#'+what).focus();
	
	if ( to == 'no')
	{
		$("#"+what).parent().find("small").css("color","red");
		$("#"+what).parent().find("span").css("color","red");
	}
	else
	{
		$("#"+what).parent().find("small").css("color","#9e9e9e");
		$("#"+what).parent().find("span").css("color","#9e9e9e");
	}
}

function locksubmit(what)
{
	if ( what == 1 )
	{
		$("#submitbutton").attr("disabled","disabled");
	}
	else
	{
		$("#submitbutton").removeAttr("disabled");
	}
}

/* ON READY */
$(document).ready(function(){
	// nothing yet
});

function favcomment(type,id)
{
	alert('Under construction ');
	return false;
}

function rateComment(id,rating,where)
{
	var html = '';
	
	var html = $.ajax({
					   type: "GET",
					   url: '/rank.comments.php?id='+id+'&r='+rating,
					   async: false,
					   global: true
					 }).responseText;
	

	var check = html.search(/span/);

	//if no errors
	if ( check != -1 )
	{
		$('#'+where).parent().find('.comments_score').html(html);
				
		return false;
	}
	else
	{
		$('#'+where).html(html);
	}

	return false;
}

function commentsPage(page,pid,order)
{
	$("#commentslist ul").html('<li><img src="/images/ajax_loading.gif" alt="loading" style="vertical-align:bottom"/> Loading..</li>');
	
	$("#order").val(order);

	if ( $("#order").val() != '' )
	{
		order = $("#order").val();
	}

	var type = $("#type").val();

	var html = '';
	var thispage;
	var order;

	var html = $.ajax({
					   type: "GET",
					   url: '/page.comments.php?id='+pid+'&page='+page+'&order='+order+'&type='+type,
					   async: false,
					   global: true
					 }).responseText;
	
	$(".pagination a").removeClass("current");
	
	$(".pagination a").each(function(){
		thispage = parseInt( $(this).html() );

		if ( thispage == page )
		{
			$(this).addClass('current');
		}
	});


	$("#commentslist ul").html(html);

	return false;
}

function comments_order(pid)
{
	commentsPage(1,pid,$("#comments_order").val());
}

function addfriend(id)
{
	alert('#');
	return false;
}

function addfav(id)
{
	var data = 'ajax=1&add='+id;

	var html = '';
	var html = $.ajax({
					   type: "GET",
					   url: _path + "myfavorites.php",
					   data: data,
					   async: false
					 }).responseText;
	
	
	if (html != 'ok')
	{
		alert(html);
	}
	else
	{
		alert('You have added this game into your favorites');
	}

	return false;
}

function addcomment() 
{
	if ( $("#comment_post").val() == '' )
	{
		alert('You must write something first.');
		return false;
	}

	if ( $("#comment_post").val().length < 5 )
	{
		alert('Your reply is too short! It must be more than 5 characters');
		return false;
	}
	
	//serialize data
	 var data = $("#addcomment_form > input, textarea").serialize();
	
	 var key = $("#key").val();
	 
	 data = data+'&key='+key;

	 var html = '';
	 var html = $.ajax({
					   type: "POST",
					   url: "/comment.php?ajax=1",
					   data: data,
					   async: false
					 }).responseText;
	
	var check = html.search(/Your reply has been added/);

	if ( check == -1 )
	{
		alert(html);
	}
	else
	{
	
		var pid = $("#comment_pid").val();

		commentsPage(1,pid,'date');
		
		$("#comment_post").val("");

		setTimeout("$('.notes').fadeOut('slow');",2500);

	}

	return false;
}

function removewallpost(me,id)
{
	if ( sure() == false )
	{
		return false;
	}

	var data = 'id='+id;
	
	var html = '';
	var html = $.ajax({
					   type: "GET",
					   url: "comments.remove.php",
					   data: data,
					   async: false
					 }).responseText;

	if (html != 'ok')
	{
		alert(html);
	}
	else
	{
		$(me).parent().parent().parent().find(".comment-content").html("<em class=\"deleted\">Comments Removed</em>");
	}

	return false;
}

oauth = {
	facebook: function() {
	
		oauth.win = window.open('/oauth.facebook.connect.php','oauthwindow','menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,width=800,height=400');

		if (!oauth.win) return true;

		return false;
	},
	twitter: function() {
		
		oauth.win = window.open('/twit.login.php','oauthwindow','menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,width=800,height=400');		

		if (!oauth.win) return true;

		return false;
	}
};

