$j(document).ready(function(){
   //$j(document).pngFix();
	

	// 'if' statments prevent plugsin being loaded if not used on page
	// plugins are separate to allow for different variable to be set
	
	if ($j('#navWrapper').length) {
		$j('#navWrapper').RYnavDropdown();
	};

// ACCORDIANS #######################################################################################	
	// home page accordian	
	if ($j('#home-slider').length) {
		$j('#home-slider').RYaccordian({listClick: 'h2'});
	};
	
	// services group with hidden services links
	if ($j('#primaryAccordian').length) {
		$j('#primaryAccordian').RYaccordian({startIndex: 1, colapseOthers: 'no'});
	};
	
	// right col, related info accordian
	if ($j('#relatedInfo #relatedAccordian').length) {
		$j('#relatedAccordian').RYaccordian({listClick: 'h3', duration:400});
		$j('#relatedAccordian li.relatedlinks p:last').css("padding-bottom","10px");
	};
	
	if ($j('#contactUs #relatedAccordian').length) {
		$j('#relatedAccordian').RYaccordian({listClick: 'h2', startIndex: '',autoStartIndex: 'yes', autoScrollTo: 'yes', duration:1000});	//
	};
	
	// projects advanced search
	if ($j('#advFilter .accordian').length) {
		$j('.accordian').RYaccordian({listClick: 'h4', startIndex: ''});	//
	};
	
	
// CAROUSEL #######################################################################################	
	// homepage carousel
	if ($j('#home-slider .carousel').length) {
		$j('.carousel').RYcarousel();
	};
	
	// project browse + banner carousel
	if ($j('#browseProjects .carousel, .banner .carousel').length) {
		$j('.carousel').RYcarousel();
	};
	
	// image gallery carousel (lightbox imagegallery is called from RYlightbox.js)
	if ($j('#innerHTML .carousel').length) {
		$j('.carousel').RYcarousel({variableWidth: 'false', switchHeaders: 'true', autoPlay: 'true'});
	};
	
// LIGHTBOXES #######################################################################################	
	if ($j('.lightbox').length) {
		if($j('ul#services .lightbox').length){
		    $j('.lightbox').each(function(){
		        var href = $j(this).attr("href");
		        $j(this).attr("href",href + '?show-accordian-list=true');
			    $j(this).RYlightbox({innerContent: ' .serviceList'});
		    });
		}else{
			$j('.lightbox').RYlightbox({innerContent: ' #innerHTML'});
		}
	};

// NAVIGATION #######################################################################################	
    // add "curent" class to selected top navigation (done via javascript so can cache rendering without varying by data)
    var selectedPath = window.location.pathname;
    $j('#navigation-primary > ul > li > a').each(function(){
		var navPath = $j(this).attr("href");
		navPath = navPath.substring(0,navPath.indexOf(".aspx")); //Remove extention
		
		if (navPath.lastIndexOf("index.html") > 0) {
		    navPath = navPath.substring(0,navPath.lastIndexOf("index.html")); //Only return first level path componant of tree
		}
		
		if (selectedPath.indexOf(navPath) == 0) {
		    $j(this).parent().addClass("current");
		    return false;
		}
    })
    

// OTHER RANDOM SCRIPTS #######################################################################################	

	// emptys search box when selected
	$j('input[type=text]').each(function(){
		var text;
		$j(this).focus(function(){
			text = $j(this).attr('value');
			if($j(this).attr('value') === 'Search' || 'Enter...'){
				$j(this).attr('value','')
			}
		});
		$j(this).blur(function(){
			if ($j(this).attr('value') === ''){
				$j(this).attr('value',text)
			}
		})
	})
	
	// checks URL for # and opens lightbox if exists
	function autoOpenLb (){
		var url = new String(document.location)
		var hash = (url.indexOf('#'));
		if (hash > 0 && hash+1 != url.length){
			var href = (url.slice(hash,url.length));
			var start = (href.indexOf(':'))
			href = (href.slice(start+1,href.length))
			$j('a[href^='+href+']').click();
		}
	}
	autoOpenLb ()
	
	// Drop down radio buttons in news section
	if($j('fieldset.news').length){
		countrySelect();
	}
	function countrySelect(){
		var speed = 200;
		$j('fieldset.news div .checkbox:checked').each(function(){
			speed = 0;
			show(this);
		});
		$j('fieldset.news div .checkbox').click(function(){
			speed = 500;
			if($j(this).is(':checked')){
				show(this)
			} else {
				hide(this)
			}
		})
		var siblings;
		
		function show(checkbox){
			siblings = $j(checkbox).parent().siblings().length;
			var newHeight = (siblings+1)*1.5;
			$j(checkbox).parent().css('backgroundColor', '#f5f5f5')
			if (siblings != 0){
				$j(checkbox).parent().parent().animate({height: newHeight+'em'}, speed);
			}
		}
		function hide(checkbox){
			if (siblings != 0){
				$j(checkbox).parent().parent().animate({height:'1.5em'}, speed);
			} else {
				speed = 0;
			}
			$j(checkbox).parent().animate({opacity:1}, speed, function(){
				$j(this).css('backgroundColor', '#fff')
			})
		}
	}

	// Advanced search options
	if($j('#advFilter').length){
		$j('#advFilter').fn_RYadvSearch();
	};


// FLASH #######################################################################################	
	// flash experience arup player
	if ($j('#experience').length) {
		loadExperiencePlayer();
	};

	// flash video player
	if ($j('.VPcontainer').length) {
		loadVideoPlayer();
	}
	if ($j('#alternativeOfficeMap').length){
		loadMap()
	}
	
// IE Hover #####################################################################################	
	$j('.submitImage, a.lightboxLink').hover(function(){
		$j(this).addClass('hover')//.css('border','1px solid #000')
	},function(){
		$j(this).removeClass('hover')//.css('border','none')
	})


// search form button rollover ##################################################################

	$j("input.submitb").hover(function() {
		$j(this).attr("src","assets/images/presentation/search_roll.jpg");
	}, function() {
		$j(this).attr("src","assets/images/presentation/search.jpg");
	});


}); // end document.ready 
// #############################
// #############################
// #############################
// #############################



