 $(document).ready(function() {
 	if(comments)loadComments(1,25);
 	if(pollId) pollBargraph();
 	Math.randomCol = function(maxVal,asFloat){
 		var rndCol =new Array('333333','5385ac','333333', '8c2d2b', '6c2621', '90cdcd', 'bdd798');
		var val = Math.random()*maxVal;
		return rndCol[asFloat?val:Math.round(val)];
		}
		
	$("#tags li a").each(function () {
         var col= '#'+Math.randomCol(6);
          $(this).css('color',col);
        } ) 
 
	$("#primary-nav ul li.loaded h4, #archive-monthly h4").click(
		function () {
			var par = $(this).parent();
			if(!par.hasClass("expnd")){
				$(this).next().fadeIn("medium");
				par.addClass("expnd");
				}
			else{
				$(this).next().hide();
				par.removeClass("expnd");
				}
			}
		);

	 $("#popular-posts #viewed h4").addClass("expnd");	
	 $("#popular-posts ul li.loaded").click(
		function () {
			$("#popular-posts ul li.loaded").removeClass("expnd");
			$("#popular-posts ul li.loaded ul").hide();
			$(this).addClass("expnd");
			$("ul",this).fadeIn("medium");
			}
		);
		
	$("div#search-form").click(	
		function () {
			$("#search-form h3").text("");
			$("#search-form input").focus();
			}
		);
		
	 $("#center-col .post-details .button h4").
	 	mouseenter(
			function () {
				$(this).parent().addClass("butt-hov");
				}
		).mouseleave(
			function () {
				$(this).parent().removeClass("butt-hov");
				}
		);
	 
	 $("#center-col .post-details .share h4").click(
		function () {
			/* zindex toggle kludge for ie */ $('.blog-teaser .blog-deets').css("z-index","500");$(this).parent().parent().parent().css("z-index","1000");
			$("#center-col .share").removeClass("butt-on");
			$(this).parent().addClass("butt-on");
			}
		);
		
	 $("#center-col .share-pop h5").click(
		function () {
			$("#center-col .email,#center-col .sociallink").removeClass("expnd");
			$(this).parent().addClass("expnd");
			}
		);

	$("div.close-share").click(
		function () {
			$("#flagbox").fadeOut("medium");
			}
		);	



	$("a.litebox").fancybox({
		'hideOnContentClick': false,
		'frameWidth':640,
		'frameHeight' :370,
		'padding': 6,
		'callbackOnShow': function(){
				if(!$('#fancy_frame').attr("name")) $('#fancy_frame').attr("src", "javascript:;");
				},
	'callbackOnClose': function(){
				if($('#fancy_frame').attr("name")) $('#fancy_frame').attr("src", "/");
				
				
				}
		});

	$("li.gus_ea a, li.gus_user a").mouseenter(function () {
				$(this).next().fadeIn("medium");
				}
		);
	$("li.gus_ea, li.gus_user ").mouseleave(function () {
				$("ul", this).fadeOut("fast");
				}
		);
		
	$("ul.post-details").mouseleave(function(){
					$("#ratefeedback").fadeOut("fast");
				});	
	
	
	$("li.thumb").click(function(){
		if(isLoggedIn){
			if(document.cookie.indexOf('viscRated'+postID)< 0){
				var vote  = ($(this).hasClass("up"))? "up":"down";
				var voteURL = document.location + "!thumb?thumb=" +vote;
				$.getJSON(voteURL, function(results){
					$("#thumbUp").text(results.up);
					$("#thumbDown").text(results.down);
					document.cookie = 'viscRated'+postID+' =true; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/';
					$("#ratefeedback").text("Thanks!").fadeIn("medium");
					});
				}
			else{
				$("#ratefeedback").text("Rated Previously").fadeIn("medium");
				}
			}
			
		 });
		 
	$("#vote").click(function(){
		if($("#poll-form").serialize() == "") {
			alert("Please check a box to vote.");
			return false;
			}
		$("#vote, #showpoll").hide();
		$("#votefeedback").fadeIn("slow");

		$.ajax({
			type: "POST",
			url: document.forms['poll-form'].action,
			data: $("#poll-form").serialize(),
			success: function() {
				    for (i=0;i<document.forms['poll-form'].vote.length;i++){
							if (document.forms['poll-form'].vote[i].checked==true){
								var bargraphId = "result"+document.forms['poll-form'].vote[i].id;
								barLength[i] = barLength[i]  + (179/totalVotes);
								$("#formbox").hide();
								
								$("#poll-results").css("top",0);
								$("#poll-results").css("position","relative");
								$("#poll-results").css("height","100%");
								$("#pollgraph").show();
								$("#poll h3").fadeIn('fast').text("Poll Results");
								$("#"+bargraphId+ " .bargraph").width(barLength[i]);
								break;
								}
							}
					}
			});
		return false;
		});
		/* search form submission validation */
		$("#search-form-ajax").submit(function () {
		 	//alert('form is posting');
		 	AjaxSearch();
			return false;
		});
	$("#showpoll").click(function(){
			$("#pollgraph").show();			
			$("#poll-results").css("position","relative");
			$("#poll-results").css("height","100%");
		})

$($("form#submitcomment")).submit(function(){
		$.ajax({
			type: "POST",
			url: document.forms['submitcomment'].action,
			data: $("#submitcomment").serialize(),
			success: function() {
			
				  	$("#comments ul").append("<li>Thank you for your comment.</li>");
					}
			});
		return false;
		});
	if(!isLoggedIn) {
		$("li.thumb").mouseenter(function(){
					$("#ratefeedback").fadeIn("medium");
				});
		}
	else{
		$("li.thumb").css("cursor","pointer").css("cursor","hand");
		}	
	});
function loadComments(pageNum,pageSize) {
	$("#comments").load(escape(postID) + '!comments?_cC=' + pageSize + '&_cP='+pageNum + '&_rf='+Math.random(), function(){
			$(".flag").click(
				function () {
					$(this).next().fadeIn("fast");
					}
					);	
			$(".close-share").click(
				function () {
					$(this).parent().fadeOut("medium");
					}
				);	
			


			});
	}
	
	
function pollBargraph(){
	$("#poll").load('/poll/'+pollId,{},function(){
		var barGraphs = $("#poll-results .bargraph");
		var pollPercents = $("#poll-form .percent");
		pollPercents.each(function(i){
			if(pollPercents[i].value<.03) pollPercents[i].value = .03;
			if(pollPercents[i].value>.97) pollPercents[i].value = .97;
			var width = pollPercents[i].value*179;
			$(barGraphs[i]).css("width", width);
			});	
		
		$("#vote").click(function(){
			if($("#poll-form").serialize() == "") {
				alert("Please check a box to vote.");
				return false;
				}
		$.ajax({
			type: "POST",
			url: document.forms['poll-form'].action,
			data: $("#poll-form").serialize(),
			success: function() { pollBargraph(); }
			});
		return false;
		});
	
	
		$("#showpoll").click(function(){
				$("#pollgraph").show();			
				$("#poll-results").css("position","relative");
				$("#poll-results").css("height","100%");
			});
			
				
		});
	}
	
function sendComment(url) {
	c =  document.getElementById("comment-text").value;
	data = {
		userComment: c
    };
	$.get(url,data,function(data){
		$("#commentcount").text(Math.abs($("#commentcount").text())+1);
		loadComments(1,25);
         });
        }
        
function flagComment(url,comment) {
data = {
	cid: comment
};
$.get(url,data,function(data){
	$(".flagbox").fadeOut("medium");
});

}         
     		