/* Author: Peter James

*/

/*log('Copyright © Peter James 1998 - 2011');*/

/*
Script: Home Flash Books
*/

if( $("#home").length )
{
	
	var flashvars = false;
	var params = {
		menu: "false",
		wmode: "transparent"
	};
	var attributes = {};
	swfobject.embedSWF("/img/home/books.swf", "video-books", "616", "614", "9.0.0", "/js/libs/expressInstall.swf", flashvars, params, attributes);
	
	$("#flash-deadMansGrip").fadeTo("fast", 0.3);
	
	function deadMansGrip() {
		//console.log("Dead Mans Grip");
		
		$("#flash-perfectPeople").fadeTo("slow", 0.3, function() {
			$("#flash-deadMansGrip").fadeTo("slow", 1);
		});
		
	}

	function perfectPeople() {
		//console.log("Perfect People");
		
		$("#flash-deadMansGrip").fadeTo("slow", 0.3, function() {
			$("#flash-perfectPeople").fadeTo("slow", 1);
		});
		
	}
	
}


(function($){
	
	/*
	Script: Home
	*/
	if( $("#home").length )
	{
		$('#js-news').ticker({
			controls: false,
			pauseOnItems: 10000
		});
	}
	
	/*
	Script: About
	*/
	if( $("#about").length )
	{
		$('.tabNavigation').click(function(e){
			e.preventDefault();
			cc = $(this);		
			$(".tabNavigation").removeClass("current");
			$(".tab:visible").fadeToggle("fast", function(){
				$($(cc).attr("href")).fadeToggle("fast");
			});
			$(cc).addClass("current");
		});
		
		// lightbox
		$('a.lightBox').click(function(){
			var hashes = $(this).attr('href').split('+');
			var imageBuilder='';
			
			for (var i = 0; i  < hashes.length; i++)  {
				imageBuilder += '<a href="'+hashes[i]+'"';
				imageBuilder += 'title="'+$(this).attr("title")+'"><img src="';
				imageBuilder += hashes[i];
				imageBuilder += '" \></a>';
			}

			var lb = $(imageBuilder);
			lb.lightBox({
				imageLoading: '/img/lightbox/lightbox-ico-loading.gif',
				imageBtnClose: '/img/lightbox/lightbox-btn-close.gif',
				imageBtnPrev: '/img/lightbox/lightbox-btn-prev.gif',
				imageBtnNext: '/img/lightbox/lightbox-btn-next.gif',
				imageBlank: '/img/lightbox/lightbox-blank.gif'
			});
			lb.filter('a:first').click();

			return false;
		});
	}
	
	/*
	Script: Reviews
	*/
	if( $("#reviews").length )
	{
		$('.scrollTo').click(function(e){
			e.preventDefault();

			$('html, body').animate({
				scrollTop: $(this.hash).offset().top
			}, 1000);

		});
				
		$(".publications ul li").hover(function(e){
			$("img", this).hide();
			$(".publicationInfo", this).show();
		},function(){
			$("img", this).show();
			$(".publicationInfo", this).hide();
		});
	}
	
	/*
	Script: Top
	*/
	if( $("a.top").length )
	{
		$('a.top').click(function(e){
			e.preventDefault();
			
			$('html, body').animate({
				scrollTop: 0
			}, 1000);
		
		});
	
	}
	
	/*
	 * Script: Tooltip
	 */
	if( $("a.tooltip, a.imgtip, area.tooltip").length )
	{
		var xOffset = 0;
		var yOffset = 20;
		var content = '';

		$("a.tooltip, a.imgtip, area.tooltip").hover(function(e){
			content = $(this).attr("title");
			$(this).removeAttr("title");
			
			if($(this).hasClass('imgtip')){
				$("body").append("<div id='imgtip'><div id='imgtipContainer'><img src="+ content +" /></div><div class='arrow'></div>"+"</div>");
				$("#imgtip").css("top",(e.pageY - xOffset) + "px")
								.css("left",(e.pageX + yOffset) + "px")
								.fadeIn("fast");
			}else{
				$("body").append("<div id='tooltip'><div id='tooltipContainer'><p>"+ content +"</p></div><div class='arrow'></div>"+"</div>");
				$("#tooltip").css("top",(e.pageY - xOffset) + "px")
								.css("left",(e.pageX + yOffset) + "px")
								.fadeIn("fast");								 
			}
			
		},function(){
			$(this).attr("title", content);
			
			if($(this).hasClass('imgtip')){
				$("#imgtip").remove();
			}else{
				$("#tooltip").remove();
			}
		});
		
		
		$("a.imgtip").mousemove(function(e){
			$("#imgtip").css("top", ( (e.pageY - Math.round($("#imgtip").height() ) ) - yOffset) + "px")
						 .css("left", ( (e.pageX - ( Math.round($("#imgtip").width()/2 ) ) ) - xOffset) + "px");
		});
		
		$("a.tooltip, area.tooltip").mousemove(function(e){
			$("#tooltip").css("top", ( (e.pageY - Math.round($("#tooltip").height() ) ) - yOffset) + "px")
						 .css("left", ( (e.pageX - ( Math.round($("#tooltip").width()/2 ) ) ) - xOffset) + "px");
		});
		
	}
		
	/*
	Script: Contact
	*/
	if( $("#contact").length )
	{
		$(":text").each(function(){
		    if ($(this).val() === ""){
		        $(this).labelify();
		    }
		});
	}
	
	/*
	Script: Books
	*/
	if( $("#books").length )
	{
		var
			speed = 1000,	// animation speed
			$wall = $('#books-list'),

		masonryOptions = {	// initial masonry options
			//columnWidth: 240, 
			itemSelector: '.book:not(.invis)',
			animate: true,
			animationOptions: {
				duration: speed,
				queue: false
			}
		};

		// run on window.load so we can capture any incoming hashes
		$(window).load(function(){
			// run masonry on start-up to capture all the boxes we'll need
			$wall.masonry(masonryOptions);
			if ( window.location.hash ) {
				// get rid of the '#' from the hash
				var possibleFilterClass = window.location.hash.replace('#', '');
				switch (possibleFilterClass) {
				// if the hash matches the following words
				case 'roy-grace' : case 'other' : 
				// set masonry options animate to false
				masonryOptions.animate = false;
				// hide boxes that don't match the filter class
				$wall.children().not('.'+possibleFilterClass)
				.toggleClass('invis').hide();
				// run masonry again, this time with the necessary stuff hidden
				$wall.masonry(masonryOptions);
				break;
			}
		}

		});

		$('#books-filtering a').click(function(){
			var 
				color = $(this).attr('class').split(' ').slice(-1),
				filterClass = '.' + color;
			;
		
			if(filterClass=='.all') {
				// show all hidden boxes
				$wall.children('.invis')
					.toggleClass('invis').fadeIn(speed);
			} else {
				// hide visible boxes 
				$wall.children().not(filterClass).not('.invis')
					.toggleClass('invis').fadeOut(speed);
				// show hidden boxes
				$wall.children(filterClass+'.invis')
					.toggleClass('invis').fadeIn(speed);

			}
			$wall.masonry({ animate: true });
			// set hash in URL
			window.location.hash = color;
			return false;
		});
	}


	/*
	 *	Script: Hall Of Fame
	 */
	if( $("#hall-of-fame").length )
	{
		//Thumbnailer.config.shaderOpacity = 1;
		$.fn.tn3.Gallery.config.autoplay = true;
		var tn1 = $('.mygallery').tn3({
			skinDir:"css/skins",
			imageClick:"fullscreen",
			image:{
				maxZoom:1.5,
				crop:true,
				clickEvent:"dblclick",
				transitions:[{
					type:"blinds"
				},{
					type:"grid"
				},{
					type:"grid",
					duration:460,
					easing:"easeInQuad",
					gridX:1,
					gridY:8,
					// flat, diagonal, circle, random
					sort:"random",
					sortReverse:false,
					diagonalStart:"bl",
					// fade, scale
					method:"scale",
					partDuration:360,
					partEasing:"easeOutSine",
					partDirection:"left"
				}]
			}
		});
	}
	
})(jQuery);
