window.addEvent('domready', function() {
    var videoImgs =  $$('#mediaPagesVideo img');
    var imgAlts = videoImgs.get('alt');
    var foundIndex = 0;

    Array.each(imgAlts, function(value, index) {
        if (location.hash == ("#" + string_to_slug(value) ) ) foundIndex = index;
    });

    var loadString = videoImgs[foundIndex].getParent().get('onclick');
    altTitle = videoImgs[foundIndex].get("alt")
    
    loadString = loadString.substr(0, loadString.lastIndexOf(")") )
    var params = loadString.substr( loadString.indexOf("(") +1,loadString.length )

    var paramsArray = eval("[" + params + "]");

    load_video_withslug(paramsArray[0], paramsArray[1], altTitle)    
});



function load_video_withslug(_media_url, _video_file, _slug, _width, _height, _id, _noauto) {
    _slug = string_to_slug(_slug);
    location.hash = _slug;
    load_video(_media_url, _video_file, _width, _height, _id, _noauto, _slug)
}

function string_to_slug(str) {
  str = str.replace(/^\s+|\s+$/g, ''); // trim
  str = str.toLowerCase();
  
  // remove accents, swap ñ for n, etc
  var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
  var to   = "aaaaeeeeiiiioooouuuunc------";
  for (var i=0, l=from.length ; i<l ; i++) {
    str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
  }

  str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
    .replace(/\s+/g, '-') // collapse whitespace and replace by -
    .replace(/-+/g, '-'); // collapse dashes

  return str;
}

function load_video(media_url, video_file, width, height, id, noauto) {
    var autoPlay = "true";
    if (noauto) {
        autoPlay = "false";
    }
    var flashvars = 
	{
		videoURL: video_file,
		autoPlay: autoPlay
	};
	
	if (!id) {
	    id = "multimediaMoviePlayer";
	}

	var params = 
	{
        bgcolor: "#000000",
		allowfullscreen: "true",
		allowscriptaccess: "always",
		allownetworking: "all",
		movie: media_url + "front/flash/dynamic-video-player/JohannusDynamicVideo.swf"		
	};

	var attributes = 
	{
		id: id
	};

    if (width == undefined) {
        width = "860";
    }    
    if (height == undefined) {
        height = "484";
    }
	swfobject.embedSWF(media_url + "front/flash/dynamic-video-player/JohannusDynamicVideo.swf", id, width, height, "9.0.0", media_url + "font/flash/video-player/playerProductInstall.swf", flashvars, params, attributes );
}

function onVideoPlaying()
{
    document.getElementById('audioplayer').stop();
}


function load_audio(media_url, audio_file, argTitle, argAuthor) {
	var flashvars = 
	{
		musicURL: audio_file,
		Author: argAuthor,
		Title: argTitle,
		autoPlay: 'false'		
	};

	var params = 
	{
        bgcolor: "#fff",
		allowfullscreen: "true",
		allowscriptaccess: "always",
		allownetworking: "all",
		wmode: "transparent"
	};

	var attributes = 
	{
		id: "multimediaAudioPlayer"
	};
//	swfobject.addLoadEvent(function () { playMP3(audio_file, argTitle, argAuthor) });
	swfobject.embedSWF(media_url + "front/flash/dynamic-music-player/JohannusDynamicMusic.swf", "multimediaAudioPlayer", "815", "50", "9.0.0", media_url + "font/flash/video-player/playerProductInstall.swf", flashvars, params, attributes );
	
}

function playMP3( audio_file, argTitle, argAuthor) {
	$('multimediaAudioPlayer').playMP3(audio_file, argTitle, argAuthor);
}
