/**
 * Global variables
 */
	var comment_edit = false;
	var request_pending = false;
	
	$(document).ready(function(){
	
		//choose_background();

    	/**
 		 * Drop the top bar down and show additional content
 		 */
	    $("#showmore").click(function () {
	    	$("#showmore_div").slideDown("slow");
	      	if ($("#showmore_div").is(":hidden")) {
	      		alert('should drop');
	        	$("#showmore_div").slideDown("slow");
	      	} else {
	        	$("#showmore_div").hide();
	      	}
	    });
	    
	    
	    /**
 		 * Clears the comment body on enter if the text has not been changed
 	     */
	   	$("#comment_body").focus(function() {
	   		if (comment_edit == false) {
	   			$(this).val("");
	   		}
	   		comment_edit = true;
	   		
	        $(this).css({'font-style' : 'normal', 'color' : '#000'});
	    });
	    
	    $("#comment_body").blur(function() {
	    	if ($(this).val().length <= 0) {
	        	$(this).css({'font-style' : 'italic', 'color' : '#333'});
	        	$(this).val("Enter your thoughts here...");
	        	comment_edit = false;
	        }
	    });
	    
	    $("#comment_body").autoResize({
    		onResize : function() {
        		$(this).css({opacity:0.8});
   			 },
    		animateCallback : function() {
        		$(this).css({opacity:1});
    		},
    		animateDuration : 100,
    		extraSpace : 15
		});
		
		/**
		 * Clears the name and email words in the input fields
		 */
		$("#comment_name").focus(function() {
	   		if ($(this).val() == 'Name') {
	   			$(this).val("");
	   		}
		});
		
		$("#comment_mail").focus(function() {
	   		if ($(this).val() == 'E-mail') {
	   			$(this).val("");
	   		}
		});
		
		/**
		 * When the user places mouse over question mark, show the facebook explanation.
		 */
		$("#fb_help").mouseover(function() {
			$("#fb_tooltip").show();
		});
		
		/**
		 * When the user removes mouse from question mark, hide the facebook explanation.
		 */
		$("#fb_help").mouseout(function() {
			$("#fb_tooltip").hide();
		});
		
		/**
		 * If the facebook object has initialized and there is a session, display log out
		 * Display connect button if not logged in or connected.
		 */
		if (FB.Facebook && FB.Facebook.apiClient.get_session()) 
			init_facebook(true);
		else
			init_facebook(false);

		/**
		 * Set up PayPal option changer
		 */
		$("input[name='print_size']").change(function() {
			$("#os0").attr("value", $("input[name='print_size']:checked").val());
		});
		
		$(".replylink").click(function () {
			$(this).hide();
			var id = $(this).attr('id').replace("replylink","");
			$('#cancellink' + id.toString()).show();
			
			$('#replyto'+id.toString()).html($('#comment_post').html());
		});
		$(".cancellink").click(function () {
			$(this).hide();
			var id = $(this).attr('id').replace("cancellink","");
			$('#replylink' + id.toString()).show();
			$('#replyto'+id.toString()).html("");
		});
		$(".cancellink").hide();

		$('.like').click(function () {
			var id = $(this).attr('id').replace("like","");
			alert(id);
			$(this).removeClass('off').addClass('on');
			$('#dislike' + id.toString()).removeClass('on').addClass('off');
		});
		$('.dislike').click(function () {
			var id = $(this).attr('id').replace("dislike","");
			alert(id);
			$(this).removeClass('off').addClass('on');
			$('#like' + id.toString()).removeClass('on').addClass('off');
		});			
	});
	
	function change_comic(comic_path) {
		$('#comic_img').attr('src',comic_path);
	}
	
	function choose_background(theme) {
		var currentHour = new Date().getHours();
		theme = theme || '';

		if (theme == '')
		{
			if ((currentHour >= 4 && currentHour < 8) || (currentHour >= 16 && currentHour < 20)) { theme = 'dusk'; }
			else if (currentHour >= 8 && currentHour < 16) { theme = 'day'; }
			else { theme = 'night'; }
		}

		if (theme == 'dusk') {
			$('#container').css("background-image", "url('public/dino2/images/header_wide_dusk.png')");
			$('body').css("background-image", "url('public/dino2/images/bg_dusk.png')");
		} else if (theme == 'day') {
			$('#container').css("background-image", "url('public/dino2/images/header_wide_day.png')");
			$('body').css("background-image", "url('public/dino2/images/bg_day.png')");			
		} else { 
			$('#container').css("background-image", "url('public/dino2/images/header_wide_night.png')");
			$('body').css("background-image", "url('public/dino2/images/bg_night.png')");			
		}
	}
	
	
