/* debug */
function debug(text) {
	if(window.console) {
		console.log(text);
	}
}

/* handler */
var startHeight = 0, endHeight = 14;
var slideshows = new Array();
var slideShowDelay = 4000, imageSwapTime = 1200;
var player, musicWindowName = 'ShirazMusicPlayer', musicPage = 'player.html', musicUrl = blog_path + '/music/' + musicPage, popup_settings = 'width=265,height=105,innerWidth=265,innerHeight=105,toolbar=no,menubar=no,scrollbars=no,resizeable=no,locationbar=no,status=no';

var showElement = function(el, time, op) {
	el.set('opacity', 0);
	el.set('visibility', 1);
	el.set('tween', {duration: time+'ms'});
	el.tween('opacity', op);
}

var hideElement = function(el, time, fn) {
	el.set('tween', {duration: time+'ms', onComplete: fn});
	el.tween('opacity', 0);
}

var swapImages = function() {
	this.currentIndex += 1;
	if(this.currentIndex >= this.urls.length)
	{
		this.currentIndex = 0;
	}
	
	if(this.currentIndex != 1 || this.images.length == this.urls.length)
	{
		this.container.getChildren()[0].dispose();
	}
	
	var newimg;
	//console.debug(this.images.length + " <= " + this.currentIndex);
	if(this.images.length <= this.currentIndex)
	{
		// TODO: wait for the loaded image. how?
		newimg = new Element('img', {'src': this.urls[this.currentIndex]});
		newimg.set('tween', {'duration': imageSwapTime});
		this.images[this.currentIndex] = newimg;
	} else {
		newimg = this.images[this.currentIndex];
	}
	newimg.setStyle('opacity', 0);
	newimg.inject(this.container.getChildren()[0], 'after');
	newimg.tween('opacity', 1);
	
	swapImages.delay(slideShowDelay, this);
}

var setElClass = function(cls) {
	this.set('class', cls);
}

var checkPlayer = function() {
	if(player != null && player.closed) {
		player.close();
		player = null;
	}
	
	return player != null;
}

var startMusic = function() {
	player = window.open(musicUrl, musicWindowName, popup_settings);
	if(checkPlayer()) {
		player.blur();
		player.opener = window;
		debug("turned on");
	}
}

var musicIsPlaying = function(check) {
	debug("music playing: " + check);
	if(check) {
		$('musicplayerlink').setProperty('class', 'on');
	} else {
		if(checkPlayer()) {
			player.close();
			player = null;
		}
		$('musicplayerlink').setProperty('class', '');
		Cookie.write("auto_start_player", "no", {duration: 365});
	}
}

window.addEvent('domready', function() {
	// check for running player
	if(Cookie.read("player_open") == "yes") { // to not open every time!
		debug("cookie sais open");
		player = window.open("", musicWindowName, popup_settings); // null instead of "" closes popup under ie8 and probably before
		if(player != null) {
			player.blur();
			if(player.location.href.substring(player.location.href.length - musicPage.length) != musicPage) {
				// seems it was not open after all so auto open or close again
				if(Cookie.read("auto_start_player") == "yes") {
					player.location.href = musicUrl;
				} else {
					Cookie.dispose("player_open");
					player.close();
					player = null;
				}
			}
		}
	}
	if(checkPlayer()) {
		debug("was on");
		player.opener = window;
		if(player.musicIsPlaying) {
			$('musicplayerlink').setProperty('class', 'on');
		}
	} else {
		if(Cookie.read("auto_start_player") == null) {
			debug("auto start");
			startMusic.delay(10);
		}
	}
	$('musicplayerlink').addEvent('click', function(e) {
	    e.stop();
	    if(checkPlayer()) {
	    	debug("stopped");
	    	musicIsPlaying(false);
	    	Cookie.dispose("player_open");
	    } else {
	    	debug("started");
	    	Cookie.dispose("auto_start_player");
	    	startMusic();
	    }
	});
	window.addEvent('unload', function() {
		if(checkPlayer()) {
			player.parentClosing();
		}
	});
	
	// image switcher
	slideshows.each(function(slideshow, index) {
		if(slideshow.urls.length > 1) {
			slideshow.container = $(slideshow.name);
			slideshow.images = new Array();
			slideshow.images.push(slideshow.container.getChildren()[0]);
			slideshow.currentIndex = 0;
			swapImages.delay(slideShowDelay - imageSwapTime, slideshow);
		}
	});
	
	showElement(document.id('content'), 1500, 1);
	
	if(menu_anim) {
    var i = 1;
    $('nav').getElements('li').each(function(el) {
      i++;
      var base = el.get('class');
      setElClass.delay((900 + i*100), el, base);
      setElClass.delay((500+i*100), el, el.get('class').replace('off', 'on'));
    });
	}
});
