/* ########################################################################### *
/* ***** DOCUMENT INFO  ****************************************************** *
/* ########################################################################### *
 * ##### NAME:  global.js
 * ##### VERSION: v1.0
 * ##### UPDATED: 29/10/08 (Mick Chmielewski, Eclipse Group)
/* ########################################################################### *

/* ########################################################################### *
/* ***** INDEX *************************************************************** *
/* ########################################################################### *
/* ##### GLOBAL JS CONFIGURATION
/* ##### INITIALISATION
/* #####
/* ##### PRESENTATION:
/* ##### ROUNDED BUTTONS
/* ##### ROUNDED CORNERS
/* ##### DROPDOWN MENU
/* ##### EMAIL A FRIEND
/* ##### TERMS & CONDITIONS
/* ##### MODULE EXPAND/COLLAPSE
/* #####
/* ##### FUNCTIONALITY:
/* ##### MEDIA PLAYER
/* ##### ARTICLE SLIDER
/* ##### JQUERY & OTHER UTILITIES
/* ########################################################################### */

/* ########################################################################### *
/* ##### GLOBAL JS CONFIGURATION
/* ########################################################################### */
var ecl_flashVersion = "9.0.28.0";
var ecl_flashMediaPlayerSWF = "media/flash/mediaPlayer.swf";
var ecl_flashExpressInstallSWF = "media/flash/expressInstall.swf";
var ecl_videoPlayerWidth = "309px";
var ecl_videoPlayerHeight = "173px";
var ecl_defaultMediaImage = "media/images/videoPlayer_defaultImage.gif";

/* ########################################################################### *
/* ##### INITIALISATION
/* ########################################################################### */

$(document).ready(ecl_init_global);

function ecl_init_global()
{
	// Rounded buttons
	ecl_rounded_buttons();
	// Search box styling in Safari
	ecl_search_box_safari();
	// Terms show/hide
	ecl_terms();
	// Hiding rating labels
	ecl_rating_labels();
	// Dropdown menu
	ecl_dropdown_menu();
	// Email a friend
	ecl_emailafriend();
	// Module Expand links;
	ecl_init_module_expand();
	// Media Player
	ecl_init_mediaPlayer();
	// Article Slider
	ecl_init_articleSlider();
	// Rounded corners
	ecl_rounded_corners();
}



/* ########################################################################### *
/* ##### PRESENTATION
/* ########################################################################### */

/* ########################################################################### *
/* ##### ROUNDED BUTTONS
/* ########################################################################### */

function ecl_rounded_buttons(){
	if ($.browser.msie == false || ($.browser.msie && $.browser.version > 6)) {
		var nodes = $("button");
		for (var i = 0; i < nodes.length; i++)
		{
			if ($(nodes[i]).hasClass("primary"))
			{
				$(nodes[i]).removeClass("button");
				$(nodes[i]).removeClass("primary");
				addClassToFront(nodes[i], "button_curve_primary");
				$(nodes[i]).after("<div class='buttonEnd_primary'></div>");
				$(nodes[i]).hover(buttonHoverOver_primary, buttonHoverOut_primary);
			} 
			else if ($(nodes[i]).hasClass("secondary"))
			{
				$(nodes[i]).removeClass("button");
				$(nodes[i]).removeClass("secondary");
				addClassToFront(nodes[i], "button_curve_secondary");
				$(nodes[i]).after("<div class='buttonEnd_secondary'></div>");
				$(nodes[i]).hover(buttonHoverOver_secondary, buttonHoverOut_secondary);
			} else {
				$(nodes[i]).removeClass("button");
				addClassToFront(nodes[i], "button_curve"); 
				$(nodes[i]).after("<div class='buttonEnd'></div>");         
				$(nodes[i]).hover(buttonHoverOver, buttonHoverOut);
			}
		}
	}
}
 
function buttonHoverOver(){
	addClassToFront(this, "button_hover");
	$(this).next("div").addClass("buttonEnd_hover");
}
 
function buttonHoverOut(){
	$(this).removeClass("button_hover");
	$(this).next("div").removeClass("buttonEnd_hover");
}

function buttonHoverOver_primary(){
	addClassToFront(this, "button_primary_hover");
	$(this).next("div").addClass("buttonEnd_primary_hover");
}
 
function buttonHoverOut_primary(){
	$(this).removeClass("button_primary_hover");
	$(this).next("div").removeClass("buttonEnd_primary_hover");
}

