// JavaScript Document

var user_id=0;
var edit=0;
var logged=false;
var thepage=0;
var save=false;
var original_text="";
var tab_id=0;

/* COMMENTS */
function SendCommentSignin()
{
	var id_val=$("#id").val();
	var title_val=document.title;
	var comment_val=$("#comment").val();	
	var rating_val=$("#rating").val();
	var custom_url_val=$("#custom_url").val();	
	if(id_val=="" || comment_val=="" || rating_val=="")
	{
		alert("You need to enter your comment");
		return false;
	}
	$("#submitcomment").attr("disabled","disabled");		
	document.body.style.cursor = 'wait';	
	$.post("index.php/comments/savetemp",{id:id_val,title:title_val,comment:comment_val,rating:rating_val,custom_url:custom_url_val},function(data){
		document.body.style.cursor = 'default';			
		if(data!="true")
			$("#submitcomment").attr("disabled","");																																																	
		if(data=="true")
			$.prettyPhoto.open('#sign-in','','');	
		else if(data=="false")
			alert("Unexpected error");
		else 
			alert(data+"\r\nTry again please");
	},"html");
}

function SendComment()
{
	var id_val=$("#id").val();
	var title_val=document.title;	
	var comment_val=$("#comment").val();	
	var rating_val=$("#rating").val();
	var custom_url_val=$("#custom_url").val();		
	if(id_val=="" || comment_val=="" || rating_val=="")
	{
		alert("You need to enter your comment");
		return false;
	}	
	$("#submitcomment").attr("disabled","disabled");	
	document.body.style.cursor = 'wait';		
	$.post("index.php/comments/save",{id:id_val,title:title_val,comment:comment_val,rating:rating_val,custom_url:custom_url_val},function(data){
		if(data!="true")
		{
			$("#submitcomment").attr("disabled","");																																																	
			document.body.style.cursor = 'default';				
		}
		if(data=="true")
			location.reload();
		else if(data=="false")
			alert("Unexpected error");
		else 
			alert(data+"\r\nTry again please");
	},"html");
}

function MoreComments(template_val,id_val,page_val)
{
	$("#morecomments").html("Loading...");
	document.body.style.cursor = 'wait';		
	$.post("index.php/comments/morecomments",{template:template_val,id:id_val,page:page_val},function(data){
		document.body.style.cursor = 'default';																																																			
		$("#morecomments").remove();																																																		
		$("#placeholder").append(data);
	},"html");	
}

function Vote(id_val,comment_id_val,type_val)
{
	document.body.style.cursor = 'wait';		
	$.post("index.php/comments/vote",{id:id_val,comment_id:comment_id_val,type:type_val},function(data){
		location.reload();
	},"html");	
	
}
/* COMMENTS */





/* IDENTITY */
function OpenRegister()
{	
	$.prettyPhoto.open('#register','','');		
}

function Register()
{
	var username_val="";
	var password_val=""; 
	var password2_val="";
	var email_val="";
	var fname_val="";
	var lname_val="";
	
	var $inputs=$('input');
	$inputs.each(function (i,el)
	{
		switch(el.id)
		{
			case "register_username":
				username_val=el.value;		
				break;
			case "register_password":
				password_val=el.value;		
				break;
			case "register_password2":
				password2_val=el.value;		
				break;
			case "register_email":
				email_val=el.value;		
				break;
			case "register_fname":
				fname_val=el.value;		
				break;
			case "register_lname":
				lname_val=el.value;		
				break;
		}
	});
	if(username_val=="" || password_val=="" || password2_val=="" || email_val=="" || fname_val=="" || lname_val=="")
	{
		alert("All fields are required");
		return false;
	}	
	//$("#submit_register").attr("disabled","disabled");		
	document.body.style.cursor = 'wait';		
	$.post("index.php/identity/register",{username:username_val,password:password_val,password2:password2_val,email:email_val,fname:fname_val,lname:lname_val},function(data){																																																																																	
		if(data!="true")
		{
			//$("#submit_register").attr("disabled","");		
			document.body.style.cursor = 'default';				
		}
		if(data=="true")
			location.reload();
		else if(data=="false")
			alert("Unexpected error");
		else
			alert(data+"\r\nTry again please");
	},"html");		
}

