
loadYUI("animation");
loadYUI("dom");
loadYUI("event");
loadYUI("container");

addOnLoad(initTemplate);


var menuAnim = 0;
var nav_hidden = false;
var mySimpleDialog = null;
var oModule = null;
var tmp_json = null;
var slider;
var render_div = document.getElementById("content_div");

function toggleHideNav() {	
	//if (!do_toggle) return;
	if (menuAnim!=0) {
		if (menuAnim.isAnimated()) return; 
	}
	menuAnim = new YAHOO.util.Anim('main_menu');
	
	if (nav_hidden) {
		menuAnim.attributes.left = { to: 0 };
		nav_hidden = false;
	} else {
		menuAnim.attributes.left = { to: -180 };
		nav_hidden = true;
	}
	menuAnim.duration = 0.3;
	menuAnim.method = YAHOO.util.Easing.easeOut;
	menuAnim.animate(); 
}

function initTemplate() {
	cycleBackgroundImage(1);
	checkBackgroundSize();

	/*
	var dialog_container = document.getElementById("dialog_container");
	mySimpleDialog = new YAHOO.widget.SimpleDialog("dlg", { 
			width: "700px", 
			height: "500px", 
			effect:{effect:YAHOO.widget.ContainerEffect.FADE,
			        duration:0.25}, 
					fixedcenter:true,
					modal:false,
				    visible:false,
				    zIndex:995,
				    close: false,
				    underlay: "none",
					draggable:false });
	mySimpleDialog.setHeader("Window");	
	mySimpleDialog.render(document.body);
	*/
	
	var tree = new YAHOO.widget.TreeView("menu_items");
	tree.render(); 

	
	

	
	var removeElement = function() {
	   var el = this.getEl();
	   el.parentNode.removeChild(el);
	}
	var myAnim = new YAHOO.util.Anim('page_loading', { 
		  opacity: {to: 0} 
		}, 1);
	myAnim.onComplete.subscribe(removeElement);
	myAnim.animate();

	if (first_url!=undefined) {
		setTimeout("loadDialog('"+first_url+"')", 500);
		//loadDialog(first_url);
	}
	if (has_mp3==1) {
		
		playTrack(1);
	}
	//setVolume(0);
	
	var init_volume = current_volume/2;

	 slider = YAHOO.widget.Slider.getHorizSlider("slider-bg", "slider-thumb", 0, 50);
	 slider.setValue(init_volume, true); 
	 slider.subscribe("change", sliderChange);	
	var flashvars = {};
	var params = { wmode: "opaque" };
	var attributes = {};

	 //swfobject.embedSWF("flash_slideshow_free/slideshow.swf", "background_container", "200", "200", "8", null, flashvars, params, attributes);
	 //swffit.fit("background_container", "200", "200");
}

var current_image_num = 1;
var num_images = 0;
function cycleBackgroundImage(image_num) {
	//alert(image_num);
	
	num_images = background_images.length - 1;
	var last_image = image_num-1;
	//if (last_image<1) last_image = num_images;
	if (image_num>num_images) {
		image_num = 1;
		last_image = num_images;
	}
    current_image_num = image_num;
    var anim = new YAHOO.util.Anim('background_image_'+image_num, {opacity: { to: 1 }}, 10);
    anim.useSeconds = false;

	var anim2 = new YAHOO.util.Anim('background_image_'+last_image, {opacity: { to: 0 }}, 10);
	anim2.useSeconds = false;
	 
	anim.animate();
	anim2.animate();

	var next_image_num = image_num+1;
	
	//if (BrowserDetect.browser!="Explorer" && num_images>1) {
	if (num_images>1) {
	
		setTimeout("cycleBackgroundImage("+next_image_num+")", 10000);
	}
}
/*
function showDialog() {
	//if (mySimpleDialog.cfg.getProperty('visible')) {
	mySimpleDialog.setHeader(tmp_json.window_title);
	mySimpleDialog.setBody(tmp_json.html);
	mySimpleDialog.show();
}

function hideDialog() {
	mySimpleDialog.hide();
}
*/