function buttonHoverOver_secondary(){
	addClassToFront(this, "button_secondary_hover");
	$(this).next("div").addClass("buttonEnd_secondary_hover");
}
 
function buttonHoverOut_secondary(){
	$(this).removeClass("button_secondary_hover");
	$(this).next("div").removeClass("buttonEnd_secondary_hover");
}

 
function addClassToFront(obj, klass){
	if(typeof obj.get == "function") {
		obj = obj.get(0);
	}
	obj.className = klass + " " + obj.className;
}

/* ########################################################################### *
/* ##### SEARCH BOX STYLING FOR SAFARI
/* ########################################################################### */

function ecl_search_box_safari(){
	if ($.browser.safari){
		//alert("this is safari");
		$(".globalSearch").addClass("safariSearch");
	}
}


/* ########################################################################### *
/* ##### ROUNDED CORNERS
/* ########################################################################### */

function ecl_rounded_corners(){
	// Global search bar, form container and modules corners
	$(".globalSearch, .boxform, .roundModule").each(function(){
		$(this).corner("8px");
	});
	
	// Main menu corners
	$(".nav_primary").each(function(){
		$(this).corner("tl tr 8px");
	});
	/*if ($.browser.msie == false || ($.browser.msie && $.browser.version > 6)) {
		$(".sf-menu a").each(function(){
			$(this).corner("tl tr 8px");
		});
	}*/
	
	// Right hand login module corners
	$(".loginModule").each(function(){
		$(this).corner("tl tr 8px");
		$(this).next(".module").addClass("afterLoginModule");
	});
	$(".loginModuleHome").each(function(){
		$(this).corner("tr 8px");
		$(this).next(".module").addClass("afterLoginModule");
	});
	
	// If there is a module after a login module apply corners to just the bottom
	if ($(".afterLoginModule").length > 0) {
		$(".afterLoginModule").corner("bl br 8px");
	}
}


/* ########################################################################### *
/* ##### HIDING RATING LABELS
/* ########################################################################### */

function ecl_rating_labels(){
	$(".ratings label").hide();
}


/* ########################################################################### *
/* ##### DROPDOWN MENUS
/* ########################################################################### */

function ecl_dropdown_menu(){
	$("ul.sf-menu").supersubs({ 
		minWidth:    12,   // minimum width of sub-menus in em units 
		maxWidth:    27,   // maximum width of sub-menus in em units 
		extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
						   // due to slight rounding differences and font-family 
	}).superfish().find('ul').bgIframe({opacity:false});	
}


/* ########################################################################### *
/* ##### EMAIL A FRIEND
/* ########################################################################### */

function ecl_emailafriend(){
	$(".nav_breadcrumbs").each(function(){
		$(this).after("<div class='emailafriend'><a href='#'><img src='css/images/img_email.gif' alt='Email to a friend' title='Email to a friend' width='20' height='13' style='border:0px transparent none;' /></a></div>")					
	});
}


/* ########################################################################### *
/* ##### TERMS & CONDITIONS
/* ########################################################################### */

function ecl_terms(){
	$(".termsDetails").each(function(){
		$(this).hide();								 
	});
	$(".termsLink").addClass("activeLink").click(function(){
		$(".termsDetails").toggle();
		return false;
	});
}


/* ########################################################################### *
/* ##### MODULE EXPAND/COLLAPSE
/* ########################################################################### */

function ecl_init_module_expand(){
	$("#content .module").each(function(){
		ecl_module_init_hide($(this))									
	});
}

function ecl_module_init_hide(node){
	node.find(".moduleContent ul.linkList").each(function(){
		var counter = 0;
		$(this).children().each(function(){
			counter++;
			if(counter>3){
				$(this).hide();	
			}
		});
		
		if(counter>3){
			var currentHeader = "<h2>"+node.find(".moduleContent .moduleHeader h2").html()+"</h2>";
			
			node.find(".moduleContent .moduleHeader").html(currentHeader+"<div class='moreLink clearfix' style='display:none'><div class='more'></div><a href='#'>More</a></div>");	
		}
		
		ecl_module_setButtonActions(node.find(".moduleContent .moduleHeader"));
	});
}

