initURL = location.href;
if (initURL.indexOf("#") > -1) {			  
    initURL = initURL.split("#/")[1];
    window.location = "/"+initURL;
}

$(document).ready(function(){
    if($('#hideNavigation').length > 0){
        hideNavigation();
    } else{
        $('#container').animate({ opacity:0}, 0, function(){
            $('#container').animate({opacity:1, "marginTop":parseInt($("#banner").height())+"px"}, 1000, "easeOutExpo");
        });
    }
    
    $('#footerApps').animate({"height":"toggle"}, 0);
    $('.apps').live('click', function(){
        $('#footerApps').animate({"height":"toggle"}, 500);
    });
    
    $('#footerApps a').live('click', function(){
        $('#footerApps').animate({"height":"toggle"}, 500);
    });
    
    $('.showNavigation').live('click', function(){
        id = $(this).attr('id').split('showNavigation');
        showNavigation(id[1], 1);
    });
    
    $('.hideNavigation').live('click', function(){
        hideNavigation();
    });
    
    
    $('.loadVideo').live('click', function(){
       if($(window).width() >= 960){
       $(this).load($(this).attr('href'), function(){
           initButtons();
           $(this).unbind();
           $(this).click(function(){
               return false;
           });
       }); 
       $('#darken').fadeIn(2000);
       } else {
            window.location = "/uploads/blog/"+$(this).attr('id')+".m4v";
       }
       return false;
    });
    initButtons();
    
    $('#darken').live('click', function(){
        $('#iframe').stop().fadeOut("slow").remove();
        $(this).stop().fadeOut("slow");
    });
    
    $('.iframeLink').live('click', function(){
       $('#darken').fadeIn("slow");
       
       var dimensions = $(this).attr('id').split(';');
       if(dimensions[0]){
           width = dimensions[0]+"px";
           halfWidth = dimensions[0]/2+"px";
       } else{
           width = "80%";
           halfWidth = "40%";
       }
       if(dimensions[1]){
           height = dimensions[1]+"px";
       } else{
           height = "80%";
       }
       $('#content').append("<iframe id='iframe' frameborder='0' width='"+width+"' height='"+height+"' style='margin-left:-"+halfWidth+";'></iframe>");
        
            $('iframe#iframe').attr('src', $(this).attr('href'));

    $('iframe#iframe').load(function() 
    {
           $(this).animate({"top":"10px"}, 1000, "easeOutBack");
           $(this).contents().find('#banner, #footer').remove();
           $(this).contents().find('#container').css({ "margin":"auto"});
           $(this).contents().find('body').css({"background":"#fff"});
           $(this).contents().find('#holder').css({"padding":"0"});

    });




       return false;
    });
    
    displayMessage();
    
    $('.siteMessage').animate({"bottom":"50px", opacity:0.8}, 800, "easeOutBounce",  function(){
        $(this).animate({opacity:0.9}, 3000, function(){
            $(this).animate({ "left":"-100%", opacity:0}, 1000, "easeInBack", function(){
                $(this).remove();
            });
        });
    });
    
    $('.siteMessage').live('click', function(){ 
        $(this).stop().animate({ width:0, opacity:0}, 500, function(){
            $(this).remove();
        });
    });

    $('.errorMessage').animate({"top":$('#banner').height()+"px", opacity:0.8}, 1000, "easeOutBounce",  function(){
        $(this).animate({opacity:1}, 2800, function(){
            $(this).animate({ "left":"-100%", opacity:0}, 1000, "easeInBack", function(){
                $(this).remove();
            });
        });
    });
    
    $('.confirm').live('click', function(){
      var answer = confirm('Are you sure?');
      return answer;
    });  
    
    $('.page, .link').live('click', function(){
        href = $(this).attr('href');

        var title = href.split("/");
        title = (title[title.length-1].charAt(0).toUpperCase() + title[title.length-1].slice(1)).replace(/-/g, " ");

        loadPage(title, href);
        $(".selected").removeClass('selected');
        $(this).addClass('selected');

        return false;
    });
});

