﻿/*
Autoscroll
*/

var myInterval;
var autoScrollPage = 1;
var clicked = false; 	// Tracks if we are autoScrolling
var showArrows = true;

function autoScrollScoller() {
	autoScrollNoOfPages = $('.page').length;

	//console.log("pages : " + autoScrollPage + " = " + autoScrollNoOfPages);
	if (parseInt(autoScrollPage) != parseInt(autoScrollNoOfPages)) {
		scrollItem(1);
		autoScrollPage++;
	} else {
		clearInterval(myInterval);
	}
}



/*
Page/Content Scroller
Author: Stephen Sinclair
Updated by : c2
*/
var items = $('.page');
var currentItem = 0;
var duration = 900;
var easing = 'easeOutQuint';


function scrollItem(direction) {
	//direction: 0=previous 1=next

	// Cancel the auto scroll if the user actually clicked a button
	if (myInterval != null && clicked ) {
		clearInterval(myInterval);
	}

	var item = items.eq(currentItem);
	var itemContent = item.children(':first');
	var nextItem = items.eq(direction ? ++currentItem : --currentItem);
	var nextItemContent = nextItem.children(':first');
	var nextItemRaw = nextItem.get(0);
	var tempWidth = parseInt(item.css('width'), 10);

	//console.log(tempWidth)

	//if we click previous, content must float right and have fixed width to give push effect
	//if we click next, content must float left and have fixed width to give push effect
	itemContent.css({
		width: tempWidth,
		'float': direction ? 'right' : 'left'
	});
	nextItemContent.css({
		width: tempWidth,
		'float': direction ? 'left' : 'right'
	});

	item.animate(
        { width: 0 },
        {
        	duration: duration,
        	easing: easing,
        	step: function (now, fx) {
        		nextItemRaw.style.width = (tempWidth - now) + 'px';
        		//console.log(nextItemRaw.style.width);
        	},
        	complete: function () {
        		//widths were fixed for slide effect, reset them to 100%
        		itemContent.css('width', '100%');
        		nextItemRaw.style.width = '100%';
        		nextItemContent.css('width', '100%');
        		setVertPosition();


        		if (direction == 1) {

        			// Find out if we have already loaded the next page
        			page = nextItem.closest('.page')
        			if (!page.next().length) {
        				// The next page is not loaded - Grab it now
        				var nextPage = items.eq(currentItem).find('.right a').attr('href')
        				if (nextPage != undefined) loadNext(nextPage, true);
        			}
        			if (!showArrows) {
        				//console.log('hiding arrows');
        				$('.scrollNavWrapper').css('display', 'none');
        			}

        		} else {

        			// Find out if we have already loaded the previous page
        			page = nextItem.closest('.page')
        			if (!page.prev().length) {
        				var prevPage = items.eq(currentItem).find('.left a').attr('href')
        				if (prevPage != undefined) loadPrevious(prevPage, true);
        			}
        			if (!showArrows) {
        				//console.log('hiding arrows');
        				$('.scrollNavWrapper').css('display', 'none');
        			}
        		}

        	}
        }
    );
}


//next
$('.scrollNav .l').live('click', function (el) {
	clicked = true;
	el.preventDefault();
	scrollItem(1);

});

//previous
$('.scrollNav .f').live('click', function (el) {
	clicked = true;
	el.preventDefault();
	scrollItem(0);

});




function loadNext(href, preLoad) {

	var loadVar = (href.replace(/ /g, "%20") + ' .main .page .content');

	if (preLoad) {preLoading = 'style="width: 0px;"'; }
	$('<div class="mask page dynamic" ' + preLoading + ' />').load(loadVar, function () {
		$(this).appendTo('#main');

		//update items
		items = $('.page');
		applyExtra();
		if (!preLoad) {
			scrollItem(1);
		}
		
	});
}


function loadPrevious(href, preLoad) {

	var loadVar = (href.replace(/ /g, "%20") + ' .main .page .content');

	if (preLoad) {preLoading = 'style="width: 0px;"'; }
	$('<div class="mask page dynamic" ' + preLoading + ' />').load(loadVar, function () {
		$(this).prependTo('#main');

		//update items
		items = $('.page');
		applyExtra();

		currentItem++;

		if (!preLoad) {
			scrollItem(0);
		}
	});
}






function applyExtra() {
	// apply the accordion setting to the new content
	applyAccordion();


	// apply the thickbox to the dynamic content
	tb_init('a.thickbox');
}



