/*
    Initialize Events
    ------------------------------------------------------------------------------------------------------*/ 
    $(document).ready(function(event){
    
    
        /* Gallery
        --------------------------------------------------------------------------------------------------*/
        $galleria               = '#gallery blockquote';
        $gallery_curtain        = '#gallery dl';
        $gallery_slide          = false;
        $still_image            = false;
        
                    
        
        /* Smuggs Deals
        --------------------------------------------------------------------------------------------------*/
        $current_deal           = $('#smuggs_deals #deal_page').attr('class');
        
        $page_toggle            = true;
        $current_page           = $('#resort #page').attr('page');
        $page_bg                = $('#resort #page #background');
        $page                   = $('#resort #page #content');
        $loader                 = $('#resort #page img.loading');
        
        $('#resort_welcome').supersleight();
        $page_bg.animate({'opacity':'.8'});
        
        
        
        $('#smuggs_deals a.toggle').bind('mouseenter',{'type':'enter'},toggle_smuggs_deals).bind('mouseleave',{'type':'leave'},toggle_smuggs_deals).bind('click',{'type':'click'},toggle_smuggs_deals);
        
        $deal_toggle            = true;
        $deal_link              = $current_deal ? $current_deal : $('#deal_navigation ol li a:first-child').attr('rel');
        $('#deal_navigation li.navigator').css({'left':$('#smuggs_deals a[rel="'+$deal_link+'"]').position().left+'px','width':$('#smuggs_deals a[rel="'+$deal_link+'"]').parent().width()+'px','visibility':'visible'});
        $('#deal_navigation li a').bind('mouseenter',{'type':'toggle'},smuggs_deals_navigation).bind('click',{'type':'select'},smuggs_deals_navigation);
        $('#deal_navigation ol').bind('mouseleave',{'type':''},smuggs_deals_navigation);
        
        
        
        /* Page Content
        --------------------------------------------------------------------------------------------------*/
        $('#resort h1 a').click(function(event){ if($page_toggle){ hide_page(); } return false; });
        
        $section_toggle         = true;
        $current_page           ? section_navigation() : '';
        
        $('#resort a.page_toggle').bind('click',{type:'click'},toggle_page);
        $('#resort a.page_toggle').bind('mouseover',{type:'mouseover'},toggle_page);
        $('#resort a.page_toggle').bind('mouseout',{type:'mouseout'},toggle_page);
        

        
        /* Initiate
        --------------------------------------------------------------------------------------------------*/
        if($current_page || $('#smuggs_deals #slide').is(':visible')){ 
            $still_image        = true;
            $($gallery_curtain).animate({'opacity':'.5'}); 
        } else {
            $($gallery_curtain).animate({'opacity':'0'});
            $('#resort_welcome #gallery img.loading').fade_out(function(event){ $gallery_slide = setInterval('gallery_show()',5000); });
        }
        
        $smuggs_deals_bounce    = 0;
        animate_smuggs_deals();
        
        $('a[rev^="external"]').bind('click',external_links);
    });

    



/*
    Global Functions
    ------------------------------------------------------------------------------------------------------*/
    
    
    /* $ Wait
    ------------------------------------------------------------------------------------------------------*/
    $.fn.wait   = function(time,type){
        time    = time || 1000;
        type    = type || "fx";
        return this.queue(type,function(){
            var self = this;
            setTimeout(function(){ $(self).dequeue(); },time);
        });
    }; 
    
    
    /* External Links
    ------------------------------------------------------------------------------------------------------*/
    function external_links(event){
        external_window($(this).attr('href')); 
        return false;
    }
     
    /* User Tracking :: Smuggs Deals & Smuggs Intro
    ------------------------------------------------------------------------------------------------------*/
    function smuggs_deals(page){
        var $season     = $('#resort a.page_toggle').attr('rel');
    
    	$.get('/pages/resort_welcome/includes/memory.inc.php',{smuggs_deals:page});
    	pageTracker._trackPageview('/'+$season+'/'+'/smuggs_deals/'+page);
    }
    
    function smuggs_intro(page,section){
        var $season     = $('#resort a.page_toggle').attr('rel');
    
    	$.post('/pages/resort_welcome/includes/memory.inc.php',{smuggs_page:page,smuggs_section:section}); 
    	pageTracker._trackPageview('/'+$season+'/'+page+'/'+section);
    }
    




