/********************************************
Highslide modal windows with common settings
*******************************************/

// Generic modal window
function popupFrame(target, frameWidth, frameHeight)
{
	if (frameWidth  == undefined) frameWidth  = 910;
	if (frameHeight == undefined) frameHeight = 450;

	return hs.htmlExpand(target, { 
				objectType: 'iframe', 
				width: frameWidth,
				height:frameHeight, 
				creditsPosition: 'bottom left',
				headingEval: 'this.a.title',
				wrapperClassName: 'titlebar' } )
}

// Generic modal window
function popupImage(target)
{
	return hs.expand(target)
}

// Modal window for videos
function popupVideo(target)
{	
	return popupFrame(target,800,560);
}

/********************************************
jQuery initialisor
*******************************************/
$(function() {
	
	// Change all image's title attribute to alt
	/*$('a img[title]').each(function(){
		$(this).attr('title',$(this).attr('alt'));
	});*/
	
	// Highlight Main menu tab to indicate current page
	function filterPath(pathString)
	{
		return pathString.replace(/^\//,'').replace(/\/(index\.\w{3,4})?$/,'')
	}
	
	function updateMainMenu()
	{
		$('#menu a').filter(function() {
									return filterPath(this.pathname) == filterPath(location.pathname);
									}).addClass('selected'); 			
	}
	
	updateMainMenu();
	
	// Initialise tooltips
	
	// Define a new tooltip style, logoStyle
	$.fn.qtip.styles.logoTip = { // Last part is the name of the style
		width: 200,
		background: '#f5a83d',
		color: 'white',
		textAlign: 'left',
		border: {
		  width: 7,
		  radius: 5,
		  color: '#f5a83d'
		},
		tip: 'bottomLeft',
		name: 'dark' // Inherit the rest of the attributes from the preset dark style
	}
	
	// Define common tooltip settings
	var ttStyle = { name: 'logoTip',tip: true };
	var ttPosition = {
		corner:{target:'topRight', tooltip:'bottomLeft'},
		target:'mouse'
	}
	
	// Apply tooltips to logo lists
	$('.logo-list a').each(function()
	{
		var imgAlt = $('img',this).attr('alt');
		imgAltArr = imgAlt.split(" - ");
		if(imgAltArr.length > 1){
			var compName = imgAltArr[0];
			imgAltArr[0] = '<b>' + compName + '</b><br/>';
		}
		imgAlt = imgAltArr.join('');
		
		// Change A's title attribute to Company Name
		$(this).closest('a').attr('title',compName);

		$(this).qtip({
			content: imgAlt,
			style: { name: 'logoTip',tip: true },
			position: {
				corner:{target:'topRight', tooltip:'bottomLeft'},
				target:'mouse'
			} 
		})
	})
	
	// Apply tooltips to all linked images with alt text
	/*$('#content a img[alt]').qtip({
		position: ttPosition,
		style: ttStyle
	});*/
	
	// Apply tooltips to Homepage's right column
	$(".column.quarter .quick-links a[title]").qtip({position: ttPosition, style: ttStyle});
	
	// Apply fades to linked images
	$('#content a img').each(function(){
		$(this).hover(
			function(){$(this).stop().animate({opacity:0.7},250)},
			function(){$(this).stop().animate({opacity:1},250)}
		)
	})
	
	$
	
	// Initialise icons for link lists
	$('UL.link-list a.pdf').closest('li').attr('class','pdf');
	$('UL.link-list a.doc').closest('li').attr('class','doc');
	$('UL.link-list a.video').closest('li').attr('class','video');
	$('UL.link-list a.wiki').closest('li').attr('class','wiki');
	$('UL.link-list a.top').closest('li').attr('class','top');
	
	// Add video trigger JS
	//$('a.video').click(function(){return popupVideo(this)}); // Broken in IE
	
	// Initialise tabs
	$("ul.tabs").tabs("div.panes > div",{effect:'fade'});
	
	// load sitemap and apply relevant tooltips
	$("#sitemap .quick-links a[title]").qtip({position: ttPosition, style: ttStyle});
	/*$("div#sitemap").load("sitemap.html #site-map", function(){
		$("#site-map .quick-links a[title]").qtip({position: ttPosition, style: ttStyle});
	});*/
	
	// Load quotes
	$('#quote').randomContent({xmlPath: "quotes.html", nodeName: "div.quote"});
	
	// Insert Flash
	
	if(swfobject.hasFlashPlayerVersion('9.0.0'))
	{
		$('#header').prepend("<div id='flash-header'><div id='flash-object'></div>");
		$('body').attr('class','flash-header');
		var flashvars = {};
		var params = {}
		params.wmode = "transparent";
		var attributes = {};
		swfobject.embedSWF("/switch-now/swf/header.swf", "flash-object", "540", "420", "9.0.0", false, flashvars, params, attributes);
	}
	
	
	
});