/**
 * This needs documentation, what is this???
 */	
	function init_request() 
	{
		try 
		{
	    	request = new XMLHttpRequest();
	    } 
	    catch (trymicrosoft) 
	    {
	     	try 
	     	{
	       		request = new ActiveXObject("Msxml2.XMLHTTP");
	     	} 
	     	catch (othermicrosoft) 
	     	{
		       	try 
		       	{
		         	request = new ActiveXObject("Microsoft.XMLHTTP");
		       	} 
		       	catch (failed) 
		       	{
		        	request = false;
		       	}  
	     	}
	 	}
	   
	   	return request;
	}
	
	
   	
/**
 * Retrieves the form fields for an item that was added to the shopping cart. Error checks the 
 * fields and then submits the item via HTTP POST.
 */
   	function item_add_to_cart(obj)
   	{
		var product_id = encodeURI(jQuery.trim($('#product_id').val()));
		var qty = 1;
		var options = "";
   		//var web_url = encodeURI(jQuery.trim($('#comment_web').val()));
      	//var type = encodeURI(jQuery.trim(type));
      	var error = "";
      	  
	    //$('#cart_loader').css({'visibility' : 'visible'});
	    
	    $.post("store_custom/add_to_cart", { product_id: product_id, 
												qty: qty,
	    					 	 				options: options,
	    					 	 				ajax: 1 },
  			function(data) {
    			//$('#cart_loader').css({'visibility' : 'hidden'});
	        	//$('#comment_error').hide();

				if (data == "") {
					alert("Sorry, there was a problem adding this item to your cart. Please try again later.");
				} else {
					alert('hey, it worked');
		        }
  		}, "html");

      	return false;
   	}
	
	
/**
 * This needs documentation, not sure what this does
 */
	function switch_admin(admin_url) 
	{
		$('#admin_loader').show();
		request = init_request();	
		if (!request)
	    	alert("Error initializing XMLHttpRequest!");
	    
		request.open("GET", admin_url, true);
		request.onreadystatechange = updateAdminPage;
		request.send(null);
	}
	
	function updateAdminPage() 
	{
		var new_html = ''
		if (request.readyState == 4) 
		{
	       	if (request.status == 200) 
	       	{
	        	new_html = request.responseText;
	        	$('#admin_content').html(new_html);
	        	$('#admin_loader').hide();
			} 
       		else
	         	alert("Status Error: " + request.status);
	     }
   	}
   	
   	function switch_post(post_id) 
	{
		request = init_request();	
		if (!request)
	    	alert("Error initializing XMLHttpRequest!");
	    
		request.open("GET", 'admin/get_post/' + post_id, true);
		request.onreadystatechange = updateEditPost;
		request.send(null);
	}
	
	function updateEditPost() 
	{
		var new_html = ''
		if (request.readyState == 4) 
		{
	       	if (request.status == 200) 
	       	{
	        	new_html = request.responseText;
	        	$('#edit_post').html(new_html);
			} 
       		else
	         	alert("Status Error: " + request.status);
	     }
   	}
   	
