
//requires jQuery

//wait for the DOM to be loaded
$( document ).ready( function() {
								   
	//GENERIC SITE ELEMENTS
	GenSiteElements.init();
	
	//IE6 FIX.
	ieSixFix.init();
	
	//NAV
	$('div#nav li a').hover(
		function() {
		$(this).animate({paddingLeft: '+=15px'}, 200);
		}
		,
		function() {
		$(this).animate({paddingLeft: '-=15px'}, 200);
		}
	);
	
	//TESTIMONIALS.
	$('blockquote.testimonial').each( function(){
		var myPara = $(this).children('p').eq(0);
		var myText = myPara.text();
		//myText = myText.replace( /&ldquo;/g, 'xxx' );
		//myText = myText.replace( /&rdquo;/g, 'xxx' );
		myText = myText.substring( 1, myText.length-1 );
		myPara.text( myText );
		myPara.append('<span class="start"></span>');
		myPara.append('<span class="end"></span>');
	} );
	
	//LAYOUT
	ContentBlock.init();
	
	//VIDEO
	swfobject.embedSWF(
		"flash/videoPlayer.swf",
		"flashVideoPlayer",
		"474",
		"267",
		"9.0.28",
		"flash/expressInstall.swf",
		{ file:'xml/showreel-play-list.xml', image: '', autostart: true, volume: 0, repeat: 'always',controlbar: 'over', skin: 'flash/bekle.swf' },
		{ allowfullscreen: true, wmode: 'transparent' }
	);
	
	//COMMERCIALS
	swfobject.embedSWF( "flash/commercials-gallery.swf", "commercialsGallery", "1120", "460", "9.0.28", "flash/expressInstall.swf", {}, { wmode: 'transparent' } );
	
	//FAQ
	FaqList.init();
	
	//CONTACT
	$('div#captchaWrapper').append('<a href="javascript:;" id="captcha_reload">Reload Image</a>');
	$('div#captchaWrapper a#captcha_reload').click(function(event){
		var mySrc = 'script/securimage/securimage_show.php?' + Math.random();
		$('img#captcha').attr( 'src', mySrc );
		event.preventDefault();
	});
	
} );//end document ready

//HAndles setting up content blocks.
ContentBlock = 
{
	init : function()
	{
		//temp
		if( $('body').hasClass('faqs') ){ return; };
		var myHeight = ContentBlock.getHeight()
		ContentBlock.setHeight(myHeight);
	}
	,
	getHeight : function()
	{
		var myMaxHeight = ContentBlock.getMaxHeight();
		return( myMaxHeight );
	}
	,
	setHeight : function( pmHeight )
	{
		$('div.contentBlock').height(pmHeight);
	}
	,
	getMaxHeight : function()
	{
		var myMaxHeight = 0;
		$('div.contentBlock').each( function(){
			var myHeight = $(this).height();
			if( myHeight > myMaxHeight ){ myMaxHeight = myHeight };
		} );
		
		//Check for Video.
		myMaxHeight = ContentBlock.checkElementHeight( myMaxHeight, 'div#flashVideoPlayerWrapper');
		
		//Check for Banner.
		myMaxHeight = ContentBlock.checkElementHeight( myMaxHeight, 'div#banner');
		
		//Check for Nav.
		myMaxHeight = ContentBlock.checkElementHeight( myMaxHeight, 'div#nav');
		
		return( myMaxHeight );
	}
	,
	checkElementHeight : function( pmCurrMaxHeight, pmId )
	{
		var myMaxHeight = pmCurrMaxHeight;
		
		//Check if element exists.
		if( $(pmId).size() == 0 ){ return(myMaxHeight); };
		
		//Get padding for content blocks.
		var paddingTop = $('div.contentBlock').css('padding-top');
		paddingTop = parseInt( paddingTop.replace( 'px', '' ) );
		var paddingBottom = $('div.contentBlock').css('padding-bottom');
		paddingBottom = parseInt( paddingBottom.replace( 'px', '' ) );
		
		//Get page top margin.
		var myPageMargin = $('div#page').css('margin-top');
		myPageMargin = parseInt( myPageMargin.replace( 'px', '' ) );
		
		var myPosY = $(pmId).offset().top - myPageMargin;
		var myHeight = $(pmId).innerHeight();
		var myTargetHeight = (myPosY + myHeight) - (paddingTop+paddingBottom);
		if( myTargetHeight > myMaxHeight ){ myMaxHeight = myTargetHeight; };
		return(myMaxHeight);
	}
};//end ContentBlock