function displayMessage(){
    $('.siteMessage').animate({"bottom":"50px", opacity:0.8}, 800, "easeOutBounce",  function(){
        $(this).animate({opacity:0.9}, 3000, function(){
            $(this).animate({ "left":"-100%", opacity:0}, 1000, "easeInBack", function(){
                $(this).remove();
            });
        });
    });
}

function hideNavigation(){
    $('.hideNavigation').remove();
    var expo = $('#banner').height();
    $('#banner').animate({"marginTop":"-"+(expo*2)+"px"}, 1800,  "easeInOutQuint");
    $('#container').animate({"marginTop":"0px"}, 1800,  "easeInOutQuint");
    $('#backgroundContainer').animate({"paddingTop":(expo/2)+"px", "marginTop":"-"+(expo/2)+"px"}, 2000,  "easeInOutQuint", function(){
        $('#container').append('<a id="showNavigation'+expo+'" class="showNavigation">Show navigation</a>');
        $('.showNavigation').fadeOut(0, function(){
            $(this).fadeIn();
        });
    });
}

function showNavigation(expo, button){
    if($('.showNavigation').length > 0){
        $('.showNavigation').remove();
    }
    $('#banner').animate({"marginTop":"0px"}, 1800,  "easeInOutQuint");
    $('#container').animate({"marginTop":expo+"px"}, 1800,  "easeInOutQuint");
    $('#backgroundContainer').animate({"paddingTop":"0px", "marginTop":"0px"}, 2000,  "easeInOutQuint", function(){
        if(button){
            $('#container').append('<a id="hideNavigation'+expo+'" class="hideNavigation">Hide navigation</a>');
            $('.hideNavigation').fadeOut(0, function(){
                $(this).fadeIn();
            });
        }
    });
}

function initSlideshow(){
    $('.slideshow').stop(true, true).animate({ left:"110%", opacity:0}, 0);
    
    var highest;
    var currentHighest = 0;
    $(".slideshow").each(function(){
      var currentHeight = $(this).height();
      if(currentHeight > currentHighest){
         currentHighest = currentHeight;
         highest = $(this).height();  
      }    
    });
    $('#slideshowContainer').css({"height":highest+"px"});
    
    advanceSlideshow(1, $('.slideshow').length);
}

function advanceSlideshow(slideNumber, totalSlides){
    if(slideNumber > totalSlides){
        slideNumber = 1;
    }
    
    $('#slideshow'+slideNumber).stop(true, true).animate({ left:0, opacity:1 }, "slow", "easeOutExpo", function(){
        $(this).stop().animate({opacity:1}, 3000, function(){
            $(this).stop().animate({ left:"-110%", opacity:1 }, "slow", "easeInBack", function(){
                $(this).stop().animate({ left:"110%", opacity:0}, 0, function(){
                    advanceSlideshow(slideNumber+1, totalSlides);
                });
                
            });
        });
    });
}

function initButtons(){
    initSlideshow();
    $('.accordion').accordion({ collapsible: true, autoHeight: false, active: false });
    $('.tabs').tabs();
    $('.button, input[type=submit]').button();
    if($("#picker").length > 0){
        $.farbtastic("#picker").linkTo('#profileColour, .colour').setColor($('#initColour').html());
    }

}

function loadPage(title, href){
    if(!title){ title = "Home"; }

    mainTitle = document.title.split("|");
    document.title = mainTitle[0]+" | "+title;
    window.location.hash = href;
    
    $('html, body').animate({scrollTop:0}, 'slow');
    
    $('#loading').fadeIn("fast", function(){
        $('#container').animate({opacity:0, "marginTop":"-1000px"}, "slow", "easeInExpo", function(){
            $('#holder').load(href+" #container", function(){
                initButtons();
                $('#container').animate({"marginTop":"1500px", opacity:0}, 0, function(){
                    $('#loading').fadeOut("slow");
                    $('#container').stop().animate({opacity:1, "marginTop":parseInt($("#banner").height())+"px"}, 1000, "easeOutCirc");
                });
                
                if($('#hideNavigation').length == 0){
                    showNavigation($('#banner').height(), 0);
                } else{
                    hideNavigation();
                }
            });
        });  
    });
}
