$('html').addClass('js');

$(document).ready(function() {
	var $pagename = $('meta[name=page-name]').attr("content"); 
	var $relativepath = $('meta[name=relative-path]').attr("content");

	jQuery.fn.exists = function(){return jQuery(this).length>0;} 

	function slideSwitch() {
		var $active = $('#slideshow img.active');

		if ( $active.length == 0 ) $active = $('#slideshow img:last');

		// use this to pull the images in the order they appear in the markup
		var $next =  $active.next().length ? $active.next() : $('#slideshow img:first');
		
		// uncomment the 3 lines below to pull the images in random order
		
		// var $sibs  = $active.siblings();
		// var rndNum = Math.floor(Math.random() * $sibs.length );
		// var $next  = $( $sibs[ rndNum ] );
		$active.addClass('last-active');

		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
		});
	}

	switch($pagename){
		case "home": case "contact_us": case "sign_in": case "learn_more":
			$('#slideshow')
				.append('<img src="'+$relativepath+'/images/headers/aus_home01.jpg" class="active" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_home02.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_home03.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_home04.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_home05.jpg" alt="" />');
			break;
		case "our_aim": case "recruitment": case "brochure":
			$('#slideshow')
				.append('<img src="'+$relativepath+'/images/headers/aus_about01.jpg" class="active" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_about02.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_about03.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_about04.jpg" alt="" />');
			break;
		case "engine_monitoring": 
			$('#slideshow')
				.append('<img src="'+$relativepath+'/images/headers/aus_enginei01.jpg" class="active" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_enginei02.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_enginei03.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_enginei04.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_enginei05.jpg" alt="" />');
			break;
		case "marine": 
			$('#slideshow')
				.append('<img src="'+$relativepath+'/images/headers/aus_marine01.jpg" class="active" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_marine02.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_marine03.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_marine04.jpg" alt="" />');
			break;
		case "utilities": 
			$('#slideshow')
				.append('<img src="'+$relativepath+'/images/headers/aus_power01.jpg" class="active" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_power02.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_power03.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_power04.jpg" alt="" />');
			break;	
		case "spares": 
			$('#slideshow')
				.append('<img src="'+$relativepath+'/images/headers/aus_spares01.jpg" class="active" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_spares02.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_spares03.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_spares04.jpg" alt="" />');
			break;
		case "news":
			$('#slideshow')
				.append('<img src="'+$relativepath+'/images/headers/aus_news01.jpg" class="active" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_news02.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_news03.jpg" alt="" />')
				.append('<img src="'+$relativepath+'/images/headers/aus_news04.jpg" alt="" />');
			break;
		default: /*nothing to do move along*/;
	}

	if($('#slideshow').exists()){
		$(function() {
			var $slideInterval = setInterval(slideSwitch,5000);
		});				
	}

	$('#nav').droppy();
	$('#nav li:has(ul) > a').addClass('expandable');	
	
	$('#main-movie').addClass('main-movie').flash({ 
		src: $relativepath+'/flash/HOME_lrg_flash.swf',
		width: 604,
		height: 216,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});
	
	$('#main-defence-movie').addClass('main-movie').flash({ 
		src: $relativepath+'/flash/DEFENCE_lrg_flash.swf',
		width: 604,
		height: 216,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});	
	
	$('#main-inhouse-movie').addClass('main-movie').flash({ 
		src: $relativepath+'/flash/INHOUSE_lrg_flash.swf',
		width: 604,
		height: 216,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});	
	
	$('#main-marine-movie').addClass('main-movie').flash({ 
		src: $relativepath+'/flash/MARINE_lrg_flash.swf',
		width: 604,
		height: 216,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});	
	
	$('#main-offshore-movie').addClass('main-movie').flash({ 
		src: $relativepath+'/flash/OFFSHORE_lrg_flash.swf',
		width: 604,
		height: 216,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});	
	
	$('#main-power-movie').addClass('main-movie').flash({ 
		src: $relativepath+'/flash/POWER_lrg_flash.swf',
		width: 604,
		height: 216,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});		
	
	$('#main-spares-movie').addClass('main-movie').flash({ 
		src: $relativepath+'/flash/SPARES_lrg_flash.swf',
		width: 604,
		height: 216,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});			
	
	$('#sidebar-movie').addClass('sidebar-movie').flash({ 
		src: $relativepath+'/flash/HOME_sml_flash.swf',
		width: 266,
		height: 216,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});
	
	$('#sidebar-movie-fm').addClass('sidebar-movie').flash({ 
		src: $relativepath+'flash/FUEL_sml_flash.swf',
		width: 266,
		height: 216,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});
	
	$('#sidebar-movie-enginei').addClass('sidebar-movie').flash({  
		src: $relativepath+'flash/ENGINE_sml_flash.swf',
		width: 266,
		height: 216,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});
	
	$('#in-house-facilities-movie').flash({ 
		src: $relativepath+'/flash/Panorama7.swf',
		width: 600,
		height: 400,
		wmode: 'opaque',
		bgcolor: '#ffffff'
	}, { version: 8,expressInstall: true});

	$('#time-lapse-movie').addClass('time-lapse-movie').flash({ 
		src: $relativepath+'/flash/timelapse.swf',
		width: 290,
		height: 205,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});

	$('#movie-1-movie').flash({ 
		src: $relativepath+'/flash/GEmov01.swf',
		width: 290,
		height: 205,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});
	
	$('#movie-2-movie').flash({ 
		src: $relativepath+'/flash/GEmov02.swf',
		width: 290,
		height: 205,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});

	$('#movie-3-movie').flash({ 
		src: $relativepath+'/flash/GEmov03.swf',
		width: 290,
		height: 205,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});

	$('#clock-uk').flash({ 
		src: 'http://24timezones.com/timescript/clock_final.swf',
		width: 70,
		height: 70,
		quality: 'high',
		wmode: 'transparent',
		flashvars: {color:'cccccc',logo:0,city:189739}
	}, { version: 8,expressInstall: true});
	
	$('#clock-wa').flash({ 
		src: 'http://24timezones.com/timescript/clock_final.swf',
		width: 70,
		height: 70,
		quality: 'high',
		wmode: 'transparent',
		flashvars: {color:'cccccc',logo:0,city:189738}
	}, { version: 8,expressInstall: true});	

	$('#vessel-tracking-movie-1').flash({ 
		src: $relativepath+'/flash/vessel_tracking_1_flash.swf',
		width: 500,
		height: 420,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});	
	
	$('#vessel-tracking-movie-2').flash({ 
		src: $relativepath+'/flash/vessel_tracking_2_flash.swf',
		width: 500,
		height: 420,
		wmode: 'opaque'
	}, { version: 8,expressInstall: true});	

	$('.block').eq(1).hide();

	$('div#middle-column #movie-links li div').eq(0).clone().appendTo('div#middle-column #holder-movie');
	$('div#middle-column #movie-links li div').hide();
	$('div#middle-column #movie-links li').addClass('anchor').bind('click',function(event){
		$('div#middle-column #holder-movie').empty();
		$('div',this).clone().appendTo('div#middle-column #holder-movie').show();
		return false;
	});
	
	$('div#middle-column #movie-links li').hover (function() { 
		$(this).addClass('anchor-hover'); 
	}, 
		function(){ 
			$(this).removeClass('anchor-hover'); 
		} 
	); 
	
	$('p:has(a.more)+div').hide();
	$('p a.more').bind('click',function(event){
		event.preventDefault();
		$(this).parent().next('div').toggle();
		$(this).html($(this).parent().next('div').is(':hidden') ?	'more&hellip;' : '&laquo; back');
		return false;
	});
	
	$('div[id^=text-]').not('div#text-main').hide();
	$('a[id^=link-]').bind('click',function(event){							   
		var itemid = $(this).attr('id').slice(5);

		$('.main-column').children().not('div.quote').hide()
		$('.main-column #text-'+itemid).show();
		if(itemid == 'main'){
			$('a#link-main').parent().hide();
			$('a.site-back').parent().show();
		}else{
			$('a#link-main').parent().show();
			$('a.site-back').parent().hide();
		} 
		return false;
	});
	
	$('div[id^=textmovie-]').not('div#textmovie-1').hide();
	$('a[id^=linkmovie-]').bind('click',function(event){							   
		var itemid = $(this).attr('id').slice(10);

		$('#content #movies').children().not('div.quote').hide()
		$('#content #movies #textmovie-'+itemid).show();
		return false;
	});	
	
	$('#footer #timezones [class^=linkcontact-]').bind('click',function(event){							   
		var itemid = $(this).attr('class').slice(12);

		$('#footer .text').children().hide();
		$('#footer .text #contact-'+itemid).show(); 
		return false;
	});
	
	$('#sidebar div.block ul li h3.linklogo:has(a.more)').css('cursor','pointer').bind('click',function(event){
		window.open($('a',this).attr('href'),'_self');
		return false;
	});
	
	var alternateColors = function($element){
		$('p:odd',$element).removeClass('even').addClass('odd');
		$('p:even',$element).removeClass('odd').addClass('even');
		$('tbody tr:odd',$element).removeClass('even').addClass('odd');
		$('tbody tr:even',$element).removeClass('odd').addClass('even');
	}	
	alternateColors('.alternate');
	
	$('a[href^=http],a[class*=newwindow]').attr('target','_blank');
	$('a[href$=pdf]').attr('target','_blank').addClass('pdf');
	$('a[href$=doc]').attr('target','_blank').addClass('doc');
	$('a[href=#][class*=coming-soon]').click(function() {return false;});
	$("a[rel^='prettyPhoto']").prettyPhoto();
});