//Handles controlling FAQ list.
FaqList = 
{
	init : function()
	{
		//Hide all answers.
		$( 'dl#faqList dd' ).hide();
		
		//Set up questions.
		$( 'dl#faqList dt' ).wrapInner( '<a href="javascript:;" class="question"></a>' );
		
		$( 'dl#faqList dt a.question' ).click( function( event ){
			var myText = $(this).parent().next().html();
			FaqList.setAnswer( myText );
			$(this).addClass( 'selected' );
			event.preventDefault();
		} );
	}
	,
	setAnswer : function( pmHtml )
	{
		$( 'dl#faqList dt a.question' ).removeClass( 'selected' );
		$( 'div#answer' ).hide();
		$( 'div#answer' ).html( '' );
		$( 'div#answer' ).html( pmHtml );
		$( 'div#answer' ).animate( { opacity: 'show' }, 500 );
		//$( 'div#answer' ).hide().html( pmHtml ).show( 'fast' );
	}
};//end FaqList


//handles setting site elements
GenSiteElements =
{
	//handles setting up site elements
	init : function()
	{
		//GenSiteElements.setBlockquotes();
		GenSiteElements.setOrderedLists();
		GenSiteElements.setExternalLinks();
	}//end function init
	,
	//handles setting up block quotes
	setBlockquotes : function()
	{
		$( 'blockquote' ).each( function(){
			var myTitle = $( this ).attr( 'title' );
			var myCite = $( this ).attr( 'cite' );
			$( this ).prepend( '<h3>' + myTitle + '</h3>' );
			$( this ).append( '<p class="cite"><a href=\"' + myCite + '\">Source</a></p>' );
		} );
	}//end function setBlockquotes
	,
	//handles setting ordered list
	setOrderedLists : function()
	{
		//edit ordered list dom to allow styling
		$( 'ol li' ).wrapInner( '<span class="olItemContent"></span>' );
		//activate ordered list styling via adding class - edit styles in stylesheet
		$( 'ol' ).addClass( 'javaScriptStyled' );
	}
	,
	//handles external links
	setExternalLinks : function()
	{
		$( 'a[rel="external"]' ).each( function(){
			$( this ).addClass( 'externalLink' );
			
		} );
		
		//$( 'a[rel="external"]' ).click( function(){
			
			//window.open( $( this ).attr( 'href' ) );
			//return( false );
		//} );
	}//end function setExternalLinks
	
	
};//end object literal siteElements


//Handles IE6 fixes.
//Use on elements which should degrade reasonably gracefully if JavaScript is not available.
ieSixFix = 
{
	init : function()
	{
		ieSixFix.setFooter();
		ieSixFix.setForms();
		//ieSixFix.setHeaders();
	}//end init
	,
	setFooter : function()
	{
		$( 'div#footer ul li:first-child' ).addClass( 'first' );
	}
	,
	setForms : function()
	{
		$( 'form input[type="text"]' ).addClass( 'text' );
		$( 'form input[type="password"]' ).addClass( 'password' );
	}//end setForms
	,
	setHeaders : function()
	{
		//$( 'div#col1 h2:first-child' ).addClass('first');
		//$( 'div#col2 h2:first-child' ).addClass('first');
		//$( 'div#col3 h2:first-child' ).addClass('first');
		//$( 'div#col1 h3:first-child' ).addClass('first');
		//$( 'div#col2 h3:first-child' ).addClass('first');
		//$( 'div#col3 h3:first-child' ).addClass('first');
	}
};//end ieSixFix