// JavaScript Document
// ------------
// @author Michael Colella - Lastpixel Studios - www.lastpixelstudios.com
// ------------
$(document).ready(function(){
	
	$("#contact .normal").hover(
      function () {
        $(this).addClass("over");
      }, 
      function () {
        $(this).removeClass("over");
      }
    );

   
  //li with fade class
   $("li.fade").hover(
		function(){
			$(this).fadeOut(100);
			$(this).fadeIn(500);
		}
	);
   
   
   // ------------
   // START - portfolio items
   // ------------
   
   $(".folio-item").hover(
      function () {
       	$(this).find(".folio-text").slideDown(250);
		$(this).find(".folio-text").css({'text-decoration' : 'none'});
      } , 
      function () {
      	$(this).find(".folio-text").slideUp(250);
      }
	  
    );
   
    // ------------
    // START - primary navigation
    // ------------
 
  	// once loaded, adjust the height of the menu bg elements
    $(".primary-nav li .menu-bg").height(0);
    // handles mouse entering primary nav item
	$(".primary-nav li a").mouseenter(function() {
		
		if ($(this).parent().is(".active")) {
			$(this).css({'cursor' : 'default'});
			return false;
		} else {
			$(this).parent().find(".menu-bg").animate({ 
				height: "100px"
			}, 250 );

		};
		
	});
	
	// handles mouse leaving primary nav item
	$(".primary-nav li a").mouseleave(function() {
		
		if ($(this).parent().is(".active")) {
			$(this).css({'cursor' : 'default'});
			return false;
		} else {
			$(this).parent().find(".menu-bg").stop();
			$(this).parent().find(".menu-bg").animate({ 
				height: "0px"
			}, 250 );

		};
		
	});
	
	
	// activate the proper primary nav menu item
	$(function() {
		$(".active .menu-bg").animate({ 
			height: "100px"
		}, 250 );
	});


	// ------------
	// secondary navigationn functions
	// ------------
	// variables
	var oldSubID;
	var curSubID;
	
	if ($('body').hasClass('index')) {
		// nothing yet
	} else if ($('body').hasClass('about')) {
		
		// set current sub ID
		curSubID = "overview";
		// set varialbes
		tarTop = $("#"+curSubID).position().top;
		// set current class
		$("#"+curSubID).addClass('active');
		// render Cufon to show highlight
		Cufon.replace(".secondary-menu-items ul li");
		
	} else if ($('body').hasClass('work')) {
		// nothing yet
	} else if ($('body').hasClass('contact')) {
		// set current sub ID
		//curSubID = "form";
		// set varialbes
		//tarTop = $("#"+curSubID).position().top;
		// set current class
		//$("#"+curSubID).addClass('active');
		// render Cufon to show highlight
		//Cufon.replace(".secondary-menu-items ul li");
	}
	
	
	// initialize first item
	//$(".secondary-menu-items ul li:first").addClass('active');
	// secondary nav hovers
	$(".secondary-menu-items ul li").hover(function() {
		$(this).addClass('hover'); //Add class "hover" on hover 
		}, function() {
		$(this).removeClass('hover'); //Remove class "hover on off
	});
	// render cufon
	$(".secondary-menu-items ul li").mouseenter(function() {
		Cufon.replace(this);											 
	});
	$(".secondary-menu-items ul li").mouseleave(function() {
		Cufon.replace(this);											 
	});
	
	// secondary nav clicks
	$(".secondary-menu-items ul li").click(function() {
		
		//Set Variables
		var tarTop = $(this).position().top
		oldSubID = curSubID;
		curSubID = $(this).attr("id");
		
		
		if ($(this).is(".active")) {  //If it's already active, then...
			return false; // Don't click through
		} else {
			
			$(".secondary-menu-items ul li").removeClass('active');
			$(this).addClass('active');
			
			// move menu bg
			$(".secondary-menu-items-active").animate({ 
				top: tarTop
			}, 250 );
			// render Cufon to show highlight
			Cufon.replace(".secondary-menu-items ul li");
			
			// hide current details and open
			$("#"+oldSubID+"-details").fadeOut(250,function () {
				$("#"+curSubID+"-details").fadeIn(350, function() {
					// IE hack --- boo to IE
					$(this).css({'height' : '100%'});
					//hasLayout
				});
			});
			
		}

	});
	
	// --------
	// portfolio nav
	// --------
	$("#port-sub-nav ul li").hover(function() {
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
	
	$("#port-sub-nav ul li").click(function() {
		$('#port-sub-nav ul li.active').removeClass('active');
		$(this).addClass('active');	
		
		var filterVal = $(this).text().toLowerCase().replace(' ','-');
		
		if(filterVal == 'all') {
			$('#port-main-content ul li.hidden').slideDown(0).removeClass('hidden');
		} else {
			
			$('#port-main-content ul li').each(function() {
				if(!$(this).hasClass(filterVal)) {
					$(this).slideUp(0).addClass('hidden');
				} else {
					$(this).slideDown(0).removeClass('hidden');
				}
			});
		}
		/*
		if(filterVal == 'all') {
			$('#port-main-content ul li.hidden').animate({width: '296px', padding: '12px'}, 250).removeClass('hidden');
		} else {
			
			$('#port-main-content ul li').each(function() {
				if(!$(this).hasClass(filterVal)) {
					$(this).animate({width: '0px', padding: '0px'}, 250).addClass('hidden');
				} else {
					$(this).animate({width: '296px', padding: '12px'}, 250 ).removeClass('hidden');
				}
			});
		}
		
		
		if(filterVal == 'all') {
			$('#port-main-content ul li.hidden').slideDown('slow').removeClass('hidden');
		} else {
			
			$('#port-main-content ul li').each(function() {
				if(!$(this).hasClass(filterVal)) {
					$(this).slideUp('normal').addClass('hidden');
				} else {
					$(this).slideDown('slow').removeClass('hidden');
				}
			});
		}
		*/
		return false;
		
	});
	
	// --------
	// portfolio items
	// --------
	/*
	$("#port-main-content ul li").hover(function() {
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
	$("#port-main-content ul li").click(function() {
		clickID = $(this).attr("id");
		// load proper portfolio details
		window.location = "details?id="+clickID;
	});
	
	// -------
	// news items
	// -------
	$("#news-main-content ul li").hover(function() {
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
	$("#news-main-content ul li").click(function() {
		clickID = $(this).attr("id");
		// load proper portfolio details
		window.location = "details?id="+clickID;
	});
	*/
	
	// -------
	// Scroll top
	// -------
	 function filterPath(string) {
		return string
		.replace(/^\//,'')
		.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
		.replace(/\/$/,'');
	}
	var locationPath = filterPath(location.pathname);
	$('a[href*=#]').each(function() {
		var thisPath = filterPath(this.pathname) || locationPath;
		if (  locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/,'') ) {
			var $target = $(this.hash), target = this.hash;
			if (target) {
				var targetOffset = $target.offset().top;
				$(this).click(function(event) {
					event.preventDefault();
					$('html, body').animate({scrollTop: targetOffset}, 400, function() {
						location.hash = target;
					});
				});
			}
		}
	});	

});

// ------------
// overlay functions
// ------------
var fullUrl = "i/overlay_background.png";
var fullBg = "";
var overlayWidth = 660;
var overlayHeight = 440;
var overlayUrl ="";
var overlayBg = "#fff";
var overlayBuffer = 50;

function initOverlay (src) {
	var wElements = calcWinDim();
	var pElements = calcPage();
	var pTop = $(window).scrollTop();
	var topNum = pTop+overlayBuffer;
		
	frameUrl = src;
	if ($("#overlay-background").length > 0) {
		//do something
		displayElements(wElements[0],pTop+overlayBuffer);
		return false;
	} else {
		// create elements
		$("body").append("<div id='overlay-background' style='width:"+pElements[0]+"px;height:"+pElements[1]+"px;position:absolute;top:0;left:0;z-index:9900;background:"+fullBg+" url("+fullUrl+");'></div><div id='overlay-content' style='padding:10px;width:"+overlayWidth+"px;height:"+overlayHeight+"px;position:absolute;top:"+topNum+"px;left:"+wElements[0]+"px;z-index:9901;background:"+overlayBg+" url("+overlayUrl+") top left no-repeat; display:none;-moz-border-radius-bottomleft:8px;-moz-border-radius-bottomright:8px;-moz-border-radius-topleft:8px;-moz-border-radius-topright:8px;'><div id='close-btn' style='position:absolute;top:-20px;right:-20px;'><img src='i/close_button.png' width='42' height='42' border='0' alt='[X]' /></div><iframe id='overlay-frame' width='"+overlayWidth+"px' height='"+overlayHeight+"px' src='' frameborder='0' scrolling='no'></iframe></div>");
		$("#close-btn").css({'cursor':'pointer'});
		$("#close-btn").click(overlayHide);
		$("#overlay-background").click(overlayHide);
		initDisplay();
		return false;
	}
}
// returns the browser width & height
function calcPage() {
	var pWidth = Math.floor($(document).width());
	var pHeight = Math.floor($(document).height());
	return [pWidth, pHeight];
}
// returns the popover left and top position
function calcWinDim() {
	var wWidth = Math.floor($(window).width()/2);
	var wHeight = Math.floor($(window).height()/2);
	var wX = wWidth-(overlayWidth/2);
	var wY = wHeight-(overlayHeight/2);
	return [wX, wY];
}
// window resize
$(window).bind("resize", resizeWindow);
function resizeWindow( e ) {
	var newWindowWidth = $(window).width();
	$("#overlay-background").css("width", newWindowWidth );
}
// init display
function initDisplay() {
	$("#overlay-content").fadeIn(350);
	$("#overlay-frame").attr("src",frameUrl); 
}
// do your stuff
function displayElements(x,y) {
	// show elements
	$("#overlay-background").css({'display':'block'});
	$("#overlay-content").css({
		'top'		:	y,
		'left'		:	x
	});
	$("#overlay-content").fadeIn(350);
	$("#overlay-frame").attr("src",frameUrl); 
	
	return false;
}
function overlayHide() {
	$("#overlay-content").css({'display':'none'});
	$("#overlay-background").css({'display':'none'});
	return false;
}

  
