// For IE hover flicker bug
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// given a JQuery selection string, the function will toggle a class of 'className'
// on the items parent whenever the item is clicked
function toggleState(target, className) {
	$(target).toggle(
		function() {
	    	$(this).parent().addClass(className);
	    },
	    function() {
	    	$(this).parent().removeClass(className);
	    }
    );
}
function tabify(el, className) {
	if(jQuery.browser.version <= 418.8 && $.browser.safari) {
		$("body").addClass("safari");
	}
	
	var container = $(el);
	container.attr("class", className);
	
	$(el + " div h2").click(function() {
		var id = $(this).parent().attr("id");
		container.attr("class", id);
	});
}


// Hello jQuery!
$(document).ready(function(){
// First, let's tell tag the page when javascript is on.
	$("body").addClass("jsOn");

// Take Action More/Less
$(".takeAction ul.moreItems").hide()
$(".takeAction a.showMore").click(function() {
	$(this).parent().toggleClass("active");
	$(".takeAction ul.moreItems").toggle();
	return false
});
// See Also More/Less
$(".seeAlso ul.moreItems").hide()
$(".seeAlso a.showMore").click(function() {
	$(this).parent().toggleClass("active");
	$(".seeAlso ul.moreItems").toggle();
	return false
});
// Social Toolbox 
  $('#shareBox').hide();
 // toggles on click
  $('.toolboxOpen').click(function() {
	$('#shareBox').toggle();
	return false;
	});
 // hides on click
  $('a.toolboxClose').click(function() {
	$('#shareBox').hide();
	return false;
  });

// addClass first
	$("#shareBox li:first").addClass("first");

// adding raquo
	$("#newsEvents li a").append("<span>&nbsp;&nbsp;&raquo;</span>");

  });
// home page
function writeFlash(flashSrc, flashWidth, flashHeight){
	document.write( '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" height="' + flashHeight + '" width="' + flashWidth + '" align="top"><param name="movie" value="' + flashSrc + '"><param name="wmode" value="transparent" name="quality" value="best"><param name="play" value="true"><embed wmode="transparent" align="top" height="' + flashHeight + '" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' + flashSrc + '" type="application/x-shockwave-flash" width="' + flashWidth + '" quality="best" play="true"><\/embed><\/object>' );
}
function writeFlashTicker(flashSrc, flashWidth, flashHeight){
	document.write( '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" height="' + flashHeight + '" width="' + flashWidth + '" align="top"><param name="movie" value="' + flashSrc + '"><param name="quality" value="best"><param name="play" value="true"><embed align="top" height="' + flashHeight + '" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' + flashSrc + '" type="application/x-shockwave-flash" width="' + flashWidth + '" quality="best" play="true"><\/embed><\/object>' );
}

//  Photo gallery ------------------------------  
function gallery(increment, visibleItems) {
	if ($(".photo .media").length == 1)
	{

	//Add buttons to scroll the photo gallery
	$("#thumbs").prepend("<a href='#next' id='scrollLeft'>Scroll Left</a>");
	$("#thumbs").append("<a href='#previous' id='scrollRight'>Scroll Right</a>");

	//Add Container for media and text
	$(".photo .media").prepend("<div id='showcase'></div>");	
	$("#showcase").prepend("<div id='image'><div id='imageWrapper'></div></div>");
	$("#showcase").append("<p id='caption'></p>");

	//Wrap the photo gallery ul in a holder tag
	$("#thumbs ul").wrap("<div id='slideHolder'></div>");
	var totalImages = $("#thumbs li").length;
	var galleryItem = $("#thumbs li:first");
	
	//Get the size of each item and any margin, padding, or border
	var totalDimensions;
	var itemSize = galleryItem.width();
	totalDimensions = new Array("margin-left", "margin-right", "padding-left", "padding-right");

	for (var n = 0; n < totalDimensions.length; n++) {
		itemSize += Number(galleryItem.css(totalDimensions[n]).replace("px", ""));
	}

	var incrementAmount = increment * itemSize;
	var visibleItemsSize = visibleItems * itemSize;
	var maxOffset = (totalImages * itemSize) - visibleItemsSize;


	//Add events to the scroll buttons to photo gallery
	var target = $("#thumbs ul");
	var imageSet = 1;
	var curImage = 0
	$("#scrollLeft").click(function() {
		scroll(target, "left", incrementAmount, maxOffset);
		imageSet --;
		curImage = curImage - 5
			if (curImage < 0)
			{
			curImage = 0
			}
			$("#counter").html("Images " +(curImage + 1)+ " - " +(curImage + 5)+ " <span>(of " +totalImages+ ")</span>")
		return false;
	});
	$("#scrollRight").click(function() {
		scroll(target, "right", incrementAmount, maxOffset);
		imageSet ++;
		curImage = curImage + 5
		if (curImage >= totalImages - 5)
		{
			curImage = totalImages - 5
		}	
			$("#counter").html("Images " +(curImage + 1)+ " - " +(curImage + 5)+ " <span>(of " +totalImages+ ")</span>")
		return false;
	});

	var totalItems = $("#thumbs li").length;
	var counter = 0;

	// Make first gallery item active by default
	$("#thumbs li:first").each(function() {
		var text = $(this).children("a").children("em").text();

		$("#counter").html("Images " +1+ " - " +5+ " <span>(of " +totalImages+ ")</span>")
		$(this).siblings().removeClass("active");
		$(this).addClass("active");
		
		var mediaContent = getMediaContent($(this));
		addMediaToPage($(this), 1, totalItems, mediaContent, text);
		
		$("#showcase p").text(text);
	});

	var $thumbs = $("#thumbs li")
	$thumbs.each(function() {
		counter ++;
		var itemNumber = counter;

		var text = $(this).children("a").children("em").text();		
		var mediaContent = getMediaContent($(this));

		//When thumbnail clicked, set as active item and load content into main display area
		$(this).click(function() {
			addMediaToPage($(this), itemNumber, totalItems, mediaContent, text);
			return false;
		});

		//Add hover effect to gallery items
		$(this).mouseover(function() {
			$(this).addClass("jsHover");
		});
		$(this).mouseout(function() {
			$(this).removeClass("jsHover");
		});
		this.onclick = function() { return false; };
	});
}
}