function ecl_module_more(me){
	var counter = 0;
	me.find("ul.linkList").children().each(function(){
		counter++;
		if(counter>3){
			if($.browser.msie){
				$(this).show();
			} else {
				$(this).animate({opacity: 0}, 1);
				$(this).slideDown(250, function(){
					$(this).animate({opacity: 1}, 250);
				});
			}
		}													 
	});
	
	me.find(".moduleHeader .moreLink").fadeOut(250, function(){
		//$(this).remove();
		//me.find(".moduleHeader h2").after("<div class='moreLink clearfix' style='display:none'><div class='less'></div><a href='#'>Less</a></div>");
		
		var currentHeader = "<h2>"+me.find(".moduleHeader h2").html()+"</h2>";
		me.find(".moduleHeader").html(currentHeader+"<div class='moreLink clearfix' style='display:none'><div class='less'></div><a href='#'>Less</a></div>");
		
		ecl_module_setButtonActions(me.find(".moduleHeader"));
	});			  
}

function ecl_module_less(me){
	var counter = 0;
	me.find("ul.linkList").children().each(function(){
		counter++;
		if(counter>3){
			if($.browser.msie){
				$(this).hide();
			} else {
				$(this).animate({opacity: 0}, 250, null, function(){
					$(this).slideUp(250);
				});
			}
		}													 
	});
	
	me.find(".moduleHeader .moreLink").fadeOut(250, function(){
		//$(this).remove();
		//me.find(".moduleHeader h2").after("<div class='moreLink clearfix' style='display:none'><div class='more'></div><a href='#'>More</a></div>");
		
		var currentHeader = "<h2>"+me.find(".moduleHeader h2").html()+"</h2>";
		me.find(".moduleHeader").html(currentHeader+"<div class='moreLink clearfix' style='display:none'><div class='more'></div><a href='#'>More</a></div>");
		
		ecl_module_setButtonActions(me.find(".moduleHeader"));
	});
}

function ecl_module_setButtonActions(node){	
		if(node.find(".more").length>0||node.find(".moreOver").length>0){
			//set MORE click
			node.find("a, div").click(function(){
				ecl_module_more(node.parents(".moduleContent"));	
				return false;
			});
			
			//set MORE hover
			node.find("a, div").hover(function(){
				node.find("div.more").addClass("moreOver").removeClass("more");;								 
			}, function(){
				node.find("div.moreOver").removeClass("moreOver").addClass("more");
			});
		} else if(node.find(".less").length>0||node.find(".lessOver").length>0){
			//set LESS click
			node.find("a, div").click(function(){
				ecl_module_less(node.parents(".moduleContent"));
				return false;
			});
			
			//set LESS hover
			node.find("a, div").hover(function(){
				node.find("div.less").addClass("lessOver").removeClass("less");;								 
			}, function(){
				node.find("div.lessOver").removeClass("lessOver").addClass("less");
			});
		}
		
		node.find(".moreLink").fadeIn();
}

/* ########################################################################### *
/* ##### FUNCTIONALITY
/* ########################################################################### */

/* ########################################################################### *
/* ##### MEDIA PLAYER
/* ########################################################################### */
var ecl_mediaPlayer_activeButton;
var int_mediaAutoPlay;

// Trigger the first item in the .mediaLinks
$(window).load(function() 
	{
		$(".mediaLinks a:first").trigger("click");
		ecl_mediaPlayer_autoPlay();
		ecl_mediaPlayer_mediaLinks();
	});

// Setup the mediaLinks slider
function ecl_mediaPlayer_mediaLinks()
{
	if ($(".mediaLinks").length > 0)
	{
		$(".mediaLinks a").click(function() 
			{
				ecl_mediaPlayer_mediaLinks_updateDescription(this)

			});
	}
}

function ecl_mediaPlayer_mediaLinks_updateDescription(linkNode)
{
	// Fade the description
	$(".mediaDescription .description").fadeOut(function()
		{ 
			// Set the description text
			$(".mediaDescription .description:first").html($(linkNode).find(".hidden").html());
			
			// Show the description
			$(".mediaDescription .description").fadeIn();
		});	
}

// Setup auto play
function ecl_mediaPlayer_autoPlay()
{
	var AUTO_PLAY_INTERVAL = 5000;
	
	if ($(".mediaPlayer .mediaLinks_autoPlay").length > 0)
	{
		int_mediaAutoPlay = setInterval(ecl_mediaPlayer_autoPlayNext, AUTO_PLAY_INTERVAL);
		
		// Clear intervals
		$("#flashMediaContainer").click(ecl_mediaPlayer_stopAutoPlay);  // On click of #flashMediaContainer
		$(".mediaLinks a").click(ecl_mediaPlayer_stopAutoPlay);			// On click of .mediaLinks a
	}
}