function showDialog() {
	if (mySimpleDialog!=null) {
		mySimpleDialog.destroy();
		mySimpleDialog = null;
	}
	mySimpleDialog = new YAHOO.widget.SimpleDialog("dlg", { 
			width: "700px", 
			height: "500px", 
			effect:{effect:YAHOO.widget.ContainerEffect.FADE,
			        duration:0.25}, 
					fixedcenter:false,
					modal:false,
				    visible:false,
				    zIndex:995,
				    close: false, 
				    underlay: "none",
					draggable:false,
					context:['content_div','bl','tr', ["beforeShow", "windowResize"]] });
	//mySimpleDialog.setHeader("Window");	
	
	
	//if (mySimpleDialog.cfg.getProperty('visible')) {
	mySimpleDialog.setHeader(tmp_json.window_title);
	mySimpleDialog.setBody(tmp_json.html);
	mySimpleDialog.render(document.body);
	mySimpleDialog.show();
}

function hideDialog() {
	if (mySimpleDialog!=null) {
		mySimpleDialog.destroy();
		mySimpleDialog = null;
	}
}

var loadDialogSuccess = function(obj_json){
	tmp_json = obj_json;
	var link_id = obj_json.link_id;
	var ytplayer = document.getElementById("myytplayer");
	if (ytplayer!=undefined) {
		ytplayer.stopVideo();
	}
	hideSWF();
	
	if (obj_json.link_id==1 || obj_json.link_id==2) {
		hideModuleContainer();
		showDialog();
		
	} else if (obj_json.link_id==3 || obj_json.link_id==4) {
		hideDialog();
		showModuleContainer(tmp_json.html);
		
	} else {
		hideDialog();
		//hideModuleContainer();
		showModuleContainer(tmp_json.html);
	}
	if (obj_json.link_id==4) {
		//alert("hi");
		if (volume_muted==0) {
			mutePlayer();
		}
		setTimeout("initYouTubePlayer('"+obj_json.init_image_tag+"', '"+obj_json.init_image_name+"')",750);
	} else {
		if (volume_muted==1) {
			unMutePlayer();
		}
	}
	
	if (obj_json.link_id==5) {
		setTimeout("embedSWF('"+obj_json.swf_file+"')",750);
	}
}

function hideModuleContainer() {
	var module_container = document.getElementById("module_container");
	module_container.style.zIndex = 1;
	var anim = new YAHOO.util.Anim("module_container", {opacity: { to: 0 }}, .3);
	anim.onComplete.subscribe(destroyModuleContainer);
	anim.animate();
}

function destroyModuleContainer() {
	var module_container = document.getElementById("module_container");
	module_container.innerHTML = '';
}

function showModuleContainer(module_content) {
	var module_container = document.getElementById("module_container");
	module_container.innerHTML = module_content;
	module_container.style.zIndex = 995;
	var anim = new YAHOO.util.Anim("module_container", {opacity: { to: 1 }}, .3);
	anim.animate();
}

function embedSWF(swf_file) {
	//<div id=swf_container></div>
	var swf_container_container = document.getElementById("swf_container_container");
	var newdiv = document.createElement('div');
	newdiv.id = "swf_container";
	swf_container_container.appendChild(newdiv);

	
	
	swf_container_container.style.zIndex = 997;
	var params = { wmode: "transparent" };
	swfobject.embedSWF(swf_file, "swf_container", "95%", "95%", "8", null, null, params);
	//var anim = new YAHOO.util.Anim("swf_container", {opacity: { to: 1 }}, .3);
	//anim.animate();
	//swffit.fit("background_container", "200", "200");
}

function hideSWF(swf_file) {
	var swf_container = document.getElementById("swf_container");
	if (swf_container!=undefined) {
		
	
	var swf_container_container = document.getElementById("swf_container_container");
	swf_container_container.style.zIndex = 1;
	swfobject.removeSWF("swf_container");
	}
	
	//var anim = new YAHOO.util.Anim("swf_container", {opacity: { to: 0 }}, .3);
	//anim.animate();
}

var loadModuleEvent = function () {
	//alert("hi");
	//oModule.setBody(tmp_json.html);
	//mySimpleDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
	//oModule.show();
	//oModule.hideEvent.unsubscribe(loadModuleEvent);
}

var loadDialogEvent = function () {
	//alert("hi");
	mySimpleDialog.setHeader(tmp_json.window_title);
	mySimpleDialog.setBody(tmp_json.html);
	//mySimpleDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
	mySimpleDialog.show();
	mySimpleDialog.hideEvent.unsubscribe(loadDialogEvent);
}

function loadDialog(bb_url) {
	makeRequest(loadDialogSuccess, bb_url);
}

var tmp_image_src = null;
var tmp_image_name = null;

