$(function() {
	
	$("li.portfolio").hover(function(){
		$("ul.portfolioSub").fadeTo("fast", 1);
	}, function(){
		$("ul.portfolioSub").fadeTo("fast", 0);
	});

    // create image loaded check function
    var is_image_loaded = function(img) {
        // IE
        if(!img.complete) {
            return false;
        }
        // Others
        if(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
            return false;
        }
        return true;
    }
    
    //declare first slide
    var first_slide = $('#slideshow img:first');
    //centre first image
	$("#slideshow, #slideshow img").height($(window).height());
    $('#slideshow img:first').css({marginLeft: ($(window).width() - $('#slideshow img:first').width()) / 2}); 
	$(window).resize(function(){
		$('#slideshow').height($(window).height());
		$('#slideshow img').each(function(){
			$(this).height($(window).height());
		});
		$("#nav a").each(function(i, element){			
			if($(this).hasClass("activeSlide")) {
				$("#slideshow img").each(function(img_i, img_element){
					if(img_i === i) {
						$(img_element).css({marginLeft: ($(window).width() - $(img_element).width()) / 2});
					}
				});
			}
		});
	});
	
    //check if first slide is loaded
    if(is_image_loaded(first_slide.get(0))) { // if it is loaded run slideshow straight away
       $('#slideshow img:first').fadeIn(2000, function() { //fadein first slide
            $('#slideshow').after('<div id="nav" style="display:none;" class="nav">').cycle({ //load cycle
				fx:     'fade',
				speed:   2000,
				timeout: 5000,
				pager:  '#nav',
				before: onBefore,
				containerResize: false,
				slideResize: false,
				fit: 1
            });
		$("#slideshow").css("visibility", "visible");
       }); 
    } else { // else show loading anim until loaded
        first_slide.load(function(e) { // function runs once first slide is loaded
            $('#loading').hide(); // hide loading anim
			
            $('#slideshow img:first').fadeIn(2000, function() { //fadein first slide
               $('#slideshow').show().after('<div id="nav" style="display:none;" class="nav">').cycle({ //load cycle
					fx:     'fade',
					speed:   2000,
					timeout: 5000,
					pager:  '#nav',
					before: onBefore,
					containerResize: false,
					slideResize: false,
					fit: 1
               });
			   $("#slideshow").css("visibility", "visible");
            });
        });
        $('#loading').show(); // show loading anim initially
    };
 
	function onBefore(curr,next,opts) {
		$(next).height($("#slideshow").height());
		$(next).css({
	    	marginLeft: ($(window).width() - $(next).width()) / 2	
	    });
	};
	
	$('.info-toggle').click(function(){
		console.log("test");
     $('#info-box').slideToggle();
   });
});
