$(document).ready(function() {  

    $.fn.extend({
        
        // Creates the page navigation slide fade animation
        pageNavigation:function(){
            return this.each(function(){
                var nav = $(this);
                // add current class to home navigation 
                $("li:first",nav).addClass('current');
                $(this).find('a').each(function(){                  
                    $(this).click(function(){
                        // dirty hack (by me)
                        if($(this).attr('href') == "http://blog.gabrielgciafdez.com"){
                            $(location).attr('href',"http://blog.gabrielgciafdez.com/");
                        }
                        if($(this).attr('href') == "mailto:info@gabrielgciafdez.com"){
                            $(location).attr('href',"mailto:info@gabrielgciafdez.com");
                        }else{
                            // find current visible page
                            var visibleId = '#'+$('#content .page:visible').attr('id');
                            // find next page to show
                            var page =  $($(this).attr('href'));
                            // get height of next page to show
                            var h = page.outerHeight(true);
                        
                            if(visibleId!=$(this).attr('href')){
                                // remove class .current on all navigation elements
                                $("li",nav).removeClass("current");
                                // add .current class to acutal item
                                $(this).parent().addClass('current');                       
                            
                                // start the animation
                                $('#content .page').each(function(){ 
                                    $(this).stop().animate({opacity: 0}, 350, function(){ })
                                    // hide current visible page
                                    $(this).hide();
                                    // start height animation of next page
                                
                                    $('#content').stop().animate({ height: h}, 300, function(){
                                        $('#content').css({height: 'auto'});
                                        // fade in next page
                                        page.show().stop().animate({ opacity: 1 }, 350, "swing", function() { 
                                            msieFilterRemove(this); 
                                        });
                                    });
                               });
                           }
                        };
                        
                        return false;
                    });
                });
            })
        },
        
        // creates the animation for the social bookmarks page
        socialAnimation:function(){
            return this.each(function(){
                $(this).hover(
                    function(){
                        $(this).stop(true, false).animate({
                            paddingLeft: 13
                        },250);
                    },
                    function(){
                        $(this).stop(true, false).animate({
                            paddingLeft: 0
                        },250);
                    }
                );
            })
        },
        
        // Functions for the Skin Changer
        /** Delete this after going live **/
        changeBackground:function(){
            return this.each(function(){
                $(this).click(function(){
                    var li = $(this).parent();
                    li.siblings().removeClass('current');
                    li.addClass('current');
                    $("body")
                        .removeClass()
                        .addClass($(this).attr('rel')); 
                    
                    return false;
                });
            })
        },
        
        changeCSS:function(){
            return this.each(function(){
                $(this).click(function(){
                    var li = $(this).parent();
                    li.siblings().removeClass('current');
                    li.addClass('current');
                    $("#cssColor").attr("href", "css/colors/"+$(this).attr('rel')+".css" );             
                    return false;
                });
            })
        }   
        /** Delete ends here **/
        
    })
    
    // Function to remove filter in IE
    function msieFilterRemove(x) { 
        if(jQuery.browser.msie) x.style.removeAttribute('filter'); 
    }   

    // Init Scrolling for gallery items
    $('#gallery .items').cycle({ 
        timeout: 0,  // milliseconds between slide transitions (0 to disable auto advance)
        fx: 'scrollHorz', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        prev: '#galPrev',
        next: '#galNext',
        pause: 1      // true to enable "pause on hover"                               
    }); 
    
    // Init Navigation scripts
    $('#navigation').pageNavigation();
    
    // Show first page on load
    $('#content .page:first').show();
    
    // Show thumbnail overlay on hover
    $('#gallery ul li a').hover(
        function(){
            $(this).find('span').stop(true,true).fadeIn();
        },
        function(){
            $(this).find('span').stop(true,true).fadeOut();
        }
    );
    
    // Init Cufon on defined items
    Cufon.replace('h2, h3');
    
    // Init PrettyPhoto
    $("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded'}); 
    
    // Init Tipsy Tooltips on Elements with class .tipsy - They need to have a title tag
    $('.tooltip').tipsy({gravity: 's', offset: 200 });
    
    // Init hover animation on social links
    $('.networks li').socialAnimation();
    
    // Init contact form
    $('#contactForm').ajaxForm({
        target: '#formResult',
        beforeSubmit: function() {
            $('#formResult').addClass('show');
        },
        success: function() {
            $('#formResult').fadeIn('slow');
        }
    });

    
}); // Close javascript