function swapGalleryImage(image_src, image_name) {
	tmp_image_src = image_src;
	tmp_image_name = image_name;
	//var gallery_image_container = document.getElementById("gallery_image_container");
	
	var anim = new YAHOO.util.Anim("gallery_image_container", {opacity: { to: 0 }}, .3);
	try_count=0;
	anim.onComplete.subscribe(swapGalleryImageEvent);
	anim.animate();
}

var try_count = 0;
var swapGalleryImageEvent = function() {
	var gallery_main_image = document.getElementById("gallery_main_image");
	var gallery_main_name = document.getElementById("gallery_main_name");
	
	var heavyImage = new Image(); 
	heavyImage.src = tmp_image_src;
	
	if (!heavyImage.complete && try_count<5) {
		setTimeout("swapGalleryImageEvent()", 500);
		try_count = try_count+1;
		//alert("waiting "+try_count);
		return false;
	}
	try_count=0;
	gallery_main_image.src = tmp_image_src;
	gallery_main_name.innerHTML = tmp_image_name;
	
	var anim = new YAHOO.util.Anim('gallery_image_container', {opacity: { to: 1 }}, .3);
	anim.animate();
}

function initYouTubePlayer(image_tag, image_name) {
	if (image_tag.length<5) {
		document.getElementById("ytapiplayer").innerHTML="no videos found";
		return;
	}
	var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
    //var myytplayer = document.getElementById("myytplayer");
   // if (myytplayer!=undefined) {
    //	swfobject.removeSWF("myytplayer");
    //}
    var ytplayer = document.getElementById("ytapiplayer"); 
    
    if (ytplayer!=undefined) {
    
    swfobject.embedSWF("http://www.youtube.com/v/"+image_tag+"&enablejsapi=1&playerapiid=ytplayer", 
                       "ytapiplayer", "640", "390", "8", null, null, params, atts, startYouTubePlayer);
    } else {
    	setTimeout("initYouTubePlayer("+image_tag+", "+image_name+")", 1500);
    }
}

var startYouTubePlayer = function() {

	setTimeout("startYouTubePlayerEvent()", 1500);
	
}

function startYouTubePlayerEvent() {
	var ytplayer = document.getElementById("myytplayer");   

	if (ytplayer!=undefined) {
		ytplayer.playVideo();
	}
	
}


function swapYouTubeImage(image_tag, image_name) {
	var ytplayer = document.getElementById("myytplayer");
	ytplayer.loadVideoById(image_tag);
	
	var gallery_main_name = document.getElementById("youtube_main_name");
	gallery_main_name.innerHTML = image_name;
}

var swapYouTubeImageEvent = function() {
	var gallery_main_image = document.getElementById("gallery_main_image");
	var gallery_main_name = document.getElementById("gallery_main_name");
	
	gallery_main_image.src = tmp_image_src;
	gallery_main_name.innerHTML = tmp_image_name;
	
	var anim = new YAHOO.util.Anim('gallery_image_container', {opacity: { to: 1 }}, .3);
	anim.animate();
}

var current_volume = 75;

var myListener = new Object();

/**
 * Initialisation
 */
myListener.onInit = function()
{
    this.position = 0;
};
/**
 * Update
 */
var current_track=1;
var player_stopped=0;
myListener.onUpdate = function()
{
	/*
    document.getElementById("info_playing").innerHTML = this.isPlaying;
    document.getElementById("info_url").innerHTML = this.url;
    document.getElementById("info_volume").innerHTML = this.volume;
    document.getElementById("info_position").innerHTML = this.position;
    document.getElementById("info_duration").innerHTML = this.duration;
    document.getElementById("info_bytes").innerHTML = this.bytesLoaded + "/" + this.bytesTotal + " (" + this.bytesPercent + "%)";
    
    var isPlaying = (this.isPlaying == "true");
    document.getElementById("playerplay").style.display = (isPlaying)?"none":"block";
    document.getElementById("playerpause").style.display = (isPlaying)?"block":"none";
    
    var timelineWidth = 160;
    var sliderWidth = 40;
    var sliderPositionMin = 40;
    var sliderPositionMax = sliderPositionMin + timelineWidth - sliderWidth;
    var sliderPosition = sliderPositionMin + Math.round((timelineWidth - sliderWidth) * this.position / this.duration);
    
    if (sliderPosition < sliderPositionMin) {
        sliderPosition = sliderPositionMin;
    }
    if (sliderPosition > sliderPositionMax) {
        sliderPosition = sliderPositionMax;
    }
    
    document.getElementById("playerslider").style.left = sliderPosition+"px";
   */
	//document.getElementById("test_text").value = this.isPlaying;
	//alert( this.isPlaying);
	if (this.isPlaying=="false" && player_stopped!=1) {
		//alert("playing next track");
		next_track_id = current_track + 1;
		
		if (next_track_id>mp3_playlist.length) {
			next_track_id = 1;
		}
		playTrack(next_track_id);
		
	}
	
};