/*
    Gallery Functions
    ------------------------------------------------------------------------------------------------------*/
    
    
    /* Gallery Show
    ------------------------------------------------------------------------------------------------------*/
    function gallery_show(event){
        var $current        = $($galleria+' div.current');
    
        if($current.length == 0){ $current = $($galleria+' div:last-child'); };
        var $next = $current.next().length ? $current.next() : $($galleria+' div:first-child');
        
        $current.addClass('previous');
        $next.css({'opacity':'0'}).addClass('current').animate({'opacity':'1'},1750,function(event){ $current.removeClass('current previous'); });
    }

    /* Toggle Gallery
    ------------------------------------------------------------------------------------------------------*/
    function toggle_gallery(event){
        clearInterval($gallery_slide);
    
        if(event == 'stop'){
            $still_image    = true; 
            $($gallery_curtain).animate({'opacity':'.5'});
        } else if(event == 'start'){
            $gallery_slide  = setInterval('gallery_show()',5000); 
            $still_image    = false;
            $($gallery_curtain).animate({'opacity':'0'});
        }
    }





/*
    Smuggs Deals Functions
    ------------------------------------------------------------------------------------------------------*/


    /* Animate Smuggs Deals
    ------------------------------------------------------------------------------------------------------*/
    function animate_smuggs_deals(event){
        if($('#smuggs_deals #slide').is(':hidden') && $('#page #content').is(':hidden')){
            $('#deal_navigation').is(':visible') ? $('#deal_navigation').hide() : '';
        
            if($smuggs_deals_bounce < 4){
                $('#smuggs_deals').animate({'left':'-750px'},750,
                    function(event){ 
                        $('#smuggs_deals').animate({'left':'-760px'},650,animate_smuggs_deals);
                    }
                );
            }
            
            $smuggs_deals_bounce++;
        }
    }
    
    
    /* Toggle Smuggs Deals
    ------------------------------------------------------------------------------------------------------*/
    function toggle_smuggs_deals(event){
        if(event.data){
            if(event.data.type == 'click'){
                if($('#smuggs_deals #slide').is(':hidden') && $deal_toggle){
                    $deal_toggle        = false;
                    $page_toggle        = false;

                    if($current_page){
                        hide_page('',true);
                    } else {
                        toggle_gallery('stop');

                        var $deal_page      = $(this).attr('href').replace('?smuggs_deals=','');
                        $(this).attr('rel','Close');
                        $('#deal_navigation').show();           
                        $('#smuggs_deals').stop().animate({'left':'0px'},400,function(event){ toggle_smuggs_deal($deal_page); $deal_toggle = true; $page_toggle = true; });
                    }
                } else {
                    $(this).attr('rel','Open');
                    toggle_smuggs_deal();
                    $('#smuggs_deals').animate({'left':'-760px'},400,function(event){ $('#deal_navigation').hide(); });

                    toggle_gallery('start');
                }

                return false;
            }

            if(event.data.type == 'enter' && $('#smuggs_deals a.toggle').attr('rel') == 'Open'){
                $('#smuggs_deals').stop().animate({'left':'-750px'},250);
            }

            if(event.data.type == 'leave' && $('#smuggs_deals a.toggle').attr('rel') == 'Open'){
                if($('#page #content').is(':hidden')){
                    $('#smuggs_deals').stop().animate({'left':'-760px'},250);
                } else {
                    $('#smuggs_deals').stop().animate({'left':'-815px'},250);
                }
            }
        } else {
            if(event           == 'hide'){
                toggle_smuggs_deal();
                $('#smuggs_deals').stop().animate({'left':'-815px'},250).children('a.toggle').attr('rel','Open');
            }

            if(event           == 'show'){
                $deal_toggle            = false;
                $page_toggle            = false;

                var $deal_page          = $('#smuggs_deals').children('a.toggle').attr('href').replace('?smuggs_deals=','');
                $('#deal_navigation').show();
                toggle_smuggs_deal($deal_page);
                $('#smuggs_deals').stop().animate({'left':'0px'},400).children('a.toggle').attr('rel','Close');

                $deal_toggle            = true;
                $page_toggle            = true;
            }
        }
    }
    

    /* Smuggs Deals Navigation
    ------------------------------------------------------------------------------------------------------*/
    function smuggs_deals_navigation(event){
        if(event.data.type         == 'toggle' && $deal_toggle){
            var $link               = $('#smuggs_deals a[rel="'+$(this).attr('rel')+'"]');
            $('#smuggs_deals li.navigator').stop().animate({'left':$link.position().left+'px'}).css('width',$link.parent().width()+'px');
        } else if(event.data.type  == 'select'){
            var $deal_page          = $(this).attr('rel');
            var $link               = $('#smuggs_deals a[rel="'+$deal_page+'"]');
            
            $('#smuggs_deals a').removeClass('current');
            $link.addClass('current');
            $('#smuggs_deals li.navigator').stop().animate({'left':$link.position().left+'px'},toggle_smuggs_deal($deal_page)).css('width',$link.parent().width()+'px');
            
            return false;
        } else {
            var $link               = $('#smuggs_deals a.current');
            
            $('#smuggs_deals li.navigator').stop().animate({'left':$link.position().left+'px'}).css('width',$link.parent().width()+'px');
        }
    }
    
    
    /* Toggle Smuggs Deal
    ------------------------------------------------------------------------------------------------------*/
    function toggle_smuggs_deal(event){
        if(event){
            var $deal_page          = event;
            var $deal_season        = $('#resort a.page_toggle').attr('rel');
            
            $('#smuggs_deals a.toggle').attr('href','?smuggs_deals='+$deal_page);
    
            if($('#smuggs_deals #slide').is(':hidden')){
                smuggs_deals($deal_page);
                $('#smuggs_deals #slide').slideDown().children('img.loading').wait().fade_out().siblings('#deal_page').attr('class',$deal_page).load('/pages/resort_welcome/smuggs_deals/'+$deal_page+'.inc.php?season='+$deal_season).wait().fade_in();
            } else {
                smuggs_deals($deal_page);
                $('#deal_page').fade_out('',400).siblings('img.loading').fade_in(function(){
                    $('#deal_page').attr('class',$deal_page).load('/pages/resort_welcome/smuggs_deals/'+$deal_page+'.inc.php?season='+$deal_season,null,function(){
                        $('#slide img.loading').fade_out().siblings('#deal_page').wait(500).fade_in();
                    });
                },400);
            }
            
            $('a[rev^="external"]').bind('click',external_links);
        } else {
            $('#smuggs_deals #slide').slideUp();
        }
    }





