var c_link = '';
var c_next = '';

jQuery(document).ready(function() {
	// AJAX comments
	jQuery("#show_more_comments").live("click",function(e) {
		e.preventDefault();
		jQuery("#spinner").show();
		var link 	= jQuery(this).attr("href");
		var chunk 	= link.split("/");
		// IE has the full URL. We need to strip out the parts we don't want
		if (jQuery.support.leadingWhitespace == false && link.substr(0,4) == 'http') {
			chunk.remove(0,4);
			link = chunk.join("/");
		}

		c_next 		= Number(chunk[2])+50;

		if (window.t_com >= c_next) {
			chunk[2] = c_next;
			c_link = chunk.join("/");
		}

		load_comments(link,false);
	});

	jQuery("#show_all_comments").live("click",function(e) {
		e.preventDefault();
		jQuery("#spinner").show();
		var link 	= jQuery(this).attr("href");
		var chunk 	= link.split("/");
		// IE has the full URL. We need to strip out the parts we don't want
		if (jQuery.support.leadingWhitespace == false && link.substr(0,4) == 'http') {
			chunk.remove(0,4);
			link = chunk.join("/");
		}
		load_comments(link,true);
	});

	jQuery("#banner_ad").show();

	// Ads notice hack
	jQuery("#banner_ad img").each(function(i) {

		if (jQuery("#banner_ad img").attr("width") == 1) {
			jQuery("#banner_ad").hide();
		}

		// this makes sure that the ads don't trigger FancyZoom..
		jQuery(this).parent("a").attr("rel","nozoom");

	});

	jQuery(".ads img").each(function(i) {

		if (jQuery(this).attr("width") < 10) {
			jQuery(this).parent("a").parent("div").css("display","none");
		}

		// this makes sure that the ads don't trigger FancyZoom..
		jQuery(this).parent("a").attr("rel","nozoom");

	});

	jQuery("#banner_ad div").each(function(i) {
		jQuery(this).css('zIndex', 200);
	});

	// AJAX poll
	if ( jQuery("#pollbox") ) {
	    jQuery("#pollbox").load("/js_poll/poll", {"actual_return":window.location.href});
	}

	// Embedded polls
	if ( jQuery("#embedded_poll").length != 0 ) {
		var poll_id = jQuery("#embedded_poll").attr("title").substr(5);
		jQuery("#embedded_poll").load("/embedded_poll/"+poll_id, {"return":window.location.href});
	}

	// FancyZoom setup
	setupZoom();

	// Sidebar most popular tab box
	jQuery(".tab-box:not(.clickthrough) ul.tabs li a").click(function(event) {
		event.preventDefault();
		var id = jQuery(this).attr('id').substr(4);
		var parent = jQuery(this).parents('.tab-box').attr('id');

		jQuery("#"+parent+" ul.tabs li").removeClass("active");
		jQuery(this).parent("li").addClass("active");

		jQuery("#"+parent+" div[id]").hide();
		jQuery("#"+parent+" #"+parent+"-"+id).show();
	});

	// Sliding photo captions
	jQuery('.boxgrid.caption').hover(function(){
    	jQuery(".cover", this).stop().animate({top:'147px'},{queue:false,duration:160});
	}, function() {
    	jQuery(".cover", this).stop().animate({top:'164px'},{queue:false,duration:160});
	});

	// Homepage slider
	jQuery("#prev a").css({cursor: "default"});
	jQuery("#slider_nav a").click(function() {
		move_slider(jQuery(this).parent("li").attr("id"));
		return false;
	});

	// Colorbox modals
	jQuery(".colorbox").colorbox();

	jQuery(".flag-comment").colorbox({width:'490px',height:'330px'});

	jQuery().bind("cbox_complete", function(event) {
		var loc = window.location.href;
		jQuery("div.hiddenFields input[name='return'],div.hiddenFields input[name='RET']").attr("value", loc);
	});

	// Print article
	jQuery("ul.tools li a.tool-print").click(function() {
		window.print();
		return false;
	});

	/*
	// Toggle advanced search form
	jQuery("#advanced_search").click(function(event) {
		if ( jQuery("#keywords").attr("value") != "" ) jQuery("#form-search-advanced input[name=keywords]").attr("value",jQuery("#keywords").attr("value"));
		jQuery("#advanced-search-form").slideToggle();
		return false;
	});

	// Advanced search precision change
	jQuery("#advanced-search-form select[name='where']").change(function(event) {
		set_cookie('where',this.value, 1, '/', '', '');
	});
	*/






	jQuery('#blawg100_star').load('http://www.abajournal.com/index.php/blawg100/star?action=check&entry_id=' + jQuery('#blawg100_star').html());
	jQuery('#blawg100_star').show();


	jQuery('#newsletter_tool_star').load('http://www.abajournal.com/index.php/newsletter/star?action=check&entry_id=' + jQuery('#newsletter_tool_star').html());
	jQuery('#newsletter_tool_star').show();

	//	Populate most read widget, Chartbeat based
	//	u is defined below above the function definition for mp_widget().
	jQuery("#most-box-1").empty().append(u);
});


// load the comment form with a click...
function loadCommentForm(entry_id) {

	jQuery('#comment_form').load('http://www.abajournal.com/index.php/inc/comments_form_ajax?entry_id=' + entry_id + '&return_url=http://' + location.host + location.pathname);

}

