$(document).ready(function() {       
    
    $('.scrollable').css('overflow', 'hidden');
    $('.item').css('float', 'left');
    
    $.loadImages([
        '/img/Title-01-pg-sub-01-pg-image.jpg', '/img/Title-01-pg-sub-02-pg-image.jpg', '/img/Title-01-pg-sub-03-pg-image.jpg', '/img/Title-01-pg-sub-04-pg-image.jpg'],
        '/img/Title-02-pg-sub-01-pg-image.jpg', '/img/Title-02-pg-sub-02-pg-image.jpg', '/img/Title-02-pg-sub-03-pg-image.jpg', '/img/Title-02-pg-sub-04-pg-image.jpg', '/img/Title-02-pg-sub-05-pg-image.jpg', '/img/Title-02-pg-sub-06-pg-image.jpg',
        '/img/Title-03-pg-sub-01-pg-image.jpg', '/img/Title-03-pg-sub-02-pg-image.jpg', '/img/Title-03-pg-sub-03-pg-image.jpg', '/img/Title-03-pg-sub-04-pg-image.jpg',
        '/img/About-Us-pg-sub-01-pg-image.jpg', '/img/About-Us-pg-sub-02-pg-image.jpg', '/img/About-Us-pg-sub-03-pg-image.jpg', '/img/About-Us-pg-sub-04-pg-image.jpg',
        '/img/News-Resources-pg-sub-01-pg-image.jpg', '/img/News-Resources-pg-sub-02-pg-image.jpg', '/img/News-Resources-pg-sub-03-pg-image.jpg',
        '/img/AdamColyer.jpg', '/img/AndyBerry.jpg', '/img/ColinGray.jpg', '/img/GemmaShipley.jpg', '/img/JennyDearman.jpg', '/img/MariaOpuni.jpg', '/img/MarkSmith.jpg', '/img/NicolaHopkins.jpg', '/img/RobertNorman.jpg', '/img/TracieWebster.jpg'
    );
    
    if(document.getElementById('subHead')){
        
        for(var i = 0; i < $('.subMenu li').length; i++) {
        
            $('.subMenu li a')[i].href = '#' + i;
            
        }
    }
    /*
    if(document.getElementById('caseStudyLinks')){
        
        for(var i = 0; i < $('#caseStudyLinks p').length; i++) {
            
            hash = i+4;
        
            $('#caseStudyLinks a')[i].href = '#' + hash;
            
        }
    }
    */

// main vertical scroll
$("#main").scrollable({

    // basic settings
    vertical: true,
    size: 1,
    clickable: false,  
    keyboard: 'static',

    onStart : function(event, i) {
        $(".tabs").hide();
    },
    onSeek : function(event, i) {
        
        for(var i = 0; i < $('#flowtabs li').length; i++) {
            horizontal.eq(i).scrollable().seekTo(0);   
        }
        
        $(".tabs").show();  
    },
    globalNav: true 

})
.navigator({ 
    // ul.tabs now functions as our navigator 
    navi:'.subMenu ul',
    history: false
});   

var horizontal = $(".scrollable").scrollable({
    clickable: false, 
    size: 1,
    history: false   
});

$('.page .tabs li a').each(function(index) {
    
    tabID = (this.id);
    
    if(tabID.substring(0, 4) == 'next'){ 
   
        tabnum = parseInt(tabID.substring(4, 6));  
        
        //tabnum = 1
        
        //alert(tabnum);
        var pgCount = new Array();
        
        pgCount[tabnum] = horizontal.eq(tabnum).scrollable().getPageAmount(pgCount[tabnum]);
        $('#pgCount' + tabnum).html(horizontal.eq(tabnum).scrollable().getPageAmount());
        $('#previous' + tabnum).css('visibility', 'hidden'); 
        
    }
    
});



function nextClick(num, pgNum){
   
   num = parseInt(num);
   tempNum = num + 1;
   var pgCount = new Array();  
   //pgNumCurrent = eval("pgCount" + tempNum);
   pgNumCurrent = pgCount[num];

   if(pgNum <= 0)
        $('#previous' + num + '').css('visibility', 'hidden');
    else
        $('#previous' + num + '').css('visibility', 'visible');
    if(pgNum+1 >= pgNumCurrent)
        $('#next' + num + '').css('visibility', 'hidden');
    else
        $('#next' + num + '').css('visibility', 'visible');  
        
    location.href = location.href + '-1';
    //alert(location.href);
}
 
function previousClick(num, pgNum){
   
   num = parseInt(num);     
   tempNum = num + 1;
   var pgCount = new Array();  
   //pgNumCurrent = eval("pgCount" + tempNum);
   pgNumCurrent = pgCount[num];  
    
   if(pgNum <= 0)
        $('#previous' + num + '').css('visibility', 'hidden');
    else
        $('#previous' + num + '').css('visibility', 'visible');
    if(pgNum+1 >= pgNumCurrent)
        $('#next' + num + '').css('visibility', 'hidden');
    else
        $('#next' + num + '').css('visibility', 'visible');        
}

    $('.tabs a').click(function() {
        
        linkID = this.id;
        
        if(linkID.substring(0, 4) == 'next'){
        
            linknum = linkID.substring(4, 6);
            pgNum = moveForward(linknum);
            nextClick(linknum, pgNum);
            
        }
        
        if(linkID.substring(0, 8) == 'previous'){
        
            linknum = linkID.substring(8, 10);
            pgNum = moveBack(linknum);
            previousClick(linknum, pgNum);
            
        }
        
    });

    $('.subMenu ul a').click(function() {
        
        
        
        for(var i = 0; i < $('#flowtabs li').length; i++) {
            resetPage(i);  
        }
    });

function resetPage(page){
    horizontal.eq(page).scrollable().getConf().speed = 1;
    horizontal.eq(page).scrollable().begin();
    horizontal.eq(page).scrollable().getConf().speed = 400; 
   $('#pgCurrent' + page).html(1);  
   $('#next' + page + '').css('visibility', 'visible'); 
   $('#previous' + page + '').css('visibility', 'hidden');
}

// Moves page left-right
function moveBack(page){
    //alert(page);
    horizontal.eq(page).scrollable().prev(); 
    pgNum = horizontal.eq(page).scrollable().getIndex();
    $('#pgCurrent' + page) .html(pgNum+1); 
    
    return pgNum;  
}

// Moves page right-left  
function moveForward(page){
    horizontal.eq(page).scrollable().next(); 
    pgNum = horizontal.eq(page).scrollable().getIndex();
    $('#pgCurrent' + page) .html(pgNum+1);   
    
    return pgNum;  
}


    $(window).bind('hashchange', function () {
        hash = window.location.hash;
        hash = hash.replace(/#/, '');
        $('.subMenu li').css('background', 'transparent');   
        
        $("#main").scrollable().seekTo(hash); 
    });
    
    if(window.location.hash){
        
        hash = window.location.hash;
        hash = hash.replace(/#/, '');
        
        $("#main").scrollable().seekTo(hash); 
        
    }
    
    $(".subMenu li").click(function() {
        
        obj = $(this).attr('id');
        
        // Set the background img
        if($('#' + obj + ' a').attr('id') != ''){
            
            img = $('#' + obj + ' a').attr('id') + '.jpg';
            
            $('.backgroundSideImgNew').css('display', 'none'); 
            $('.backgroundSideImgNew').css('background-image', 'url(/img/' + img + ')'); 
            $('.backgroundSideImgNew').fadeIn(500, function(){  
            $('.backgroundSideImgNew').css('background-image', 'url(/img/' + img + ')'); 
            $('.backgroundSideImgOld').fadeOut(1000, function(){
                    // in the callback  
                    $('.backgroundSideImgOld').css('background-image', 'url(/img/' + img + ')');
                    $('.backgroundSideImgOld').css('display', 'block');
                    //$('.backgroundSideImgOld').css('display', 'none');
                });
            });
                
        }
        
        anchor = $('#' + obj + ' a').attr('href'); 
        window.location.href = anchor; 
    
    });


    $("#caseStudyLinks a").click(function() {  
        id = this.id;
        seekNum = id.replace(/cs-/, '');
        
        if(this.id == 'cs-' + 'seekNum')
        {
            $("#main").scrollable().seekTo(seekNum);  
            return false;
        }
        else
        {
            return true;
        }
    });  

    $("#timeResourceLinks a").click(function() {  
        id = this.id;
        seekNum = id.replace(/cs-/, '');
        $("#main").scrollable().seekTo(seekNum);
        $('#subHead').css('background', 'transparent');  
        return false;
    }); 
    
    $(".allCaseStudy a").click(function() {  
        $("#main").scrollable().seekTo(3);  
        return false;
    }); 
    
    $(".allNews a").click(function() {  
        $("#main").scrollable().seekTo(0);  
        return false;
    });  
    
    $(".allTeam a").click(function() {  
        $("#main").scrollable().seekTo(3);  
        
        $('.backgroundSideImgNew').css('display', 'none');   
        $('.backgroundSideImgNew').css('background-image', 'url(/img/About-Us-pg-sub-01-pg-image.jpg');    
        
        $('.backgroundSideImgNew').fadeIn(500, function(){  
        $('.backgroundSideImgNew').css('background-image', 'url(/img/About-Us-pg-sub-01-pg-image.jpg'); 
        $('.backgroundSideImgOld').fadeOut(1000, function(){
                // in the callback  
                $('.backgroundSideImgOld').css('background-image', 'url(/img/About-Us-pg-sub-01-pg-image.jpg');
                $('.backgroundSideImgOld').css('display', 'block');
                //$('.backgroundSideImgOld').css('display', 'none');
            });
        });
        
        return false;
    });  
    
    $("#newsLinks a").click(function() { 
    
        id = this.id;
        seekNum = id.replace(/ns-/, '');
        $("#main").scrollable().seekTo(seekNum);  
        return false;
    });  
    
    $("#ourTeam a").click(function() {  
        id = this.id;
        seekNum = id.replace(/ot-/, '');
        $("#main").scrollable().seekTo(seekNum);  
        
        obj = $(this).attr('id');
        
        // Set the background img
        if($('#' + obj + ' a').attr('id') != ''){
            
            img = $('#' + obj + ' span').attr('id') + '.jpg';
            
            $('.backgroundSideImgNew').css('display', 'none'); 
            $('.backgroundSideImgNew').css('background-image', 'url(/img/' + img + ')'); 
            $('.backgroundSideImgNew').fadeIn(500, function(){  
            $('.backgroundSideImgNew').css('background-image', 'url(/img/' + img + ')'); 
            $('.backgroundSideImgOld').fadeOut(1000, function(){
                    // in the callback  
                    $('.backgroundSideImgOld').css('background-image', 'url(/img/' + img + ')');
                    $('.backgroundSideImgOld').css('display', 'block');
                    //$('.backgroundSideImgOld').css('display', 'none');
                });
            });
                
        }
        
        //anchor = $('#' + obj + ' a').attr('href'); 
        //window.location.href = anchor;         
        
        
        return false;
    });  
    

});


