// INTERNAL VARIABLES
var df = "http://www.saffrolla.com/zens-ajax.php";	// ajax php relay script
var _uacct = "UA-1912578-1";						// google analytics

var runOnce = false;			// flag
var lastUrl = '';				// skip
var sUrl = new Array();
	sUrl[0] = uid;
	sUrl[1] =  'test.mp3';
	
// ----- LAYOUT
function findScreenSize(margin, min){
    var visHeight =$(window).height();
		  
	 visHeight = visHeight - margin;
	 if (visHeight < min) visHeight = 340;
	 return visHeight;
};
function setScreenSize(h){
	//h = h-10;
	$("#liquidleftcolumn").height(h);
	$("#fixedcontent").height(h-30);
	$("#liquidrightcontent").height(h);
	//$("#liquidcolumns").height(h);
};
function updateScreenSize(){
	var ss = findScreenSize(355,140);
	setScreenSize(ss);
};
function updateTitle(hash){
	document.title = "Saffrolla Sounds > "+hash.replace(/_|-/g, ' ');
	return false;
}

// ----- FLASH JS API
function playSong(filepath, artist, songname, cover){
	flashProxy.call(
	'loadSongFromUrl', 
	filepath, 
	artist,	
	songname,
	cover
	); 
	// highlight
	//var sendo = new Array()
	//sendo[0] = uid;
	//sendo[1] = filepath;
	//song(sendo);
	
	// update title
	updateTitle('playing '+songname+' by '+artist);
	return false;
};

function loadFeed(filepath) {
	flashProxy.call(
	'loadFeed', 
	filepath,
	'true'
	); 
	return false;
};

// ----- UPDATE LIVE CSS
// highlight a specific .mp3 file that may be visible
function song(url){
	//alert('array into '+url);
	if (url[1] == undefined) return false;
	if (url[1] == sUrl) return false;
	
	// locate all divs with post-id and remove class active
	$("#fixedcontent #content .post").removeClass("highlight"); 
	
	// attach playing to specific div
	if (url[1].lastIndexOf('mp3') > -1) $("img[@longdesc*=" +url[1]+ "]").parents("div").parents("div").addClass("highlight");

	sUrl = url;
	return false;
};

function updateArtist(ar){
	// locate all li with title and remove class active
	$("ul.block_links li").removeClass("current-cat"); 
	
	// locate all li with html = ar and add class active
	if (ar != 'music') $("ul.block_links a[@href*=/" +ar+ "/]").parents("li").addClass("current-cat");
	
	//if (ar != 'music') $("ul.block_links a[=" +ar+ "]").parents("li").addClass("current-cat");
	return false;
};


function bioLink(){
	$('#bio:eq(0)').hide();

	//refresh click events on new dom
	 $("a[@title='biography']").click(function()
		  {
		  	// reveal hidden bio div (toggle it)
			$('#bio:eq(0)').slideToggle('fast');
			return false;
	});
}


// Load PHP file with correct parameters into div 'content'
function callPhpFile(hash, md){
	
	 if( (hash) && (hash != lastUrl) ) 
	 {		
	 
	 	lastUrl = hash;
		// empty the content box & animate
		$("#fixedcontent #content").slideUp("fast",function(){
			$("#fixedcontent #content").empty( );
			$("#fixedcontent #content").html( '<div class="loader"></div>' );
			//$("#fixedcontent #content").addClass('loader');
			$("#fixedcontent #content #loader").height(100);
			$("#fixedcontent #content #loader").slideDown("fast");
		});
		
		// AJAX data: 
		q = "cname="+hash;			//+"&pname="+pagename;
		
		// BEGIN AJAX call
		$.ajax({
		
		type: "GET",
		url: df,
		data: q,
		
		success: function(pg)
			{
				$("#fixedcontent #content #loader").fadeOut("slow");
				// ADD
				$("#fixedcontent #content").empty( );
				//$("#fixedcontent #content").removeClass('loader');
				$("#fixedcontent #content").html( pg );
				$("#fixedcontent #content").height( 1 );
				
				// UPDATE
				bioLink();
				
				// REVEAL
				$("#fixedcontent #content").slideDown("fast");
							 
				// EVALUATE
				ifr("#fixedcontent #content h1");
				setupClicks('div.artist a');
				
				updateTitle(hash);
				// update link.
			 	updateArtist(hash);
				song(sUrl);
				
				// TRACK
				urchinTracker();
			}
			
		});
		// END AJAX
	}
	
};
function cleanHash(hr){
	//delete current hashes
	var test = hr.indexOf('#')+1;
	if (test)	 hr = hr.substring(0,test-1);

	// delete trailing slash
	if (hr.charAt(hr.length-1) == '/') hr = hr.substring(0, hr.lastIndexOf('/'));
	
	// extract the post_slug :
	var st = hr.lastIndexOf('/');
	var hash = hr.substring(st+1);
	return hash;
}
function getHostAndPath () {
	return window.location.protocol + '//' + window.location.host + window.location.pathname;
}
		
function setupClicks(idd){
	// find all music links and extract their href
	// from doc 
	// $("a[@href*=/content/gallery]").click(function() {
	//$("ul.block_links > li > a").click(function(){
	var clickDiv = idd+"[@href*=/category]";
	// <-- BEGIN clicks
	$(clickDiv).click(
	
		function()
		{
			// extract url & hash
	 		var hr = $(this).attr("href");
			var hash = cleanHash(hr);
						
			// update load section with loading ct
		   var ct = $(this).html();
			
			 // callphpload is called at once.
			$.historyLoad(hash);
			return false;
		   
		   //callPhpFile(pl);
		}
		
	 );
	// <-- END clicks
	return false;
}

function setupFlash(hash){

	// Music Machine
	$('#header > #flash').flashembed(
		{
		    src: 'http://www.saffrolla.com/wp-content/themes/saffrolla2/swfs/musicmachine.swf',
		    width: '100%',
		    height: 320,
			id:'musicmachine',
			menu: false
		},
		{ 
	        lcId: uid,
			cat: hash
	    }
	); 
	
}

function setup(){
	/*  tracker software*/
	var thisHash = cleanHash( getHostAndPath() );
	// create the proxy to communicate with javascript....
	//flashProxy = new FlashProxy(uid, "http://www.saffrolla.com/wp-content/themes/saffrolla2/js/swfjs/JavaScriptFlashGateway.swf")
	// create ajax page links
	setupClicks('ul.block_links > li > a');
	// music link itself
	setupClicks('#navigation > ul > li > a.current');
	//default page navs
	setupClicks('div.artist a');
	 
	// add flash to header
	setupFlash(thisHash);
	
	// hide biography and enable button
	bioLink();	
					 
	 // find and set column sizes
	updateScreenSize();
	  		 
	 // call first page
	$.historyInit(callPhpFile);
}


// ------------------ EVENTS --------------------
jQuery(document).ready(function(){
	// irritating ie 6 bug doesn't load on ready .. so set up runOnce var
	runOnce = true;
	setup();
});

/*
window.onload = updateScreenSize;
window.onload = function(){
	if (runOnce != true){
		//setup();
	}	
}
*/

jQuery(window).resize(function() { 
	updateScreenSize();
} );
