google.load("swfobject", "2.1");
google.load("jquery", "1.3.1");
google.load("jqueryui", "1.5.3");

/* namespace idol.world */

var idol = idol ? idol : {};
idol.world = {};

idol.world.update_disponibilities = function() {
	var tmp_country_code = null;

	if (google.loader.ClientLocation != null) {
		idol.world.update_disponibilites_with_country_code(google.loader.ClientLocation.address.country_code);
	} else {
		idol.world.update_disponibilites_without_country_code();
	}
}

idol.world.update_disponibilites_with_country_code = function(country_code) {
	$('.disponibilities span.territoire').each(function(i) {
		if (country_code == null || $(this).hasClass(country_code)) {
			$(this).show();
		}
	});
}

idol.world.update_disponibilites_without_country_code = function() {
	var plateformes_links_hash = {}
	
	$('.disponibilities span.territoire a.plateforme').each(function(i) {
		var tmp_link = $(this);
		var tmp_url = tmp_link[0].href;
		var tmp_plateforme = tmp_link[0].innerText || tmp_link[0].textContent;
		
		if (!plateformes_links_hash[tmp_plateforme]) {
			plateformes_links_hash[tmp_plateforme] = {}
		}
		
		plateformes_links_hash[tmp_plateforme][tmp_url] = tmp_link;
	});
	
	$.each(plateformes_links_hash, function(tmp_plateforme, val) {
		var tmp_keys = getKeys(val);
		var tmp_length = tmp_keys.length;
		
		var tmp_link = null;
		
		if (tmp_length == 1) {
			tmp_link = $(val[tmp_keys[0]]);
		} else {
			tmp_link = $("<div class='plateforme border_accent_color over_accent_color' style='cursor:pointer'>" +tmp_plateforme +"<div style='clear:both;'></div></div>");
			
			$.each(val, function(url, link) {
				var classes = link.parent().attr("class").split(' ');
				
				$.each(classes, function(index, territoire) {
					if (territoire != "territoire") {
						tmp_link.append("<a class='plateforme border_accent_color over_accent_color' style='display:none;' href='" +link[0].href +"' target='_blank'>" +territoire +"</a>");
					}
				});
			});
			
			tmp_link.click(function() {
				$(this).children(".plateforme").toggle();
			});
		}
		
		$(tmp_link).insertBefore(".disponibilities .clear");
	});
}

idol.world.update_biographies = function() {
	var tmp_user_language = null;
	
	if (navigator.language) {
	  tmp_user_language = navigator.language.substring(0, 2);
	} else if (navigator.userLanguage) {
	  tmp_user_language = navigator.userLanguage.substring(0, 2);
	} else {
		return;
	}
	
	if ($('.biography .content.' +tmp_user_language).size() != 0) {
		$('.biography .content').each(function(i) {
			if (!$(this).hasClass(tmp_user_language)) {
				$(this).hide();
			}
		});
	} else {
		$('.biography .content').each(function(i) {
			if (!$(this).hasClass('en')) {
				$(this).hide();
			}
		});
	}
}

idol.world.track_click = function() {
	$(document).ready(function() {
		$(".tracklisting li").click(function(){
			$(".tracklisting li").removeClass("playing");
			
			var str_artist_ids = $(this).find(".artist_ids").text();
			
			var reg=new RegExp(" ", "g");
			
			$(".artist").removeClass("playing");
			
			var artist_ids = str_artist_ids.split(reg);

			for (var i=0; i<artist_ids.length; i++) {
				var tmp_artist = $(".contributors").find("#" +artist_ids[i]);
				tmp_artist.addClass("playing");
			}
						
			$(this).addClass("playing");
			
			var num_support = $(this).find(".support").text();
			var num_piste = $(this).find(".num").text();
			
			play_track("idol_player", num_support, num_piste);
		});
	});
}

idol.world.download_click = function() {
	$(document).ready(function() {
		$('.plateforme').click(function() {
			try {
				var pageTracker = _gat._getTracker("UA-7091562-3");
				pageTracker._trackPageview("/download");
			} catch(err) {}
		})
	});
}

idol.world.init = function() {
	idol.world.update_disponibilities();
	idol.world.update_biographies();
	idol.world.track_click();
	idol.world.download_click();
}

function show_more() {
	$('#secondary').toggle("slow");
}

function getKeys(h) {
  var keys = [];
  for (var key in h)
    keys.push(key);
  return keys;
}

google.setOnLoadCallback(idol.world.init);