function includeJS(destination) {
    try {
        $('script[src="'+destination+'"]').remove();
        
    }   catch(e){}
    var e=window.document.createElement('script');
    e.setAttribute('src',destination);
    window.document.body.appendChild(e);
}

function incJS(js) {
    for(var i=0;i<js.length;i++){
        $('.fadeCover').html("");
        $('.fadeCover').append('<script type="text/javascript" src="'+js[i]+'"></script>')
    }
}


var page='index.php',ajaxLock=false,pageTitle="LOLENE - Official Website",playerHTML='';


function faderHeight(){
        return $('.header').height()-$('.topArea').height()+$('.mainBody').height()+50;
}

$(document).ready(function(){
    $('.playerOpen').click(function(){
            
            if($(this).hasClass('playerClose')){
                $('.playerContainer').slideToggle();
                $(this).removeClass('playerClose');
                $('.fadeCover').css('top','43px')
                playerHTML=$('.playerContainer').html();
                $('.playerContainer').html('');
            }
            else {
                if($('.playerContainer').html()==''){
                    $('.playerContainer').html(playerHTML);
                }
                $('.playerContainer').slideToggle();
                $(this).addClass('playerClose');       
                $('.fadeCover').css('top','80px')     
            }
    });
    
    $('.topArea li a.click').click(function(e){
        e.preventDefault()
        if(!ajaxLock && page!=$(this).attr('href')){
            ajaxLock=true;
            page=$(this).attr('href');
            
            
            window.location.hash=page
            
            $('.fadeCover').css({height:faderHeight()+'px'})    
            $('.fadeCover').fadeIn('slow',function(){

                $.ajax({
                    url:page,
                    global:true,
                    data: {ajax:1},
                    success: function(data){
                        var cls='';
                        var bgimage='images/background.jpg';
                            
                            if(page.match('news.php')){
                                cls='newspage';
                                bgimage='images/pagebg.jpg';
                            }
                            
                            else if(page.match('about.php')){
                                cls='subpage';
                                bgimage='images/sub-background.jpg';
                            }
                            else if(page.match('tour.php')){
                                cls='tourpage';
                                bgimage='images/poolbg.jpg';
                            }
                            else if(page.match('extras.php')){
                                cls='extras_page';
                                bgimage='images/background_extras.jpg';
                            }
                        
                        $('.mainBody').html(data);
                        $('.fadeCover').css({height:faderHeight()+'px'})    
                        
                        $('img').imgpreload(function(){
                            $.imgpreload(bgimage,function() {
                            
                                $('.outerMost').attr('class','outerMost '+cls);
                                $('.topArea li').removeClass('active');
                                
                                // load about page
                                if(page.match('about.php')){
                                    /*includeJS('js/extra.js')
                                    includeJS('js/photo.js')*/
                                    incJS(['js/extra.js','js/photo.js','js/lyrics.js'])
                                    
                                    $(document).ready(function(){
                                         $('.about').parent().addClass('active')
                                          
                                    }) 
                                }
                                // audio page
                                else if(page.match('audio')){
                                    /*includeJS('js/home.js')
                                    includeJS('js/video.js')*/
                                    incJS(['js/home.js','js/video.js'])
                                    $(document).ready(function(){
                                        window.location.hash="tabs-2" ;    
                                        $('#tabs').addClass('tabPlace')
                                        $('.audio').parent().addClass('active')
                                        $('.audioArea').css('display','block')
                                        $('.videoArea').css('display','none')
                                        document.title = pageTitle;
                                    })
                                }
                                // news page
                                else if(page.match('news.php')){
                                    //includeJS('js/news.js')
                                    incJS(['js/news.js'])
                                    $(document).ready(function(){
                                         $('.news').parent().addClass('active')
                                         
                                    }) 
                               
                                }           
                                // video page      
                                else if(page.match('video')){
                                    /*includeJS('js/home.js')
                                    includeJS('js/video.js')*/
                                    incJS(['js/home.js','js/video.js'])
                                    $(document).ready(function(){
                                         $('.video').parent().addClass('active')
                                         
                                    }) 
                                }                 
                                // tour page
                                else if(page.match('tour.php')){
                                    /*includeJS('js/tour.js')*/
                                    incJS(['js/tour.js'])
                                    $(document).ready(function(){
                                         
                                         $('.tour').parent().addClass('active')
                                         
                                    }) 
                               
                                }                 
                               
                                $(document).ready(function(){
                                     $('.fadeCover').css({height:faderHeight()+'px'})    
                                     $('.fadeCover').fadeOut('slow')
                                }) 
                           })
                        })
                    
                    ajaxLock=false;
                    document.title = pageTitle;
                    },
                dataType: "html"
                })
            });
        }
    })
    
    $('.logo').click(function(e){
        e.preventDefault()
        if(!ajaxLock && page!='index.php'){
            ajaxLock=true;
            page='index.php';
            window.location.hash='';
            
            $('.fadeCover').css({height:faderHeight()+'px'})    
            $('.fadeCover').fadeIn('slow',function(){
				
				
                $.get(page,{ajax:1},
                function(data){
                    var cls='altpage';
                    var bgimage='images/background.jpg';
                    
                    $('.mainBody').html(data);
                    $('.outerMost').attr('class','outerMost '+cls);
                    $('.fadeCover').css({height:faderHeight()+'px'})    
                    
                    $('img').imgpreload(function(){
                        
                        $.imgpreload(bgimage,function() {
                            $('.fadeCover').fadeOut('slow',function(){
                                
                                $('.topArea li').removeClass('active');
                                
                                //includeJS('js/main.js')
                                incJS(['js/main.js'])
                                $(document).ready(function(){
                                     $('.fadeCover').css({height:faderHeight()+'px'})    
                                     $('.fadeCover').fadeOut('slow')
                                }) 
                            });
                        });
                    });
                    ajaxLock=false;
                    document.title = pageTitle;
                })
            });
        }
    })
    
      
    $('.fadeCover').css({height:faderHeight()+'px'})    
    
    document.title = pageTitle;
});


