// var xmlFile = "../spinner/"; // <-- the new way when it's ready..
var xmlFile = "../spinner/";
var isScrolling = false;
var itemId = 1;
var maxItems;
var intervalId = 0;
var pause = 11000;
var timerRunning = false;
var itemHeight = 72;
var _splashId = -1;

// this function is called after jQuery(document).ready(function() from header.php..
function initScroller () {

	if (navigator.userAgent.indexOf("iPad") > -1) {
		$j.ajax({
			type: "GET",
			url: xmlFile,
			dataType: "xml",
			success: parseXml
		});
	}
	else {
		loadFlashObj();
	}
}

function parseXml (xml) {
	var thumb;
	var title;
	var largeImg;
	var header;
	var splashContent;
	var i = 1;
	$j(xml).find("item").each(function() {
		thumb = $j(this).attr("thumb");
		title = $j(this).find("title").text();
		largeImg = $j(this).attr("image");
		header = $j(this).find("header").text();
		splashContent = $j(this).find("content").text();
		$j("#isc-thelist").append('<li id="item' + i + '"><div class="item-overlay hide-overlay"><img src="/wp-content/themes/invoke/images/item-pointer.png" /></div><img src="' + thumb + '" /><p class="scroll-title">' + title + '</p></li>');
		$j('#spinner-splash').append('<div id="sp' + i + '" class="splash-img"><div id="splash-content"><div id="splash-bg"></div><div id="s-content"><h4>' + header + '</h4><p>' + splashContent + '</p></div></div><img src="' + largeImg + '" /></div>');
		i++;
	});

	$j('#s-content a').click (function () {
		trackEvent ($j(this).parent().text());
	});

	$j.each ($j('#s-content a'), function (key, val) {
		// console.log ('in ' + $j(this).parents('.splash-img').find('img'));
		$j(this).parents('.splash-img').find('img').wrap('<a href="' + val + '" target="_blank" onclick="trackEvent(\'' + $j(this).text() + '\')" />');
	});

	maxItems = i - 1;
	// set scroller..
	setScroller ();
}

function trackEvent (val) {
	// console.log ('trackEvent: ' + val);
	pageTracker._trackEvent('spinner','user-click',val);
}

function setScroller () {

	myScroll = new iScroll('isc-wrapper', {
		snap: 'li',
		momentum: true,
		hScrollbar: false,
		vScrollbar: false,
		onScrollMove: function () {
			isScrolling = true;
		},
		onScrollEnd: function () {
			//console.log ('onScrollEnd..');
			isScrolling = false;
		},
		onScrollStart: function () {
			//console.log ('onScrollStart..');
			timerRunning = false;
			clearInterval (intervalId);
		},
		onTouchEnd: function () {
			//console.log ('onTouchEnd..');
			if (!timerRunning) startTimer();
		},
		onBeforeScrollEnd: function () {
			//console.log ('onBeforeScrollEnd..');
		}
	});

	$j('#isc-thelist li').click (function () {
		/*
		if (!isScrolling) {
			showSplash(this);
		}
		*/
		showSplash(this);
	});

	rndNum = Math.floor (Math.random() * maxItems ) + 1;
	myScroll.scrollToElement('li:nth-child(' + rndNum + ')', 800);
	$j('#sp' + rndNum).addClass('active');
	$j('#item' + rndNum).find('.item-overlay').removeClass('hide-overlay');

	startTimer ();

}

function startTimer () {
	timerRunning = true;
	intervalId = setInterval ("updateScroller()", pause);
}

function updateScroller () {
	//itemId++;
	//myScroll.scrollTo(0, itemHeight, 500, true); // doesnt work..
	// if (itemId > maxItems) itemId = 1;
	// myScroll.scrollToElement('li:nth-child(' + itemId + ')', 800);
	//console.log (rndNum);

	rndNum = Math.floor (Math.random() * maxItems ) + 1;
	myScroll.scrollToElement('li:nth-child(' + rndNum + ')', 800);
	showSplash($j('#item' + rndNum).get(0));
	$j('#sp' + rndNum).addClass('active');
	// $j('#item' + rndNum).find('.item-overlay').removeClass('hide-overlay');
}

function showSplash (item) {
	
	// console.log (_splashId + " : " + item.id.substr(4));

	var itemId = item.id.substr(4);
	if (_splashId == itemId) return; // items are same..

	_splashId = itemId;
	var active = $j('#spinner-splash div.active');
	// console.log ('in here: ' + active);
	// var activeId = $j(active).get(0).id.substr(2);
	var activeId = $j(active).get(0).id.substr(2);
	
	if (_splashId == activeId) return; // items are the same..

	//$j('#item' + activeId).find('.item-overlay').addClass('hide-overlay');
	$j('.item-overlay').addClass ('hide-overlay');

	$j(item).find('.item-overlay').removeClass('hide-overlay');

	var next = $j('#sp' + _splashId);
	$j(next).find('#splash-content').css({ opacity: 0.0 }).delay(1300).animate({ opacity: 1.0 }, 600);
	$j(active).addClass('last-active');

	$j(next).css({ opacity: 0.0 }).addClass('active').animate({opacity: 1.0}, 600, function () {
		$j(active).removeClass('active last-active');
	});

}

//********** END ************//

homeFlash3 = function () {
	//alert ('load scroller..');
}


loadFlashObj = function() {
	var so = new SWFObject("wp-content/themes/invoke/swf/header.swf", "player", "955", "300", "8", "#000000");
	if(so.installedVer.major<8) {
		document.getElementById('flash').innerHTML = '<div style="text-align:center;padding-top:100px;font-size:14px;font-weight:bold">Flash player version 8.0 or greater is required <br/>to play video content on this site.<br/><br/>If you don\'t have it, download it from:<br/> <a href="http://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank">Adobe Flash Player Download site</a></div>';
	} else {
		so.addVariable("xmlFile", xmlFile);
		so.write("flash");
	}
}
//]]>