function Signin()
{
	var email_val="";
	var password_val="";	

	var $inputs=$('input');
	$inputs.each(function (i,el)
	{
		switch(el.id)
		{
			case "signin_email":
				email_val=el.value;		
				break;
			case "signin_password":
				password_val=el.value;		
				break;
		}
	});
	if(email_val=="" || password_val=="")
	{
		alert("All fields are required");
		return false;
	}	
	//$("#submit_signin").attr("disabled","disabled");	
	document.body.style.cursor = 'wait';		
	$.post("index.php/identity/signin",{email:email_val,password:password_val},function(data){
		if(data!="true")
		{
			//$("#submit_signin").attr("disabled","");																																													
			document.body.style.cursor = 'default';	
		}
		if(data=="true")
			location.reload();
		else if(data=="false")
			alert("Unexpected error");
		else
			alert(data+"\r\nTry again please");
	},"html");	
}

function Signout()
{
	$.post("index.php/identity/signout",null,function(data){
			location.reload();	
	},"html");				
}
/* IDENTITY */


function Comment(url)
{
	var comment=$("#lightbox-comment").val();
	var rating=$("#lightbox-rating").val();
	if(comment=="" || rating=="" || url=="")
	{
		alert("All fields are required");
		return false;
	}
	$("#comment").val(comment);
	$("#rating").val(rating);
	$("#custom_url").val(url);
	$("#lightbox-submitcomment").attr("disabled","disabled");
	if(logged)
		SendComment();	
	else
		SendCommentSignin();
}

function Search()
{
	var term=$("#search").val();
	document.location="http://www.epikwiki.com/"+term;
}

function Edit(term_val)
{
	var secondtoolbar="cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,cleanup,|,preview,|,forecolor,backcolor";
	
	if(user_id!="" && edit==1)
	{
		secondtoolbar="cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,fullscreen,|,preview,|,forecolor,backcolor";
	}
	
	save=true;
	$("#show_edit").hide();
	$("#hide_edit").show();
	original_text=$("#Content").html();
	thetext="<form onsubmit='return false;'><textarea id='wiki' style='width:550px; height:500px;'>"+original_text+"</textarea></form>";
	$("#Content").html(thetext);	

	/* START EDITOR */
	$("#wiki").tinymce({
			// Location of TinyMCE script
			script_url : '/js/tiny_mce/tiny_mce.js',
			// General options
			theme : "advanced",
			cleanup: true,
			plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",
			// Theme options
			theme_advanced_buttons1 : "save,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : secondtoolbar,
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : false,
			setup : function(ed)
			{
      	ed.onSaveContent.add(function(ed, o)
				{
					if(save)
					{
						// Output the element name
						var text_val=ed.getContent();
						document.body.style.cursor = 'wait';			
						var url="wiki/save";
						if(tab_id>0)
						{
							url="tabs/save";
							term_val=tab_id;
						}
						$.post(url,{term:term_val,content:text_val,page:thepage},function(data){
							document.body.style.cursor = 'default';	
							if(data=="true")
								location.reload();
							else if(data=="false")
								alert("Error, data not saved");
							else if(data!="true")
								alert("Unexpected error");
						},"html");												
					}
      	});
   		}			
	});
	/* START EDITOR */

}

function HideEdit()
{
	save=false;
	$("#Content").html(original_text);
	$("#hide_edit").hide();	
	$("#show_edit").show();
}

function NewsletterAdd()
{
	var email_val=$("#newsletter_email").val();
	if(email_val=="")
	{
		alert("Email is required");
		return false;
	}
	$.post("index.php/newsletter/add",{email:email_val},function(data){
		if(data=="true")
		{
			$("#newsletter_email").val("");			
			alert("Thank you");
		}
		else
		{
			alert("Error, try again");
		}
	},"html");
}

function EditSignin()
{
	$.prettyPhoto.open('#sign-in','','');		
}

function ShowFullComment(id)
{
	$("#short_"+id).toggle();	
	$("#"+id).toggle();
}

$(document).ready(function()
{
	$("a[rel^='prettyPhoto']").prettyPhoto();	
});



function EnterToSignin(e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13)
		Signin();
}