/**
 * Retrieves the form fields for a comment submission. Error checks the 
 * fields and then submits the comment via HTTP POST.
 */
   	function comment_add(obj, type)
   	{
   		var comment_name = encodeURI(jQuery.trim($('#comment_name').val()));
      	var comment_email = encodeURI(jQuery.trim($('#comment_mail').val()));
      	var comment_body = encodeURI(jQuery.trim($('#comment_body').val()));
      	var comment_decoded = jQuery.trim($('#comment_body').val());
      	var post_id = encodeURI(jQuery.trim($('#post_id').val()));
      	var pic_url = encodeURI(jQuery.trim($('#comment_pic').val()));
      	var web_url = encodeURI(jQuery.trim($('#comment_web').val()));
      	var type = encodeURI(jQuery.trim(type));
      	var error = "";

      	
      	if (comment_body == "" || comment_edit == false) {
      		error = "Forgot to write something?";
      	}
      	else if (comment_name == "" || comment_name == "Name") {
      		error = "Enter your name, yo.";
      	}
      	else if (comment_email == "" || comment_email == "E-mail") {
      		error = "Enter an e-mail address, yo.";
      	}
      	else if (comment_body.indexOf("href") != -1 || comment_body.indexOf("%5Burl") != -1) {
      		error = "No spam, yo.";
      	}
      	else { }

      	if (error != "") {
      		$('#comment_error').show();
      		$('#comment_error').html(error);
      		return false;
      	}
      	    
      	var comic_img = $("#comic_img").attr('src');
      	if (FB.Facebook && FB.Facebook.apiClient.get_session()) 
      		publish_feed(comment_decoded, comic_img, document.URL);
      		
	    $('#comment_loader').css({'visibility' : 'visible'});

	    $.post("comments/add", { comment_name: comment_name, 
	    					 	 comment_email: comment_email,
	    					 	 comment_body: comment_body,
	    					 	 comment_pic: pic_url,
	    					 	 comment_web: web_url,
	    					 	 post_id: post_id,
	    					 	 type: type,
	    					 	 ajax: 1 },
  			function(data) {
    			$('#comment_loader').css({'visibility' : 'hidden'});
	        	$('#comment_error').hide();

				if (data == "") {
					alert("Sorry, there was a problem adding your comment. Please try again later.");
				} else {
		        	if ($('#comment_count').html() == '0') {
		        		$('#comments_all').html(data);
		        	} else {
		        	   	$('#comments_all').html($('#comments_all').html() + data);
		        	}
		        	$('#comment_count').html(parseInt($('#comment_count').html()) + 1);
		        }
  		}, "html");

      	return false;
   	}
   	
   	
	function place_vote(voteReceived, comic_id)
	{	
		
		if (!request_pending) {
			request_pending = true;
			var remove = show_like(voteReceived);
			
			$.post("comments/set_like", { vote: voteReceived,
			 						  	  comic_id: comic_id,
			 						  	  remove: remove,
										  ajax: 1 },
	  			function(data) {
					if (data == "")
						alert("Sorry, there was a problem with this operation. Please try again later.");
					else
						$("#current-score").html(data);
			    }
	  		, "html");
	  		request_pending = false;
	  	}
      	return false;
	}
   	
/**
 * Get the user preference for the object
 */   	
 	function show_like(like)
	{
		if ($('#vote_up').hasClass("on") && like >= 1) {
			$('#vote_up').removeClass('on');
			$('#vote_up').addClass('off');
			return 1;
		} else if ($('#vote_down').hasClass("on") && like <= 0) {
			$('#vote_down').removeClass('on');
			$('#vote_down').addClass('off');
			return 1;
		}
		
		if (like >= 1) {
			$('#vote_up').addClass('on');
			$('#vote_up').removeClass('off');
			$('#vote_down').addClass('off');
			$('#vote_down').removeClass('on');
		} else {
			$('#vote_up').addClass('off');
			$('#vote_up').removeClass('on');
			$('#vote_down').addClass('on');
			$('#vote_down').removeClass('off');
		} 
		return 0;
	}	

/** 
 * Set the user preference for the object

	function set_like(like, post_id)
	{	
		show_like(like);
		
		$.post("comments/set_like", { like: like,
		 						  	  post_id: post_id,
									  ajax: 1 },
  			function(data) {
				if (data == "")
					alert("Sorry, there was a problem with this operation. Please try again later.");
				
		    }
  		, "html");

      	return false;
	}
	 */
/**
 * Saves the current page as a bookmark to the browser.
 */
	function addBookmark(title, url) 
	{ 
		if (window.sidebar) { 
			window.sidebar.addPanel(title, url,""); 
		} else if( document.all ) { 
			window.external.AddFavorite( url, title); 
		} else if( window.opera && window.print ) { 
			return true; 
		}
	} 

// End of file