$(function() {

Cufon.replace('.noheading h4,span#show_chart_options', { fontFamily: 'FlamaSemiBold' });
Cufon.replace('p.main_intro,a.change_button', { fontFamily: 'FlamaSemiBold' });
Cufon.replace('h2:not(div.review_head h2, div.results_right h2, #disqus_thread h3, .articleStory h2.story_title)', { fontFamily: 'FlamaCondensedMedium' });
Cufon.replace('div.footer ul', { fontFamily: 'FlamaCondensedMedium' });
Cufon.replace('fieldset.chart_filter', { fontFamily: 'FlamaCondensedMedium' });
Cufon.replace('div.back_to_chart_links', { fontFamily: 'FlamaCondensedMedium' });
Cufon.replace('a.back_to_chart', { fontFamily: 'FlamaCondensedMedium' });
Cufon.replace('h2.page_title,h2.section_title', { fontFamily: 'FlamaCondensedBold' });
Cufon.replace('.aside h3:not(.subheading)', { fontFamily: 'FlamaCondensedMedium' });
Cufon.replace('div.comments div.section_left h4', { fontFamily: 'FlamaCondensedMedium' });
Cufon.replace('.box h3, .box h4, ol.album_chart li div.score_wrap span, dl.review_album_info dt.average_rating', { fontFamily: 'FlamaCondensedMedium' });

	
	
	$('fieldset.chart_filter input').customInput();

	var inputFields = $('input[type="text"]'),
	    holderTxtArr = [];
		
	for (var i=0;i<inputFields.length;i++) {
		var holderTxt = inputFields.get(i).defaultValue;
		holderTxtArr.push(holderTxt);
	}
	inputFields.each(function(index){
		$(this).focus(function() { if(this.value==holderTxtArr[index]){this.value=''}; });
  		$(this).blur(function() { if(this.value==''){this.value=holderTxtArr[index]}; });
	});
	

	$("ol.album_chart li").each(function(index) {
		if (!$(this).hasClass("top_album")) {
			var bg = $(this).css('backgroundColor');
				bg = bg.indexOf('#') === -1 ? rgb2hex($(this).css('backgroundColor')) : bg;
				//console.log(index + ' - ' + bg);
				$(this).hover(function () {
					if (bg == "#d2cdc1") {
						$(this).stop().animate({ backgroundColor: "#b8b3a9" }, "fast");
					} else if (bg == "#ef9025") {
						$(this).stop().animate({ backgroundColor: "#d68120" }, "fast");					
					} else {
						$(this).stop().animate({ backgroundColor: "#d2cdc1" }, "fast");
					}
				}, function () {
					if (bg == "#d2cdc1") {
						$(this).stop().animate({ backgroundColor: "#d2cdc1" }, "fast");
					} else if (bg == "#ef9025") {
						$(this).stop().animate({ backgroundColor: "#ef9025" }, "fast");					
					} else {
						$(this).stop().animate({ backgroundColor: bg }, "fast");
					}
				});			
		}
	});
	

	$("ol.album_chart li").css({"cursor":"pointer"});
	$("ol.album_chart li a").click(function(event) { event.stopImmediatePropagation(); });
	$("ol.album_chart li").click(function(event) {
		//event.stopImmediatePropagation();
		var links = $(this).find("a"); // .cover
		if (links.length == 0) {
			return;
		}
		
		var firstLink = $(links[0]);
		if (firstLink.attr('target') == '_blank') {
			window.open(firstLink.attr('href'));
		} else {
			window.location = firstLink.attr("href");
		}
	});
	
	//$(".search label").hide();
	$(".search input:text").labelify({
	  text: "label"
	});
	
	
	//$("a.changeColumns").click(function() {
	//	$("body").toggleClass("alt");
	//	return false;
	//});
							   
	
$('.thin a[href^="http://"]')
  .attr({
    target: "_blank", 
    title: "Any Decent Music"
  });
	
	var $itemsToShow = $('ul.spy_widget li.hide'),
			toggleSwitch = $('a#toggle'),
		  oriText = toggleSwitch.html(),
		  altText = '<span>Less tracks</span>';
	
		toggleSwitch.click(function(e) {
			if (!$itemsToShow.is(':visible')) {
				$itemsToShow.slideDown('normal');
				toggleSwitch.html(altText);
			} else {
				$itemsToShow.slideUp('normal');
				toggleSwitch.html(oriText);
			}
			e.preventDefault();
		});	
	
	
	/*
	var hotTracksWidget = (function(widget) {
		var $items = $('>li',widget),
		    total = $items.length,
		    height = $items.get(0).offsetHeight,
		    totalHeight = total * height,
		    totalHeightN = totalHeight * -1,
		    limit = 2,
		    interval = 7500,
		    delayed = null,
		    counter = 1,
		    vPosition = 0,
		    wrapper = widget.wrap('<div class="spy_wrapper" />').parent().css('height',height*limit + 'px'),
		    toggleSwitch = $('a#toggle'),
		    oriText = toggleSwitch.html(),
		    altText = '<span>Less tracks</span>';

		toggleSwitch.click(function(e) {
			if (delayed) {
			clearTimeout(delayed);
			delayed = null;
			widget.animate({'top':'0px'},'slow',function() {
				wrapper.animate({'height':totalHeight + 'px'},'normal');
				counter = 1;
				vPosition = 0;
				toggleSwitch.html(altText);
			});
			} else {
			wrapper.animate({'height':height*limit + 'px'},'normal',function() {
				toggleSwitch.html(oriText);
				delayed = setTimeout(rotate,3000);
			});
			}
			e.preventDefault();
		});		
		
		function rotate() {
			if (vPosition <= totalHeightN + height*limit) {
				widget.animate({'top':'0px'},'slow');
				counter = 1;
				vPosition = 0; 
			} else {
				widget.animate({'top':'-'+height*counter+'px'},'slow');
				counter++;
				vPosition -= height; 
			}
			
			delayed = setTimeout(rotate,interval);
		}
		
		rotate();
	})($('ul.spy_widget'));
	*/	
});
var hexDigits = new Array
        ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"); 

