// PRELOAD IMAGES
var imageArray = new Array('_images/common/banner.jpg','_images/common/tweet_top.gif');
var preloadImages = new Array();
for(i=0; i<imageArray.length; i++) {
	preloadImages[i] = new Image();
	preloadImages[i].src = imageArray[i];
}


// STOP JW PLAYER FOR EXTERNAL LINKS
window.addEvent('domready', function(){
	$(document.body).getElements('a[href^=http://]').addEvents({
    	'click': function(){
      		 player.sendEvent('STOP');
      	}
    })
});

// SET OPACITY & HOVER FX FOR SOCIAL BUTTONS
window.addEvent('domready', function() {
	$(document.body).getElements('img[class=social]').set('opacity', .25);
	$(document.body).getElements('img[class=social]').addEvents({
		'mouseenter': function() {
			this.get('tween', {property: 'opacity', duration: 200}).start(1);
		},
		'mouseleave': function() {
			this.get('tween', {property: 'opacity', duration: 200}).start(.25);
		}
	});
});

// JW PLAYER SHOW/HIDE
window.addEvent('domready', function(){
	$('trigger').addEvents({
		'mouseenter': function(){
			$('wrapper').set('tween', {
				duration: 200,
				transition: Fx.Transitions.Quad.easeOut
			}).tween('height', '327px');
		},
		'mouseleave': function(){
			$('wrapper').set('tween', {
				duration: 200,
				transition: Fx.Transitions.Quad.easeOut
			}).tween('height', '28px');
		}
	});
});


// SELECT PLAYLIST TRACK
var playingTrack = '';
function selectTrack(mynum, myid) {
	mynum -= 1;
	if(playing == 0) {
		$(myid).className = 'stop';
		$('player').sendEvent('item', mynum);
		playing = 1;
		//stopme(myid);
	}
	else {
		if(playingTrack != '' && playingTrack != myid) {
			$(playingTrack).className = 'play';
			$(myid).className = 'stop';
			$('player').sendEvent('item', mynum);
			//stopme(myid);
		}
		else {
			$(myid).className = 'play';
			$('player').sendEvent('STOP');
			playing = 0;
		}
	}
	playingTrack = myid;
}
function resetTracklist() {
	$(playingTrack).className = 'play';
	playingTrack = '';
}

window.addEvent('load', function() {
	var rand = Math.floor(Math.random()*1000001)
	if($('theplayer')) {
		//createPlayer();
		var s1 = new SWFObject('_embed/player.swf','player','430','328','9');
		s1.addParam('allowscriptaccess','always');
		s1.addParam('allowfullscreen','true');
		s1.addParam('wmode','transparent');
		s1.addParam('flashvars','file=_embed/playlist.xml?' + rand + '&frontcolor=666666&lightcolor=1e96d2&playlistsize=298&skin=_embed/jk2.swf&playlist=bottom&volume=75&repeat=none&autoscroll=true');
		s1.write('theplayer');
		
		var player = null;
		function playerReady(thePlayer) {
			player = window.document[thePlayer.id];
			addListeners();
		}
		
		
		function addListeners() {
			if (player) { 
				player.addModelListener("STATE", "stateListener");
			} else {
				setTimeout("addListeners()",100);
			}
		}
		
		
		function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
			currentState = obj.newstate; 
			previousState = obj.oldstate; 
		}
	}
});

// EMBED JW PLAYER
var currentState = "NONE"; 
var previousState = "NONE"; 

//var player = null;
function playerReady(thePlayer) {
	//player = document.getElementById(thePlayer.id);
	addListeners();
}


function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}


function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 

	if ((currentState == "COMPLETED") && (previousState == "PLAYING")) {
		if(playing == "1") {
			playing = 0;
			$(playingTrack).className = 'play';
			player.sendEvent('STOP');
		} 
	}
}

function stopme() {
	player.sendEvent('STOP');
}


// SELECT NAV
var nav = new Array("home","music","news","contact");
function select(id, newsid) {
	//if(statusTimer) clearTimeout(statusTimer);
	for(var i = 0; i < nav.length; i++) {
		$(nav[i]).className = "";
	}
	$(id).className = "on";
	var tempFade = new Fx.Tween('temp', { duration: 50, property: 'opacity' });
	var contentFade = new Fx.Tween('left', { duration: 200, property: 'opacity' });
	tempFade.start(0,1).chain(function() {
		contentFade.start(1,0);
	});
	var theurl = id + '.php' + '?newsid=' + newsid + '&' + Math.random();
	new Request.HTML({
		url: theurl,
		method: 'get',
		update: $('left'),
		evalScripts: true,
		onComplete: function() {
			// STOP JW PLAYER FOR EXTERNAL LINKS
			window.addEvent('domready', function(){
				$(document.body).getElements('a[href^=http://]').addEvents({
			    	'click': function(){
			      		 player.sendEvent('STOP');
			      	}
			    })
			});
			// DO THE REST...
			var tempFade = new Fx.Tween('temp', { duration: 50, property: 'opacity' });
			var contentFade = new Fx.Tween('left', { duration: 200, property: 'opacity' });
			contentFade.start(0,1).chain(function(){
				tempFade.start(1,0).chain(function() {
					$('left').set('opacity', 1);
				});
			});
		}
	}).send();
}

// VALIDATE CONTACT FORM
function validateMe() {
	var name = document.contact.name;
	var email = document.contact.email;
	var subject = document.contact.subject;
	var message = document.contact.message;
	
	if(name.value == '') {
		alert('Please provide your name...');
		name.focus();
		return false;
	}
	if(email.value == '') {
		alert('Please provide your email...');
		email.focus();
		return false;
	}
	if(subject.value == '') {
		alert('Please provide a subject...');
		subject.focus();
		return false;
	}
	if(message.value == '') {
		alert('Please provide a message...');
		message.focus();
		return false;
	}
}

