/*
jQUERY FUNCTIONS
--------------------------------------------------*/
$(document).ready(function() {
	
	// SEARCH BOX
	$('div#site-navigation form input.text').toggleVal('defaultval');
		
	// POPUP WINDOW
	$('.popupwindow').popupwindow();
	
	// ADD LINK TO HEADER
	$('h1').prepend('<a href="/" title="Global Journalist Home"></a>');
	
	// ENHANCEMENTS TO LINKS
	$('div#site-content a[href^="mailto"]').addClass('email');
	//$('div#site-content a[href^="http://"]').not('p.breadcrumb a').addClass('external');
	//$('a[href*=".pdf"]').addClass('pdf');
	
	$('#list-emprint li:first h4 span').html('&nbsp;<img src="/images/new.gif" />');
	$('#list-emprint li:last').css({
		borderBottom:'none',
		paddingBottom:'0'
	});
	
	$('select#jumplist-country').change(function() {
		window.location = '#' + $(this).val();
	});
	
	// Equalize heights of all story archive quarters
	if ( $('body').is('#stories') ) {
		var quarterHeight = 0;
		
		$('div.list-quarters').each(function() {
			quarterHeight = $(this).outerHeight();
		});
		
		$('div.list-quarters').css('height',quarterHeight);
	}
	
});

AudioPlayer.setup("/_swf/player.swf", {  
	width: 370,
	initialvolume: 80,
	transparentpagebg: "yes",
	animation: "no",
	loader: "336699"
});


// Default value
jQuery.fn.toggleVal = function(focusClass) {
	this.each(function() {
		$(this).focus(function() {
			// clear value if current value is the default
			if($(this).val() == this.defaultValue) { $(this).val(""); }
			
			// if focusClass is set, add the class
			if(focusClass) { $(this).addClass(focusClass); }
		}).blur(function() {
			// restore to the default value if current value is empty
			if($(this).val() == "") {
				$(this).val(this.defaultValue);
				
				// if focusClass is set, remove class
				if(focusClass) { $(this).removeClass(focusClass); }
			}
		});
	});
}


// POPPUP WINDOW FUNCTION
jQuery.fn.popupwindow = function(p)
{

	var profiles = p || {};

	return this.each(function(index){
		var setting, parameters, mysettings, b, a;
		
		// for overrideing the default settings
		mysettings = (jQuery(this).attr("rel") || "").split(",");

		
		settings = {
			height:600, // sets the height in pixels of the window.
			width:600, // sets the width in pixels of the window.
			toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			left:50, // left position when the window appears.
			top:50 // top position when the window appears.
		};

		// if mysettings length is 1 and not a value pair then assume it is a profile declaration
		// and see if the profile settings exists


		if(mysettings.length == 1 && mysettings[0].split(":").length == 1)
		{
			a = mysettings[0];
			// see if a profile has been defined
			if(typeof profiles[a] != "undefined")
			{
				settings = jQuery.extend(settings, profiles[a]);
			}
		}
		else
		{
			// overrides the settings with parameter passed in using the rel tag.
			for(var i=0; i < mysettings.length; i++)
			{
				b = mysettings[i].split(":");
				if(typeof settings[b[0]] != "undefined" && b.length == 2)
				{
					settings[b[0]] = b[1];
				}
			}
		}
		
		parameters = "height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;
		
		jQuery(this).bind("click", function(){
			var name = "PopUpWindow" + index;
			window.open(this.href, name, parameters).focus();
			return false;
		});
	});

};