//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
 rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

function hex(x) {
  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
 }


 /**
 * jQuery.fn.sortElements
 * --------------
 * @param Function comparator:
 *   Exactly the same behaviour as [1,2,3].sort(comparator)
 *   
 * @param Function getSortable
 *   A function that should return the element that is
 *   to be sorted. The comparator will run on the
 *   current collection, but you may want the actual
 *   resulting sort to occur on a parent or another
 *   associated element.
 *   
 *   E.g. $('td').sortElements(comparator, function(){
 *      return this.parentNode; 
 *   })
 *   
 *   The <td>'s parent (<tr>) will be sorted instead
 *   of the <td> itself.
 */
 jQuery.fn.sortElements = (function () {

 	var sort = [].sort;

 	return function (comparator, getSortable) {

 		getSortable = getSortable || function () { return this; };

 		var placements = this.map(function () {

 			var sortElement = getSortable.call(this),
                parentNode = sortElement.parentNode,

 			// Since the element itself will change position, we have
 			// to have some way of storing its original position in
 			// the DOM. The easiest way is to have a 'flag' node:
                nextSibling = parentNode.insertBefore(
                    document.createTextNode(''),
                    sortElement.nextSibling
                );

 			return function () {

 				if (parentNode === this) {
 					throw new Error(
                        "You can't sort elements if any one is a descendant of another."
                    );
 				}

 				// Insert before flag:
 				parentNode.insertBefore(this, nextSibling);
 				// Remove flag:
 				parentNode.removeChild(nextSibling);

 			};

 		});

 		return sort.call(this, comparator).each(function (i) {
 			placements[i].call(getSortable.call(this));
 		});

 	};

 })();
