  $(document).ready(function() { initPlayer(); });

  //initplayer
  function initPlayer() {
	// hide this video
	$('div#am_Heckscher').hide();

	// bind the click of the play button of playlist/radio/podcast.
	$("a").click(function(e){

		if($(this).hasClass("linkListen")
				|| $(this).hasClass("am_blocContentRadioPlay")
				|| $(this).hasClass("playlistTrackListen")
				|| $(this).hasClass("playMyPlaylist") ){
            e.preventDefault(); // so we won't follow the link.

            dialogBox("Extinction de voix : pour achever sa mue, Allomusic va se taire très provisoirement. Pour revenir très vite avec une offre <b>d'écoute de musique gratuite, légale, complète et actualisée chaque semaine</b> :  tous les singles et albums des Majors (Universal, Sony, EMI, Warner) et toujours plus de jeunes talents et d'indépendants... Dès le mois d'août, tous les titres, toutes les nouveautés, bref, toute la musique sera sur AlloMusic. En prime, une surprise sonnante et trébuchante : <b>sur AlloMusic, on gagnera à écouter !</b>","Extinction de voix");
            return false;
        }
        /*
		if($(this).hasClass("linkListen")
				|| $(this).hasClass("am_blocContentRadioPlay")
				|| $(this).hasClass("playlistTrackListen")
				|| $(this).hasClass("playMyPlaylist") ){
			//if( typeof(top.frames['am_frmOhlin']) != "undefined"){
				//ok get the stuff data from the accesskey (need a better way to pass data.
                var play='';
				e.preventDefault(); // so we won't follow the link.
				var thiis = $(this);
				var thisid = $(this).attr('id');
                
				//playlist in build mode cannot manage multiple forms in a form
				if($(this).hasClass("playlistTrackListen")){
					var data = $(this).children("input#playlistFormId").attr("value");
				}else{
					var data = $(this).siblings("form").children("input#playlistFormId").attr("value");
				}

                data = urldecode(data);
				var evaledData = eval('(' + data + ')');

            if( typeof(top.frames['am_frmOhlin']) != "undefined"){
                //one track
				if ( typeof( evaledData.items ) == "undefined" ) {
						parent.frames['am_frmOhlin'].player.setPlaylist([evaledData]);
						//not item, so index defined
						evaledData.index = 0;

				}else{
					//playlist provided
					parent.frames['am_frmOhlin'].player.setPlaylist(evaledData.items);
				}
				//set current position
				parent.frames['am_frmOhlin'].player.setPosition(evaledData.index);
				//force reinit
				parent.frames['am_frmOhlin'].player.playPause();
				return false;
			} else {
                if ( typeof( evaledData.items ) == "undefined" ) {
                    play='track';
                } else {
                    play='playlist';
                    if($(this).attr('type') != '') play = $(this).attr('type');
                }
                var url = document.location.href;
                var baseUrl = 'http://www.allomusic.com';
                var urlClean =  url.substr(baseUrl.length);
                if (urlClean.indexOf("#")>0) urlClean = urlClean.substring(0,urlClean.indexOf("#"));
                if(urlClean=="" || urlClean=="/") urlClean="/accueil";
                //var play = data;
                window.location = baseUrl +'/direct/?v='+urlClean+'&play='+play+'&id='+thisid;
                return false;
            }
        
		}
        */
	});

	$(".addToMyPlaylist").click(function(e){
		 e.preventDefault();
		 var element = $(this);
		 var urlChange = element.attr('href');
		 var urlBack = top.frames['am_frmContent'].location.pathname;
		 dialogAjaxCall(urlChange,'Ajouter à ma playlist  ',urlBack);
         return false;
	});

	// bind the click of the play button of playlist/radio/podcast.
	$("a#am_ohlinVideoPlayFromArtist").click(function(e) {
		if( typeof(top.frames['am_frmOhlin']) != "undefined"){
			//ok get the stuff data from the accesskey (need a better way to pass data.
			e.preventDefault(); // so we won't follow the link.
			var data = $('a#am_ohlinVideoPlayFromArtist').attr('accesskey');
			data = urldecode(data);
			var evaledData = eval('(' + data + ')');

			$.each(evaledData.items, function(i,item){
				var jiwaTrack;
				if(item.link != null && item.type == "video"){
					///console.log(item.artistName + '/' + item.title + ' has a non null link and is a video.');
					// play this video in the player.
					href = item.link;

					//modify it as to put a convinient width.
					replaceExpr = new RegExp("&width=335", "i");
					videoSrc = href.replace(replaceExpr,"&width=290px");

					// Build the div content.
					video = '<div style="width:390px;height:285px;"><iframe src="' + videoSrc + '" width="290px" height="100%" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" hspace="0" vspace="0"></iframe>';

					$('div#am_HeckscherContent').empty().html(video);
					var offset = $('a#am_HeckscherVideoHead').offset();
					if(offset == null){
						var destination = 0;
					}else{
						var destination = offset.top;
					}
					$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1000 )
					$('div#am_Heckscher').show();
				}
				else {
					///console.log(item.artistName + '/' + item.title + ' has a null link or is not video.');
				}
			 });
		}
	});


	// bind the click of the video <a> so we can open the fuckin' player. I'm so tired.
	$('li > a#am_HeckscherLink').click(function(e) {
		e.preventDefault(); // so we won't follow the link.

		//get the href.
		href = ($(this).attr("href"));

		//modify it as to put a convinient width.
		replaceExpr = new RegExp("&width=335", "i");
		videoSrc = href.replace(replaceExpr,"&width=290px");

		// Build the div content.
		video = '<div style="width:390px;height:285px;"><iframe src="' + videoSrc + '" width="290px" height="100%" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" hspace="0" vspace="0"></iframe>';

		//$('div#am_HeckscherContent').empty().html(video); //fix  empty not working
		$('div#am_HeckscherContent').html(video);
		var offset = $('a#am_Heckscher').offset();
		if(offset == null){
			var destination = 0;
		}else{
			var destination = offset.top;
		}
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1000 );
		$('div#am_Heckscher').show();

	});

	$('div > a#am_HeckscherLink').click(function(e) {
		e.preventDefault(); // so we won't follow the link.

		//get the href.
		href = ($(this).attr("href"));

		//modify it as to put a convinient width.
		replaceExpr = new RegExp("&width=335", "i");
		videoSrc = href.replace(replaceExpr,"&width=290px");

		// Build the div content.
		video = '<div style="width:390px;height:285px;"><iframe src="' + videoSrc + '" width="290px" height="100%" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" hspace="0" vspace="0"></iframe>';

		//$('div#am_HeckscherContent').empty().html(video); //fix  empty not working
		$('div#am_HeckscherContent').html(video);
		var offset = $('a#am_Heckscher').offset();
		if(offset == null){
			var destination = 0;
		}else{
			var destination = offset.top;
		}
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1000 );
		$('div#am_Heckscher').show();

	});

	// bind the click of the video <a> so we can open the fuckin' player. I'm so tired.
	$('li > a#am_HeckscherLinkLarge').click(function(e) {
		e.preventDefault(); // so we won't follow the link.

		//get the href.
		href = ($(this).attr("href"));

		//modify it as to put a convinient width.
		replaceExpr = new RegExp("&width=335", "i");
		videoSrc = href.replace(replaceExpr,"&width=749px");

		//modify it as to put a convinient width.
		replaceExpr = new RegExp("&height=285", "i");
		videoSrc = videoSrc.replace(replaceExpr,"&height=435px");

		// Build the div content.
		video = '<div style="width:749px;height:435px;"><iframe src="' + videoSrc + '" width="749px" height="100%" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" hspace="0" vspace="0"></iframe>';

		$('div#am_HeckscherLargeContent').empty().html(video);
		var offset = $('a#am_HeckscherVideoHeadLarge').offset();
		if(offset == null){
			var destination = 0;
		}else{
			var destination = offset.top;
		}
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1000 )
		$('div#am_HeckscherLarge').show();

	});




	// bind the click of the video <a> so we can open the fuckin' player. I'm so tired.
	$('li > a#am_HeckscherLinkPopup').click(function(e) {
		e.preventDefault(); // so we won't follow the link.

                //get the href.
		href = ($(this).attr("href"));

		//modify it as to put a convinient width.
		replaceExpr = new RegExp("&width=335", "i");
		videoSrc = href.replace(replaceExpr,"&width=290px");
		// Build the div content.
		video = '<div style="width:749px;height:435px;"><iframe src="' + videoSrc + '" width="749px" height="100%" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" hspace="0" vspace="0"></iframe>';
                dialogBoxVideo(video,'');
        
        pageTracker._trackPageview();

	});

	$('div > a#am_HeckscherLinkPopup').click(function(e) {
		e.preventDefault(); // so we won't follow the link.
		//get the href.
		href = ($(this).attr("href"));

		//modify it as to put a convinient width.
		replaceExpr = new RegExp("&width=335", "i");
		videoSrc = href.replace(replaceExpr,"&width=290px");
		// Build the div content.
		video = '<div style="width:749px;height:435px;"><iframe src="' + videoSrc + '" width="749px" height="100%" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" hspace="0" vspace="0"></iframe>';
                dialogBoxVideo(video,'');
        
        pageTracker._trackPageview();

	});



  }

  /**
   * call ajax url in dialog box
   * provide urlback to return to
   *
   * @param string url
   * @param string dialogtitle
   * @param string urlback
   * @return void
   */
  function dialogAjaxCall(url, dialogtitle, urlback)
  {
	 $.ajax({
         type: "GET",
         url: url,
         data :{backurl : urlback},
         cache: false,
         beforeSend : function(msg){
         },
         success: function(result){
        	 //dialog box
        	 dialogBox(result,dialogtitle);
         }
     });
  }


  /**
   * jquery dialog implementation
   *
   * @param string txt
   * @param string dialogtitle
   * @return void
   */
  function dialogBox(txt, dialogtitle)
  {
		 //dialog box
		 $("#dialog").html(txt);
	     $("#dialog").dialog({
	           bgiframe:true,
	           autoOpen:false,
	           draggable:true,
	           modal:true,
	           resizable:false,
	           title: dialogtitle,
	           closeOnEscape:true,
	           buttons:{
	             "fermer":function() {
	                 $(this).dialog('destroy');
	             }
	         }
	     });
	     $("#dialog").dialog( 'open' );
  }

      /**
       * jquery dialog implementation
       *
       * @param string txt
       * @param string dialogtitle
       * @return void
       */
      function dialogBoxVideo(txt, dialogtitle)
      {
                     //dialog box
                     $("#dialog").html(txt);
                 $("#dialog").dialog({
                       bgiframe:true,
                       autoOpen:false,
                       draggable:true,
                       modal:true,
                       resizable:false,
                       width: 780,
                       height: 560,
                       title: dialogtitle,
                       closeOnEscape:true,
                       buttons:{
                         "fermer":function() {
                             $(this).dialog('destroy');
                         }
                     }
                 });
                 $("#dialog").dialog( 'open' );
      }

	function urldecode (str) {
	    // Decodes URL-encoded string
	    //
	    // version: 909.322
	    // discuss at: http://phpjs.org/functions/urldecode
	    // +   original by: Philip Peterson
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +      input by: AJ
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   improved by: Brett Zamir (http://brett-zamir.me)
	    // +      input by: travc
	    // +      input by: Brett Zamir (http://brett-zamir.me)
	    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   improved by: Lars Fischer
	    // +      input by: Ratheous
	    // +   improved by: Orlando
	    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
	    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
	    // *     returns 1: 'Kevin van Zonneveld!'
	    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
	    // *     returns 2: 'http://kevin.vanzonneveld.net/'
	    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
	    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'

	    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';

	    var replacer = function (search, replace, str) {
	        var tmp_arr = [];
	        tmp_arr = str.split(search);
	        return tmp_arr.join(replace);
	    };

	    // The hash_map is identical to the one in urlencode.
	    hash_map["'"]   = '%27';
	    hash_map['(']   = '%28';
	    hash_map[')']   = '%29';
	    hash_map['*']   = '%2A';
	    hash_map['~']   = '%7E';
	    hash_map['!']   = '%21';
	    hash_map['%20'] = '+';
	    hash_map['\u00DC'] = '%DC';
	    hash_map['\u00FC'] = '%FC';
	    hash_map['\u00C4'] = '%D4';
	    hash_map['\u00E4'] = '%E4';
	    hash_map['\u00D6'] = '%D6';
	    hash_map['\u00F6'] = '%F6';
	    hash_map['\u00DF'] = '%DF';
	    hash_map['\u20AC'] = '%80';
	    hash_map['\u0081'] = '%81';
	    hash_map['\u201A'] = '%82';
	    hash_map['\u0192'] = '%83';
	    hash_map['\u201E'] = '%84';
	    hash_map['\u2026'] = '%85';
	    hash_map['\u2020'] = '%86';
	    hash_map['\u2021'] = '%87';
	    hash_map['\u02C6'] = '%88';
	    hash_map['\u2030'] = '%89';
	    hash_map['\u0160'] = '%8A';
	    hash_map['\u2039'] = '%8B';
	    hash_map['\u0152'] = '%8C';
	    hash_map['\u008D'] = '%8D';
	    hash_map['\u017D'] = '%8E';
	    hash_map['\u008F'] = '%8F';
	    hash_map['\u0090'] = '%90';
	    hash_map['\u2018'] = '%91';
	    hash_map['\u2019'] = '%92';
	    hash_map['\u201C'] = '%93';
	    hash_map['\u201D'] = '%94';
	    hash_map['\u2022'] = '%95';
	    hash_map['\u2013'] = '%96';
	    hash_map['\u2014'] = '%97';
	    hash_map['\u02DC'] = '%98';
	    hash_map['\u2122'] = '%99';
	    hash_map['\u0161'] = '%9A';
	    hash_map['\u203A'] = '%9B';
	    hash_map['\u0153'] = '%9C';
	    hash_map['\u009D'] = '%9D';
	    hash_map['\u017E'] = '%9E';
	    hash_map['\u0178'] = '%9F';
	    hash_map['\u00C6'] = '%C3%86';
	    hash_map['\u00D8'] = '%C3%98';
	    hash_map['\u00C5'] = '%C3%85';

	    for (unicodeStr in hash_map) {
	        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
	        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
	    }
	    //console.log(ret);
	    // End with decodeURIComponent, which most resembles PHP's encoding functions
	    ret = decodeURIComponent(ret);

	    return ret;
	}


	function isArray(obj) {
		return obj.constructor == Array;
	}




	
