$(document).ready(function(){
	/* IE6 PNG SUPPORT
	---------------------------------------------------------- */
    // Fix background image caching problem
	
	if ($.browser.msie && $.browser.version.substr(0,1) < 7 ) 
	{
        try 
		{ 
            document.execCommand("BackgroundImageCache", false, true); 
        } 
		catch(err) 
		{
			//console.log(err.toString());
		}
		
		
		if ( $("#main-display").length > 0 ) {
			// set fixes here
			//DD_belatedPNG.fix('#logo, #hero-spot, .hero-next, .hero-prev, #arrow-control, .learn-more, .testimonial-prev, .testimonial-next, #wificiency-icon');
		}
		
    }
	
	/* MAIN NAVIGATION ADJUSTMENTS
	---------------------------------------------------------- */
	// re-float the last 2 elements
	var navlist = $('#main-nav li');
	for (var i = 0; i < navlist.length; i++) 
	{
		var text = $( navlist[i] ).children('a').html();
		switch( text.toLowerCase() )
		{
			case 'contact us' :
				//console.log( $( navlist[i] ).html() );
				//$( navlist[i] ).addClass('secondary');
				break;
			case 'about' :
				//console.log( $( navlist[i] ).html() );
				//$( navlist[i] ).addClass('secondary');
				break;
		}
	}

	/* BORDER RADIUS STYLING
	---------------------------------------------------------- */
 	applyBorderRadius( "#main-nav.link-list li a", 8 );
 	applyBorderRadius( "#main-nav.link-list li ul.drop-down li a", 0 );
 	applyBorderRadius( "ul.drop-down li a", 3 );

	applyBorderRadius( "#arrow-control", 3 );

	/* HIDE PAGE ELEMENTS
	---------------------------------------------------------- */
	hidePageElement( ".meta" );

	/* MAIN DISPLAY (HOMEPAGE ONLY)
	---------------------------------------------------------- */
    // select #flowplanes and make it scrollable. use circular and navigator plugins 
    var hero = $( "#panes" ).scrollable(
	{	
		clickable: false,
		size: 1,
		loop: false,
		next: '.hero-next',
		prev: '.hero-prev',
		
 		onSeek: function() 
		{ 
			$( ".hero-current" ).text( this.getPageIndex() + 1 );
			
			if( this.getPageIndex() == 0 )
			{
				$( ".hero-prev" ).css({ backgroundPosition: "0px 0px" });	
			}
			else
			{
				$( ".hero-prev" ).css({ backgroundPosition: "-31px 0px" });
			}
			
			if( this.getPageIndex() == this.getPageAmount() - 1 )
			{
				$( ".hero-next" ).css({ backgroundPosition: "0px 0px" });
			}
			else
			{
				$( ".hero-next" ).css({ backgroundPosition: "-31px 0px" });
			}
    	}
		
	}).mousewheel(200).navigator({ 
        navi: ".tabs",
        naviItem: 'li',
        activeClass: 'active' 
 
    }); 
	
    $( "ul#lower-tabs" ).tabs( "#lower-items > div",
	{
		current: 'active'
	});
	
	$( ".hero-prev" ).click( function()
	{
		hero.prev();
	});
	
	/* TESTIMONIALS
	---------------------------------------------------------- */
	$( "div#testimonial-scrollable" ).scrollable(
	{	
		clickable: false,
		size: 1,
		next: '.testimonial-next',
		prev: '.testimonial-prev',
		
		onSeek: function() 
		{ 
			$( ".testimonial-current" ).text( this.getPageIndex() + 1 );
			
			if( this.getPageIndex() == 0 )
			{
				$( ".testimonial-prev" ).css({ backgroundPosition: "0px 0px" });	
			}
			else
			{
				$( ".testimonial-prev" ).css({ backgroundPosition: "-31px 0px" });
			}
			
			if( this.getPageIndex() == this.getPageAmount() - 1 )
			{
				$( ".testimonial-next" ).css({ backgroundPosition: "0px 0px" });
			}
			else
			{
				$( ".testimonial-next" ).css({ backgroundPosition: "-31px 0px" });
			}
    	}
	}); 
	
	/* SECTOR FIELDS
	---------------------------------------------------------- */
	var objSectorField = $(".sector-field");
	objSectorField.hide();
	$( ".sector-toggle" ).click( function(){
		// Toggle the sector field on and off
		switch( $(this).attr('value') )
		{
			case 'yes' :
				if( $( objSectorField ).is(':visible') ) return;
				objSectorField.slideToggle('fast');
				break;
				
			case 'no' :
			default:
				objSectorField.hide();
				break;
		}
	} );
	
	/* LOGIN & REGISTER BUTTONS
	---------------------------------------------------------- */
	// Hide the panes
	$( "#login-pane" ).hide().css({'left' : -9999, 'top' : -9999});
	$( "#search-pane" ).hide().css({'left' : -9999, 'top' : -9999});;

	$( "a.login" ).hover( function()
	{
		// Position the display panes before showing
		positionFormPane( '#login-pane', 'a.login' );

		// append a dimmer
		initDimmer();
		
		$( "#login-pane" ).slideToggle("fast");
		$( "#search-pane" ).hide();

		$(this).toggleClass( "current" );
		$( "a.search" ).removeClass( "current" );
	});
	
	$( "a.search" ).hover( function()
	{
		// Position the display panes before showing
		positionFormPane( '#search-pane', 'a.search' );

		// append a dimmer
		initDimmer();
		
		$( "#search-pane" ).slideToggle("fast");
		$( "#login-pane" ).hide();
		
		$(this).toggleClass( "current" );
		$( "a.login" ).removeClass( "current" );
	});
	
	$( "a.cancel" ).click( function()
	{
		$( this ).parent().parent().parent().hide();
		$( "a.search" ).removeClass( "current" );
		$( "a.login" ).removeClass( "current" );

		// remove the dimmer
		disposeDimmer();
	});
	
	/* MOZILLA & WEBKIT ROUNDED CORNERS
	---------------------------------------------------------- */
	//TODO: Webkit enabled browser detection
	function applyBorderRadius( strElement, intRadius )
	{
		var ele 		= $( strElement );
		var radius 		= intRadius + 'px';
		if( $( ele ).length != 0 && radius != null  )
		{
			ele.attr('style', '-moz-border-radius: ' + radius );//'-webkit-border-radius: ' + intRadius);
		}
	}
	
	/* APPLY OPACITY
	---------------------------------------------------------- */
	function applyOpacity( strElement, intOpacity )
	{
		var ele 		= $( strElement );
		var op	 		= intOpacity;
		if( $( ele ).length != 0 && op != null  )
		{
			ele.css( { opacity: .25 } );
		}
	}
	
	/* HIDE PAGE ELEMENTS
	---------------------------------------------------------- */
	function hidePageElement( strElement )
	{
		var ele			= $( strElement );
		ele.hide();
	}

	/* GET COORDINATES OF SELECTED ELEMENT
	---------------------------------------------------------- */
	function findCoordinates( strElement )
	{
		var ele			= $( strElement );
		var offset		= ele.offset();
		var dimensions	= [ ele.width(), ele.height() ];
		var objResult	= new Object();
		
		objResult.x 		= Math.floor( offset.left );
		objResult.y 		= Math.floor( offset.top );
		objResult.width 	= Math.floor( dimensions[0] );
		objResult.height 	= Math.floor( dimensions[1] );
		
		return objResult;
	}
	
	// positions the form panes
	function positionFormPane( objTarget, objSource )
	{
		if( objTarget == null || objSource == null ) return;

		var intOffset = 2;
		var objPaneInfo = findCoordinates( objSource );
		$( objTarget ).css( { 'left' : ( objPaneInfo.x - ( $( objTarget ).width() - objPaneInfo.width - intOffset )  ), 'top' : 23 } );
	}
	
	// adds a dimmer div behind the form panels
	function initDimmer()
	{
		$('body').append('<div id="dimmer"></div>');
		if( $('#dimmer').length > 0 )
		{
			// set the dimmer below the forms
			$('#dimmer').css( {'position' : 'absolute', 'left' : 0, 'top' : 0, 'z-index' : 500, 'width' : $(document).width(), 'height' : $(document).height() });
			$('#dimmer').click( function(){
				if( $( '#login-pane:visible' ).length > 0 )
				{
					$( "a.login" ).removeClass( "current" );
					$( '#login-pane' ).toggleClass( "current" ).slideToggle();
				}
				else if( $( '#search-pane:visible' ).length > 0 )
				{
					$( "a.search" ).removeClass( "current" );
					$( '#search-pane' ).toggleClass( "current" ).slideToggle();
				}
				
				// remove the dimmer
				disposeDimmer();
			});	
		}
	}
	
	function disposeDimmer()
	{
		if( $('#dimmer').length > 0 )
		{
			$('#dimmer').remove();
		}
	}

});