function ecl_mediaPlayer_autoPlayNext()
{
	var currentNode = $(".mediaLinks a.active:first");
	//$(currentNode).next("a").trigger("click");
	
	var nextNode = $(currentNode).next("a");
	
	if ($(nextNode).html() == null)
	{
		nextNode = $(".mediaLinks a:first");
	}
		
	ecl_launchMedia(nextNode);
	ecl_mediaPlayer_mediaLinks_updateDescription(nextNode);
}

// This function stops the auto-rotate / auo-play functionality
// Note this function is called from within the Flash for IE6. Consequently,
// if you change the name of the fucntion here you will need to modify the Flash file
// and recompile.
function ecl_mediaPlayer_stopAutoPlay()
{
	clearInterval(int_mediaAutoPlay);
	//alert("Clear");
}

function ecl_init_mediaPlayer()
{
	// Wire up the links
	$(".ecl_showMediaLink").each(function(){
		$(this).click(function(){
			ecl_launchMedia(this);
			return false;
		});
	});
}

function ecl_launchMedia(node)
{
	// Grab the query string values from the <a> tag
	var queryVars = $.query($(node).attr("href"));
	
	// Assign variables / flash params
	var mediaFile = queryVars["ecl_mediaFile"];
	var imageFile = queryVars["ecl_stillImage"];
	var pauseOnLoad = queryVars["ecl_mediaPauseOnLoad"];
	var ecl_flashContainerId = queryVars["ecl_flashContainer"];
	var debug = queryVars["ecl_debug"];
	var mediaTitle = queryVars["mediaTitle"];
	
	if (imageFile == undefined)
	{
		imageFile = ecl_defaultMediaImage;
	}
	
	// Determine if the video should play in a modal window or in page
	if (ecl_flashContainerId == "modalWindow")
	{
		// Display media in modal window
		ecl_launchModalWindow(unescape(mediaTitle), "329px", function()
		  { 
				// Set the width and height of video - by setting the #contentContainer
				$("#contentContainer").width(ecl_videoPlayerWidth);
				$("#contentContainer").height(ecl_videoPlayerHeight);
				
				// Show the media in page
				showVideo(mediaFile, imageFile, pauseOnLoad, "contentContainer", debug);
		  });
	} 
	else 
	{	
		// Show the media in page
		showVideo(mediaFile, imageFile, pauseOnLoad, ecl_flashContainerId, debug);
	}
	
	// Set this as the active item
	$(ecl_mediaPlayer_activeButton).removeClass("active");	// Unset the previous button
	ecl_mediaPlayer_activeButton = node;					// Update object to new button
	$(node).addClass("active");								// Set the new button to active
	
	return false;	
}

function showVideo (mediaFile, imageFile, pauseOnLoad, ecl_flashContainerId, debug) 
{
	if (!mediaFile) return true;
	if (!imageFile) return true;
	
	// Get width and height of container element
	var ecl_flashWidth = $("#" + ecl_flashContainerId).width();
	var ecl_flashHeight = $("#" + ecl_flashContainerId).height();	

	$("#" + ecl_flashContainerId).hide();

	/*var playerVersion = swfobject.getFlashPlayerVersion();
	var majorVersion = playerVersion.major
	var minorVersion = playerVersion.minor
	var releaseVersion = playerVersion.release*/

	if (swfobject.hasFlashPlayerVersion("6.0.65")) {
		var flashvars = {};
		flashvars.displayWidth = ecl_flashWidth;
		flashvars.displayHeight = ecl_flashHeight;
		flashvars.pauseOnLoad = pauseOnLoad;
		flashvars.mediaFile = mediaFile;
		flashvars.imageFile = imageFile;
		flashvars.debug = debug;
	
		var params = {};
		params.allowFullScreen = "true";
		params.allowScriptAccess = "always";
		params.menu = "false";
		params.salign = "LT";
		params.scale = "noscale";
		params.wmode = "transparent";	
		
		var attributes = {};
		attributes.id = ecl_flashContainerId;
		swfobject.embedSWF(ecl_flashMediaPlayerSWF, ecl_flashContainerId, flashvars.displayWidth, flashvars.displayHeight, ecl_flashVersion, ecl_flashExpressInstallSWF, flashvars, params, attributes);
	} else {
		$("#" + ecl_flashContainerId).html('<p><strong>Sorry, you require the <a href="http://www.adobe.com/go/getflash">Adobe Flash</a> player to view this content.</strong></p>');	
	}
	
	$("#" + ecl_flashContainerId).show();

	// Prevent click action
	return false;
}