/*
    Smuggs Intro Functions
    ------------------------------------------------------------------------------------------------------*/
    

    /* Section Navigation
    ------------------------------------------------------------------------------------------------------*/
    function section_navigation(event){        
        var $current_section                    = $('#section_navigation a.current').attr('rel');
        var $current_season                     = $('#resort a.page_toggle').attr('rel');
    
        if(event){
            if(event.data.type                 == 'toggle'){
                var $link                       = $('#section_navigation a[rel="'+$(this).attr('rel')+'"]');
                $('#section_navigation li.navigator').stop().animate({'left':$link.position().left+'px'}).css('width',$link.parent().width()+'px');
            } else if(event.data.type          == 'select'){
                var $new_section                = $(this).attr('rel');
                
                if($new_section != $current_section){
                    var $link                   = $('#section_navigation a[rel="'+$new_section+'"]');
                    $('#section_navigation a').removeClass('current');
                    $link.addClass('current');
                    
                    $('#section_navigation li.navigator').stop().animate({'left':$link.position().left+'px'}).css('width',$link.parent().width()+'px');
                    
                    $('div[section='+$current_section+']').fade_out(function(event){
                        if(!$('div[section='+$new_section+'] blockquote img').attr('video_path') && $('div[section='+$new_section+'] blockquote img').attr('url')){
                            var $video          = $('div[section='+$new_section+'] blockquote img').attr('url');
                            var $image          = $('div[section='+$new_section+'] blockquote img').attr('src');
                            var $video_id       = $('div[section='+$new_section+'] blockquote img').attr('id');
                            
                            swfobject.embedSWF('/media/video_player.swf?video_path='+$video+'&image_path='+$image,$video_id,'300','225','8.0.0','/media/expressInstall.swf',{'tracker':'/index.php/videos/'+$video_id},{'wmode':'transparent','allowFullScreen':true},{'video_path':$video,'z-index':2});
                        }
                        
                        if(!$('div[section='+$new_section+'] #its_included-'+$current_page+'-'+$new_section).attr('loaded')){
                            var $its_included   = '<img id="its_included-'+$current_page+'-'+$new_section+'" class="its_included" src="/pages/resort_welcome/images/universal/page/its_included.png" alt="It\'s Included in your stay" title="It\'s Included in your stay"/>';
                            $('div[section='+$new_section+']').append($its_included);
                            
                            swfobject.embedSWF('/pages/resort_welcome/media/its_included.swf?season='+$current_season +'&page='+$current_page+'&section='+$new_section,'its_included-'+$current_page+'-'+$new_section,'110','110','9.0.0','/media/expressInstall.swf',{},{'wmode':'transparent'},{'loaded':'true','z-index':2,'styleclass':'its_included'});
                        }
                        
                        $('div[section='+$new_section+']').fade_in(); 
                    });
                    
                    smuggs_intro($('#resort_welcome #resort a#'+$current_page).attr('id'),$new_section);
                }
                
                return false;
            } else {
                var $link                       = $('#section_navigation a.current');
            
                $('#section_navigation li.navigator').stop().animate({'left':$link.position().left+'px'}).css('width',$link.parent().width()+'px');
            }
        } else {            
            $section_link   = $current_section ? $current_section : $('#section_navigation li a:first-child').attr('rel');
            $('#section_navigation li.navigator').css({'left':$('#section_navigation a[rel="'+$section_link+'"]').position().left+'px','width':$('#section_navigation a[rel="'+$section_link+'"]').parent().width()+'px','visibility':'visible'});
            $('#section_navigation li a').bind('mouseenter',{'type':'toggle'},section_navigation).bind('click',{'type':'select'},section_navigation);
            $('#section_navigation').bind('mouseleave',{'type':''},section_navigation);
        }
    }
    
    
    /* Toggle Page
    ------------------------------------------------------------------------------------------------------*/
    function toggle_page(event){
        var $toggle_type        = event.data.type;
        
        if($toggle_type        == 'click'){
            var $content        = $(this).attr('id');
            
            if($content != $current_page){
                if($page_toggle){
                    $page_toggle= false;
                                        
                    if($current_page){ 
                        hide_page($content);
                    } else {
                        toggle_gallery('stop');
                        show_page($content);
                    }
                }
            }
            
            return false;
        }
        
        if($toggle_type        == 'mouseover'){
            $page_toggle && $current_page != $(this).attr('id') ? $(this).stop().animate({left:'745px'},300) : '';
        }
        
        if($toggle_type        == 'mouseout'){
            $page_toggle && $current_page != $(this).attr('id') ? $(this).stop().animate({left:'770px'},300) : '';
        }
    }
    

    /* Hide Page
    ------------------------------------------------------------------------------------------------------*/
    function hide_page(new_page,smuggs_deals){
        $page_toggle                        = false;

        var $current_button                 = $('#resort_welcome #resort a#'+$current_page);
        var $current_header                 = $('#resort_welcome #resort h1[page="'+$current_page+'"]');
        
        $current_button.data('top',$current_button.attr('rev'));

        
        $page.hide();
        $page_bg.slideUp();
        
        $current_header.animate({'left':'960px'},500);
        $current_button.animate({'left':'770px'},500,function(e){
            $current_button.animate({'top':$current_button.data('top')},function(e){
                if(new_page){
                    show_page(new_page);
                } else {
                    $current_page           = '';
                    smuggs_intro('','');
                    $deal_toggle            = true;
                    
                    if(!smuggs_deals){
                        $page_toggle        = true;
                        toggle_gallery('start'); 
                    } else {
                        $('#smuggs_deals #slide').is(':hidden') ? toggle_smuggs_deals('show') : '';
                    }
                }
            });
        });
    }
    
    
    /* Show Page
    ------------------------------------------------------------------------------------------------------*/  
    function show_page(new_page){
        var $new_page       = new_page;
        var $new_button     = $('#resort_welcome #resort a#'+$new_page);
        var $new_header     = $('#resort_welcome #resort h1[page="'+$new_page+'"]');
        var $current_season = $('#resort a.page_toggle').attr('rel');
        
        $deal_toggle        = false;
        toggle_smuggs_deals('hide');
        
        
        $new_button.animate({top:'10px'},function(event){
            $page_bg.slideDown(function(event){ $loader.fade_in(); });
            
            $new_button.animate({left: '40px'});
            $new_header.animate({left:'260px'},function(event){
                $page.load(
                    '/pages/resort_welcome/sections/'+$new_page+'.'+$new_button.attr('rel')+'.php?season='+$current_season,
                    null,
                    function(event){
                        $page.supersleight();
                    
                        setTimeout(function(event){
                            $loader.fade_out();
                            $page.show(); 
                            $current_page           = new_page;
                            
                            $('a[rev^="external"]').bind('click',external_links);
                            section_navigation();
                            
                            if(!$('div[section="overview"] blockquote img').attr('video_path') && $('div[section="overview"] blockquote img').attr('url')){
                                var $video          = $('div[section="overview"] blockquote img').attr('url');
                                var $image          = $('div[section="overview"] blockquote img').attr('src');
                                var $video_id       = $('div[section="overview"] blockquote img').attr('id');
                                
                                swfobject.embedSWF('/media/video_player.swf?video_path='+$video+'&image_path='+$image,$video_id,'300','225','8.0.0','/media/expressInstall.swf',{'tracker':'/index.php/videos/'+$video_id},{'wmode':'transparent','allowFullScreen':true},{'video_path':$video,'z-index':'2'});
                            }
                            
                            if(!$('div[section="overview"] #its_included-'+$current_page+'-overview').attr('loaded')){
                                var $its_included   = '<img id="its_included-'+$current_page+'-overview" class="its_included" src="/images/winter/resort_welcome/its_included/fun_guarantee.png" alt="It\'s Included in your stay" title="It\'s Included in your stay"/>';
                                $('div[section="overview"]').append($its_included);
                                
                                swfobject.embedSWF('/pages/resort_welcome/media/its_included.swf?season='+$current_season+'&page='+$current_page+'&section=overview','its_included-'+$current_page+'-overview','110','110','9.0.0','/media/expressInstall.swf',{},{'wmode':'transparent'},{'loaded':'true','z-index':'2','styleclass':'its_included'});
                            }
                            
                            smuggs_intro($current_page,'overview');
                            $deal_toggle            = true;
                            $page_toggle            = true;
                        },900); 
                    }
                );
            });
        });
    }