/**** REL = EXTERNAL ***/
$(document).ready(function()  {
	$("a").each(function(){
		if($(this).attr("href") && $(this).attr("rel") == "external") {
			$(this).attr("target","_blank");
		}
	});
})
/*******/

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return null;
}

// Find any forms with class of ajax and adjust their onsubmit actions
$(document).ready(function(){
	$("form.ajax").each(function(){
		target = $(this).parent();
		$(this).ajaxForm({
			target:target,
			type:'post',
			beforeSubmit: 	formSubmitting,
			success:		formSubmitResponse
		});
	});
});

function formSubmitting(formData, jqForm, options)
{
	jqForm.fadeOut("fast");
	return true;
}
function formSubmitResponse(responseText, statusText)
{
	$("form.ajax").each(function(){
		target = $(this).parent();
		$(this).ajaxForm({
			target:target,
			type:'post',
			beforeSubmit: 	formSubmitting,
			success:		formSubmitResponse
		});
	});
}
function ConvertToFlowplayer()
{
	 $(this).parent().flowplayer(
	 	 SETTINGS.adminSkinRoot+'/flash/flowplayer-3.0.5.swf',
		 {
 			autoPlay:true,
 			autoBuffereing:true
		 }
	 )
	 
	 $(this).parent().css({
	 	width: $(this).width(),
	 	height: $(this).height()
	 });
}

// function to pre-load images such as the background images for the tab hover effect. This prevents a flicker on first load. -taylor
// code from:http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
  var cache = [];
  // Arguments are image paths relative to the current page.
  var preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }



var active = false;
function initPage()
{

    // add a span in the last <li> element of the nav that is needed for the design
    $("#nav ul li:last-child").append('<span class="bookend">&nbsp;&nbsp;&nbsp;</span>');

    // pre-load the hover-state background image of the navigation so that the tab bar doesnt have any flickering when first moused over
    preLoadImages("../img/bg-nav-button-end.png");

	if($.fn.cycle)
	{
		$(".slideshow ul").cycle({
			fx:'fade',
			speed:2500,
			timeout:6000
		});
	}
	if($.fn.edenCalendar) $(".calendar").edenCalendar();
	if($.fn.tabs) $('#tabvanilla > ul').tabs({ fx: { height: 'toggle', opacity: 'toggle' } });
	if($.fn.flowplayer){
		 $('.video[href*=.flv] img').load(ConvertToFlowplayer);
	}
	if($.fn.galleria){
		$(".gallery > ul").galleria({
			history:false,
			clickNext:true,
			onImage:function(image,caption,thumb)
			{
				image.css('display','none').fadeIn();
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				// add a title for the clickable image
				image.attr('title','Next image >>');
				image.parents(".gallery").trigger("image_change");			
			},
			onThumb:function(thumb)
			{
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});
		
		if($.fn.jcarousel)
		{
			$("ul.galleria").addClass("jcarousel-skin-tango").jcarousel({
				initCallback: carousel_initCallback
			});
		}
		
		// Setup slideshow buttons
		$(".gallery .controls .buttons").click(function(){
			gal = $(this).parents(".gallery");
			if($(this).hasClass("inactive"))
			{
				// Currently not playing
				gal.everyTime('5s', 'slideshow', function() {
					$.galleria.next();
				});					
			}else{
				// Already playing
				gal.stopTime('slideshow');
			}
			$(this).toggleClass("inactive").toggleClass("active");
		});
	}
}
function carousel_initCallback(carousel)
{
	$(".gallery").bind("image_change",function(){
		var activeIndex = parseInt($("li.active",carousel.list).attr("jcarouselindex")-1);
		carousel.scroll(activeIndex);
		return false;
	});
}

$(document).ready(function(){
	initPage();
});


