// when page loads succesfully
$(document).ready(function(){
	//show all the comments
	$("#comment_read").hide();
	$("#no_comments_yet").hide();
	$("#comment_help").show();
	
	var emptyBox = true;
	var active_paragraph = readCookie(cookie_name());
	var active_paragraph;

	var x_mouse;
	var y_mouse;
	
	if(/* get_hash() */ return_comment()){
	 active_paragraph = get_hash();
	}
	
	
	
	
	if(active_paragraph){
	 	 emptyBox = true;
	 	 id = active_paragraph;
		 $("#comment_help").hide();
		 active_paragraph = (active_paragraph > 0) ? " paragraph " + active_paragraph : "whole page"
		 $("#active_paragraph").text(active_paragraph);
 
		 //alert(id);
		 if($.browser.mozilla) { 
			 $("#comment_read").fadeIn("fast");
		 }
		 else{
			 $("#comment_read").show();
		 }
		 
		 $(".content").each(function(i){
			this.style.background = '#FFF';
		 });
		 
		 if($('.content')[id].style.background == 'rbg(255,255,255) none repeat scroll 0% 0%' ){
			$('.content')[id].style.background = '#FFF';
			$("#comment_contentIndex").val(0);
			$("#comment_help").show();
			
		 }
		 else {
			$('.content')[id].style.background = '#D1D5D9';
			$("#comment_contentIndex").val(id);
			//emptyBox = false;
			
 
			$(".cp_commentBody").each(function(i){			 
				if(this.id == id){
				 //alert('sdf');
				 emptyBox = false;
				 this.style.display = 'block';
				}
				else{
				 this.style.display = 'none';
				}			 
			});
		 }
		 
		$("#no_comments_yet").hide();
		 
		 //alert(emptyBox);
		 if(emptyBox){
			$("#no_comments_yet").show();
		 }
		 else{
			$("#no_comments_yet").hide();
		 }
	}
	
	
	/**
	 *
	 */
	 /*
	function request_comments(){
		$.ajax({
		 type: "GET",
		 ifModified: true, 
		 timeout: 5000, 
		 url: "../../../comments/feed",
		 error: function(msg){
			 //alert('error:' + msg);
		 },
		 success: function(msg){
			 hash = hex_md5(msg);
			 //alert(hash);
			 //alert(readCookie('comments_hash'));
			 if(hash == readCookie('comments_hash')){
				 createCookie('comments_hash',hash,1);
				 $("#newcomment").text('');			 
		 	 }
			 else{
				 $("#newcomment").text('new comment');			 
			 }
		 } 
		});
	}
	*/
	
	
	/*setInterval(request_comments, 6000);*/

	// Update chat when the form is used:
	/*
	$('form').submit(function(){
		element = $('input[@name=input]').get(0);
		mymessage   = element.value;
		myname = $('input[@name=name]').get(0).value;
		$.post('jq_chat.php', {message: mymessage, name: myname});
		element.value = '';
		return false;
	});
	*/
	//updateChat();


	 
	/**
	 * when they double click the read button.. i am ashamed i did this (copy over the code from above)
	 * but i could not figure out how to disable double click without causing serious problems..
	 * and how does jquery make functions? i can't figure that out either. i'll fix this in the future.
	 * i promise :)
	 */
	
	$(".paragraph_read").dblclick(function(){

	 
	})	


	/**
	 * when they click the read button
	 */
	
	$(".paragraph_read").click(function(){
	 	reRoot(); 
	 	var id = parseInt($(this).attr('id'));
		emptyBox = true;
		 
		
	 	if(document.getElementById('comment_contentIndex').value == id){
		
		 $("#comment_read").hide();
		 
		 if($.browser.mozilla) { 
			 $("#comment_help").fadeIn("fast");
		 }
		 else{
			 $("#comment_help").show();
		 }
		 
		 $(".content").each(function(i){
			this.style.background = '#fff';
		 });
		 $("#comment_contentIndex").val(-1);
		 eraseCookie(cookie_name());
		 //alert(readCookie(cookie_name()));
		}
		
		else {
		 $("#comment_help").hide();
 
		 if($.browser.mozilla) { 
			 $("#comment_read").fadeIn("fast");
		 }
		 else{
			 $("#comment_read").show();
		 }
		 
		 $(".content").each(function(i){
			this.style.background = '#fff';
		 });
		 
		 if($('.content')[id].style.background == 'rbg(255,255,255) none repeat scroll 0% 0%' ){
			$('.content')[id].style.background = '#FFF';
			$("#comment_contentIndex").val(0);
			$("#comment_help").show();

			
		 }
		 else {
			$('.content')[id].style.background = '#D1D5D9';
			$("#comment_contentIndex").val(id);
 
			$(".cp_commentBody").each(function(i){
			 	//emptyBox = true;
			 
				if(this.id == id){
				 emptyBox = false;
				 this.style.display = 'block';
				}
				else{
				 this.style.display = 'none';
				}			 
			});
		 }
	
		 createCookie(cookie_name(),id,7);	
		 active_paragraph = readCookie(cookie_name());
		 active_paragraph = (active_paragraph > 0) ? " paragraph " + active_paragraph : "whole page"
		 $("#active_paragraph").text(active_paragraph);
		 
		}
		
		//createCookie('page', , 7) {
		
		if(emptyBox){
		 $("#no_comments_yet").show();
		}
		else{
		 $("#no_comments_yet").hide();
		}
		
		
	});
	

	
	 


	/**
	 * 
	 */
	$("#page_comments_action").click(function(){		
		//$("#comment_write").hide();
		$("#comment_reply").hide();
		if($.browser.mozilla) { 
			$("#comment_read").fadeIn("fast");
		}
		else{
			$("#comment_read").show();
		}
		 $(".cp_commentBody").each(function(i){			 
			 if(this.id == "0"){
				 this.style.display = 'block';
			 }
			 else{
				 this.style.display = 'none';
			 }			 
		 });

	});


	/**
	 * 
	 */
	$("#all_comments_action").click(function(){		
		//$("#comment_write").hide();
		$("#comment_reply").hide();
		if($.browser.mozilla) { 
			$("#comment_read").fadeIn("fast");
		}
		else{
			$("#comment_read").show();
		}

		$(".content").each(function(i){
		 this.style.background = '#FFF';
		});

		 $(".cp_commentBody").each(function(i){			 
				 this.style.display = 'block';
		 });

	});

	

	/**
	 * 
	 */
	$("#searchbox").focus(function(){
		$("#searchbox").val("");
	});



	/**
	 * 
	 */
	 
	 /*
	$("#resize_comment_area").mousedown(function(){
	 	y_mouse = 0;
	 	y_mouse = y_mouse_position();
	});

	$("#resize_comment_area").mousemove(function(){
	 	var resize_by = y_mouse_position() - y_mouse;
		alert(resize by);
	}
	*/
	
	/**
	 * 
	 */
	$(".cp_goToText").click(function(fn){		
	 	$(".content").each(function(i){
		 this.style.background = '#fff';
		});	 
		var id = parseInt($(this).attr('id'));		
		$('.content')[id].style.background = '#D1D5D9';
	});


	/**
	 * 
	 */
	$(".cp_replyLink").click(function(){
		//$("#comment_write").hide();
		// $("#comment_read").hide();
		/*
		if($.browser.mozilla) { 
			$("#comment_reply").fadeIn("fast");
		}
		else{
			$("#comment_reply").show();
		}
		*/
	});
	

	/**
	 * 
	 */
	$(".cp_closeReplyBox").click(function(){
		//$("#comment_write").hide();
		$("#comment_reply").hide();
		$("#comment_read").fadeIn("fast");
	});


});


// Chat must also be updated when nothing is happening:
// We have to provide a random number or else IE will not refresh properly:
function updateChat() {
//	num = Math.random();
	//$("div #cp_comments").load('http://www.nailchipper.com/projects/commentpress/wp-content/themes/commentpress/new_comment.php') ;   
	//setTimeout('updateChat()', 1000);       
}

function show_reply(id){
}

function cancel_reply(id){
 
}

function get_path(){
 return window.location.pathname.toString().substr(0, (window.location.pathname.toString().length - 1) );
}

function get_hash(){
 	//alert(window.location.hash.toString());
 if(window.location.hash.toString().length && window.location.hash.toString().indexOf("#comment") && window.location.hash.toString() != "#respond")
	return window.location.hash.toString().substr(1);
 else
	return false;
 //else if(window.location.hash.toString().indexOf("#comments-"))
//	return window.location.hash.toString().substr(9);	
}


function return_comment(){
 if(window.location.hash.toString().indexOf("#comment"))
	return true;
 else
	return false;
}


function cookie_name(){
 return 'cp' + get_path().replaceAll("/","-");
}

function x_mouse_position(){

}


function y_mouse_position(){


}


function spin(){
 return true;
}

/*

cp_comments


*/

