$(document).ready(function(){
	
	// Search Tab Panel
	$('.Search').hide().after('<div class="SearchTab">Search</div>');
	$('.SearchTab').click(function(){
		$('.Search').slideToggle();
	});
	
	// Extra Info Panels
	$('.Extra1, .Extra2').find('h4:first').wrap('<div class="ExtraHeading"></div>');
	$('.ExtraHeading').each(function(){
		$(this).siblings().wrapAll('<div class="ExtraContent"></div>');
	});
	$('.ExtraContent').hide();
	$('.ExtraHeading').click(function(){
		$(this).next('.ExtraContent').addClass('Active');
		$('.ExtraContent').not('.Active').slideUp();
		$(this).next('.ExtraContent').slideToggle();
		$('.ExtraContent.Active').removeClass('Active');
	});
	
	// Prepare Home Page Header Area
	$('.HeaderMenu h1').each(function(index){
		$(this).data('index',index);
		var panel = $(this).nextUntil('h1');
		if(panel.length > 0){
			panel.wrapAll('<div class="HeaderArea"></div>');
		}
	}).wrapAll('<div class="DivLeft"></div>');
	$('.HeaderArea').wrapAll('<div class="DivRight"></div>');
	$('.HeaderArea').each(function(){
		$(this).find('p:first').siblings().wrapAll('<div class="TextOverlay"></div>');
	});
	
	var onHeader = false;
	var onOption = false;
	
	// Animate Home Page Header Area
	$('.DivLeft h1').hover(function(){
		onOption = true;
		$('.Header').hide();
		var index = $(this).data('index');
		var targetposition = -306 + (index * 51)
		var targetposition2 = (index * 270 *-1)
		$('.DivLeft').css({'backgroundPosition': 'right ' + targetposition + 'px'});
		$(this).addClass('Active');
		$('.HeaderArea.Active').removeClass('Active').hide();
		$('.HeaderArea:nth-child(' + (index + 1) + ')').addClass('Active').show();
		$('.HeaderWideWrap').animate({'backgroundPosition': '0 ' + targetposition2});
	},function(){
		onOption = false;
		var thish1 = $(this);
		setTimeout(function(){
			if(onHeader == false && onOption == false){
				defaultHeader();
				$(this).removeClass('Active');
			}
			if(onHeader == false){
				
			}
		}, 10);
	});
	
	$('.HeaderArea').hover(function(){
		onHeader = true;
	},function(){
		onHeader = false;
		defaultHeader();
		$('.DivLeft h1').css({'color': '#fff'});
	});
	
	function defaultHeader(){
		$('.HeaderArea.Active').removeClass('Active').hide();
		$('.DivLeft .Active').removeClass('Active');
		$('.DivLeft').css({'backgroundPosition': 'right -400px'});
		$('.Header').show();
	}
	

	
	// News animation
	$('.ListArticles').after('<div class="Navigation"><div class="LeftArrow"></div><div class="PauseButton"></div><div class="RightArrow"></div><div class="NextNews"><span class="NextNewsTitle">Next News story: </span><span class="NextNewsName"></span></div></div>').cycle({ 
		fx:     'scrollHorz', 
		speed:  1000, 
		timeout: 3000, 
		next:   '.RightArrow', 
		prev:   '.LeftArrow',
		slideExpr: '.NewsListArticleWrap',
		fit: 1,
		after: NextNews
	});
	$('.PauseButton').click(function(){
		if($(this).data('state') != 'paused'){
			$('.ListArticles').cycle('pause');
			$(this).css({'backgroundPosition': '0 -38px'});
			$(this).data('state','paused');
		}else{
			$('.ListArticles').cycle('resume');
			$(this).css({'backgroundPosition': '0 0'});
			$(this).data('state','play');
		}
	});
	function NextNews(currSlideElement, nextSlideElement, options, forwardFlag){
		$('.NextNewsName').text($(nextSlideElement).next().find('.NewsListTitle').text());
		if($('.NextNewsName').text() == ''){
			$('.NextNewsName').text($('.MiddleContent .NewsListArticleWrap:first .NewsListTitle').text());
		}
	}
	
	
	
	// Header Area Fade
	$('.Header p').each(function(){
		$(this).add($(this).nextUntil('p')).wrapAll('<div class="Slide"></div>');
	});
	$('.Header').cycle({ 
		fx:     'fade', 
		speed:  'slow', 
		timeout: 3000, 
		slideExpr: '.Slide',
		fit: 1
	});
	
	//prettyPhoto
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animation_speed: 'fast', /* fast/slow/normal */
		slideshow: 5000, /* false OR interval time in ms */
		autoplay_slideshow: false, /* true/false */
		opacity: 0.80, /* Value between 0 and 1 */
		show_title: true, /* true/false */
		allow_resize: true, /* Resize the photos bigger than viewport. true/false */
		default_width: 500,
		default_height: 344,
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_square', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		horizontal_padding: 20, /* The padding on each side of the picture */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		wmode: 'opaque', /* Set the flash wmode attribute */
		autoplay: true, /* Automatically start videos: True/False */
		modal: false, /* If set to true, only the close button will close the window */
		deeplinking: true, /* Allow prettyPhoto to update the url to enable deeplinking. */
		overlay_gallery: true, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
		keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
		changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
		callback: function(){}, /* Called when prettyPhoto is closed */
		ie6_fallback: true,
		flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="/Editor/Flash/Fleet-animation.swf" /><embed src="/Editor/Flash/Fleet-animation.swf" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>'
	});
	
});
