// JavaScript Document
$(document).ready(function(){
	$("ul.nav li a").hover(
	  function () {
		$(this).animate({
				opacity: 0.25
			}, 
			100);
	  }, 
	  function () {
		$(this).animate({
				opacity: 1
			}, 
			100);
	  }
	);
	
	$("body.photos .thumbs li a").click(function(){
		if (!$(this).hasClass("active")){
			var num = $(this).parent().index();
			$(".thumbs li a").animate({opacity:1},250).removeClass("active");
			$(this).animate({opacity: .5}, 250);
			$(this).addClass("active");
			slidePhoto(num);
		}
		return false;
	});
	$("body.photos .gallery ul li").click(function(){
		if ($(this).hasClass("active")){
			var num = $(this).index();
			if ((num+1) > ($(this).parent().children().size()-1)){
				dest = 0;
			} else {
				dest = num+1;
			}
			$(".thumbs li a").animate({opacity:1},250).removeClass("active");
			$(".thumbs li:eq("+(dest)+") a").animate({opacity: .5}, 250);
			$(this).addClass("active").css("z-index",99);
			
			slidePhoto(dest);
		}
		return false;											   
	});
	$("body.photos .gallery ul li").hover(
	  function () {
		$(this).parent().find(".info").animate({
				opacity: 1
			}, 
			250);
	  }, 
	  function () {
		$(this).parent().find(".info").animate({
				opacity: 0
			}, 
			250);
	  }
	);
	
	
	// on page load
	if ($("body.videos").exists()){
		$("body.videos ul li a").click(function(){
			var title = $(this).attr("title");
			var copy = $(this).parent().find(".info").html();
			var vidSource = $(this).attr("data-video-source");
			var vidID = $(this).attr("data-video-id");
			var num = $(this).parent().index();
			
			createVideo(title,copy,vidSource,vidID,num);
			
			
			return false;
		});
		
		var firstTitle = $("body.videos ul li.active a").attr("title");
		var firstCopy = $("body.videos ul li.active .info").html();
		$("#videoTitle").html(firstTitle);
		$("#videoCopy").html(firstCopy);
	}

	// MUSIC page
	if ($("body.music").exists()){
		$("body.music .lyric a").click(function(){
			var id = $(this).attr("id");
			id = id.replace("view","");
			
			if ($(this).hasClass("active")){
				$("#lyrics"+id).slideUp();	
				$(this).removeClass("active");
			} else {
				$("#lyrics"+id).slideDown();
				$(this).addClass("active");
			}
			
			return false;
		});
		
	}
});

function slidePhoto(num){
	/*var width = $("body.photos .gallery li").width() + 25;
	//width  = width - (20 * num); // for the dropshadow on either side
	var dest = Math.ceil(width * num) * -1;
	$("body.photos .gallery ul").animate({
				marginLeft: dest
			}, 
			250);
	*/
	$("body.photos .gallery ul li.active").animate({
				opacity: 0
			}, 
			500).removeClass("active").css("z-index",0);
	$("body.photos .gallery ul li:eq("+num+")").animate({
				opacity: 1
			}, 
			500).addClass("active").css("z-index",99);
}

function createVideo(title, copy, vidSource,vidID, num){
	var video ="";
	
	$("#videoTitle").html(title);
	$("#videoCopy").html(copy);
	$("body.videos ul li").removeClass("active");
	$("body.videos ul li:eq("+num+")").addClass("active");
	
	if (vidSource == "vimeo"){
		video = '<iframe src="http://player.vimeo.com/video/'+vidID+'?title=0&amp;byline=0&amp;portrait=0&amp;color=2bc1c5" width="700" height="393" frameborder="0"></iframe>';
	} else if (vidSource == "youtube"){
		video = '<iframe title="YouTube video player" class="youtube-player" type="text/html" width="700" height="393" src="http://www.youtube.com/embed/'+vidID+'" frameborder="0"></iframe>';	
	}
	$("#viewer").html(video);
	
}





/* jQUERY */
/*	******************************
		PLUGIN - Utility Functions 
		Author: Jack Lukic - KNI (all plugins)
		Notes: Used to extend jQuery functionality and shorten code
	******************************	*/

jQuery.fn.extend({
	// test if el exists
	exists: function() {
		if(this.size() > 0) {
			return true;
		}
		else {
			return false;	
		}
	}
});
