jQuery(function($) {
	
	var content = 'portfolio';
	
    Shadowbox.init();
	
    /*$("#picture ul").cycle({
        fx: 'fade',
        speed: 4000,
        width: '800px',
        pause: 1,
        timeout: 2500
    });*/

    $("#backward").mouseover(function() {
        $(this).css({
            "opacity": "0.6"
        });
    }).mouseout(function() {
        $(this).css({
            "opacity": "0.2"
        });
    }).click(function() {
        if (currentpicture == 1) {
            currentpicture = totalpictures;
            $("#current").html(totalpictures);
        } else {
            currentpicture = currentpicture - 1;
            $("#current").html(currentpicture);
        }
		var caption = $("#pictures ul li:nth-child("+currentpicture+") a div.caption").html();
		$("#caption").html(caption);
        return false;
    });
    $("#forward").mouseover(function() {
        $(this).css({
            "opacity": "0.6"
        });
    }).mouseout(function() {
        $(this).css({
            "opacity": "0.2"
        });
    }).click(function() {
        if (currentpicture == totalpictures) {
            currentpicture = 1;
            $("#current").html(currentpicture);
        } else {
            currentpicture = currentpicture + 1;
            $("#current").html(currentpicture);
        }
		var caption = $("#pictures ul li:nth-child("+currentpicture+") a div.caption").html();
		$("#caption").html(caption);
        return false;
    });

    $("#footerportfolio").click(function() {
        if (content != 'portfolio') {
            $("#" + content).slideUp('slow', function() {
				$('#contact').load('contact.html');
			});
            $("#right-col").slideUp('slow',function() {
                $("#picture").slideUp('slow',function() {
					$("#footer ul li>a").css({"font-weight": "normal"});
                    $("#footer").animate({
                        borderTop: '0',
                        marginLeft: '0px',
                        top: '7px'
                    },1000,function() {
                        $(this).css({
                            "border-top": "none",
                            "padding": "5px 0"
                        });
                        // "border-bottom":"1px solid #000000",
                        $("#portfoliosub").fadeIn('slow',function() {
                            loadPortfolio('01_playa');
                        });
                    });
                });
            });
            content = 'portfolio';
            $("#footer").addClass("footer");
        }
        return false;
    });

    $("#portfoliosub a").click(function() {
        loadPortfolio($(this).attr("id"));
        return false;
    });

    $("#footercontact").click(function() {
        loadContent('contact');
        return false;
    });
    $("#footerbio").click(function() {
        loadContent('bio');
        return false;
    });
    $("#footerabout").click(function() {
        loadContent('about');
        return false;
    });
	
	$("#submitcform").live("click",function(){
		$.get("form.php", $("#contactform").serialize(), function(data){
			$("#contact").html( data );
		});
		return false;
	});
	loadPortfolio('01_playa');
	
	
	
	    // retrieve list of slides from server 
	    $.getJSON('slidelist.php', startSlideshow); 

	    function startSlideshow(slides) { 
	        /* server returns an array of slides which looks like this: 
	        [ 
	            'images/beach2.jpg', 
	            'images/beach3.jpg', 
	            'images/beach4.jpg', 
	            'images/beach5.jpg', 
	            'images/beach6.jpg', 
	            'images/beach7.jpg', 
	            'images/beach8.jpg' 
	        ] 
	        */ 

	        var totalSlideCount = 1 + slides.length; 

	        var $slideshow = $('#picture'); 

	        // markup contains only a single slide; before starting the slideshow we  
	        // append one slide and prepend one slide (to account for prev/next behavior) 
	        $slideshow.prepend('<img src="'+slides.pop()+'" />'); 
	        $slideshow.append('<img src="'+slides.shift()+'" />'); 
			
	        // start slideshow 
	        $('#picture').cycle({ 
	            fx: 'fade', 
	            startingSlide: 1,  // start on the slide that was in the markup 
	            timeout:  2500, 
	            speed:    4000,
	            before:   onBefore 
	        }); 

	        function onBefore(curr, next, opts, fwd) { 
	            // on Before arguments: 
	            //  curr == DOM element for the slide that is currently being displayed 
	            //  next == DOM element for the slide that is about to be displayed 
	            //  opts == slideshow options 
	            //  fwd  == true if cycling forward, false if cycling backward 

	            // on the first pass, addSlide is undefined (plugin hasn't yet created the fn yet) 
	            if (!opts.addSlide) 
	                return; 

	            // have we added all our slides? 
	            if (opts.slideCount == totalSlideCount) 
	                return; 

	            // shift or pop from our slide array  
	            var nextSlideSrc = fwd ? slides.shift() : slides.pop(); 

	            // add our next slide 
	            opts.addSlide('<img src="'+nextSlideSrc+'" />', fwd == false); 
	        }; 
	    }; 
	
	
	
});

loadContent = function(name) {
    if (content != name) {
        if (content == 'portfolio') {
            $("#navigation").fadeOut('slow');
			$("#counter").fadeOut('slow');
            $("#pictures").fadeOut('slow',
            function() {
                $("#portfoliosub").fadeOut('fast');
                $("#footer").animate({
                    top: '589px'
                },500,function() {
                    $("#footer").animate({
                        borderTop: '1px solid #000000',
                        marginLeft: '450px',
                        width: '535px',
                    },1000,function() {
                        $(this).css({
                            "border-bottom": "none",
                            "padding": "5px 0 0"
                        });
                    });
					$("#footer"+name).css({"font-weight":"bold"});
                    $("#footerportfolio").css({"font-weight": "normal"});
                });
                $("#picture").slideDown('slow',
                function() {
                    $("#right-col").slideDown('slow',
                    function() {
                        $("#" + name).slideDown('slow');
                        content = name;
                    });
                });
            });
        } else {
            $("#" + content).slideUp('slow',function() {
                $("#" + name).slideDown('slow',function(){
					$("#content").css("border-bottom","1px solid #000000")
				});
                content = name;
            });
        }
    }
}

loadPortfolio = function(name) {
    $.ajax({
        type: "POST",
        url: "getnext.php",
        data: "name=" + name,
        success: function(msg) {
            $("#navigation").fadeOut('slow');
			$("#counter").fadeOut('slow');
            $("#pictures").fadeOut('slow',
            function() {
                $("#pictures").html(msg);
                Shadowbox.setup("a.lightbox-link", {
                    overlayOpacity: "0.9"
                });
                $("#pictures ul").cycle({
                    fx: 'fade',
                    speed: 1500,
                    width: '800px',
                    pause: 1,
                    timeout: 0,
                    prev: '#backward',
                    next: '#forward'
                });
                currentpicture = 1;
                totalpictures = $("#pictures ul li").length;
                $("#current").html(1);
                $("#total").html($("#pictures ul li").length);
                $("#navigation").fadeIn('slow');
				$("#counter").fadeIn('slow');
				
				var caption = $("#pictures ul li:first a div.caption").html();
				$("#caption").html(caption);
				
            }).fadeIn('slow');
            $("#footerportfolio").css({
                "font-weight": "bold"
            });
            $("#portfoliosub a").css({"font-weight": "normal"});
            $("#" + name).css({
                "font-weight": "bold"
            });
        }
    });
}

window.onload = function() {
    // set up all anchor elements with a class to work with Shadowbox
    Shadowbox.setup("a.lightbox-link", {
        overlayOpacity: "0.9"
    });
};
