$(document).ready(function(){$(".single").colorbox();$("a[rel='graphic-gallery']").colorbox();$("a[rel='illus-gallery']").colorbox();$("a[rel='brand-gallery']").colorbox();$("a[rel='web-gallery']").colorbox();$("a[rel='photo-gallery']").colorbox();$("a[rel='motion-gallery']").colorbox();$(".video-gal").colorbox({iframe:true,innerWidth:"75%",innerHeight:"75%"});$('small#view-resume a').click(function(){$('section#cv').fadeTo("slow",0.85);$('body').addClass('fixed');$('#wrapper-content').addClass('all-fixed');$('#nav-first-child').css("visibility","hidden")});$('.close-btn').click(function(){$('section#cv').fadeOut("slow");$('body').removeClass('fixed');$('#wrapper-content').removeClass('all-fixed');$('#nav-first-child').css("visibility","visible")});$('li.work-item').hover(function(){$(this).find('div.work-caption').stop(false,true).css("visibility", "visible").fadeIn("fast")},function(){$(this).find('div.work-caption').stop(false,true).fadeOut("slow")});$("nav").hover(function(){$(this).find("li.fade-option").stop().animate({left:"25",opacity:1},"fast").css("visibility","visible")},function(){$(this).find("li.fade-option").stop().animate({left:"0",opacity:0},"meddium").css("display","inline")})
	
		var wrapper = $(".work-caption");
		var theItem =$(".work-caption h3");
		var contHeight=$(wrapper).height();
		var contMiddle=contHeight/2;
		var titleMiddle=$(".work-caption h3").height()/2;
		var theMiddle=contMiddle-titleMiddle;

		$('.work-caption').each(function(i) {
			var wh = $(this).height();
			var ih = $('h3', this).height();
			
			if (wh > ih) {
			$('h3', this).css('margin-top', (wh-ih)/2 + 'px');
			} else {
			$('h3', this).css('margin-top',0);
			}
		});
		
		var $paneTarget = $('#list-works');
		$('.works-filters li a').click(function(){
		var categoria = $(this).attr('href').substr(1);
		var $target = $paneTarget.find('li:eq(0)');
		$paneTarget.stop().scrollTo( $target , 800 );
		addBreakClass(categoria);
		});
		
		function addBreakClass(target) {
			var visibles = 0;	
			$('#list-works li').each(function() {	
			$(this).removeClass('break');	
			if (target=='all' || $(this).hasClass(target)) {
			visibles++;
			if (visibles==1 || visibles%20 == 0 ) $(this).addClass('break');
			}	
			});
		}
		addBreakClass('all');
		
//FADE PRELOADING IMAGES FUNCTION
$.fn.imgFolioloader = function(options){    
       
    // Default options
    var defaults = {
        delay:100,
        preload_parent:"a",
        ondone:function(){ },
        oneachload:function(image){  },
        fadeSpeed:300,
        preloadClass: 'preload'
    },
        
    // Extend our default options with the options the user passes in
    opts = $.extend(defaults, options),
        
    // Cache & hide the images passed to our plugin
    images = $(this).css({"visibility":"hidden",opacity:0}),
        
    // Prepend our icon to the body to make it load fast
    icon = $("<img />",{
        
            id : 'loadingicon',
            src : opts.icon
        
        })
        .hide()
        .prependTo("body"),
    
    // Create a new array to store the number completed
    completed = new Array(images.length),
    
    // How long until the image should run
    delaySum = 0,
    
    // The function that will run our nice animation effect!
    runAnimation = function(image) {
        
        var $image = $(image);
        
        $image
            .css("visibility","visible")       
            .animate({opacity:1}, opts.fadeSpeed, function() {
                $image.parent()
                    .removeClass('preload');
            });        
     
        
    };
    
    // Style the Parent
    images
        .parent()
            .addClass(opts.preloadClass);

    // But here's where my plugin really get's different than the NetTuts version! 
    // Instead of waiting on running a checkInterVal
    // of our images, we can simply rely on the load events!  
    images.each(function() {
        var $this = $(this);
        $this
            .load(function() {                
                $this
                    .css("visibility","visible")       
                    .delay(delaySum)
                    .animate({opacity:1}, opts.fadeSpeed, function() {
                        $this.parent()
                        .removeClass('preload');
                    });
               
                delaySum += opts.delay;
            });
    });        
    
    // Once again, instead of checking with a timeout,
    // we use the load event and remove the icon
    icon.load(function() { icon.remove(); });
       
};

// Run it!
targetImages = $('#list-works a img');
targetImages.imgFolioloader();


	//DETECT iPad/iPhone devices for manage fixed position elements
	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
	if (agentID) {
        $('#theme-wrapper').delay('200').remove();
 		$('section#cv{').css('width','120%');
		$('#cboxOverlay').css({'width': '110%', 'height': '250%'});
	}
//$( window ).scroll( function ( ) { $( "#theme-wrapper" ).css( "top", ( $( window ).height() + $( document ).scrollTop() - 90 ) +"px" );  } );

	//	//stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
//if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
//{
//     $("#theme-wrapper").css("position", "static");
//};

});