function playTrack(track_id) {
	var max_num = mp3_playlist.length-1;
	if (track_id>max_num) {
		track_id = 1;
	}
	
	if (track_id < 1) {
		track_id = max_num;
	}
	
	current_track = track_id;
	play(mp3_playlist[track_id]);
	document.getElementById("track_name").innerHTML=track_id+": "+mp3_titles[track_id];
}

function nextTrack() {
	var next_track_id = current_track+1;
	playTrack(next_track_id);
}

function prevTrack() {
	var next_track_id = current_track-1;
	playTrack(next_track_id);
}

var volume_muted = 0;

function mutePlayer(){
	volume_muted = 1;
	continueVolumeTo(current_volume, 0);
}

function unMutePlayer(){
	volume_muted = 0;
	continueVolumeTo(0, current_volume);
}

function volumeTo(target_volume) {
	continueVolumeTo(current_volume, target_volume);
}

function continueVolumeTo(this_volume, target_volume) {
	setVolume(this_volume);
	if (this_volume==target_volume) return;
	if (this_volume>target_volume) {
		this_volume = this_volume-5;
		if (this_volume<target_volume) this_volume = target_volume;
	} else {
		this_volume = this_volume+5;
		if (this_volume>target_volume) this_volume = target_volume;
	}
		
	setTimeout("continueVolumeTo("+this_volume+", "+target_volume+")", 25);
}

function getFlashObject()
{
    return document.getElementById("player_container");
}
function play(mp3_file)
{
	
	player_stopped = 0;;
	if (has_mp3==0) return;
	//alert("playing: "+mp3_file);
    //if (myListener.position == 0) {
       getFlashObject().SetVariable("method:setUrl", mp3_file);
    //}
    getFlashObject().SetVariable("method:play", "");
    getFlashObject().SetVariable("enabled", "true");
}

function continuePlay()
{
	player_stopped = 0;
	document.getElementById("track_pause").src="templates/default/images/track-pause.jpg";

	if (has_mp3==0) return;
	
    getFlashObject().SetVariable("method:play", "");
    getFlashObject().SetVariable("enabled", "true");
    moveEQ(current_volume);
}

function pause()
{
	player_stopped = 1;
	document.getElementById("track_pause").src="templates/default/images/track-play.jpg";

    getFlashObject().SetVariable("method:pause", "");
    moveEQ(0);
}

function togglePause() {
	if (player_stopped==1) {
		continuePlay();
	} else {
		pause();
	}
}

function stop()
{
	player_stopped = 1;
	if (has_mp3==0) return;
    getFlashObject().SetVariable("method:stop", "");
}

function setPosition()
{
	if (has_mp3==0) return;
    var position = document.getElementById("inputPosition").value;
    getFlashObject().SetVariable("method:setPosition", position);
}

function setVolume(volume)
{
	if (has_mp3==0) return;
    //var volume = document.getElementById("inputVolume").value;
    getFlashObject().SetVariable("method:setVolume", volume);
    moveEQ(volume);
}

function moveEQ(volume) {
	var eq_container = document.getElementById("eq_container");
	var new_top = 25 - volume/4;
	//document.getElementById("test_text").value = eq_container.style.top;
	//alert(new_top);
	eq_container.style.top = new_top+"px";
}

var sliderChange = function() {
	var actualValue = slider.getValue();
	var new_volume = actualValue*2;
	setVolume(new_volume);
	current_volume = actualValue;
}

function checkBackgroundSize() {
	//alert(num_images);
	if(typeof(num_images) == 'undefined') {
		//alert("undefined");
		return;
	}
	
	if (num_images>1) {
		//alert("num images");
		return;
	}
	var background = document.getElementById("background_image_image_"+current_image_num);
	var main_body = document.getElementById("main_body");
	
	if (background.offsetHeight<main_body.offsetHeight) {
		//alert("yes");
		background.style.height = "100%";
		background.style.width = '';
	} else if (background.offsetWidth<main_body.offsetWidth) {
		//alert("no");
		background.style.width = "100%";
		background.style.height = '';
	}
	//alert("hi");
	//alert(background.offsetHeight+" "+main_body.offsetHeight);
	
}