jQuery(document).ready( function($) {
	$('a.tip_bottom').tipTip();

	if ( $('div#reading-panel').length == 0 || $('div#reading-panel').css('display') == 'none' ) {
		$('span.read-more a').attr('target', '_blank');
		$('div#content h3 a').attr('target', '_blank');
	}

	if ( ($('div#reading-panel').length) && $('div#reading-panel').css('display') != 'none' && !($.browser.mobile) ) {
  	var offset = $('div#reading-panel').offset();
  	var header_height = offset.top;

  	var window_height = $(window).height();
  	var new_height = window_height - header_height;
	
		$('div#content').height(new_height).getNiceScroll().onResize();
		$('div#reading-panel').height(new_height);
		$('iframe#reading-iframe').height(new_height);
	
		$(window).resize(resizeWindow);
		$('div#content div').resize(resizeWindow);
		$('a.ajax-append').live('click', resizeWindowEvent);
	
		$('div#content').niceScroll({touchbehavior:false,cursorcolor:"#777",cursoropacitymax:0.5,cursorwidth:8,cursorborderradius:"4px"}); 
	}

  $('a.trigger').live('click', function(e) { 
		if ( $('div#content').hasClass('show-entry-excerpt') ) {
			$('div#content').addClass('hide-entry-excerpt').removeClass('show-entry-excerpt');
		} else {
			$('div#content').addClass('show-entry-excerpt').removeClass('hide-entry-excerpt');
		}

  	this.blur();
  	e.preventDefault();
  	return false;
	});
	
	$('a.promote').live('click', function(e) {
  	var ts = new Date().getTime();
		var active_anchor = $(this);
		active_anchor.html('<img src="/wp-content/themes/kickpress-reader/images/loader.gif" />');

	  $.getJSON(this.href + '?' + ts, function(data) {
      if ( 'success' == data.status ) {
      	if ( 'Term Removed' == data.messages.note) {
					active_anchor.removeClass('featured');
					active_anchor.html('<img src="/wp-content/themes/kickpress-reader/images/featured.png" />');
      	} else if ( 'Term Added' == data.messages.note ) {
					active_anchor.addClass('featured');
					active_anchor.html('<img src="/wp-content/themes/kickpress-reader/images/featured_active.png" />');
				}
			} else if ( 'failure' == data.status ) {
				alert('A server error occured while attempting to change the "Featured" status of this article');
			}
	    //$.each(data, function(i, entry) {});
	  });

  	this.blur();
  	e.preventDefault();
  	return false;
	});
	
	$('a.post-status').live('click', function(e) {
  	var ts = new Date().getTime();
		var active_anchor = $(this);
		var post_admin_bar = "div#" + $(this).attr('rel') + " ul";

		var current_image = active_anchor.html();
		active_anchor.html('<img src="/wp-content/themes/kickpress-reader/images/loader.gif" />');

	  $.getJSON(this.href + '?' + ts, function(data) {
      if ( 'success' == data.status ) {
      	if ( 'Published' == data.messages.note ) {
					$(post_admin_bar).addClass('published').removeClass('unpublished');
					active_anchor.html('<img src="/wp-content/themes/kickpress-reader/images/published.png" />');
      	} else if ( 'Unpublished' == data.messages.note ) {
					$(post_admin_bar).addClass('unpublished').removeClass('published');
					active_anchor.html('<img src="/wp-content/themes/kickpress-reader/images/unpublished.png" />');
				}
			} else if ( 'failure' == data.status ) {
				alert('A server error occured while attempting to change the "Featured" status of this article');
			}
	    //$.each(data, function(i, entry) {});
	  });

  	this.blur();
  	e.preventDefault();
  	return false;
	});

	$('a.reload').live('click', function(e) {
		var wrapper = jQuery('div#content div#' + this.rel);
		if(wrapper.length > 0) {
	  	var targetContent = jQuery('ul:first', this.parentNode);
	    var targetOffset = wrapper.offset().top;
			var targetTop = $(window).scrollTop();
	  	var ts = new Date().getTime();

	    wrapper.animate({opacity: 0}, 500);
	    $('div#content').addClass('dynamic-loader');
			if((targetOffset <= targetTop)) {
	    	$('html,body').animate({scrollTop: targetOffset}, 500);
	    }
	    wrapper.load(this.href + '?' + ts, function() {
	      wrapper.animate({opacity: 1}, 500, function() {
	        $('div#content').removeClass('dynamic-loader');
	        $(this).css('filter', 'none');
	      });
	    });

	  	this.blur();
	  	e.preventDefault();
	  	return false;
	  }
	});

});

var resizeWindowEvent = function(e){
  resizeWindow();
};

var resizeWindow = function(){
  var offset = jQuery('div#reading-panel').offset();
  var header_height = offset.top;

  var window_height = jQuery(window).height();
  var new_height = window_height - header_height;

	jQuery('div#content').height(new_height).getNiceScroll().onResize();
	jQuery('div#reading-panel').height(new_height);
	jQuery('iframe#reading-iframe').height(new_height);
};
