/* THUMBNAIL NAV FUNCTIONS
---------------------------------------------------------*/


function tnPrevNextInit() {
	
	if (tnRow<=1) $("#tn-prev").addClass("arrow-null");
		else $("#tn-prev").removeClass("arrow-null");
		
	if (tnRow>=tnRowCount) $("#tn-next").addClass("arrow-null");
		else $("#tn-next").removeClass("arrow-null");
	
}

function tnInit() {
	tnRowHeight = $("#thumb-holder a").outerHeight(true);
	tnPrevNextInit();
	$("#thumb-holder a img").attr("alt","");
}

function tnOver(el) {
	if (!$(el).hasClass('active')) {
		
		$(el).stop().animate({
			backgroundColor: "#969d00"
		}, 300, "linear");
		
		var id	= $(el).attr('id') + 'box';
		var t	= $("#thumb-nav");
		var pb	= $("#portfolio-belt");
		var bl	= t.position().left + $(el).position().left;
		var bt	= pb.position().top + t.position().top + t.height() + 6;
		var cli	= "<div class='roll-client'>" + $(el).find('.client-data').html() + "</div>";
		var prj	= "<div class='roll-project'>" + $(el).find('.project-data').html() + "</div>";
		
		$("<div id='"+ id +"' class='roll-box'>"+ cli + prj +"</div>")
			.insertAfter(pb)
			.css({
				'left': bl,
				'top': bt,
				'display': 'none'
			})
			.animate({
				width: 'show'
			}, 400);
	}
}

function tnOut(el) {
	if (!$(el).hasClass('active')) {
		$(el).stop().animate({
			backgroundColor: "#fff"
		}, 100, "linear");
		var id = $(el).attr('id') + 'box';
		$("#"+id).remove();
	}
}

function tnMove(dir) {
	
	var move = false;
	
	if (tnRow<tnRowCount && dir=='next') {
		tnRow++;
		move = true;
	} else if (tnRow>1 && dir=='prev') {
		tnRow--;
		move = true;
	}
	
	if (move) {
		
		var newMargin = -(tnRow-1) * tnRowHeight;
		
		$("#thumb-holder").stop().animate({
			marginTop: newMargin
		}, 800, "easeOutQuart");
		
		var ofStart = "/images/of/" + tnRow + ".png";
		$("#of-start").attr("src",ofStart);
		
		tnPrevNextInit();
		
	}
}

/* IMG NAV FUNCTIONS
---------------------------------------------------------*/

function imgPrevNextInit() {
	
	if (imgNum<=1) $("#img-prev").addClass("arrow-null");
		else $("#img-prev").removeClass("arrow-null");
		
	if (imgNum>=imgCount) $("#img-next").addClass("arrow-null");
		else $("#img-next").removeClass("arrow-null");

}

function imgPrevNextClick(dir) {
	
	var newImgNum = imgNum;
	
	if (dir == 'prev') {
		newImgNum--;
	} else if (dir == 'next') {
		newImgNum++;
	}
	
	if (newImgNum > 0 && newImgNum <= imgCount) {
		$("#imgnav-" + newImgNum).trigger('click');
	}
	
}

function imgClick(imgID,imgPath,imgBorder) {
	
	if (!$("#imgnav-" + imgID).hasClass('active')) {
	
		imgNum = imgID;
		$("#nav-column .imgnav").removeClass('active');
		$("#imgnav-" + imgID).addClass('active');
		
		imgPrevNextInit();
		
		$("#img-holder").hide();
		if (imgBorder==1) $("#img-holder").addClass('img-border');
			else $("#img-holder").removeClass('img-border');
		$("#img-holder img").attr('src',imgPath);
		
	}
	
	return false;
}

function preloadNext() {
	if (imgCount > 1 && imgNum < imgCount) {
		var nextImgFile = $("#imgnav-" + (imgNum + 1)).attr('onclick').toString().split('"');
		if (nextImgFile[1] != '') $.cacheImage(nextImgFile[1]);
	}
}


/* RECOGNITION FUNCTIONS
---------------------------------------------------------*/

function recInit() {
	
	$("#recognition-holder img").each(function(i) {
		$(this).data("rolltext",$(this).attr("alt"));
		$(this).attr("alt","");
	});
}

