//if javascript is enabled, hide the media data content.
document.write("<style type=\"text/css\"> #media-data{ display:none; } </style>");

var scrollable_attributes = {api: true, size: 4, speed: 500};

var jsReady = false;

jQuery(document).ready(function(){

	jsReady = true;
	
	/*set the state of the audio controls */
	var audio_state = $("#audio-controls li#state").text();
	toggleAudio(audio_state);
	
	$("#flash").show();
	$("#media").removeClass("hide");
	
	//assign click events
	$("#audio-controls li#on a").click(function(){changeSound("1")});
	$("#audio-controls li#off a").click(function(){changeSound("0")});
	$("#video a").click(function(){mediaFilter("video")});
	$("#screenshot a").click(function(){mediaFilter("screenshot")});
	$("#wallpaper a").click(function(){mediaFilter("wallpaper")});
	
	//make the controls visible
	$("#audio-controls").removeClass('hide');
	
	//loop the data
	$("#media-data img").each(function(){		
		//move the image into the media div		
		$(this)
			.click(function(){
	        	changeSlide($(this).attr("id"))
		    })
			.appendTo("#media-thumbs")
			.attr("style", "display:none");
	});
	
	//load up the videos by default and make the first one active;
	mediaFilter("video");	
	var videos = $("#media-thumbs img.video");
	$(videos[0]).addClass("active");
	
});

function isReady(){
	return jsReady;
}

function mediaFilter(type){
	
	$("#media-filter li a").removeClass("active");
	$("#"+type+" a").addClass("active");
	
	var images = $("#media-thumbs img");
	
	//clear active class on all
	images.removeClass("active");
	
	images.fadeOut('fast', function(){	
		//make sure they have all faded out.. then call scrollable to initialize
		if(images.index(this) == images.length-1) {
			var api = $("div.scrollable").scrollable(scrollable_attributes);
			api.getConf().item = "."+type;
			api.reload();
			api.begin();
		}
	});
	//fade in the selected set
	$("#media-thumbs img."+type).fadeIn(1000);
	
}


function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function changeSlide(value) {
	thisMovie("Media").sendCharToActionScript(value);
}

function changeLanguage(value) {
	thisMovie("Media").sendLangToActionScript(value);
}

function changeSound(value) {	
	var exdate = new Date(); 
	exdate.setDate(exdate.getDate()+180);
	document.cookie='Settings[sound]' + "=" +escape(value)+ ";path=/;expires="+exdate.toGMTString();
	thisMovie("Media").sendGlobalSoundToActionScript(value);	
	
	toggleAudio(value);
}

function toggleAudio(value){
	$("#audio-controls li a").removeClass("inactive");
	if(value == 0){
		$("#audio-controls li#on a").addClass("inactive");
	} else {
		$("#audio-controls li#off a").addClass("inactive");
	}
}