// Updated search form - 5/13/2010

	function search_activate() {
		jQuery("#site_search_advanced_controls").slideDown('fast');
	}

	function search_deactivate() {
		jQuery("#site_search_advanced_controls").slideUp('fast');
	}


// Blawg star action

function starBlawg(entry_id) {

	jQuery('#blawg100_star').load('http://www.abajournal.com/index.php/blawg100/star?action=update&entry_id=' + entry_id);

	return false;
}


// Newsletter star action

function starArticle(entry_id) {

	jQuery('#newsletter_tool_star').load('http://www.abajournal.com/index.php/newsletter/star?action=update&entry_id=' + entry_id);

	return false;
}




var slider_pos = 0;

function move_slider(dir) {
	if ( dir == "prev") {
		slider_pos = ( slider_pos == 0 ) ? 0 : slider_pos + 602;
		if ( slider_pos == 0 ) {
			jQuery("#prev").css({backgroundPosition: "0 0"});
			jQuery("#prev a").css({cursor: "default"});
			jQuery("#next").css({backgroundPosition: "0 -16px"});
			jQuery("#next a").css({cursor: "pointer"});
		}
	} else {
		slider_pos = ( slider_pos == -602 ) ? -602 : slider_pos - 602;
		if ( slider_pos == -602 ) {
			jQuery("#prev").css({backgroundPosition: "0 -32px"});
			jQuery("#prev a").css({cursor: "pointer"});
			jQuery("#next").css({backgroundPosition: "0 -48px"});
			jQuery("#next a").css({cursor: "default"});
		}
	}

	jQuery("#slider_tray").animate({left:slider_pos});
}

function set_cookie( name, value, expires, path, domain, secure )  {
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}

	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function load_comments( link, all ) {
	jQuery.get("/inc/article_comments/"+link, function(data) {
		var code = jQuery(data);
		jQuery("#spinner").hide();
		if (all) {
			jQuery("#comments_box").empty();
		} else {
			if (c_link != "") {
				code.children("#show_more_comments").attr("href",c_link);
			}
		}

		jQuery("#comments_box").find("#comment_pages").remove();

		jQuery("#comments_box").append(code);

		if (all) {
			jQuery("#comment_pages").remove();
		} else {
			if (window.t_com < c_next) {
				jQuery("#comment_pages").remove();
			}
		}


		// this loads the comment count for the article toolbar, grabbing the number from the comments box
		// evidently, this was messing up styles in IE... not sure why, so disabled for now.

		/*
		jQuery("#comment_count").html('<a href="#comments" class="tool-comments" title="View comments"><span>' + jQuery("#comment_total_live").html() + '</span> Comments');
		jQuery("#comment_count").show();
		*/

	}, "html");
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

jQuery(window).load(function() {

	// size magazine image featured box to fit the image
	jQuery("#article-sidebar .segment img").each(function(i) {
		jQuery("#article-sidebar").width(jQuery(this).width());

	});

	// and the other ones...
	jQuery("#river img").each(function(i) {
		jQuery(this).parent(".clearfix").width(jQuery(this).width());
	});

	jQuery("#river a img").each(function(i) {
		jQuery(this).parent().parent(".clearfix").width(jQuery(this).width());
	});

});


// poll results - view dynamically - added by Jason on 4/13/2010

function poll_view_results( entry_id ) {

	jQuery("#lg-poll-" + entry_id + " .question").hide();
	jQuery("#lg-poll-" + entry_id + " form").hide();
	jQuery("#lg-poll-" + entry_id + " p").hide();

	jQuery("#poll_show_results_box_" + entry_id).show();

}

//	Most Popular Widget, Chartbeat based

var u;

function mp_widget(data)
{
	var count = 1;
	var limit = 10;
	var used = '';
	u = document.createElement("ol");

	jQuery.each(data, function(i,n)
	{
		var l = document.createElement("li");
		var a = document.createElement("a");
		var x = (n.i.indexOf("- ABA Journal") != -1) ? n.i.slice(0,n.i.indexOf("- ABA Journal")) : n.i;
		x = (x.indexOf("- News") != -1) ? x.slice(0,x.indexOf("- News")) : x;
		x = (x.indexOf("- Magazine") != -1) ? x.slice(0,x.indexOf("- Magazine")) : x;
		var v = '';
		// v = ' (' + n.visitors + ')';
		var t = document.createTextNode(x + v);

		if ( n.path.charAt(n.path.length - 1) == "/" )
		{
			n.path	= n.path.substr(0, n.path.length - 1);
		}

		var url_title = n.path.match(/[^/]*$/i);
		a.href = n.path;
		a.appendChild(t);
		l.appendChild(a);
		if ( x != '' && x != '0' && x != '1' && count <= limit && used.indexOf( "|" + url_title ) == -1 && x.indexOf("- Law News Now") == -1 && x.indexOf("Daily News") == -1 && n.path.indexOf("/gallery") == -1 && n.path.indexOf("/topics") == -1 && n.path.indexOf("/blawg100") == -1 && n.path.indexOf("/search") == -1 && n.path.indexOf("/submissions") == -1 && n.path.indexOf("/authors") == -1 && x.indexOf("Newsletter Weekly") == -1 )
		{
			count++;
			u.appendChild(l);
			used	= used + "|" + url_title;
		}
	});
}

