function getjson( url , container, vars, bookmark, callback )
{

	var containers = container.split("/");
	
	if (bookmark != false) {
		// SET URL HASH FOR BOOKMARKS
		var requestUri = get_request_uri(url);
		//window.location = '#' + requestUri; // + '/container/' + container;
		//$.history.load( requestUri );
	}
	
	if( vars == null )
	{
		var vars = '';
	}
	
	$('.ajax_loader').fadeIn('fast');
	
	try{
		$.post( url , vars , function( data ){
			
			var json = data;
			
			for( var i = 0 ; i < json.response.length; i++ )
			{
				
				switch( json.response[i].type )
				{
					case 'HTML':
						$(containers[i]).html(json.response[i].data);
						break;
					case 'SUCCESS':
						success( json.response[i].data );
						break;
					case 'WARNING':
						warn( json.response[i].data );
						break;
					case 'JSCRIPT':
						eval( json.response[i].data );
						break;
				}
				
			}
			
			// Google Analytics
//			if ( pageTracker != undefined && bookmark != false ) {
//				pageTracker._trackPageview(requestUri);
//			}
			
			if( callback != undefined )
			{
				eval( callback );
			}
			
			$('.ajax_loader').hide();
			
		},'json');
	}
	catch(e){
		warn(e);
		$('.ajax_loader').hide();
	}
	
	return false;
	
}


function get_request_uri( url )
{
	var requestUri = '';
	var elementPosition = -1;
	var qstring_array = url.split('?');
	var qstring = '';
	
	if (qstring_array.length > 1) {
		qstring = qstring_array[1];
	}
	
	url = url.replace("http://","");
			
	requestUriArray = url.split('/');
	requestUriArray.shift();
	
	for( var i = 0; i < requestUriArray.length; i++ )
	{
		if( requestUriArray[i] == 'element' || requestUriArray[i] == 'container' )
		{
			elementPosition = i + 1;
		}
		else
		{
			if( elementPosition != i )
			{
				requestUri += '/' + requestUriArray[i];
			}	
		}
	}
	
	return requestUri + ( qstring != '' ? '?' + qstring : '' );
}


function getjson_popup( url , container, vars, bookmark, popup_selector )
{
	return getjson( url , container, vars, bookmark, 'popup(\'' + popup_selector + '\');' );
}

function close_popup( popup_selector )
{
	if ($(popup_selector).dialog('isOpen')) {
		$(popup_selector).dialog('close');
	}
	return false;
}

function popup( popup_selector )
{
	$(popup_selector).dialog('open');
	init_tab();
	
	if( !$(popup_selector).dialog('isOpen') ) {
		$(popup_selector).dialog({
			modal: true,
			resizable: false,
			position: ['center',20],
			closeOnEscape: true,
			width: 630,
			height: 'auto'
		});
	}
}

var cur_cal = '';

function reload_cal(year,month) {											
	// Ajax
	
	$.get("ajax.php", { type: 'calendar', year: year, month: month }, function(data)
	{	
		cur_cal = '';
		$('#calendar').html(data);
		//$('#calendar').slideDown('slow');
	});
}

function cal(year,month) {											
	// Ajax
	if($('#cal_pop').is(":visible")){
		$('#cal_pop').slideUp('slow',function(){
	//if($('#calendar').is(":visible")){
		//$('#calendar').slideUp('slow',function(){
			reload_cal(year,month);
		});
	}else{
		reload_cal(year,month);
	}
}

function caltog_slide(new_cal){
	var day_entries = $('#'+new_cal).html();
	$('#cal_pop').html(day_entries);
	$('#cal_pop').slideDown();
}

function caltog(new_cal){
	
	if(new_cal != cur_cal){	
		if($('#cal_pop').is(":visible")){
			$('#cal_pop').slideUp('slow',function(){	  
					caltog_slide(new_cal);
					cur_cal = new_cal;
			});
		}else{
			caltog_slide(new_cal);
			cur_cal = new_cal;
		}
	}
	
}

function ajax_fetch(x,y)
{ return true; }


function plasma( video_path , image_path , description )
{
	try
	{
		clearInterval(intervalId);
	}
	catch(e)
	{
	}
	
	$('select').hide();
	
	if( video_path.lastIndexOf(".flv") != -1 )
	{
		var so = new SWFObject('assets/flash/mediaplayer.swf','mpl','680','450','8');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addVariable('height','450');
		so.addVariable('width','680');
		so.addVariable('file',video_path);
		so.addVariable('image',image_path);
		so.addVariable('autostart','true');
		so.write('player');
		$('#plasma #panel_strap').html( '' );
		$('#plasma #description').html( description );
	}
	
	if( video_path.lastIndexOf(".mp3") != -1 )
	{
		var so = new SWFObject('assets/flash/mediaplayer.swf','mpl','680','20','8');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addVariable('height','20');
		so.addVariable('width','680');
		so.addVariable('file',video_path);
		so.addVariable('image',image_path);
		so.addVariable('autostart','true');
		so.write('player');
		$('#plasma #panel_strap').html( '<img src="/assets/images/mic.gif" /> ' + description );
		$('#plasma #description').html( '' );
	}

	
	
	$('#plasma').fadeIn('fast');
	
	//$('#outer_wrapper').fadeTo('fast',0.2);
	return false;

}

function embed_player( video_path, image_path, container_id, width, height, auto_play  )
{
	var so = new SWFObject('assets/flash/mediaplayer.swf','mpl',width,height,'8');
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','true');
	so.addVariable('height',height);
	so.addVariable('width',width);
	so.addVariable('file',video_path);
	so.addVariable('image',image_path);
	
	if (auto_play) {
		so.addVariable('autostart', 'true');
	}
	
	so.write(container_id);
}

function hideplasma()
{
	$('select').show();
	$('#plasma').fadeOut('slow');
	//$('#outer_wrapper').fadeTo('fast',1);
	return false;
}

function init_sublinks()
{
	// Show sublinks - uses a.active on parent
	
	var active_div = $('#right_col .main_links a.active');
	var parent_div = $(active_div).parent();
	
	// Only try to show if sublinks exist
	if($('.sub_link', parent_div).length > 0)
	{
		$(active_div).addClass('shown');
		$('.sub_link', parent_div).show();
	}
}

function init_tab()
{
	$('.tab-blurb').hide();
	
	$('.tab-content-pad').mouseover( function(){
		$('.tab-blurb').show();
		$('.tab-blurb-bg').height( $('.tab-blurb-pad').outerHeight() );
	});
	
	$('.tab-content-pad').mouseout( function(){
		$('.tab-blurb').hide();
	});
}

$(document).ready(function(){
	init_sublinks();
});


function show_sublinks(div)
{
	if($(div).hasClass('shown'))
	{
		// Hide all open sublinks
		$('#right_col .main_links .sub_link').hide();
		
		// Remove any instances of shown class
		$('#right_col .main_link a.shown').removeClass('shown');
	}
	else
	{
		// Hide all open sublinks
		$('#right_col .main_links .sub_link').hide();
		
		// Remove any instances of shown class
		$('#right_col .main_link a.shown').removeClass('shown');
		
		// Add shown to triggered class
		$(div).addClass('shown');
		
		var target_div = $(div).parent();
		$('.sub_link',target_div).show();
	}
	
	
}

function show_flowplayer(playerid,server,externalpath) {
flowplayer(playerid,"/assets/player/flowplayer.commercial-3.1.2.swf", {
 key: '#@85eac9fd3f2f66b4738',
 clip: externalpath
}); 
}

