/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			}
		},
		design : {
			covers : function(){
				var $elements = $('div.box-a div.featured p.album img');
				$elements.each(function(){
					$(this).css('background','none').after('<span>&middot;</span>');
				});
			},
			rearrange : function(){
			  var $boxes = $('#main .box-a');
			  
			  $boxes.each(function(){
			  	var $id = $(this).attr("id");
			  	var $source = "#info-" + $id;
			  	var $album = $("#info-" + $id + " blockquote").html();
			  	var $destination = "p#album-" + $id;
			  	var $buylink = $("#info-" + $id + " p.album-link a");
			  	
			  	
			  	$($destination).html($album);
			  				  	
			  	// Make sure there's a link to buy the album
			  	if ($buylink.length > 0)
			  	{
			  		$("#info-" + $id + " p.album a").attr({href: $buylink.attr("href")});
			  	} else {
			  		$("#info-" + $id + " p.album a").remove();
			  	}
			  	
			  	// Clean up
			  	$("#info-" + $id + " p.album-link, #info-" + $id + " blockquote").remove();
			  	$($source + ' .caption').show();
			  	
			  	// Fix annoying Firefox bug with Flash and overflows...
			  	$($source + ' #audio_player_' + $id + ' .audio_player embed').attr('wmode', 'transparent').css('overflow', 'hidden');
			  });
			}
		}
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.design.covers();
	Engine.design.rearrange();
	
});
