$(document).ready(function() {
	var matrixAuto = 0;
	var matrixNoBorder = 0;
	var matrixVideo = 0;
	var matrixRotate = 0;
	var matrixHash = 0;
	var limitCycle = 0;
	var cycleDelay = 10;
	var docTitle = document.title;
	if($("#matrixLayout").hasClass("matrixAuto")){
		matrixAuto = 1;
	}
	if($("#matrixLayout").hasClass("matrixNoBorder")){
		matrixNoBorder = 1;
	}	
	if($("#matrixLayout").hasClass("matrixVideo")){
		matrixVideo = 1;
	}
	if($("#matrixLayout").hasClass("matrixRotate")){
		matrixRotate= 1;
	}
	if($("#matrixLayout").hasClass("matrixHash")){
		matrixHash= 1;
	}
	if($("#matrixLayout").hasClass("limitCycle")){
		limitCycle= 6;
	}
	if(matrixAuto){
		$(".item").each(function () {
			var cat_i = $(this).attr("id");
			var cat_t = $(this).attr("title");
			$(this).removeAttr("title");
			$("#categories").append('<a href="#'+cat_i+'"><span>'+cat_t+'</span></a>');
		});
	}
	if(matrixNoBorder){
		$('#categories a:last').css("border-bottom","0");
	}	
	// function for clicking on a report link
	function rbind(){ 
		$('#categories a').click(function(e){
			 // Prevent default click action
			 e.preventDefault();
			// remove current selected item bg
			$("#categories a").removeClass("selected");
			$("#categories").removeClass("blackTxt");
			// get id of current button clicked
			var rep_click = $(this).attr("href");
			var repid = rep_click.replace("#","");
			// hide any current report
			if($(rep_click).hasClass("blackTxt")){
				$("#categories").addClass("blackTxt");	
			}
			if(matrixVideo){
				// remove any existing playing video
				swfobject.removeSWF("matrix-video");
				if($(rep_click + " .videoContainer").append('<div id="matrix-video">&nbsp;</div>')){
					// get javascript for current video
					$.getScript('scripts/videos/'+ repid +'.js', function() {										   
						// show new clicked report										   
						$(rep_click).fadeIn("slow", function(){
							if(jQuery.browser.msie){
								$(this).get(0).style.removeAttribute('filter');
							}
						});	
					});
				}	
			}
			else{
				// show new clicked report
				$(rep_click).fadeIn("slow", function(){
					if(jQuery.browser.msie){
						$(this).get(0).style.removeAttribute('filter');
					}
				});
			}
			// hide any current report
			$("#details .item:not("+rep_click+")").fadeOut("slow", function(){
				if(jQuery.browser.msie){
					$(this).get(0).style.removeAttribute('filter');
				}
			});
			// set current button to selected class
			$(this).addClass('selected');
			if(matrixHash){
				document.location.hash = rep_click;
				document.title = docTitle;
			}
			return false;
		});
		if(matrixRotate){
			$.getScript("JS/esricore/styles/jquery.timers.js", function(){
				$('#categories a').mousedown(function () {	
					// stop auto play
					$(document).stopTime('autoplay');
				});
				$(document).everyTime(cycleDelay * 1000,'autoplay',function(i) {
					if($("#categories a:last").hasClass("selected")){
						$("#categories a:first").click();
					}
					else{
						$("#categories a.selected").next("a").click();
					}
				}, limitCycle);
			});			
		}
		if(matrixVideo){	
			// Previous panel button
			$(".previous-panel").click(function() {
				// if current panel is first
				if($("#categories a:first").hasClass("selected")){
					// click last panel
					$("#categories a:last").click();
				}
				else{
					// click previous panel
					$("#categories a.selected").prev("a").click();	
				}
				window.location.hash = $("#categories a.selected").attr("href");
			});
			// Panel Navigation: next panel button
			$(".next-panel").click(function() {	
				// If current panel is the last
				if($("#categories a:last").hasClass("selected")){
					// click first panel
					$("#categories a:first").click();
				}
				else{
					// click next panel
					$("#categories a.selected").next("a").click();
				}
				window.location.hash = $("#categories a.selected").attr("href");
			});
		}
	};
	// loading hash function
	function mload(){ 
		// if hash exists #value
		if(window.location.hash && window.location.hash != "" && window.location.hash != "#"){
			var ihash = window.location.hash;
			// make sure pound sign is not there
			ihash = ihash.replace("#","");
			// add pound sign
			var m_hash = ("#"+ihash);
			// see if element exists in the html
			if($('#categories a[href='+m_hash+']').length > 0){
				// click category of hash id
				$('#categories a[href='+m_hash+']').click();
				// scroll to selected
				$("div#categories").scrollTo('a[href='+m_hash+']');
			}
			else{
				// if hash value doesnt exist in html
				$('#categories a:first').click();
			}
		}
		else{
			// if no hash, load first item
			$('#categories a:first').click();
		}
	};
	// Get scrolling script
	$.getScript("/JS/esricore/scripts/jquery.scrollto.js", function(){
		// execute functions
		rbind();
		mload();
	});
});
