// JavaScript Document

$(document).ready(function()
{
	

	// for FontSizer
	var options = { min: -2.5, max: 5};
	//Initialize the font sizer for the site.
	$.FontSizer.Init(options);
	
	
	// so the links don't work!
	$('.printTextResize').show();
	$('.sizeDown').click(function(){ return false; });
	$('.sizeUp').click(function(){ return false; });
	
	
	// main top nav
	$('#mainNav li a').each(function(){
		if(!$(this).parent().attr("class")){ // if the nav hovered is not "on"
			$(this).hover(function(){
				$(this).parent().addClass( $(this).attr("class") + "List" );
			}, 
			function(){
				$(this).parent().removeClass( $(this).attr("class") + "List" );
			});	
		}
	});
	
	// for the mainNav on state
	
	$('#mainNav li.on a').each(function(){
		$(this).parent().addClass( $(this).attr("class") + "List" );
	});
	
	// button hover
	$('.transparentHover').hover(function(){
		$('img',this).css({opacity: .65});
	}, function(){
		$('img',this).css({opacity: 1});
	});
	
	// quicklinks drop down
	$('#quicklinksSelect').hover(function(){
			$('.quicklinksSelectDrop').css({display: "block"});
		},
		function(){
		    $('.quicklinksSelectDrop').css({display: "none"});
		}		
	);
	
	// add to calendar drop down
	$('.addtoCal').hover(function(){
			$(this).children('.addtoCalDrop').css({display: "block"});
			$(this).children('.select').css({background: "url(/Images/buttons/calendar-add-ro.gif) no-repeat"});
		},
		function(){
		    $(this).children('.addtoCalDrop').css({display: "none"});
			$(this).children('.select').css({background: "url(/Images/buttons/calendar-add-off.gif) no-repeat"});
		}		
	);
	
	// for jobs at mcec page
	$("div.job-container").click(function (e) {   
	    if(!($.browser.msie && $.browser.version == "6.0"))
	    {
            var $targetContent = $(this).children("div.job-description");
            if ($targetContent.is(":hidden")){
                $targetContent.slideDown("fast");
            }
            else{
                $targetContent.hide();
            }
        }
    });

	// for home page mainNav
	$('.home #mainNav li a').each(function(){
		var parent = $(this).parent().attr("class");
		$(this).hover(function(){
			$(this).parent().addClass( parent + "List" );
		},
		function(){
			$(this).parent().removeClass( parent + "List" );
		});
	});
	
	// image download hover
	$('.imgDownload li a').each(function(){
		$(this).hover(function(){
			$(this).prepend("<img src='/images/links/thumbDownload.gif' class='download' alt='Download' />");
		},
		function(){
			$('img.download').remove();
		});
	});	
	
	$('.gallery li a').hover( function(){
		caption = $('img', this).attr('alt');
		$('.captionHover').text(caption);
		$('.captionHover', this).show();											 
	}, function(){
		$('.captionHover').hide();
	});

	//global var
	var currentOne;
	var nextOne;
	var prevOne;

	//gallery
	$('.gallery li a').click(function(){
		
		$('.loadingBar').show();
		$('.gallery ul').css({visibility: 'hidden'});
		
		currentOne = $(this).parent('li');
		nextOne = currentOne.next('li');
		prevOne = currentOne.prev('li');
		
		caption = $('img', this).attr('alt');
		$('.caption').text(caption);
		
		$('.previousBtn').show();
		$('.nextBtn').show();
		$('.backThumbs').show();
		$('.caption').show();
		
		$('.largeImg').attr({src : currentOne.children('a').attr('href')}).load(function(){	
			$('.loadingBar').hide();
			$('.watermark').css({'opacity':0.4}).show();
			$(this).show();
		});
		
		return false;
	});
	
	$('.nextBtn').click(function(){
		
		if(!nextOne.children('a').attr('href')){
			$('.loadingBar').css({display: 'none'});
			btt();
		}
		else{
			currentOne = nextOne;
			nextOne = currentOne.next('li');
			prevOne = currentOne.prev('li');
			
			$('.loadingBar').show();
			$('.largeImg').hide();
			$('.caption').text('')
			caption = currentOne.children('a').children('img').attr('alt');
			$('.caption').text(caption);
			
			$('.largeImg').attr({src : currentOne.children('a').attr('href')}).load(function(){
				$('.loadingBar').hide();
				$('.caption').show();
				$('.watermark').css({'opacity':0.4}).show();
				$(this).show();
			});
		}
		return false;
	});
	
	$('.previousBtn').click(function(){
		if(!prevOne.children('a').attr('href')){
			$('.loadingBar').hide();
			btt();
		}
		else{
			currentOne = prevOne;
			nextOne = currentOne.next('li');
			prevOne = currentOne.prev('li');
			
			$('.loadingBar').show();
			$('.largeImg').hide();
			$('.caption').text('')
			caption = currentOne.children('a').children('img').attr('alt');
			$('.caption').text(caption);
			
			$('.largeImg').attr({src : currentOne.children('a').attr('href')}).load(function(){
				$('.loadingBar').hide();
				$('.caption').show();
				$('.watermark').css({'opacity':0.4}).show();
				$(this).show();
			});
		}
		return false;
	});

	$('.backThumbs').click(function(){
		$('.loadingBar').hide();
		btt();
		return false;
	});
	
	
	//overlay
	var _newHeight = $(".home .container").height();
	var _bodyWidth = $("body").width();
	var _bodyHeight = $("#wrap").height();
	var _containerWidth = $(".home .container").width();
	var _leftDistance = (_bodyWidth - _containerWidth) / 2;
	var _newWidth = _bodyWidth - _leftDistance;
	$("span.modalOverlay").css({'opacity': '0.5', 'width': _bodyWidth, 'height': _bodyHeight});
	$(".home .overlay").css({'height': _newHeight - 48, 'left': _leftDistance,'width': _newWidth});  
	
	$(window).resize(function(){
		var _bodyWidthR = $("body").width();
		var _bodyHeightR = $("#wrap").height();
		var _leftDistanceR = (_bodyWidthR - _containerWidth) / 2;
		var _newWidthR = _bodyWidthR - _leftDistanceR;
		$("span.modalOverlay").css({'opacity': '0.5', 'width': _bodyWidthR, 'height': _bodyHeightR});
		$(".home .overlay").css({'height': _newHeight - 48, 'left': _leftDistanceR,'width': _newWidthR});  
	})
	
	
	function do_sIFR(){
  		// replacement statements go here
		sIFR.replace(teuton, {
  		selector: 'h1',
  		wmode: 'transparent', 
  		css: '.sIFR-root { font-weight:normal; color: #ffffff; leading: -4;}'
		});
	};
		
	// when load document, take overlay stuff out of news-container, and shove it up the top.
	$('#wrap').prepend($('#overlayStuff'));
	var attachmentContent;
	var attachmentRemove = false;
	
	//activate overlay
	$(".open-overlay").click(function(){
									  
		$(".modalOverlay").fadeIn('normal', function(){
			$(this).next().fadeIn('normal', function(){
				do_sIFR(); //Replace h1 element as this is missed as its not display on page load
				if(!attachmentRemove){
					$('.news-attachment', this).fadeIn();
					
				}
				else{
					$('.news-attachment', this).append(attachmentContent).fadeIn();
					attachmentRemove = false;
				}
			});
		});
		return false;			
	})

	//deactivate overlay
	$(".close-overlay").click(function(){
		//attachmentContent = $('#overlayStuff .news-attachment').html(); // store content before getting rid of it.
		$('.overlay .news-attachment').fadeOut('normal', function(){
			$(".overlay").fadeOut('normal', function(){
				$(this).prev().fadeOut();
			});
		});
		if(thisMovie("Video")){
			onStopVideo();
		}
		return false;
	})

	// HACK ALERT
	// route stop video call to base document
	function onStopVideo()
	{
		//alert('stop 1 : '+ thisMovie("Video"));
		thisMovie("Video").stopVideo();
	}

	function thisMovie(movieName)
	{
		//alert("thisMovie");
		if (navigator.appName.indexOf("Microsoft") != -1)
		{
			return window["Video"];
		}
		else
		{
			return document[movieName];
		};
	};
	
	//function thisMovie(movieName)
	//{
		//var swfMovie;
//
		//var isIE = navigator.appName.indexOf("Microsoft") != -1;
		//
		//if (isIE) swfMovie = window[movieName];
		//else swfMovie = document[movieName];
		//
		//check if it is embedded in a form...
		//if ( swfMovie==undefined ) eval("swfMovie = document.forms[0]."+movieName);
//
		// Assign global movie name var to the swfMovie to help flash find the object when adding callback
		//eval( movieName + " = swfMovie;" );
//
		//return swfMovie;
	//}
	
	$('.ccRoomTable tbody tr').hover(function(){
		$(this).addClass('ccRoomTableHover');
		$('th', this).prepend('<div id="ccRoomTableTool"><p>view room on interactive map</p></div>');
		$('th p', this).css({'opacity':0.8});
	}, function(){
		$(this).removeClass('ccRoomTableHover');
		$('#ccRoomTableTool').remove();
	});
	
	$('.ccRoomTable tbody tr').click(function(){
		var linkHref = $(this).children('th').children('a').attr('href');
		window.location = linkHref;
	});
	
	// Site Switcher
	var ssStatus = false;
	$('#siteSwitcher').show();
	
	// calculate height of <ul>
	var ssUL = $('#siteSwitcher ul');
	var ssULheight = ssUL.height();
	ssUL.css('margin-top', '-'+ssULheight+'px');
	
	$.easing.easeOutQuint = function(x,t,b,c,d){
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	};
	
	
	
	var openSS = function(e){
		var trgt = $(e.target);
		jQuery(document).bind('click', onBlurList);
		if(ssStatus == false) { 
			ssUL.animate({ marginTop:0}, {duration:600, easing:'easeOutQuint'});
			$('#siteSwitcher #ssTitle span').unbind('click');
			$('#siteSwitcher #ssTitle span').bind('click', onBlurList);
			$('span', this).addClass('hover');
			ssStatus = true;
		}
		return false;
	}
	
	var onBlurList = function(e) {
		var trgt = $(e.target);
		if(ssStatus == true) { 
			ssUL.animate({marginTop:-ssULheight}, {duration:600, easing:'easeOutQuint'});
			$('#siteSwitcher #ssTitle span').unbind('click');
			$('#siteSwitcher #ssTitle span').bind('click', openSS);
			$('#siteSwitcher #ssTitle span').removeClass('hover');
			ssStatus = false;
		}
		jQuery(document).unbind('click');
		if( trgt.parent('a').attr('id') == 'ssTitle'){
			return false;
		}
	}
	
	$('#siteSwitcher #ssTitle span').bind('click', openSS);
	
	// back to top scroller
	$('a[href*=#wrap]').click(function() {
		$.scrollTo('#wrap', 600, {easing: 'easeOutQuint'});
		return false;
	  });
	
	
	// landing page bannerImage cycle.
	if( $('.landingPage .bannerImage').length > 0){
		$('.landingPage .bannerImage').cycle({
			fx:'fade',
			speed:1000,
			timeout:5000
		});
	}
	
	// landing page bannerImage cycle.
	if( $('#homeWrap #bgImages').length > 0){
		$('#homeWrap #bgImages').cycle({
			fx:'fade',
			speed:1000,
			timeout:5000
		});
	}
	
	// footeTiles hover
	if($('#extra').length > 0){
		$('.relatedInterests li a').hover(function(){
			$(this).css('opacity', 0.65);
		}, function(){
			$(this).css('opacity', 1);
		});
	}
	
}); // end of document.ready()

