//Slick jQuery Stuff

$(document).ready(function() {
	
	//Limit Comment Text
	$('.commentlimit').each(function() {
		$(this).attr("div_height", $(this).height());
		
		if($(this).height() > 65) {
			$(this).height('65px').after('<div class="showmore roundbottom">Show More</p>');
		}    
	});
	
	$('.showmore').click(function() {
		var div_height = $(this).prev('.commentlimit').attr("div_height") + "px";
		$(this).prev('.commentlimit').animate({"height": div_height}, 400);
		
		$(this).remove();
	});

	//Ajax Rating
	$("form.rating").submit(function() { return false; });
	$(".btn-like, .btn-dislike").click(function() {
		
		if (!$(this).hasClass('rated')) {
			var action 		= $(this).closest('form').attr("action");
			var entry_id 	= $(this).parent().find('input[name="entry_id"]').val();
			var rating 		= $(this).val();
			
			$.post(action, {entry_id:entry_id, rating:rating}, function() {
				//alert("Rating Added..."+entry_id+":"+rating);
	
			});
			
			$("#action-"+entry_id+" .btn-like, #action-"+entry_id+" .btn-dislike").addClass('rated');
	
			$("#action-"+entry_id+" .btn-like").fadeOut('slow');
			
			$("#action-"+entry_id+" .btn-dislike").fadeOut('slow', function() {
				$("#action-"+entry_id+" #stats").after('<span class="thanks">Thanks For Voting!</span>');
			});
		}
	});
	
	//Tooltips
	if(jQuery().tipsy) {
		$('.tooltip').tipsy({gravity: 's', fade: true});
		$('img.tooltip').tipsy({gravity: 's', fade: true, title: 'alt'});
	}
	
	//Clear Search on Focus
	$('input[type=search]').focus(function() {
		if ($(this).val() == "Search") {
			$(this).val("");
		}
	});
	
	$('input[type=search]').focus(function() {
		if ($(this).val() == "Search") {
			$(this).val("");
		}
	});
	
	//Embed Button
	$('.embed').hide();
	$('.btn-embed').click(function() 
	{
		var embed = '#embed-' + $(this).attr('id');
		$(embed).slideToggle("fast");
	});	
	
	//Share Button
	$('.share').hide();
	$('.btn-share').click(function() 
	{
		var share = '#share-' + $(this).attr('id');
		$(share).slideToggle("fast");
	});
	
	//Messages
//	$('.msg').delay(3000).fadeOut(2000);
//	$('.msg').hover(function(){
//		$(this).stop(true, true).fadeIn("fast");
//	},function(){
//		$(this).stop(true, true).fadeOut(2000);
//	});
	
//	$('.msg').click(function(){
//		$(this).stop(true, true).fadeOut(2000);
//	});
	
	
	//Toggle Divs
	$('.toggle').next('div').hide();
	$('.toggle').click(function () {
		$(this).next('div').slideToggle("slow");
		$(this).addClass('active');
	});

	//Scroll To Top
	$("#scroll").hide().removeAttr("href");
		
	$(window).scroll(function () {
		if($(window).scrollTop()=="0") {
			$("#scroll").fadeOut("slow")
		} else {
			$("#scroll").fadeIn("slow")
		}
	});
	
	//Highlight Form Field
	$('.crud form input[type=text], .crud form textarea').focus(function() {
		$('.crud form').find('div').removeClass('active');
		$(this).parent('div').addClass('active');
	});	
	
	$("#scroll").click(function() {
		$('html, body').animate({scrollTop:0}, 'slow')
	});
	
	//Zebra Table Rows For Older Browsers 
	//??not working??
	//$('tr:nth-child(odd)').addclass('odd');
			
	//Animated Anchor Scroll
	$('a[href*=#]').click(function(event) {
		event.preventDefault();
		
		var anchor = this.href.split("#");
		var anchor = anchor[1];
		var anchor = $("#"+anchor).offset().top;
	
		$("html, body").animate({scrollTop: anchor}, 'slow');
	});
	
	//Confirm
	$('.confirm').click(function() { 
		var confirmed = confirm('Are you sure you want to do this?');
		
		if (!confirmed) {
			return false;
		}
	});

	$('.confirm_delete').click(function() { 
		var confirmed = confirm('Are you sure you want to delete this and all related data?');
		
		if (!confirmed) {
			return false;
		}
	});
		
	//Sortable Tables
	$('.sort').click(function(event) { 
//		event.preventDefault();
		
//		var location= $(this).attr("href");
//		$(this).removeAttr('href');
 
//		$('.sortable').load(location) 
		
		
	});		
	
});

