document.write('<link rel=\"stylesheet\" href=\"/css/wg-js.css\" type=\"text/css\" media=\"screen\" />')
google.setOnLoadCallback(function() {
	$(document).ready(function() {	
		lightbox();
		stackFadeIn($(".fadeIn"));
		newWindowLinks();
		clickable();
		carousel();
		tableStripes();
		
	});	
});

/* Sequentially fade in a collection of elements (assumes they are already hidden in CSS using "visibility:hidden") */
function stackFadeIn(jQueryCollection) {
	if (jQueryCollection.length > 0) {
       	var jQueryImgObj = $(jQueryCollection.splice(0,1));	
		imageLoad(jQueryImgObj);
		setTimeout(function(){stackFadeIn(jQueryCollection)},100);
	}
}
function imageLoad(jQueryImgObj) {	
	var newImage = new Image();	
	newImage.onload = function() {
		jQueryImgObj.parent().css({height:jQueryImgObj.height()+"px"})
		jQueryImgObj.css({display:"none",visibility:"visible"})
		jQueryImgObj.fadeIn(1000);
	}
	newImage.src = jQueryImgObj.attr("src");
}

/* Target new windows */
function newWindowLinks() {
	$('a[rel=external]').live('click',function(){
		window.open($(this).attr('href'));
		return false;
	});
}

/* Clickable blocks */
function clickable() {
	$(".clickable:has(a):not(.disabled)").live("click",function() {
		var link = $("a",this).eq(0).attr("href");
		window.location = link;
	}).hover(function(){
		if(!$(this).hasClass("disabled")) {
			$(this).addClass("hover").css({"cursor":"pointer"});	
		}		
	},function(){
		$(this).removeClass("hover").css({"cursor":"default"});
	})
}

/* Carousel */
function carousel() {
	var carouselSpeed = 1000;
	$(".carousel").each(function() {
		var carousel	= $(this);
		var blocks 		= $("li",this);
		var blockCount	= blocks.size();
		var blockWidth	= parseInt(blocks.css("width").split("px")[0]);
		var	fullWidth	= blockCount*blockWidth;
		var viewPort	= $(".carouselInner",carousel);
		var	vpWidth		= parseInt(viewPort.css("width").split("px")[0]);
		var list 		= $("ul",carousel);	
		var step 		= carousel.hasClass("four") ? blockCount-4 : blockCount-2;

		if(fullWidth>vpWidth) {
			list.css({width:fullWidth+"px"});
			carousel.append('<a href="#" class="carouselLeftArrow">Left</a><a href="#" class="carouselRightArrow">Right</a><div class="scrollbarWrapper"><div class="scrollbarTarg"></div></div>');
			$(".carouselLeftArrow").css({opacity:0.3,cursor:"default"});
			var currLeft = parseInt(list.css("left").split("px")[0]);
			// Attach right click
			$(".carouselRightArrow",carousel).click(function(){
				$(".carouselLeftArrow").css({opacity:1,cursor:"pointer"})
				var maxLeft 	= -(fullWidth-vpWidth);	
				var newLeft		= (currLeft-vpWidth);									
				if(newLeft<maxLeft) {
					newLeft = maxLeft;
					$(this).css({opacity:0.3,cursor:"default"})
				} else {
					$(this).css({opacity:1,cursor:"pointer"})
				}
				currLeft = 	newLeft;
				list.stop().animate({left:newLeft+"px"},carouselSpeed);			
				$(".scrollbarTarg",carousel).slider('option', 'value', (newLeft*-1)/blockWidth);
				return false;
			})
			// Attach left click
			$(".carouselLeftArrow",carousel).click(function(){
				$(".carouselRightArrow").css({opacity:1,cursor:"pointer"})
				var maxLeft 	= 0;	
				var newLeft		= (currLeft+vpWidth);									
				if(newLeft>maxLeft) {
					newLeft = maxLeft;
					$(this).css({opacity:0.3,cursor:"default"})
				} else {
					$(this).css({opacity:1,cursor:"pointer"})
				}
				currLeft = 	newLeft;	
				list.stop().animate({left:newLeft+"px"},carouselSpeed);	
				$(".scrollbarTarg",carousel).slider('option', 'value', (newLeft*-1)/blockWidth);
				return false;
			})	
			// Now create scrollbar
			$(".scrollbarWrapper",carousel).css({width:(15*blockCount)+"px",marginLeft:-(15*blockCount)/2+"px"})		
			$(".scrollbarTarg",carousel).slider({
				min: 0,
				max: step,
				animate:true,	
				step: 1,
				slide: function(e,ui) { 
					newLeft = -(blockWidth*ui.value);
					currLeft = 	newLeft;	
					list.stop().animate({left:newLeft+"px"});
					if(ui.value==0) {
						$(".carouselRightArrow",carousel).css({opacity:1,cursor:"pointer"})
						$(".carouselLeftArrow",carousel).css({opacity:0.3,cursor:"default"})
					} else if(ui.value==(step)) {
						$(".carouselLeftArrow",carousel).css({opacity:1,cursor:"pointer"})
						$(".carouselRightArrow",carousel).css({opacity:0.3,cursor:"default"})
					} else {
						$(".carouselRightArrow",carousel).css({opacity:1,cursor:"pointer"})
						$(".carouselLeftArrow",carousel).css({opacity:1,cursor:"pointer"})
					}
				} 
			});		
		}		
	})
}

/* Add stripes to tables */
function tableStripes() {
	$("table tr:even").addClass("alt");	
}

/* Activate lightbox */
function lightbox() {
	$('#galleryList li a').lightBox({
		imageLoading: '/js/jquery-lightbox-0.5/loading.gif',
		imageBtnClose: '/js/jquery-lightbox-0.5/close.gif',
		imageBtnPrev: '/js/jquery-lightbox-0.5/prev.gif',
		imageBtnNext: '/js/jquery-lightbox-0.5/next.gif'		
	});
}

