var ill = {

mainContent: null,
mainContentOH: null,
rawContent: null,
rawContentDefault: null,
rawContentMH: null,
hashEvent: '',

player: null,
playList: null,
playerSetts: null,

log: function(msg) {
	if (typeof window.console != 'undefined' && typeof window.console.log != 'undefined') {
		window.console.log(msg);
	}
},
preinit: function () {
	var currLoc = window.location.href;
	if (currLoc == 'http://'+window.location.hostname+'/' || currLoc == 'http://'+window.location.hostname) {
		window.location.href = 'http://'+window.location.hostname+'/index.php#';
	}
	
	soundManager.url = 'illegal/soundmanager2/';
	soundManager.flashVersion = 9;
	soundManager.usePeakData = true;
	soundManager.useHTML5Audio = false;
	soundManager.useHighPerformance = false;
	soundManager.consoleOnly = true;
	soundManager.debugMode = false;
	soundManager.debugFlash = false;
	soundManager.waitForWindowLoad = true;
	soundManager.wmode = 'transparent';

	soundManager.onready(function() {
	if (soundManager.supported()) {
		ill.player = soundManager;
		ill.player.usePeakData = true;
	} else {
		ill.player = false;
		jQuery('div#illPlayer').html('player not supported!');
	}
	});

},
init: function() {
	jQuery(window).unload(ill.deinit);
	jQuery(window).focus(ill.focus);
	
	ill.rawContent = jQuery('div#contentRaw');
	ill.rawContentDefault = ill.rawContent.html();
	ill.rawContentMH = ill.rawContent.height();
	ill.mainContent = jQuery('div#contentMain');
	ill.mainContentOH = ill.mainContent.height()  - ill.rawContentMH + ill.mainContent.css('padding-top') + ill.mainContent.css('padding-bottom');
	var tempMainContentHeight = ill.mainContent.height() + ill.mainContentOH;
	ill.mainContent.css('height', tempMainContentHeight+'px');
	ill.mainContent.css('overflow', 'hidden');
	
	ill.hijack('body');

	var currLoc = window.location.href;
	if (currLoc.indexOf('/index.php#') != -1 && currLoc.lastIndexOf('#') != currLoc.length-1) {
		ill.doLoad(currLoc.replace('/index.php#', '/index.php?'));
	} else {
		ill.doLoad('/index.php?page=main_home');
	}

	jQuery(window).bind( 'hashchange', function( eo ) {
		if (jQuery && ill.hashEvent != eo.fragment) { 
			ill.doLoad('/index.php?'+eo.fragment);
			ill.hashEvent = eo.fragment;
			eo.stopPropagation();		
		}
	});
	jQuery(window).trigger( 'hashchange' );

	ill.playerInit();	
	ill.playerGetList();
	ill.log('illinit done!');
},
deinit: function(eo) {
	ill.playerDeinit();
},
focus: function	(eo) {
	ill.playerFocus();
},

hijack: function(base) {
	jQuery(base).find('a[href^=http://'+window.location.hostname+']:not([rel=nofollow])')
				.each(function(index, obj) {
		var jObj = jQuery(obj);
		jObj.attr('href', jObj.attr('href').replace('/index.php?', '/index.php#'));
//		jObj.click(ill.loadContent);
	});
	ill.log('anchors hijacked');
},
loadContent: function(eo) {
	var jObj = jQuery(eo.target);
	ill.doLoad(jObj.attr('href').replace('/index.php#', '/index.php?'));
},
doLoad: function(url) {
	ill.mainContent.animate({height: ill.rawContentMH+ill.mainContentOH });	
	ill.rawContent.html(ill.rawContentDefault);
	ill.rawContent.load('http://'+window.location.hostname+url, 
						function(rText, rStatus, xhr) {
							ill.hijack(ill.rawContent);
							ill.mainContent.animate({height: ill.rawContent.height()+ill.mainContentOH });
						});
},

playerInit: function() {
	jQuery('div#illPControls').disableTextSelect();
	if (jQuery.cookie('illPlayer') != null) { 
		ill.log('player already active!');
		return;
	}
	window.name = 'illPlayer';
	ill.playerSetts = {active: true, volume: 50};
	ill.playerStoreSetts;
	jQuery('div#illPPeakV').attr('class', 'illPPeak illPPeakVal'+ill.playerCalcPeak(50, 100));
		
},
playerDeinit: function() {
	if (window.name == 'illPlayer') {
		jQuery.cookie('illPlayer', null);
		window.name = null;
	}
},
playerFocus: function() {
	ill.log('got focus');
},
playerGetList: function() {
	jQuery.getJSON(
		'http://'+window.location.hostname+'/index.php?page=playlist', 
		function(data, textStatus, xhr) {
			var init = (ill.playList == null);
			ill.playList = data;
			if (init) {
				jQuery('div#illPName').text(ill.playList[0].title);
				ill.playerLoad();
			}
		});
},
playerLoad: function() {
	if (ill.player == false) {return; }

	var idx = null;
	var trackName = jQuery('div#illPName').text();
	for (idx in ill.playList) {
		if (ill.playList[idx].title == trackName) {
			break;
		}
	}
	if (idx != null) {
		if (ill.player == null) {
			window.setTimeout('ill.playerLoad();', 1000);
			return;
		}
		ill.player.createSound({
			id: 'illOST',
			url: 'http://'+window.location.hostname+ill.playList[idx].file,
			autoLoad: true,
			autoPlay: false,
			volume: ill.playerSetts.volume,
			usePeakData: true,
			onid3: function() {
				var trackdata;
				trackdata = (this.id3.TPE1 == null) ? 'Unknown Artist' : this.id3.TPE1;
				trackdata += '<br />';
				trackdata += (this.id3.TIT2 == null) ? 'Unknown Track' : this.id3.TIT2;
				jQuery('div#illPName').html(trackdata)
			},
			whileplaying: ill.playerUIWhilePlaying
		});	
		jQuery('div#illPControlPlay').addClass('enabled');
		jQuery('div#illPControlVolup').addClass('enabled');
		jQuery('div#illPControlVoldown').addClass('enabled');
	}
},
playerCalcPeak: function(currVal, maxVal) {
	return Math.round(currVal*15/maxVal);
},
playerLoadSetts: function() {
	eval ('ill.playerSetts = jQuery.cookie(\'illPlayer\');');
},
playerStoreSetts: function() {
	var settsString = '';
	for(var sett in ill.playerSetts) {
		settsString += sett+': ';
		if (typeof ill.playerSetts[sett] == 'string') {
			settsString += '"'+ill.playerSetts[sett]+'", ';
		} else {
			settsString += ill.playerSetts[sett]+', ';
		}
	}
	jQuery.cookie('illPlayer', '{'+settsString+'}');
},
playerPlay: function(obj) {
	if (jQuery(obj).hasClass('enabled') && ill.player != null) {
		var playing = ill.player.getSoundById('illOST');
		if(playing.playState == 0) {
			ill.player.play('illOST');
		 	jQuery(obj).text('pause');
		 	jQuery('div#illPControlStop').addClass('enabled');
		} else if (playing.paused) {
			playing.resume();
		 	jQuery(obj).text('pause');
		} else { 
			playing.pause();
		 	jQuery(obj).text('play');
		}
	}
},
playerStop: function(obj) {
	if (jQuery(obj).hasClass('enabled') && ill.player != null) {
		ill.player.stop('illOST');
	}
	jQuery(obj).removeClass('enabled');
 	jQuery('div#illPControlPlay').text('play');
 	jQuery('div#illPTime').text('---:-- / ---:--');
	for (i = 15; i >= 0 ; i--) {
		jQuery('div#illPPeak'+i).attr('class', 'illPPeak illPPeakVal0');
	}
},
playerVolup: function(obj) {
	if (jQuery(obj).hasClass('enabled') && ill.player != null && ill.playerSetts.volume < 100) {
		ill.playerSetts.volume += 5;
		ill.player.setVolume('illOST', ill.playerSetts.volume);	
		jQuery('div#illPPeakV').attr('class', 'illPPeak illPPeakVal'+ill.playerCalcPeak(ill.playerSetts.volume, 100));
		ill.playerStoreSetts();
	}
},
playerVoldown: function(obj) {
	if (jQuery(obj).hasClass('enabled') && ill.player != null && ill.playerSetts.volume > 0) {
		ill.playerSetts.volume -= 5;
		ill.player.setVolume('illOST', ill.playerSetts.volume);	
		jQuery('div#illPPeakV').attr('class', 'illPPeak illPPeakVal'+ill.playerCalcPeak(ill.playerSetts.volume, 100));
		ill.playerStoreSetts();
	}
},
playerTimeCalc: function(timeVal) {
	if (timeVal == null || typeof timeVal != 'number') { return '---:--'; }
	timeVal = Math.floor(timeVal/1000);
	return ('000'+Math.floor(timeVal/60)).substr(-3)+'/'+('00'+timeVal%60).substr(-2);
},
playerUIWhilePlaying: function() {
	var timeString = ''; //---:-- / ---:--
	timeString  = ill.playerTimeCalc(this.readyState == 3 ? this.duration : this.durationEstimate);
	timeString += ' / ';
	timeString += ill.playerTimeCalc(this.playState == 1 ? this.position : null);
 	jQuery('div#illPTime').text(timeString);
 	
	var i;
	for (i = 15; i > 0 ; i--) {
		jQuery('div#illPPeak'+(i)).attr('class', jQuery('div#illPPeak'+(i-1)).attr('class'));
	}
	jQuery('div#illPPeak0').attr('class', 'illPPeak illPPeakVal'+ill.playerCalcPeak(this.peakData.left+this.peakData.right, 2));
}

};

//raw init
ill.preinit();


