
/**
 * scroll to the target index.
 */
var  scrollTo = function(index) {
	$('#viewport').trigger( 'goto', [index] );
}

/**
 * jump to the target index.
 */
var  jumpTo = function(index) {
	var $item = $('#viewport .page').get(index);
	$('#viewport').scrollTo( $item, {duration: 0, offset: -190} );
	$('#viewport').trigger( 'notify', [index] );
}

var setBackground = function(index) {
	// set background image
	var imgSrc = $('#viewport .page').eq(index).find(".bgData").attr("src");
	var $bgImg = $("#background img")
	if (imgSrc && imgSrc != $bgImg.attr("src")) {
		// TODO animate transition
		$bgImg.attr("src", imgSrc);
	}
}

var nextMenuItem = function() {
	var $activeItem = $('.activemenu');
	var $nextItem = $activeItem.parent().next().children(".menuitem");
	$nextItem.trigger('click');
}

var getCurrentDocId = function() {
	var docId = $('.activemenu').parent().attr("docid");
	if (!docId || docId == undefined) {
		docId = "1";
	}
	return docId;
}

var printWindow = function() {
	var docId = getCurrentDocId();
	window.open('index.php?id='+docId+"&print", 'welcome','width=1050,height=550');
}

/**
 *
 */
var navigateTo = function(hash) {
	var docId = hash ? hash : "1";
	var i = hash.indexOf("/");
	if (i>=0) {
		docId = hash.substring(0, i);
	}
	if (docId.length == 0) {
		docId = "1";
	}
	var $anchor = $("li[docid='" + docId + "']").children("a");
	var $selected = $(".activemenu");
	if ($anchor.attr("href") != $selected.attr("href")) {
		// trigger a click only if selected anchor has changed
		$anchor.trigger("evt_accordion", [false])
	}
}

/**
 * the current parent document. All the children will be displayed in a row
 */
var currentDoc = -1;

/**
 * "easing" function for scrolling effect
 */
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery(function( $ ){

	/**
	 * Set up serialScroll on the viewport
	 */
	$('#viewport').serialScroll({
		items:'.page',
		duration:700,
		force:true,
		lock:false,       // true ignores events while animating
		jump:false,        // allows click on adjacent items to scroll to them
		constant: false,  // true means the speed stays constant regardless of the number of items to scroll
		cycle:false,
		axis:'x',
		easing:'easeOutQuart',
		offset: -190,
		lazy:true,// NOTE: it's set to true, meaning you can add/remove/reorder items and the changes are taken into account.
		interval:0, // yeah! I now added auto-scrolling
		step:1 // scroll 1 item each time
	});
});

/**
 * setup accordian menu effects
 */
ddaccordion.init({
	headerclass: "submenuheader", //Shared CSS class name of headers group
	contentclass: "submenu", //Shared CSS class name of contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click" or "mouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: true, //persist state of opened contents within browser session?
	toggleclass: ["", ""], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["suffix", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "fast", //speed of animation: "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated, directclick){ //custom code to run whenever a header is opened or closed

		if (state=="block" && isuseractivated==true){  //if header is expanded as a result of the user clicking on it
			// de-activate active menu items
			$(".activemenu").removeClass("activemenu");
			
			var docId = header.parentNode.getAttribute("docId");
			var parentDocId = header.parentNode.parentNode.parentNode.getAttribute("docId");
			var href = header.getAttribute("href");
			var i = href.lastIndexOf("/");
			href = i>=0 ? href.substring(i) : href;
			
			if (!parentDocId) {
				parentDocId = 0; 
			}

			// get the index of the current selected item amongst its siblings
			var $p = $(header).parent();
			var index = $p.parent().children().index($p);
			
			$(header).addClass("activemenu");

			// push onto history
			if (directclick) {
				var hash = 	docId + href;	
				//alert("add to history: " + hash);
				$.historyLoad(hash);
			}
			
			// notify google analytics
			pageTracker._trackPageview("#"+docId+href);

			if (parentDocId == currentDoc) {
				setBackground(index);
				// scroll to document index
				scrollTo(index);
			}
			else {
				// load sibling documents and scroll to document index - NOTE HARD-CODED ARTICLE ID
				$("#row").load("index.php?id=195&docId=" + parentDocId, {}, function() {
					setBackground(index);
					// jump to document
					jumpTo(index);
				});
				currentDoc = parentDocId;
			}
			
		}
	}
});




