$(document).ready(function(){
	
	// DOM id for the bg video element
	var bgVideoPlayerID = 'bg_video';
	
	// Set still image for background while the swf is loading
	setupBackgroundImage();
	
	// Grab the requested video file from the mark-up
	var video_file = $('div#bg_video a').attr('href');
	
	// Make sure we get a video defined and have Flash.
	// :NOTE: Flash 9.0.115 was when the F4V video format was first supported
	if (video_file && swfobject.hasFlashPlayerVersion("9.0.115")) {
	    
	  // Set the initial state of the sound button, and video player, based on the cookie
	  var mute_video = ($.cookie('mute_video') == null ) ? "true" : $.cookie('mute_video');

	  $('#top_nav a#sound_button').toggleClass("mute", mute_video == "true");
	  $('#top_nav_ie6 a#sound_button .sound_on').toggle(mute_video == "true");
	
	  playVideo(bgVideoPlayerID, video_file, mute_video);
	  
	  // Add the click behavior for the sound button
	  $('a#sound_button').click( function () {
		// Toggle the sound button
		$('#top_nav a#sound_button').toggleClass("mute");
		$('#top_nav_ie6 a#sound_button .sound_on').toggle();
		$('#top_nav_ie6 a#sound_button .sound_off').toggle();
		
		// Toggle the Cookie
		$.cookie('mute_video', ($.cookie('mute_video') == "true") ? "false" : "false", {path: '/'});
		
		// Toggle the video player
		getFlashMovie(bgVideoPlayerID).__asProxy_toggleSound();
		
		return false;
      });
	}
	// If not, use a static BG image
	else if ((badBrowser() == false) && swfobject.hasFlashPlayerVersion("9")) {
	  // Add the flash warning if it hasn't been shown before
	  if (getFlashVersionCookie('flashWarning') != 'seen') {
		
		  
		// Close link
		$('#warningClose').click(function(){
			setFlashVersionCookie('flashWarning','seen');
			$('#flashWarning').slideUp('slow');
			return false;
		});	  
	  }

	  // Add the background image
	  $('div#bg_video').html('<img id="bg_image" src="/wp-content/themes/post_ranch_inn/images/pri_still_background.jpg">');
	  $('div#bg_video').css('overflow', 'hidden');
	  
	  // Call resize when the browser window is resized
	  $(window).resize(_resize);
	  
	  // Make sure to call resize after the background image is fully loaded
	  $('img#bg_image').load(function(){ _resize(); });
	}
	else {
	  $('div#bg_video').html('<img id="bg_image" src="/wp-content/themes/post_ranch_inn/images/pri_still_background.jpg">');
	  $('div#bg_video').css('overflow', 'hidden');
	  
	  // Call resize when the browser window is resized
	  $(window).resize(_resize);
	  
	  // Make sure to call resize after the background image is fully loaded
	  $('img#bg_image').load(function(){ _resize(); });		
	}
	
});



//Code for example C
$("input.buttonCAdd").click(function(){$("div.contentToChange").find("p").not(".alert").append("<strong class=\"addedtext\"> This text was just appended to this paragraph</strong>")});
$("input.buttonCRemove").click(function(){$("strong.addedtext").remove()});
//show code example C
$("a.codeButtonC").click(function(){$("pre.codeC").toggle()});

/**
 * Swap in and play the requested background
 * video with the FLV player
 */
function playVideo(dom_id, bg_video_file, mute_video) {
	

	var flashvars = {
		video_file : bg_video_file,
		mute: mute_video
	};

	var params = {
		menu : "false",
		scale : "noScale",
		salign : "tl",
		bgcolor : "#ffffff",
		wmode : "transparent"
	};

	var attributes = {
		id : dom_id,
		name : dom_id
	};

	swfobject.embedSWF("/wp-content/themes/post_ranch_inn/swfs/bg_video_player.swf", "bg_video", "100%", "100%", "9.0.115", "expressInstall.swf", flashvars, params, attributes);

}

/**
 * Scale the background image, on browser window
 * resize, keeping the image aspect ratio
 */
function _resize(){
	// CSS min width/height of the container
	var minW = parseInt($('div#bg_video').css('min-width').substring(0,$('div#bg_video').css('min-width').indexOf("px")));
	var minH = parseInt($('div#bg_video').css('min-height').substring(0,$('div#bg_video').css('min-height').indexOf("px")));
	
	// Target window size, with minimums
	var winW = Math.max($(window).width(), minW);
	var winH = Math.max($(window).height(), minH);
	
	// Init image width/height vars
	var imgW = 0;
	var imgH = 0;

	// Calculate aspect ratio
	var initW = 1024, initH = 768;
	var ratio = initH / initW;

	// Fit to target window width first
	imgW = winW;
	imgH = winW * ratio;

	// If image height doesn't fill target window
	// size, fit to height instead
	if (imgH < winH) {
		imgH = winH;
		imgW = imgH / ratio;
	}

	// Set image size
	$('img#bg_image').width(imgW).height(imgH);
};

/**
 * Helper to deal with IE quirk in locating Flash movies
 *
 * @param {String} movieName
 */
function getFlashMovie(movieName){
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}

function getFlashVersionCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end   = document.cookie.indexOf(";",c_start);
			if (c_end == -1) {
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function setFlashVersionCookie(c_name,value,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays === null) ? "" : ";expires=" + exdate.toGMTString());
}

/**
 * Set the still background image size and set up resizing event
 */
function setupBackgroundImage() {
    	
	$('img#bg_still_image').load(function(){ _bgresize(); });
	$(window).resize(_bgresize);
}

/**
 * Scale the still background image, on browser window
 * resize, keeping the image aspect ratio
 */
function _bgresize(){
	// CSS min width/height of the container
	var minW = parseInt($('div#bg_still_container').css('min-width').substring(0,$('div#bg_still_container').css('min-width').indexOf("px")));
	var minH = parseInt($('div#bg_still_container').css('min-height').substring(0,$('div#bg_still_container').css('min-height').indexOf("px")));
	
	// Target window size, with minimums
    var winW = Math.max($(window).width(), minW);
    var winH = Math.max($(window).height(), minH);
	
	// Init image width/height vars
	var imgW = 0;
	var imgH = 0;

	// Calculate aspect ratio
	var initW = 1024, initH = 578;
	var ratio = initH / initW;

	// Fit to target window width first
	imgW = winW;
	imgH = winW * ratio;

	// If image height doesn't fill target window
	// size, fit to height instead
	if (imgH < winH) {
		imgH = winH;
		imgW = imgH / ratio;
	}

	// Set image size
	$('img#bg_still_image').width(imgW).height(imgH);
};