/* 
 * parse the media url and determine the type of media file
 * return the appropriate html to display the media
 */
function getMediaContent(el) {
	var link = el.children("a").attr("href");
	var altText = el.children("a").children("img").attr("alt");

	var splitLink = link.split("?");
	var end = splitLink[0].lastIndexOf(".");
	var mediaType = splitLink[0].substring(end+1);
	var height, width;
	var mediaContent;

	//Generate code based on the type of media to be displayed
	switch(mediaType) {

		//Flash
		case "swf":
			//Get all the parameters for the flash movie
			var urlParams = link.split("?");
			link = urlParams[0];
			var properties = getQueryString(urlParams[1]);
			
			width = properties['width'];
			height = properties['height'];
			
			//If dimensions not defined or too large then set to max size
			if(!height || height > 270) height = 270;
			if(!width || width > 320) width = 320;
			
			flashVars = 'var1='+ properties['var1'];

			mediaContent = '<object class="item" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" height="' + height + '" width="' + width + '" align="top">'+
				'<param value="noscale" name="scale"/>'+
			'<param name="movie" value="'+ link +'">'+
			'<param name="quality" value="high">'+
			'<param value="'+ flashVars +'" name="flashvars"/>'+
			'<embed flashvars="'+ flashVars +'" height="' + height + '" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' + link + '" type="application/x-shockwave-flash" width="' + width + '" quality="high">'+
			'<\/embed><\/object>';
			break;

		//Images
		case "jpg":
		case "jpeg":
		case "gif":
		case "png":
		case "bmp":
			mediaContent = '<img alt="' +  altText + '" class="item" src="' + link + '" />';
			break;
	}
	
	return mediaContent;
}

/*
 * After all the work has been done, this will add the html to the page
 * It also removes any existing media in the showcase
 */
function addMediaToPage(el, itemNumber, totalItems, mediaContent, text, wallpaper) {
	//Display Counter
	$("#showcase").empty();
	$("#showcase").prepend("<div id='image'><div id='imageWrapper'></div></div>");
	$("#showcase").append("<p id='caption'></p>");

	//Set current item as active
	el.siblings().removeClass("active");
	el.siblings().removeClass("jsHover");
	el.addClass("active");

	//Remove existing content
	try {
		document.gallery-media.Stop();
	}
	catch(e){}
	$("#imageWrapper .item").css("display", "none");
	$("#imageWrapper").empty();
	
	//Add new content
	document.getElementById("imageWrapper").innerHTML = mediaContent;
	$("#showcase p#caption").text(text);
	
	//Remove existing content
	try {
		document.gallery-media.Stop();
	}
	catch(e){}
	$("#imageWrapper .item").css("display", "none");
	$("#imageWrapper").empty();
	//Add new content
	document.getElementById("imageWrapper").innerHTML = mediaContent;
	$("#showcase p#caption").text(text);
}

/*
 * Get the dimensions of the media content from a query string
 * Returns an array containing width and height
 */
function getDimensions(params) {
	var dim = new Array();
	params = params.split("&");
	dim[0] = params[1].substring(6);
	dim[1] = Number(params[0].substring(7)) + 16;
	return dim;
}

function getQueryString(params) {
	var props = new Array();
	
	$(params.split("&")).each(function(i) {
		var param = this.split("=");
		props[param[0]] = param[1];
	});

	return props;
}

/*
 * Scroll the gallery the required amount in the specified direction
 */
function scroll(container, direction, amount, maxOffest) {
	var cur = container.css("left");

	if(cur == "auto") cur = 0;
	else cur = Number(cur.replace("px", ""));

	switch(direction) {
		case "left":
			cur = cur + amount;
			if(cur > 0) cur = 0;
			break;
		case "right":
			cur = cur - amount;
			if(cur < - maxOffest) cur = - maxOffest;
			break;
	}

	container.css("left", cur+"px");
}
$(function() {
			gallery(5, 5);
		});


/*
 * Wrap the image in a div with specified class
 * Grab the text from the alt tag
 * Append a p tag to the div with text of alt tag

function renderCaption(img, className) {
	var altText = img.attr("alt");
	var width = img.attr("width") || img.css("width");
	width = width + "";
	if(width.indexOf('px') == -1) width = width + "px"

	img.removeClass(className);
	img.wrap("<div class='"+className+"'></div>");
	img.parent().css("width", width);
	img.parent().append("<p>" + altText + "</p>");
}


function imageCaptions() {
	// Photos with Captions
	$("img.imageWithCaption").each( function() {		
		// if it has no alt text to use as a caption, don't bother doing anything else
		if($(this).attr("alt")) {
			renderCaption($(this), 'imageAndCaption');
		}
	});
}

$(window).load(function(){
		imageCaptions();
		var colsHeight = $(".wide .cols").height()
		$(".wide .cols").css({ height:colsHeight+31 });

});  */// end of window.onload
