
$(document).ready(function() {
    
    //create slideshow
    $('#slideshow').cycle({ 
        fx: 'fade',
        speed: 2000,
   	    timeout: 8000
    });
    
    $('#site_services').hide();
    $('#desktop_services').hide();
    $('#g5_over').hide();
    
    function positionContainer() {
        
        var winH = $(window).height();
        var winW = $(window).width();
        var contH = $('#container').height();
        
        var topMargin = Math.floor((winH - contH) * 0.5);
        
        if (topMargin < 0) {
            topMargin = 5;
        }
        
        $('#container').css('margin-top', topMargin);
        $('#nav').css('top', topMargin);
        $('#right').css('top', topMargin);
        
        //stretch grey bar
        if (winW > 700) {
            $('#right').css('width', (winW - 625));
        }
        
    }
    
    function changeLayout() {
        
        var winH = $(window).height();
        var contH = $('#container').height();
        
        if (winH < contH) {
            $('#nav').css('position', 'static').css('float', 'left');
            $('#right').css('position', 'absolute').css('top', 0).css('left', 625).css('width', 'auto');
            $('#left').css('margin-left', 0);
        }
        
    }
    
    function centreContent() {
        
        var panelHeight = 426;
        //var panelHeight = 401;
        
        if ($('#centre').outerHeight() < panelHeight) {
            $('#centre').css('margin-top', ((panelHeight - $('#centre').outerHeight()) / 2));
        }
        
        if ($('#client_holder').outerHeight() < panelHeight) {
            $('#client_holder').css('margin-top', ((panelHeight - $('#client_holder').outerHeight()) / 2));
        }
        
        if ($('#centre_narrow').outerHeight() < panelHeight) {
            $('#centre_narrow').css('margin-top', ((panelHeight - $('#centre_narrow').outerHeight()) / 2));
        }
        
    }
    
    
    
    positionContainer();
    changeLayout();
    centreContent();
    
    $(window).resize(function() {
        
        positionContainer();
        
    });
    
    //modal windows
    
    $('#serv_link').live('click', function(e) {
        
        e.preventDefault();
        
        $('#shadow').fadeOut('fast');  
        $('#site_services, #desktop_services, a.close').fadeOut('fast');
        
        $('<div id="shadow"></div>').insertAfter('#slideshow');
        $('<a href="#" class="close"><b>x</b> close</a>').insertAfter('#slideshow').hide();
        $('#site_services').insertAfter('#shadow');
        
        $('#shadow').css('opacity', 0.7).fadeIn(1000);
        
        $('a.close').fadeIn('slow', function() {
            
            if ($.browser.msie) {
                this.style.removeAttribute('filter');
            }
            
        });
        
        $('#site_services').addClass('modal').fadeIn('slow', function() {
            
            if ($.browser.msie) {
                this.style.removeAttribute('filter');
            }
            
        });
        
    });
    
    $('#desk_link').live('click', function(e) {
        
        e.preventDefault();
        
        $('#shadow').fadeOut('fast');  
        $('#site_services, #desktop_services, a.close').fadeOut('fast');
        
        $('<div id="shadow"></div>').insertAfter('#slideshow');
        $('<a href="#" class="close"><b>x</b> close</a>').insertAfter('#slideshow').hide();
        $('#desktop_services').insertAfter('#shadow');
        
        $('#shadow').css('opacity', 0.7).fadeIn(1000);
        
        $('a.close').fadeIn('slow', function() {
            
            if ($.browser.msie) {
                this.style.removeAttribute('filter');
            }
            
        });
        
        $('#desktop_services').addClass('modal').fadeIn('slow', function() {
            
            if ($.browser.msie) {
                this.style.removeAttribute('filter');
            }
            
        });
        
    });
    
    $('#g5_link, #g5_link2').live('click', function(e) {
        
        e.preventDefault();
        
        $('#shadow').fadeOut('fast');  
        $('#g5_over, a.close').fadeOut('fast');
        
        $('<div id="shadow"></div>').insertAfter('#slideshow');
        $('<a href="#" class="close"><b>x</b> close</a>').insertAfter('#slideshow').hide();
        $('#g5_over').insertAfter('#shadow');
        
        $('#shadow').css('opacity', 0.7).fadeIn(1000);
        $('a.close').fadeIn('slow', function() {
            
            if ($.browser.msie) {
                this.style.removeAttribute('filter');
            }
            
        });
        
        $('#g5_over').addClass('modal').fadeIn('slow', function() {
            
            if ($.browser.msie) {
                this.style.removeAttribute('filter');
            }
            
        });
        
    });
    
    $('.close').live('click', function(e) {
        e.preventDefault();
        $('#shadow').fadeOut('fast');  
        $('#site_services, #desktop_services, #g5_over, a.close').fadeOut('fast');
    });
      
    $('#shadow, #site_services, #desktop_services, #g5_over').live('click', function() { 
        $('#shadow').fadeOut('fast');  
        $('#site_services, #desktop_services, #g5_over, a.close').fadeOut('fast');  
    });
    
    //ajax
    
    $('#nav li a').live('click', function(e) {
        
        e.preventDefault();
        
        $('#nav li a').removeClass('selected');
        $(this).addClass('selected');
        
        var toLoad = $(this).attr('href')+' #left';
        $('#left').fadeOut(500, loadContent);
        $('#cont_holder').append('<div id="load"><img src="images/loader.gif" /></div>');
        $('#load').hide().fadeIn('fast');
        
        
        function loadContent() {
            
            $('#cont_holder').load(toLoad,'', showContent);
            
        }
                
                

                /*$('#left').hide().fadeIn('normal', function() {
            
                    $('#load').fadeOut('slow');
            
                    if ($.browser.msie) {
                        this.style.removeAttribute('filter');
                    }
            
                });*/
                
                
            
        
        function showContent() {
            
            positionContainer();
            changeLayout();
            centreContent();
            $('#shadow, a.close, #site_services, #desktop_services, #g5_over').fadeOut('fast');
            $('#left').hide().fadeIn('normal', hideLoader);
            
        }
        
        function hideLoader() {
            $('#load').fadeOut('normal');
            
            if ($.browser.msie) {
                this.style.removeAttribute('filter');
            }
        }
        
        
        
    });
    
    
    
});