function btt(){
	currentOne = null;
	nextOne = null;
	prevOne = null;
	$('.previousBtn').hide();
	$('.nextBtn').hide();
	$('.backThumbs').hide();
	$('.largeImg').hide();
	$('.watermark').hide();
	$('.gallery ul').css({visibility: 'visible'});
	$('.caption').text('').hide();
}

/*
function sizeIFrame() {
	var helpFrame = $('iframe');
	var innerDoc = (helpFrame.get(0).contentDocument) ? helpFrame.get(0).contentDocument : helpFrame.get(0).contentWindow.document;
	//helpFrame.height(innerDoc.body.scrollHeight + 10);
	alert(innerDoc.body.scrollHeight);

	// re-align decoration after iframe has changed
	$('.leftTriBg').css({marginTop: innerDoc.body.scrollHeight+"px"});
	$('.bottomTriBg').css({marginTop: innerDoc.body.scrollHeight+"px"});
	$('.leftBlock').css({marginTop: innerDoc.body.scrollHeight+"px"});
}
*/
// image roll over script

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function openInteractiveBrochure(url, title)
{	
	msgWindow = window.open(url,title,'toolbar=no,location=no, width=' + (screen.availWidth).toString()-10 +
	    ',height=' + (screen.availHeight).toString()-10 + ',directories=no,status=yes,scrollbars=no,resizable=yes,menubar=no');

}
//function stopVideo(){
	//thisMovie("Video").stopVideo();
	//alert('stop 2 : '+ thisMovie("Video"));
//}
//function thisMovie(movieName){
	//if (navigator.appName.indexOf("Microsoft") != -1){
	  //return window[movieName];
	 //}
	 //else{
		//return document[movieName];
	 //};
//};