var gOverride = {
  gColor: '#EEEEEE',
  gColumns: 20,
  gOpacity: 0.35,
  gWidth: 10,
  pColor: 'rgba(150,150,150,.5)',
  pHeight: 12,
  pOffset: 0,
  pOpacity: 0.55,
  center: true,
  gEnabled: false,
  pEnabled: true,
  setupEnabled: true,
  fixFlash: true,
  size: 960
};

var widonts = '.recent-track a';

if(document['DD_belatedPNG']) {
  DD_belatedPNG.fix('*');
}

$.easing.def = "easeInOutCirc";

(function($){
  
  $.preLoadImages([
    '/media/grpahics/desigr_title.png',
    '/media/graphics/desigr_title_hover.png',
    '/media/graphics/comment-write.png',
    '/media/graphics/comment-write_hover.png',
    '/media/graphics/comment-write_red.png',
    '/media/graphics/comment-write_dark.png',
    '/media/graphics/reload.png',
    '/media/graphics/reload_hover.png',
    '/media/graphics/portfolio.png',
    '/media/graphics/portfolio_hover.png',
    '/media/graphics/portfolio-active.png',
    '/media/graphics/portfolio_hover-active.png',
    '/media/graphics/ajax-loader.gif',
    '/media/graphics/ajax-loader_small.gif',
    '/media/graphics/article_next.gif',
    '/media/graphics/article_next_hover.gif',
    '/media/graphics/article_next_inactive.gif',
    '/media/graphics/article_prev.gif',
    '/media/graphics/article_prev_hover.gif',
    '/media/graphics/article_prev_inactive.gif',
    '/media/graphics/tag_hover.gif',
  ]);
  
  $(window).load(function(){
    $('#article-images').cycle({
  	  slideExpr: '.image',
  	  before: function(c, n){
  	    $('#article-images').animate({
  	      height: $(n).outerHeight(),
        }, 350);
  	  },
  	  timeout: 0,
  	  speed: '1500',
  	  easing: 'easeInOutQuad',
  	  next: '#article-images'
  	});
  });
  
	$(document).ready(function(){
	  
	  $('#favorite-artists h2').append('<a href="/music/favorite_itunes_artists_listing/8" class="reload">Reload</a>');
	  $('#favorite-artists').each(function(){
	    $this = $(this);
  	  $('h2 a.reload', $this).click(function(){
  	    $trigger = $(this);
  	    $trigger.addClass('loading');
  	    $('#artists-listing').fadeOut();
  	    $.ajax({
  	      url: $(this).attr('href'),
  	      cache: true,
  	      dataType: 'html',
  	      success: function(html){
  	        $trigger.removeClass('loading').addClass('reloaded');
  	        $('#artists-listing').empty().html(html).fadeIn();
  	      }
  	    });
  	    return false;
  	  });
	  });
	  
	  $('#portfolio-random').each(function(){
	    $this = $(this);
	    $('#portfolio-listing', $this).hide();
	    $('h2 a', $this).toggle(function(){
	      $trigger = $(this);
	      $trigger.addClass('loading');
	      $.ajax({
	        url: $(this).attr('href'),
	        cache: true,
	        dataType: 'html',
	        success: function(html){
	          $trigger.removeClass('loading').addClass('active').text('Hide my portfolio');
	          $('#portfolio-listing', $this).html(html).slideDown('slow', 'easeInCirc');
	        }
	      });
	      return false;
  	  }, function(){
  	    $trigger = $(this);
  	    $trigger.removeClass('active').addClass('loading').text('View my portfolio');
	      $('#portfolio-listing', $this).slideUp({
	        duration: 'slow',
	        easing: 'easeOutCirc',
	        complete: function(){
	          $trigger.removeClass('loading');
	          $(this).empty();
	        }
	      });
  	  });
	  });

    $('#return-top').localScroll({
      duration: 1500,
      hash: false
    });

 /* $(widonts).each(function(){ 
  *   $(this).html($(this).html().replace(/\s([^\s<]{0,10})\s*$/,'&nbsp;$1'));
  * });
  */
	  
	  viewportHeight = $(window).height();
	  
	  $(window).resize(function(){ 
	    viewportHeight = $(window).height();
	    if($(window).width() <= 480) $('#persistent').css('position', 'relative');
    });
	  
	  persistentLoc = $('#persistent').offset().top;
	  origLoc = $('#persistent').offset().top;
	  persistentHeight = $('#persistent').outerHeight();
	  
	  $(window).scroll(function(){
      persistentLoc = $('#persistent').offset().top;
  	  persistentHeight = $('#persistent').outerHeight();
  	  
  	  if((!(navigator.userAgent.match(/iPhone/i)) && !(navigator.userAgent.match(/iPod/i))) || !($(window).width() > 480)) {
  	    if($(window).scrollTop() > persistentLoc) {
  	      if(viewportHeight >= persistentHeight) {
  	        $('#persistent').css('position', 'fixed');
  	        origLoc = persistentLoc;
  	        
  	        /* recheck the height, just in case the user resized on us */
  	        userResized = (viewportHeight > $(window).height()) ? true : false;
    	      
    	      viewportHeight = $(window).height(); /* play with the new value */
  	      }
  	    } else {
  	      if(($(window).scrollTop() < origLoc) || $(window).width() <= 480) $('#persistent').css('position', 'relative');
  	      viewportHeight = $(window).height(); /* recheck the height, just in case the user resized on us */
  	    };
      };
	  });

    $('input#site-search-string').attr('placeholder', 'Search');
		
    // $("a:not(a[href*="+location.hostname+"])").not('.same-window').attr('target', '_blank').addClass('external-link');
		
		$('a[rel=external]').attr('target', '_blank').addClass('external-link');
		
		$baseUrl = $('base').attr('href');
		
		$footnoteCount = 0;
				
		$('.copy span.footnote').each(function(){
		  $footnoteCount++;
		  $(this).append('<a href="#footnote-id-'+$footnoteCount+'" class="footnote-citation">'+$footnoteCount+'</a>');
		  if($('.copy .footnotes').size() <= 0) $('#comments').before('<div class="footnotes"><h3>Footnotes</h3><ol></ol></div>');
		  $('.copy .footnotes ol').append('<li id="footnote-id-'+$footnoteCount+'">'+$(this).attr('title')+'</li>');
		});
		
		/* Comment form validation */
		
		$.validator.addMethod('twitterer', function(value, element) {
			if((value.substr(0, 1) == '@') && value.length > 1) return true;
			else return false;
		}, 'Does not appear to be a valid Twitter handle.');
		
		$.validator.addMethod('twittersafe', function(value, element) {
			if(value.length > 140) return false;
			else if(value.length < 2) return false;
			else return true;
		}, 'Total message must be less than 140 characters long.');
		
		$('form#comment-respond').validate({
			errorElement: 'div',
			errorPlacement: function(error, element) {
				error.appendTo(element.parent("div"));
			}
		});
		
		$('#respond-twitter-comment').charCounter(140, {
			container: "#response-length-counter",
			errorclass: 'error',
			format: "You have %1 characters remaining."
		});
		
		$('form#comment-twitter-respond').validate({
			onKeyUp: false,
			event: 'change',
			errorElement: 'div',
			errorPlacement: function(error, element) {
				error.appendTo(element.parent("div"));
			},
			rules: { tweet: { twittersafe: true }}
		});
		
		/* Comment reponse form tabs */
		
    // $.tools.addTabEffect("slide", function(i) { this.getPanes().hide().eq(i).slideToggle('fast'); });
		$("ul.tabs").tabs("div.panes > div", { history: true });
		
		/* Paragraph-specific comments */
		
		/*$('#body-copy p').each(function(i){
		  var commentCount = $('.comment[data-paragraph='+i+']').size();
		  $(this).qtip({
        content: commentCount,
        position: { corner: { target: 'topLeft', tooltip: 'topRight' }},
        style: {
          width: 30,
          padding: 5,
          background: '#302D28',
          color: 'white',
          border: { width: 2, radius: 7, color: '#302D28' },
          tip: 'rightMiddle'
        },
        hide: {
          when: 'mouseout',
          fixed: true
        }
      });
		});*/
	});
})(jQuery);