// used to switch path for local vs live
// var flashExpPath is set in header of HTML in local
var local;
var flashExpPath;

if (local == 'true') {
	flashExpPath = "assets/flash/experience/build/index.html";
	flashMapPath = "assets/flash/officeMap/build/index.html";
} else {
	flashExpPath = "assets/flash/experience/build/index.html";
	flashMapPath = "assets/flash/officeMap/build/index.html";
}

// needs to be outside of "document.ready" to work
// set globally because they are called by other plugins
var swfName = "experienceFlash";
var containerName = "experience";

//stayTop()
function openExperience(){
	var windowHeight = $j(window).height()
	movieById(swfName).height = windowHeight;
	$j('#'+containerName).css('height', windowHeight);		//.css('height','100%');

	if ($j('#lightboxBg').length){
	} else {	
		$j('body').append('<div id="experienceBg"></div>');
		$j('#experienceBg').css({'width':$j(window).width(), 'height': $j(document).height()-$j(window).height(), 'top': $j(window).height(), 'opacity':.95})
	}
	pauseVideo();
}
function closeExperience(){
	movieById(swfName).height = "26";
	$j('#'+containerName).css('height','26px');
	$j('#experienceBg').remove()
}

function pauseVideo(){
	if ('#videoPlayer'){
		movieById("videoPlayer").pauseVideo();
	}
}

function movieById(id){
	return (navigator.appName.indexOf("Microsoft") != -1)? document.getElementById(id) : document[id];
}


// sets video size in lightbox
var ARUP_videoWidth = 576;
var ARUP_videoHeight = 370;

function loadVideoPlayer(){
	$j('.VPcontainer').each(function(){
		var thisURL = $j(this).children('input[type=hidden]').attr('value')
		var VP_flashvars = {}
		VP_flashvars.smilPath = thisURL;

		$j('.VPcontainer').flash(
			{ 
				src: flashExpPath+'swfs/VideoPlayer.swf',
				width: ARUP_videoWidth,
				height: ARUP_videoHeight,
				id: "videoPlayer",
				name: "videoPlayer",
				flashvars: VP_flashvars
			},
			{
				expressInstall: true,
				version: '9.0.115'
			}
		);
	})	 
}
function loadExperiencePlayer(){
	var EX_flashvars = {xmlUrl:"/System/ExperienceArupXml.aspx"};
	$j('#experience').flash(
		{ 
			src: flashExpPath+'swfs/Experience.swf',
			width: '100%',
			height: '100%',
			id: swfName,
			name: swfName,
			flashvars: EX_flashvars
		},
		{
			expressInstall: true,
			version: '9.0.115'
		}
	);
}

// global locations settings
function resizeMap(height){
	movieById('OfficeMap').height = height;
}

function movieById(id){
	return (navigator.appName.indexOf("Microsoft") != -1)? document.getElementById(id) : document[id];
}

function loadMap(){
	$j('#alternativeOfficeMap').flash(
		{
			src: flashMapPath+'swfs/Loader.swf',
			base: MAP_base,
			width: MAP_size.width,
			height: MAP_size.height,
			id:"OfficeMap",
			name:"OfficeMap",
			flashvars: MAP_flashvars
		},
		{
			expressInstall: true,
			version: '9.0.115'
		}
	)
	//if(swfmacmousewheel) swfmacmousewheel.registerObject(attributes.id);
};

// control videos


