/*********************************************************
Title: Site Specific Functions - DEV VERSION
Author: tim@texturedpixel.com
*********************************************************/

/*********************************************************
ACTIVATE FLYOUTS: activates flyout menus on main header menu.  Uses jQuery to load, no need to call the function.
PARAMS: none
*********************************************************/	
$(function() {
	$('header nav .mainnav').hover(
		function() {
			$(this).children('.subnav').stop(true,true).slideToggle(300);
			$(this).addClass('navActive');
		},
		function() {
			$(this).children('.subnav').stop(true,true).slideToggle(300);
			$(this).removeClass('navActive');
		}
	)
});
/*********************************************************
ACTIVATE FOOTER TOOLTIPS: activates site's footer tooltips.  Uses jQuery to load, no need to call the function.
PARAMS: none
*********************************************************/	
$(function() {
	$("footer [title]").tooltip({effect:'fade',fadeInSpeed: 500,fadeOutSpeed: 0,offset: [-5,0]});
});

/*********************************************************
PORTFOLIO VIEWER: activates portfolio viewer - calls all related functions. 
- Requires jQuery 1.4.4+
- run after imageloader completes
PARAMS: none
*********************************************************/	
function initiatePortfolioViewer() {
	/*remove loader tags*/
	$('.loader').fadeOut(1000, function() {
    	$('.loader').remove();
    	$('#portfolioViewer nav').fadeIn(300, function() {
    		$('#portfolioViewer-mainImg').fadeIn(300);
    		runPortfolioViewer();
    	});
  	});
}
/*********************************************************
RUN PORTFOLIO VIEWER: activates portfolio viewer - calls all related functions. 
- Requires jQuery 1.4.4+
- run after imageloader completes
PARAMS: none
*********************************************************/	
function runPortfolioViewer() {
	$('#portfolioViewer nav ul li a').click(function() {
		//get attributes
		var imgHref = $(this).attr('href'),
			imgTitle = $(this).find('img').attr('title');

		/*make sure any current images are removed*/
		$('#portfolioViewer-mainImg').html('');
		
		/*show loader*/
		$('#portfolioViewer-mainImg').html('<div class="loader" style="display:none; margin:0 auto; padding-top:150px"><img src="store/img/loader.gif" /></div>');
		$('#portfolioViewer-mainImg .loader').fadeIn(100);
		
		/*add img*/
		$('#portfolioViewer-mainImg').append('<div id="small" style="display:none;"><img class="mainImg" src="'+imgHref+'" /><p>'+imgTitle+'</p></div>');
		
		/*load img*/
		$("#portfolioViewer-mainImg .mainImg").imagesLoaded(function(){
			$('#portfolioViewer-mainImg .loader').fadeOut(300, function() {
    			$('#portfolioViewer-mainImg .loader').remove();
    			$('#portfolioViewer-mainImg #small').fadeIn(300);
  			});
		});
		return false;
	});
}

/*********************************************************
NAME: IMG LOADING FUNCITON
$('img.photo',this).imagesLoaded(myFunction)
- mit license. paul irish. 2010.
- callback function is passed the last image to load as an argument, and the collection as `this`
- needed because .load() doesn't work on cached images
PARAMS: callback function
*********************************************************/
$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len   = elems.length;  
  elems.bind('load',function(){
      if (--len <= 0){ callback.call(elems,this); }
  }).each(function(){
     if (this.complete || this.complete === undefined){
        var src = this.src;
        this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
        this.src = src;
     }  
  }); 
  return this;
};

/* ~TG~ */
