$(function() {
  var $highlight = $('#highlight').cycle({
		fx: 'fade',
		before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
		  var current_number = parseInt($(nextSlideElement).attr("rel"));
		  
		  $("#highlight_cycle > li").each(function() {
		    if (parseInt($(this).attr("rel")) == current_number) {
		      $(this).children().addClass("here");
		    } else {
		      $(this).children().removeClass("here");
		    }
		  });
		}
	});
	
	$(".highlight_nav > li").mouseenter(function() {
		$highlight.cycle('pause');
		
		var $highlight_placeholder = $("#highlight");
		$highlight_placeholder.find("div.playlist_element").remove();

	  var i = $(this).attr("rel");
	  $highlight.cycle(parseInt(i));
	});
	
	$(".highlight_nav > li").mouseleave(function() {
		$highlight.cycle("resume");
	});
	
	$("#highlight").mouseenter(function() {
		$highlight.cycle("pause");
	});
	
	$("#highlight").mouseleave(function() {		
		if (!$("#highlight > div:visible").hasClass("playlist_element")) {
			$highlight.cycle("resume");
		}
	});
	
	$(".index_playlist").find("li").click(function() { 
		$highlight.cycle("pause");
		
		var index = parseInt($(this).attr("rel"));
		var $highlight_placeholder = $("#highlight");		
		var playlist_item = playlist[index]["playlist_item"];
		
		var item = "";
		if (playlist_item["item_type"] == "Video") {
			item = playlist_item["item"]["code"];
		} else if (playlist_item["item_type"] == "Track") {
			item = "<img src='" + $(this).attr("musicus:image") + "'>";
			item += 
				"<object type=\"application/x-shockwave-flash\" data=\"/player_mp3_multi.swf\" width=\"568\" height=\"25\">\
        	<param name=\"bgcolor\" value=\"#000000\" />\
        	<param name=\"movie\" value=\"/player_mp3_multi.swf\" />\
        	<param value=\"mp3=" + $(this).attr("musicus:file") + "&amp;width=568&amp;height=25&amp;showvolume=1&amp;showlist=0&amp;bgcolor=000000&amp;bgcolor1=000000&amp;loadingcolor=D5D5D5&amp;sliderovercolor=D5D5D5&amp;buttonovercolor=D5D5D5&amp;currentmp3color=D5D5D5&amp;scrollbarovercolor=D5D5D5&amp;\" name=\"FlashVars\" />\
      	</object>"
		}
		
		$highlight_placeholder.find("div.playlist_element").remove();
		$highlight_placeholder.append("<div rel='-1' class='playlist_element' style='display: none'>" + item + "</div");
		
		$highlight_placeholder.children().fadeOut('slow');
		$highlight_placeholder.children(".playlist_element").fadeIn('slow');
	});
	
	// comments
	if ($.browser.msie) {
  	function forceIEReflow() {
  		$('.out')[0].className += '';
  	}
  	FB.Event.subscribe('xfbml.render', forceIEReflow);
  	FB.Event.subscribe('comment.create', forceIEReflow);
	}
});

function resizeEmbedForPlayListItems(objects, width, height) {	
	for (var i=0; i<objects.length; i++) {
		var obj = objects[i]["playlist_item"];
		
		if (obj["item_type"] != "Video") { continue; }
				
		var code = obj["item"]["code"];
		var source_width_blocks = code.match(/width=\"?(\d+)\"?/gi);
		var source_height_blocks = code.match(/height=\"?(\d+)\"?/gi);
		
		if (source_width_blocks.length != source_height_blocks.length) {
			continue;
		}
		
		for (var j=0; j < source_width_blocks.length; j++) {
			var source_width = source_width_blocks[j].match(/\d+/);
			var source_height = source_height_blocks[j].match(/\d+/);
			
			if (source_width.length == 0 || source_height.length == 0)  {
				continue;
			}
			
			if (parseInt(source_width) == width) {
				continue;
			}
			
			if (height == null || height == undefined) {
				height = Math.floor(width * parseInt(source_height[0]) / parseFloat(source_width[0]));
			}
			
			obj["item"]["code"] = obj["item"]["code"].replace(source_width_blocks[j], 'width="' + width + '"');
			obj["item"]['code'] = obj["item"]["code"].replace(source_height_blocks[j], 'height="' + height + '"');
		}
	}
	
	return objects
}