function recOver(el) {
	
	var id	= $(el).attr('id') + 'box';
	var rh	= $("#recognition-holder");
	var br	= rh.width() - $(el).position().left - $(el).outerWidth(true) + 40;
	var bt	= rh.position().top + 36;
	var txt	= $(el).attr('alt');
	var txt	= "<div class='rec-roll-text'>" + $(el).data("rolltext") + "</div>";
	
	$("<div id='"+ id +"' class='rec-roll-box'>"+ txt +"</div>")
		.insertAfter(rh)
		.css({
			'right': br,
			'top': bt,
			'display': 'none'
		})
		.animate({
			width: 'show'
		}, 400);
}

function recOut(el) {
	
	var id = $(el).attr('id') + 'box';
	$("#"+id).remove();
}


/* SUBNAV FUNCTIONS
---------------------------------------------------------*/

function subnavClick(el) {
	
	var id = $(el).attr('id') + '-drop';
	var drop = $("#"+id);
	
	if (drop.is(":visible")) {		// Roll everything up
		
		var dropParent = drop.parent();
		if (drop.parent().attr("class") == "jScrollPaneContainer") {
			dropParent.slideUp(400, function(){
				drop.jScrollPaneRemove();
				drop.attr('style','');
			});
		}
		drop.slideUp(200);
		
		// check to see if there are any other dropdowns visible
		var otherDrops = false;
		$(".subnav-drop:not(#"+id+")").each(function(){
			if ($(this).is(":visible")) {
				otherDrops = true;
			}
		});
		if (!otherDrops) {
		
			// fade the text column	in
			$("#text-column").fadeTo(200, 1.00, function(){
				$(this).css('opacity','');
			});
		}
		
	} else {	// Roll a drop menu out
		
		// fade the text column	out
		$("#text-column").fadeTo(200, 0.08);
		
		// set a general maximum height for the drop menus
		var maxH = 225;
		
		// get the height of the contents inside the hidden subnav
		drop.css({'margin-bottom':0,'height': 0,'display': 'block'});
		var realH = $("#"+id+" .subnav-inner").outerHeight(true);
		
		// either go up to the max height and add a scroll pane, or just go up to the regular height
		if (realH > maxH) {
			drop.css({'display': 'none','margin-bottom':10,'height': maxH});
			drop.slideDown(500, function(){
				$(this).jScrollPane({
					scrollbarWidth: 5,
					scrollbarMargin: 15,
					wheelSpeed: 60
				});				  
			});
		} else {
			drop.css({'display': 'none','height': realH});
			drop.slideDown(500);
		}
	}
}



/* DOCUMENT READY INITIALIZATION
---------------------------------------------------------*/

$(document).ready(function () {
	
	// THUMBNAILS
	
		tnInit();
		$("#thumb-holder a").hover(
			function () { tnOver(this) }, 
			function () { tnOut(this) }
		);
		$("#tn-prev").click(
			function () { tnMove('prev'); return false; }
		);
		$("#tn-next").click(
			function () { tnMove('next'); return false; }
		);
		
	// INIT PROJECT IMAGES IF THERE ARE ANY
	
	if ($("#img-holder").length > 0) {
	
	// IMG NAV
	
		imgPrevNextInit();
		$("#img-prev").click(
			function () { imgPrevNextClick('prev'); return false; }
		);
		$("#img-next").click(
			function () { imgPrevNextClick('next'); return false; }
		);
		
	// SET IMAGES TO FADE IN & PRELOAD NEXT IMAGE
		
		$("#img-holder").hide();
		
		// for all images when they load
		$("#img-holder img")
			.load(function() {
				$("#img-holder").show(500);
				preloadNext();
			})
		
		// in case the first image is cached, and load doesn't fire
		if ($("#img-holder img")[0].complete) {
			$("#img-holder").show(500);
			preloadNext();
		}
	
	} // END PROJECT IMAGES IF
	
	// RECOGNITION
		
		recInit();
		$("#recognition-holder img").hover(
			function () { recOver(this) },
			function () { recOut(this) }
		);
		
	// SET UP SUBNAV LINKS
		
		$("#subnav .subnav-link a").click(
			function () { subnavClick(this); return false; }
		);

});