/* ########################################################################### *
/* ##### ARTICLE SLIDER
/* ########################################################################### */

jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	 return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

function ecl_init_articleSlider()
{

	// check if article slider is on this page
	if ($(".module .articleSlider").length > 0)
	{
		$(".module .articleSlider").each(function(){
			var currentSlider = $(this);
		
			// Insert the slider button(s) if there is more than 2 articles
			if (currentSlider.find(".articleList").length > 1) 
			{
				currentSlider.find(".moduleHeader h2").after("<div class='articleSliderNav' style='display: none'><div class='sliderPrev'></div><div class='sliderNext'></div></div>");
				
				//setup button hover states
				currentSlider.find("div.sliderPrev").hover(function(){
					currentSlider.find("div.sliderPrev").addClass("sliderPrevOver");								
				}, function(){
					currentSlider.find("div.sliderPrev").removeClass("sliderPrevOver");
				});
				
				currentSlider.find("div.sliderNext").hover(function(){
					currentSlider.find("div.sliderNext").addClass("sliderNextOver");							
				}, function(){
					currentSlider.find("div.sliderNext").removeClass("sliderNextOver");
				});
				
				var ulCounter = 1;
				currentSlider.find(".articleList").each(function(){
					$(this).removeClass("clearfix").addClass("floatLeft").addClass("articleListItem_"+ulCounter);
					ulCounter = ulCounter+1;
					$(this).children().each(function(){
						$(this).css("width", 245);
					});
				});
				
				currentSlider.find(".articleSliderContainer").wrap("<div class='articleSliderWindow' style='overflow: hidden'></div>");
				currentSlider.find(".articleSliderContainer").css("width", currentSlider.find(".articleList").length * 300);
				
				currentSlider.find(".articleSliderNav .sliderPrev").animate({opacity:0.5}, 0);
				currentSlider.find(".articleSliderNav").fadeIn();
				
				// Call SerialScroll plugin on articleSlider class
				currentSlider.serialScroll({
					target:'.articleSliderWindow',
					items:'ul.articleList',							//slide LI items
					offset:0,							//when scrolling to photo, stop 200 before reaching it (from the left)
					prev:'.sliderPrev',
					next:'.sliderNext',							
					start:0, 							//as we are centering it, start at the 2nd
					duration:1200,						//1.2 sec slide duration
					force:true,		
					stop:true,							//stop animation and start a new one when clicked during animation
					lock:false,							//allow slider to respond to events
					cycle:false, 						//pull back once you reach the end
					easing:'easeOutQuart', 				//use this easing equation for a funny effect
					onBefore:function(e, elem, $pane, $items, pos){
						var lastItem = "articleListItem_"+(ulCounter-1);
							
						if($(elem).hasClass("articleListItem_1")){
							//alert("first");
							currentSlider.find(".articleSliderNav .sliderPrev").animate({opacity:0.5}, 250);
							currentSlider.find(".articleSliderNav .sliderNext").animate({opacity:1}, 250);
						} else if($(elem).hasClass(lastItem)){
							currentSlider.find(".articleSliderNav .sliderPrev").animate({opacity:1}, 250);
							currentSlider.find(".articleSliderNav .sliderNext").animate({opacity:0.5}, 250);						
						} else {
							currentSlider.find(".articleSliderNav .sliderPrev").animate({opacity:1}, 250);
							currentSlider.find(".articleSliderNav .sliderNext").animate({opacity:1}, 250);	
						}
					}
				});
			}
		});
	}
}

/* ########################################################################### *
/* ##### JQUERY & OTHER UTILITIES
/* ########################################################################### */

// Supply a query string and parse it into a key value pairs

jQuery.query = function(s) 
{
		var r = {};
		if (s) {
				var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
				q = q.replace(/\&$/, ''); // remove the trailing &
				jQuery.each(q.split('&'), function() {
						var splitted = this.split('=');
						var key = splitted[0];
						var val = splitted[1];
						// convert numbers
						if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
						// convert booleans
						if (val == 'true') val = true;
						if (val == 'false') val = false;
						// ignore empty values
						if (typeof val == 'number' || typeof val == 'boolean' || val.length > 0) r[key] = val;
				});
		}
		return r;
};

/* FIN */