// SEND MESSAGE
function sendMessage(name, email, subject, message) {
	var contentFade = new Fx.Tween('formdiv', { duration: 200, property: 'opacity' });
	contentFade.start(.35);
	var theurl = 'contact.php?action=submitForm&name=' + escape(name) + '&email=' + escape(email) + '&subject=' + escape(subject) + '&message=' + escape(message);
	new Request.HTML({
		url: theurl,
		method: 'get',
		update: $('formdiv'),
		evalScripts: true,
		onComplete: function() {
			contentFade.start(1);
			setTimeout(function() {
				var messageFade = new Fx.Tween('message', { duration: 200, property: 'opacity' });
				messageFade.start(0).chain(function() {
					contentFade.start(.35).chain(function() {
						$('message').style.display = 'none';
						contentFade.start(1);
					});
				});
			 }, 3000);
		}
	}).send();
}

// GET DOWNLOAD
function getDownload(transid, pass) {
	var contentFade = new Fx.Tween('formdiv', { duration: 200, property: 'opacity' });
	contentFade.start(.35);
	var theurl = 'download.php?action=submitForm&transid=' + transid + '&email=' + pass;
	new Request.HTML({
		url: theurl,
		method: 'get',
		update: $('formdiv'),
		evalScripts: true,
		onComplete: function() {
			contentFade.start(1);
			/*
setTimeout(function() {
				var messageFade = new Fx.Tween('message', { duration: 200, property: 'opacity' });
				messageFade.start(0).chain(function() {
					contentFade.start(.35).chain(function() {
						$('message').style.display = 'none';
						contentFade.start(1);
					});
				});
			 }, 3000);
*/
		}
	}).send();
}

// DOWNLOAD FILE
function dl(transid) {
	var theurl = '_inc/download.php?id=' + transid;
	new Request.HTML({
		url: theurl,
		method: 'get',
		update: $('dlmess'),
		evalScripts: true,
		onComplete: function() {
			
		}
	}).send();
}



// DOWNLOAD STATUS
/*
var dlState = "0";
function downloadStatus() {
	var theurl = 'dlstatus.php';
	new Request.HTML({
		url: theurl,
		method: 'get',
		update: $('dlmess'),
		evalScripts: true,
		onComplete: function() {
			//dlStatus = 1;
		}
	}).send();
	
	if(dlState == "0") {
		var statusTimer = setTimeout(function(){
			downloadStatus();}, 1000);
	}
	else {
		clearTimeout(statusTimer);
	}
}
*/

// GET URL STRING AFTER HASH (#)
/*
Function: $get
	This function provides access to the "get" variable scope + the element anchor

Version: 1.3

Arguments:
	key - string; optional; the parameter key to search for in the url's query string (can also be "#" for the element anchor)
	url - url; optional; the url to check for "key" in, location.href is default

Example:
	>$get("foo","http://example.com/?foo=bar"); //returns "bar"
	>$get("foo"); //returns the value of the "foo" variable if it's present in the current url(location.href)
	>$get("#","http://example.com/#moo"); //returns "moo"
	>$get("#"); //returns the element anchor if any, but from the current url (location.href)
	>$get(,"http://example.com/?foo=bar&bar=foo"); //returns {foo:'bar',bar:'foo'}
	>$get(,"http://example.com/?foo=bar&bar=foo#moo"); //returns {foo:'bar',bar:'foo',hash:'moo'}
	>$get(); //returns same as above, but from the current url (location.href)
	>$get("?"); //returns the query string (without ? and element anchor) from the current url (location.href)

Returns:
	Returns the value of the variable form the provided key, or an object with the current GET variables plus the element anchor (if any)
	Returns "" if the variable is not present in the given query string

Credits:
		Regex from [url=http://www.netlobo.com/url_query_string_javascript.html]http://www.netlobo.com/url_query_string_javascript.html[/url]
		Function by Jens Anders Bakke, webfreak.no
*/
function $get(key,url){
	if(arguments.length < 2) url =location.href;
	if(arguments.length > 0 && key != ""){
		if(key == "#"){
			var regex = new RegExp("[#]([^$]*)");
		} else if(key == "?"){
			var regex = new RegExp("[?]([^#$]*)");
		} else {
			var regex = new RegExp("[?&]"+key+"=([^&#]*)");
		}
		var results = regex.exec(url);
		return (results == null )? "" : results[1];
	} else {
		url = url.split("?");
		var results = {};
			if(url.length > 1){
				url = url[1].split("#");
				if(url.length > 1) results["hash"] = url[1];
				url[0].split("&").each(function(item,index){
					item = item.split("=");
					results[item[0]] = item[1];
				});
			}
		return results;
	}
}

// SCROLL TO NAV TOP (FOR BOTTOM LINKS)
function scrollThis() {
	var winScroller = new Fx.Scroll(window);
	winScroller.start(0, 385);
}

// SLEEP FUNCTION
/*
function sleep(seconds) {
    // http://kevin.vanzonneveld.net
    // +   original by: Christian Doebler
    // +   bugfixed by: Brett Zamir (http://brettz9.blogspot.com)
    // %          note: For study purposes. Current implementation could lock up the user's browser. 
    // %          note: Consider using setTimeout() instead.
    // *     example 1: sleep(1);
    // *     returns 1: 0
    
    var start = new Date().getTime();
    while (new Date() < start + seconds*1000) {}
    return 0;
}
*/