 // jquery code waits for document to load
 jQuery.noConflict();
 jQuery(document).ready(function() {
	
	//jquery script gets all images within li elements within in the header div 
	//sets the onmouseover and onmouseout method to swap the values of the 'name' and 'src' attributes
    jQuery("#header").find("li").each(function(i) {
		jQuery(this).find("img").hover(
			//over
			function() {
				swapImage(jQuery(this));
			},
			//out
			function() {
				swapImage(jQuery(this));
			}
		)
   })
    
	/* Takes a jquery object and swaps the 'src' attribute with the 'name' attribute
	 * Handles the IE6 PNG transparency fix
	 * Does not flip the image if it contains the css class "noflip"*/
    function swapImage(img) {
		var oldImg = getCurrentSRC(img);
		var newImg = img.attr('name');
		if(newImg && newImg != "") {
			if(!img.hasClass('noflip')) {
				img.attr('name', oldImg);
				if(usingPNGFix(img)) {
					img.attr('src', newImg);
					img.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + newImg + '",sizingMethod="scale")');
				} else {
					img.attr('src', newImg);
				}
			}
		}
    }
   
    /* Takes a jQuery object, checks to see if the user is using IE6.
	 * If not, return the value of the src attribute.
	 * if they are using IE6 then check to see if ie6 png transparency fix is in effect
	 * If it is, use that as the current image. Otherwise use the image src attribute
	 * png transparency fix puts the url in the style attribute unter the 'filter' css property
	 * 'filter' is specific to IE*/
    function getCurrentSRC(img) {
		if(vIE() != 6 || !usingPNGFix(img)) {
			return img.attr('src');
		} else {
			// using IE6 png fix meaning the src attribute is set to clear.gif
			// grab the image src instead from css filter property
			var filter = img.css('filter');
			var start = filter.indexOf("src=") + 5;
			var end = filter.substring(start).indexOf("\"") + start;
			return filter.substring(start, end);
		}
    }
	
	/* Returns true if the browser is Internet Explorer 6 or lower
	 * and it is using the css filter property (IE Only) with Microsoft.AlphaImageLoader*/
	function usingPNGFix(img) {
		if(vIE() < 7 && vIE() > 0) {
			var filter = img.css('filter');
			return (filter != null) && (filter.toLowerCase().indexOf("src=") != -1);
		}
	}
	
	/* Returns the version of IE the user is using.
	 * Returns -1 if they are not using IE*/
	function vIE(){
		if(navigator.appName=='Microsoft Internet Explorer') {
			return parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]);
		} else {
			return -1;
		}
	}
	
	
	// Videos within the text
	function insertVideo() {
		jQuery("#content").find(".adobe-video-position1, .adobe-video-position2, .adobe-video-position3").each(function () {
			var vidLink = jQuery(this);
			var url = vidLink.attr('href');
			var img = vidLink.find("img").eq(0);
			var w = img.attr('width');
			var h = img.attr('height');
			var handler = "window.open('" + url + "','video', 'menubar=no,width=" + w + ",height=" + h + ",toolbar=no'); return false;";
			vidLink.attr('onclick', handler);
		});
	
		jQuery("#content").find(".youtube-video-position1, .youtube-video-position2, .youtube-video-position3").each(function () {
			var vidContainer = jQuery(this);
			var ytid = vidContainer.attr('id');
			//get language code from 'lang' attribute. Use english (en) if none is provided
			var languageCode = (vidContainer.attr('lang') != '') ? ('&hl=' + vidContainer.attr('lang')) : ('&hl=en');
			var w = vidContainer.attr('width');
			var h = vidContainer.attr('height');
			var color1 = "0x7CB56A";
			var color2 = "0x375C2B";
			var ytURL = 'http://www.youtube.com/v/' + ytid + languageCode + '&rel=0&color1=' + color1 + '&color2=' + color2 + '&border=1&fs=1&';
			var inner = '\n<div>\n\t<object width="' + w + '" height="' + h + '">\n\t\t<param name="movie" value="' + ytURL + '"/>\n\t\t<param name="allowFullScreen" value="true"/>\n\t\t<param name="allowscriptaccess" value="always"/>\n\t\t<embed src="' + ytURL + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + w + '" height="' + h + '"></embed>\n\t</object>\n</div>\n'
			vidContainer.html(inner);
		});
		
		// Videos on the right site
		jQuery("#content").find(".youtube-video-position4").each(function () {
			var vidContainer = jQuery(this);
			var ytid = vidContainer.attr('id');
			
			//get language code from 'lang' attribute. Use english (en) if none is provided
			var languageCode = (vidContainer.attr('lang') != '') ? ('&hl=' + vidContainer.attr('lang')) : ('&hl=en');
			var w = vidContainer.attr('width');
			var h = vidContainer.attr('height');
			var flashvars = {data: ytid};
			if(vidContainer.hasClass('debug')) {
				flashvars.debug = true;
			}
			var params = {
				menu: "false",
				allowScriptAccess: "always",
				scale: "noscale",
				wmode: "transparent"
			};
			var attributes = {};
			swfobject.embedSWF("/~/media/AA87C1755894411A81074A7C63E63EBE.ashx", ytid, w, h, "9.0.0", "", flashvars, params, attributes);
			SWFID = ytid;
		});
		
	}
	
	insertVideo();
	
	
 });
