/* --------------------------------------------------------------------------------------------
	Natalie Downe
	natalie.downe@torchbox.com
-------------------------------------------------------------------------------------------- */

jQuery.noConflict();

jQuery(document).ready(function($) {
	$('.featEvent .popuplink').click(function(){
		window.open($(this).attr("href"),'Events','width='+$(this).attr("rel").split("x")[0]+',height='+$(this).attr("rel").split("x")[1]);
		return false;
	});
});

var u_fadeSlide = 1.2; // the duration over which to fade the slide - if you set it to 0 the fade will be instantanious
var u_fadeSlideInterval = 6000; // the interval between end of one fade and start of another in ms



var g_rotatingElements;
var g_currentElement; // as interger index of g_rotatingElements

// uses yahoo dom, event and animation
var $D = YAHOO.util.Dom; // http://developer.yahoo.com/yui/docs/YAHOO.util.Dom.html
var $E = YAHOO.util.Event;
var $ = $D.get; // get element by ID

function processFade() {

	// fade the top slide onto the bottom
	var anim = new YAHOO.util.Anim(g_rotatingElements[g_currentElement], { opacity: { from: 1, to: 0 } }, u_fadeSlide, YAHOO.util.Easing.easeBoth);
	anim.onComplete.subscribe(function() { endFade() });
	anim.animate();
	anim = null;
}

function endFade() {

	// remove the top class on the current element
	$D.removeClass(g_rotatingElements[g_currentElement], 'topContainer');

	if(g_currentElement == g_rotatingElements.length - 1) {
		// have reached the end of the list, setting back to begining;
		g_currentElement = 0;
	} else {
		g_currentElement = g_currentElement + 1;
	}

	// new current element is the old next one
	$D.addClass(g_rotatingElements[g_currentElement], 'topContainer');
	$D.removeClass(g_rotatingElements[g_currentElement], 'nextContainer');


	if(g_currentElement == g_rotatingElements.length - 1) {
		// have now reached the end of the list, setting back to begining;
		YAHOO.util.Dom.setStyle(g_rotatingElements[0], 'opacity', 1);
		$D.addClass(g_rotatingElements[0], 'nextContainer');
	} else {
		YAHOO.util.Dom.setStyle(g_rotatingElements[g_currentElement + 1], 'opacity', 1);
		$D.addClass(g_rotatingElements[g_currentElement + 1], 'nextContainer');
	}

	// reset the opacity of the last photo to be normal
	if(g_currentElement == 0) {
		// previous photo was the last one
		YAHOO.util.Dom.setStyle(g_rotatingElements[g_rotatingElements.length - 1], 'opacity', 1);
	} else {
		YAHOO.util.Dom.setStyle(g_rotatingElements[g_currentElement - 1], 'opacity', 1);
	}

	// reset
	setTimeout(processFade, u_fadeSlideInterval)
}

function setupRotatingImage() {
	// get array of pointers to divs to rotate - all these are positioned absolute inside the container position relative
	g_rotatingElements = $D.getElementsByClassName('mainImageContainer','div','mainImageWraper');

	for (i=0;i<g_rotatingElements.length;i++) {
		// at this point rotatingElements[0] = id i+1
	}
	g_currentElement = 0;
	setTimeout(processFade, u_fadeSlideInterval);
}

// on load
$E.addListener(window,'load',setupRotatingImage);
