/**** EDEN TOUR ***/
$(document).ready(function() {
    $("#tourControls li").click(function() {
    	$("#tourSlides .slide").fadeOut();
    	$("#tourControls .active").removeClass('active');
        $("#"+$(this).attr("rel")).fadeIn();
        $(this).addClass('active');
    });
	$("#tourArrowRight").click(function() {
		var activeIndex = $("#tourControls LI").index($(".active")[0]);
    	if (activeIndex == 8) {
    		activeIndex = 0;
    	} else {
    		activeIndex++;
    	}		
    	$("#tourSlides .slide").fadeOut();
    	$("#tourControls .active").removeClass('active');
        $("#"+$("#tourControls LI:eq(" + activeIndex + ")").attr("rel")).fadeIn();
        $("#tourControls LI:eq(" + activeIndex + ")").addClass('active');
    }); 
	$("#tourArrowLeft").click(function() {
		var activeIndex = $("#tourControls LI").index($(".active")[0]);
    	if (activeIndex == 0) {
    		activeIndex = 8;
    	} else {
    		activeIndex--;
    	}		
    	$("#tourSlides .slide").fadeOut();
    	$("#tourControls .active").removeClass('active');
        $("#"+$("#tourControls LI:eq(" + activeIndex + ")").attr("rel")).fadeIn();
        $("#tourControls LI:eq(" + activeIndex + ")").addClass('active');
    });
});
/*******/

/**** REL = EXTERNAL ***/
function feedbackWindow() { 
	var width=490;
	var height=495;
	var topPos = ((screen.height / 2) - ( height / 2));
	var leftPos = (screen.width / 2) - (width /2);
	window.open('http://research.clientopinions.com/k/SsVSSSsQRPsPsPsP','ClientOpinions','resizable=no,width='+width+',height=' + height + ',left='+leftPos+',top='+topPos+',scrollbar=no,maximize=no,dialog=yes');
}
/*******/
/**** 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()
	 });
}
var active = false;
function initPage()
{
	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();
});

