var globalMode;
var newMastImageArray = new Array();
var newMastTitleArray = new Array();
var totalTracker = 0;
var slideShowDuration = 5000;
var slideTransDuration = 700;
var fadeInTimeOut;
var slideTimeOut;
var slidePause = 0;

function setMode(mode) {
	
	switch (mode){
		case "full":
		$('#content h2 span').hide();
		$('#content .icon').css("padding-top", "6px").css("padding-right", "15px").css("padding-bottom", "10px").css("padding-left", "0px");
		$('#content h2, #content .postPreview, #content .icon, #content .postInfo, #pageNav').fadeIn("fast");

		break;
		case "list":
		$('#content .icon, #content .postInfo, #content .postPreview, #pageNav').hide();
		$('#content h2').css("padding-bottom", "5px");
		$('#content h2 span, #content h2').fadeIn("fast");
		break;
		case "icon":
		$('#content h2 span, #content h2, #content .postInfo, #content .postPreview, #pageNav').hide();
		$('#content .icon').css("padding", "14px");
		$('#content .icon').fadeIn("fast");
		break;
	}
	
}

function setCookie(mode) {
	
	//$('#content').fadeOut("fast");
	
	var queryString = "?mode=" + mode;
	
	if (mode != globalMode) {
	
		$.ajax({
			url: 'set-cookie.php' + queryString,
			success: function(data) {
				//$("#cookieGet").html(data).fadeIn("fast");
				//modeShow = data;
				setMode(mode);
				globalMode = mode;
			}
		});
		
		//alert(mode + " and global is " + globalMode);
	}
	//alert(queryString);
}

function getCookie() {
	
	//$('#content').hide();
	
	var mode;
	
	$.ajax({
		url: '/get-cookie.php',
		success: function(data) {
			
			//if (data != "tile") {		
				setMode(data);
			//}
			
			globalMode = data;
			//alert('mode is '+data);
		}
	});
	
	//alert("get cookie and view in "+mode);
}


function fadeInMastPrev() {
	
	totalTracker--;
	
	$('#mast #loadStatus').show();
	
	if (totalTracker < 0) {
			totalTracker = (newMastImageArray.length - 1);
	}
	
	$('<img />').attr('src', newMastImageArray[totalTracker])
		.load(function(){
			$('#mast #loadStatus').hide();
			$('#mastImage').css("background-image","url("+newMastImageArray[totalTracker]+")").stop(true).fadeTo(slideTransDuration,1);
			$('#mastImage h2').html(newMastTitleArray[totalTracker]);
    });
}

function fadeInMastNext() {
	
	totalTracker++;
	
	$('#mast #loadStatus').show();
	
	if (totalTracker >= newMastImageArray.length) {
		totalTracker = 0;
	}
	
	$('<img />').attr('src', newMastImageArray[totalTracker])
		.load(function(){
			$('#mast #loadStatus').hide();
			//$('#mastImage').stop(true, true).fadeTo(slideTransDuration,1);
			$('#mastImage').css("background-image","url("+newMastImageArray[totalTracker]+")").stop(true, true).fadeTo(slideTransDuration,1);
			$('#mastImage h2').html(newMastTitleArray[totalTracker]);
	});
}

$(function() {
	
	
	
});


function mastSlideShow() {
	
		$(function() {
			if (slidePause == 0) {	
				var fadeInTimeOut = setTimeout("fadeInMastNext()", slideTransDuration);
				$('#mastImage').stop(true, true).fadeTo(slideTransDuration,0);	
			}
			else  {
				clearTimeout(fadeInTimeOut);
				clearTimeout(slideTimeOut);
			}
			
			slideTimeOut = setTimeout("mastSlideShow()", slideShowDuration);
		});
}

$(function() {
	
	var opacity = 1, durationDelay = 2000;
	
	//var defaultPrev = $("#mastPrev").css("background-image");
	//var defaultNext = $("#mastNext").css("background-image");
	
	$('#mast').hover(function () {
		slidePause = 1;
		$('#mastPrev, #mastNext').stop(true, true).fadeTo("slow",1);
		//$('#mastPrev').css("background-image",defaultPrev),$('#mastNext').css("background-image",defaultNext);
	}, function () {
		slidePause = 0;
		$('#mastPrev, #mastNext').stop(true, true).fadeTo(durationDelay,opacity).fadeTo("slow",0);
	});
});


$(document).ready(function() {
	
	//PAGE SCROLL NAV
	$('.postItem').hover(function () {
		$('.pageNav', this).show();
	}, function () {
		$('.pageNav', this).hide();
	});
	
	//MAST IMAGE SLIDE LIST
	
	$('#mastPrev').css("background-image","url('/images/mastPrev.gif')").fadeTo(0,0);
	$('#mastNext').css("background-image","url('/images/mastNext.gif')").fadeTo(0,0);
	
	$.ajax({
		type: "GET",
		url: "/mast.xml",
		dataType: "xml",
		success: function(xml) {
			var i = 0;
			$(xml).find("mastImage").each(function(){
				newMastImageArray[i] = "/images/mast/"+$(this).text();
				newMastTitleArray[i] = $(this).attr("title");
				i++;
			});
			
			//$('#mastImage').css("background-image","url("+newMastImageArray[0]+")");
			//$('#mastImage h2').html(newMastTitleArray[0]).fadeIn("fast");
			
			//fadeInMastNext();
			
			//slideTimeOut = setTimeout("mastSlideShow()", slideShowDuration);
		}
	});
	
	//MAST NAV
	
	$('#mastNext').click(function() {
		$('#mastImage').stop(true, true).fadeTo(slideTransDuration,0);
		setTimeout("fadeInMastNext()", slideTransDuration);
	}),
	$('#mastPrev').click(function() {
		$('#mastImage').stop(true, true).fadeTo(slideTransDuration,0);
		setTimeout("fadeInMastPrev()", slideTransDuration);

	});
	
	//SET VIEW MODE
	$('#content h2.listDate, #content h2, #content .icon, #content .postPreview, #content .postInfo, #pageNav').hide();
	
	getCookie();
	
	$(".viewMode").css("display","block");
	
});
