$(document).ready(function(){
 	var scrollingElement = 'body';
	if ($.browser.msie) {
		scrollingElement = "html";
	} 

    // scroll to top every time!
    $(scrollingElement).animate({scrollTop: 0});
     



	var flashAndIE9issue = false;
	var flashVersion = getFlashVersion().split(',').shift();
	if (  ( $('html').hasClass('ie9') )  &&  ( flashVersion < 11 )  ) {
	    //alert('Please download Adobe Flash version 11.00 or higher');
	    ///// http://labs.adobe.com/downloads/flashplayer11.html
	    flashAndIE9issue = true;
	}
	
	
	function getFlashVersion(){
		// ie
	  	try {
	    	try {
	      		// avoid fp6 minor version lookup issues
	      		// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
	      		var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
	      		try { axo.AllowScriptAccess = 'always'; }
	      		catch(e) { return '6,0,0'; }
	    	} catch(e) {}
	    	return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
	  		// other browsers
	  	} catch(e) {
	    	try {
	    		if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
	        		return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
	      		}
	    	} catch(e) {}
	  	}
	  	return '0,0,0';
	}







	//---------------------------------------------------------------------
	//  initiate prettyPhoto on any marked elements
	//---------------------------------------------------------------------
    $("a[rel^='prettyPhoto']").prettyPhoto({
        //overlay_gallery: true
        theme: 'dark_square', /* pp_default / light_rounded / dark_rounded / light_square / dark_square / facebook */
        allow_resize: false, /* Resize the photos bigger than viewport. true/false */
        default_width: 1100,
        default_height: 730,
        opacity: 0.80
    });
   
   
   
    //---------------------------------------------------------------------
	//  initiate prettySociable on any marked elements
	//---------------------------------------------------------------------
    $.prettySociable();
    $.prettySociable.settings.websites.facebook.active=true;
    $.prettySociable.settings.websites.twitter.active=true;
    $.prettySociable.settings.websites.delicious.active=true;
    $.prettySociable.settings.websites.digg.active=true;
    $.prettySociable.settings.websites.linkedin.active=true;
    $.prettySociable.settings.websites.reddit.active=true;
    $.prettySociable.settings.websites.stumbleupon.active=true;
    $.prettySociable.settings.websites.tumblr.active=true;
   
   
  
	
	


    //---------------------------------------------------------------------
	//  Toggle launch item contents (main slide up and down)
	//---------------------------------------------------------------------
	$('.brandforce_panel_segment .button_div_type_01').click(function() {
		var theID = $(this).attr('id'); // for example '#segment_trigger_1'
		var theToggleID =  theID.replace(/segment_trigger_/ig, "segment_toggle_");
        
        var activeTrigger = $('.brandforce_panel_segment .button_div_type_01.active').not(('#' + theID));
        if ( activeTrigger.size() > 0 ) {
	        var activeTriggerID = activeTrigger.attr('id');
	        var theActiveToggleID =  activeTriggerID.replace(/segment_trigger_/ig, "segment_toggle_");
	        activeTrigger.toggleClass("active");
			$( ('#' + theActiveToggleID) ).slideToggle("slow");
		}
		
        var thisPanel = $(this).parent().parent().parent();
        $(this).toggleClass("active");
        var isActive = $(this).hasClass('active');
        if ( isActive ) {
            $(this).hide();
            $(this).slideToggle("slow");
        }
       
    
        $( ('#' + theToggleID) ).slideToggle("slow", function(){
            if (isActive ) {
                $(this).css('display', 'inline-block');
                var selector = $('.brandforce_hidden_text_gradient:visible', this);
                addTextGradients(selector);
            }
        });
        if ( isActive ) {
            var targetOffset = thisPanel.offset().top - 4;
            $(scrollingElement).animate({scrollTop: targetOffset});
        }
    });







    //---------------------------------------------------------------------
	//  Toggle variable to allow control of movie scrolling
	//---------------------------------------------------------------------
	var mouseover_MOVIE = false;
	$('.nextMovie, .prevMovie').mouseenter(function(){
	    mouseover_MOVIE = true;
	}).mouseleave(function(){
	    mouseover_MOVIE = false;
	});





    //---------------------------------------------------------------------
	//  Mouseover and click movie scrolling
	//---------------------------------------------------------------------
	var stopMovieScroll = false;
	$('.nextMovie').bind('click mouseover', function() {
		if (stopMovieScroll) {
			return;
		}
		stopMovieScroll = true;
		var currentLeft = $('.brandforce_movie_scroller').css('left');
		var containerOffset = $('.brandforce_movie_scroll').offset().left;
		var lastOffset = $('.brandforce_movie_element.last').offset().left;
		var lastVis = (lastOffset - containerOffset) < 729; // 729 and its last on screen
		var hideButton = (lastOffset - containerOffset -243) < 729;
		if ( ! lastVis ) {
			$('.brandforce_movie_scroller').animate({
				'left': ((parseInt(currentLeft) -243) + 'px'
			)}, 1000, function() {
				$('.prevMovie').show();
				stopMovieScroll = false;
				if ( ! hideButton ) {
					if ( mouseover_MOVIE) {
						$('.nextMovie').trigger('click');
					}
				}
			});
			if ( hideButton ) {
				$('.nextMovie').hide();
			}
		} else {
			$(this).hide();
			stopMovieScroll = false;
		}
	});
	$('.prevMovie').bind('click mouseover', function() {
	if (stopMovieScroll) {
			return;
		}
		stopMovieScroll = true;
		var currentLeft = $('.brandforce_movie_scroller').css('left');
		var containerOffset = $('.brandforce_movie_scroll').offset().left;
		var firstOffset = $('.brandforce_movie_scroller').offset().left;
		var lastVis = (firstOffset - containerOffset) >= 0; // 0 and its first on screen
		var hideButton = (firstOffset - containerOffset +243) >= 0;
		
		if ( ! lastVis ) {
			$('.brandforce_movie_scroller').animate({
				'left': ((parseInt(currentLeft) +243) + 'px'
			)}, 1000, function() {
				$('.nextMovie').show();
				stopMovieScroll = false;
				if ( ! hideButton ) {
					if ( mouseover_MOVIE) {
						$('.prevMovie').trigger('click');
					}
				}
			});
			if ( hideButton ) {
				$('.prevMovie').hide();
			}
		} else {
			$(this).hide();
			stopMovieScroll = false;
		}
	});

    
    
    
    
    
    
    
    
    //---------------------------------------------------------------------
	//  Headline scroller
	//---------------------------------------------------------------------
 	var slideAction; 
 	slideAction = setInterval(slideHeading, 4000);
   
   	function slideHeading() {
   		var iphc = $('.intro_panel_headline').size();
    	var iphac = $('.intro_panel_sliding_content .active')[0].className.split(' ');
    	var ipha = parseInt( iphac[1].substring(22) );
   		var iphna = ipha + 1;
   		if ( iphna > iphc ) {
   			iphna = 1;
   		}
    	$('.intro_panel_sliding_content .active').removeClass('active').stop(true,true).animate({
    		'left': '-700px'
    	}, 1400, 'easeOutQuart', function(){
    		$(this).css('left', '1000px');
    	});
    	
    	$( ('.intro_panel_headline_p' + iphna ) ).addClass('active').stop(true,true).animate({
    		'left': '270px'
    	}, 1400, 'easeOutQuart', function(){
    		//complete;
    	});
    	
    }
    
    
    
    
    
    
    
    //---------------------------------------------------------------------
	//  Setup NextSlide Elements (positioning and classes added for IE compatibility)
	//---------------------------------------------------------------------
    $('.brandforce_movie_element').each( function( i ) {
    	var thisID = $(this).attr('title');
    	$('.brandforce_nextslide_object', this).addClass(('nextSlideID_' + thisID));
    	$('.brandforce_movie_overlay', this).data('nextSlideID', thisID);
    	$(this).removeAttr('title');
    	$('.brandforce_nextslide_object', this).appendTo('body');
    	//appending to body moves these divs to the body and lets IE use z-index appropriately (IE stacks z-index ranges not as linier)
    });
    
  
  
  
    //---------------------------------------------------------------------
	//  lanuch nextSlide player on movie poster click
	//---------------------------------------------------------------------
    $('.brandforce_movie_overlay').live('click', function() {
    	$('#dark_shade').css({
			'filter': 'alpha(opacity=80)' // jQuery applies this inline overwitting IE errors
		}).fadeIn();
		
		if ( flashAndIE9issue ) {
			// we need to dowload flash v11 beta for this to work on IE9
			$('body').append('<div class="ie9FlashDownloadMsg">Please note that Internet Explorer 9.0 will require flash version 11.0 or higher. Consider using another browser such as Firefox or you can download the updates from <a href="http://labs.adobe.com/downloads/flashplayer11.html">Adobe downloads.</a></div>');
		} else {
			var nextSlideToPlay = $(this).data('nextSlideID');
			$(('.nextSlideID_' + nextSlideToPlay)).fadeIn();
		}
    	window.clearInterval(slideAction);
    });
    
    
    
    
    

    //----------------------------------------------------------------------------------------------------
	// SHOW PRIVACY STATEMENT
	//----------------------------------------------------------------------------------------------------
    $('.form_note_1, .privacy_statement_link').click(function() {
    	$('#shade').fadeIn('slow');
    	$('#popbox').fadeIn('slow');
    	window.clearInterval(slideAction);
    	
    	var theURL = "privacy.statement.php";
    	
    	$.ajax({
		    url: (theURL),
		    cache: false,
		    success: function (html) {
		    	$('#popbox #popContent').html("");
		    	$('#popbox #popContent').html(html);
		    }
     	}); 
    });
    
    
    
  	//---------------------------------------------------------------------
	//  Close popup and shade functions
	//---------------------------------------------------------------------
    $('#shade, #dark_shade, #closePop, .closeNextslide, .ie9FlashDownloadMsg').live('click', function(){
    	$('#popbox').fadeOut('slow');
    	$('#shade').fadeOut('slow');
    	$('#dark_shade').fadeOut('slow');
    	$('.brandforce_nextslide_object').fadeOut('slow');
    	$('.ie9FlashDownloadMsg').fadeOut('slow').remove();
    	slideAction = setInterval(slideHeading, 4000);
    });
    
    
    

    
    
    //---------------------------------------------------------------------
	//  Scroll to contact form from contact link
	//---------------------------------------------------------------------
    $('.contact_us_link').click(function() {
    	var targetOffset = $('.brandforce_panel_05').offset().top;
    	$(scrollingElement).animate({scrollTop: targetOffset});
    });
    
    
    
    
    
    
    
    //---------------------------------------------------------------------
	//  Lanuch content from the sub navigation menu
	//--------------------------------------------------------------------- 
    $('.nav_item').click(function() {  
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        var theID = $(this).attr('id');
        var theContentID =  theID.replace(/content_nav_/ig, "content_");
        $( ('#' + theContentID) ).parent().find('.toggle_content_div').not(('#' + theContentID)).hide();
        $( ('#' + theContentID) ).show();
        var selector = $('.brandforce_hidden_text_gradient:visible');
        addTextGradients(selector);
        var selector = $(this);
        var others = $(this).siblings();
        removeTextGradient(others);
        others.removeClass('brandforce_hidden_text_gradient');
        if ( $(this).is(':visible') ) {
        	addTextGradients(selector);
        } else {
        	$(this).addClass('brandforce_hidden_text_gradient');
        }
    });
    
    
    
    
    
    
    
    
    //---------------------------------------------------------------------
	//  disable text selection for major UI elements
	//--------------------------------------------------------------------- 
    $('.nav_item, .nav_heading, .button_div_type_01, .button_div_type_02').disableSelection();
    
    
    
    
    
    //---------------------------------------------------------------------
	//  ensure first story of sub navigation menu is active at launch
	//--------------------------------------------------------------------- 
    $('#segment_toggle_3 .brandforce_toggle_nav .nav_item:first').trigger('click');
    
    
    
    
    
    //---------------------------------------------------------------------
	//  ensure Headlines have brandforce gradient at launch
	//--------------------------------------------------------------------- 
    addTextGradients( $('.intro_panel_headline') );
    
    
    
    
    
    //---------------------------------------------------------------------
	//  Add brandforce gradient to elements passed
	//--------------------------------------------------------------------- 
    function addTextGradients(selector) {
    	selector.each( function( i ) {
	    	var originalFS = parseInt( $(this).css('font-size') );
	    	$(this).css('font-size', ((originalFS + 2) + 'px'));
	    	if ( originalFS > 30 ) {
	    		$(this).css('font-size', ((originalFS + 9) + 'px'));
	    	}
	    	if (  ( $.browser.msie ) && ( originalFS < 24 )  ) {
	    		$(this).css('font-size', ((originalFS + 21) + 'px'));
	    	}
	    	if (  ( $.browser.msie ) && ( originalFS > 20 )  ) {
	    		$(this).css('font-size', ((originalFS + 9) + 'px'));
	    	}
	    
	        $(this).pxgradient({
	          step: 5,
	          colors: ["#790050","#b30067","#d82265"],
	          dir: "x" //direction x or y
	        }).disableSelection().css({
	        	'cursor': 'pointer',
	        	'font-size': ( originalFS + 'px')
	        });
	        if (  ( $.browser.msie ) && ( originalFS < 24 )  ) {
	    		$(this).css('font-family', 'Arial, Verdana, Sans-serif');
	    	}
		});
    }
    
    
    
    //---------------------------------------------------------------------
	//  Remove brandforce gradient from elements passed
	//--------------------------------------------------------------------- 
    function removeTextGradient(selector) {
        selector.each( function( i ) {
            if ( $(this).find('.pxg-source').size() > 0 ) {
                var original = $(this).find('.pxg-source').html();
                $(this).find('.pxg-source').remove();
                $(this).find('.pxg-set').remove(); //del
                $(this).html(original).removeClass('sn-pxg');
            }
        });
    }
    
    
    
    
    
    
    
    //---------------------------------------------------------------------
	//  Add website href to story site references
	//--------------------------------------------------------------------- 
    $('.copy_web a').each( function( i ) {
    	var theLink = $(this).html();
    	var startText = theLink.substring(0, 7);
    	var httpString = "";
    	if (startText != "http://") {
    		httpString = "http://";
    	}
    	$(this).attr('href', httpString + theLink);
    });
    
    
    
    
}); // end document ready












//---------------------------------------------------------------------
//  Add leading zeros
//--------------------------------------------------------------------- 
function padDigits(n, totalDigits) { 
	n = n.toString(); 
	var pd = ''; 
	if (totalDigits > n.length) { 
		for (i=0; i < (totalDigits-n.length); i++) { 
			pd += '0'; 
    	} 
	} 
	return pd + n.toString(); 
} 






//---------------------------------------------------------------------
//  Submit contact form after validation
//--------------------------------------------------------------------- 
function submitContact() {
   	validateForm();
   	if (  $('#contactForm').find('.warn').size() < 1 ) {
    	$('#contactForm').ajaxSubmit({
            success: function (data) {
                if ( data === "true" ) {
	                $('#contactForm').find('input, textarea').val('');
	                $('#contact_thanks').fadeIn('slow');
	            } else {
	            	alert('Please enter the letters as seen in the diagram.\nIf you are having trouble please try a different image');
	            } 
            }
        });
    }
}




//---------------------------------------------------------------------
//  Close contact-made thank-you note
//--------------------------------------------------------------------- 
$('#contact_thanks').click(function(){
	$(this).fadeOut('slow');
});

























