/*
Jquery Onload eventsfor shackleton centenrary expedition maps and media.
Author: Glen Swinfield (Erskine Design)
*/

$(document).ready(function(){
    
    map_scroll();
    load_day_info();
    setup_flags();
    playlist_onclick();
    
    $("ul#media_tabs").tabs();
    
    // start scrolling from the bottom of the map
    $('div#main_map').scrollTo({ top:250, left:0}, 800,
        
        function(){
            
            $('a#scroll_map_top').show();
            $('a#scroll_map_bottom').hide();
        }
        
    );
    
    // draggable day info box
    if($("div#day_info").length > 0) {
        $("div#day_info").height($("div#day_info").height()).draggable({containment:'parent'});
    }
    
    // close & animate the donate box
    $('a#donate_close').click(
    
        function() {
        
            $('a#donate_close').effect("transfer", { to: "a.donate" }, 500, function(){
            
                $('a.donate').removeClass('grey');
                $('div#map_wrapper div#support').remove();
                
            });
            
            return false;
        
        }
    
    );
});






// Click event for media player links
function playlist_onclick () {
    $('ol.media_listing li a').click(
        function(){
			swap_media(this);
			return false;
		}
    );
}
	
	
			
// Swap media currently being viewed
// Asynchronously call segment of media single entry page.
// uses autostart=true for smoothness
function swap_media(obj) {

    // audio, video or image
    var media_type = $(obj).parent().parent().attr('id');
    
    // href is the single enrty page url
    var media_single_entry = $(obj).attr('href');
    // title is the actual path to the media file.
    var media_path = $(obj).attr('title');
    
    // current selector
    $('ol#' + media_type + ' li.cur').removeClass('cur');
    $(obj).parent().addClass('cur');
    
    var replacer_div = '';

    if(media_type == 'video_list') {replacer_div = 'video_here';}	
    if(media_type == 'audio_list') {replacer_div = 'audio_here'}
    $('div#'+replacer_div).height($('div#'+replacer_div).height());	
    $('div#'+replacer_div).load(media_single_entry + ' div#the_media',
    
        function(){
            if(media_type == 'audio_list') {
                var s1 = new SWFObject('/player/player.swf',"ply","525","35","9","#FFFFFF");
            }
            
            if(media_type == 'video_list') {
                var s1 = new SWFObject('/player/player.swf',"ply","525","390","9","#FFFFFF");
            }
            s1.addParam('allowfullscreen','true');
            s1.addParam('allowscriptaccess','always');
            s1.addParam('wmode','opaque');
            if(media_type == 'audio_list') {
                s1.addParam('flashvars','file='+media_path+'&skin=/player/er-skin-audio.swf&autostart=true');
            }
            
            if(media_type == 'video_list') {
                s1.addParam('flashvars','file='+media_path+'&skin=/player/er-skin-video.swf&autostart=true');
            }
            s1.write('preview_'+replacer_div);
            $('div#'+replacer_div).css({height:''});
            playlist_onclick();
        }
    );
}
	
	
function swap_image() {

    $('ol#image_listing li a').click(
        
        function(){
        
            $('div#image_here div').height($('div#image_here div').height());
            $('img.main_image').hide();
            
            var img = $(this).attr('href');
            var title = $(this).attr('title');
            
            $('div#image_here').height($('div#image_here').height());
        
            $('img.main_image').attr('src',img);
            $('h4.main_image').html(title);
            $('img.main_image').show();
            $('div#image_here div').css({height: ''});
            
            return false;
        
        }
    
    );


}



function map_scroll () {


    $('a#scroll_map_top').click(function(){
    
        $('div#main_map').scrollTo({ top:0, left:0}, 800, function(){
        
            $('a#scroll_map_top').hide();
            $('a#scroll_map_bottom').show();
        
        } );
        
        return false;
    });
    
    $('a#scroll_map_bottom').click(function(){
    
        $('div#main_map').scrollTo({ top:250, left:0}, 800, function(){
        
            $('a#scroll_map_top').show();
            $('a#scroll_map_bottom').hide();
        
        }  );
        
        return false;
    });

} 


function load_day_info() {

    $('li#day_info_prev a, li#day_info_next a').click(

        // on click function
        function(){
        
            // what page is the content in
            var s_entry_page = $(this).attr('href');
            s_entry_page = s_entry_page.replace('http://shackletoncentenary.org','');
            
            var href_segments = Array();
            href_segments = s_entry_page.split('/');
            
            var basename_ext = href_segments[href_segments.length - 1];
            
            var basename_array = Array();
            basename_array = basename_ext.split('.');
            var basename = basename_array[0];
            
            $('li.flag_cur').removeClass('flag_cur');
            var map_class = $(this).attr('href');
            var map_class_array = Array();
            map_class_array = map_class.split('/');
            var map_class_end = Array();
            map_class_end = map_class_array[map_class_array.length - 1].split('.');
            map_class = map_class_end[0]
            
            $('div#map_wrapper ol li#'+map_class).removeClass('flag').addClass('flag_cur')
         
            // get the entry basename for relating to map flag using string functions
            var exit_dir = 'right';
            var entry_dir = 'left';
            
            if($(this).hasClass('next')) {
            
                var exit_dir = 'left';
                var entry_dir = 'right';
            
            }
            
            // stop div form collapsing by setting height
            $('div#day_info_inner').height($('div#day_info_inner').height());
            $('div#day_info_meta').height($('div#day_info_meta').height());
            $('div#day_info_sizer').height($('div#day_info_sizer').height());
            
            // slide out the content fo the meta div
            $('#day_info_meta').hide("slide", { direction: exit_dir }, 500,
            
                // slide out callback (load content)
                function() {
                
                   $('div#day_info_inner').load(s_entry_page + ' #day_info_inner > ',
                   
                        // callback after load
                        function(){
               
                            load_day_info();
                            $('li#'+basename).addClass('cur')
                            //$('#day_info_meta').hide();
                            //$('#day_info_meta').show("slide", { direction: entry_dir }, 500);
                        
                        }
                   
                   );
                
                }
            
            );
            
            return false;

        }
                    
    );

} // end load_day_info()




function continue_reading(){

    $('a.continue_reading').click(
    
        function() {
        
            $('div#extended_body').slideToggle();
            return false;
        }
    
    );

}

function setup_flags() {
    
    var map_class = $('div#map_wrapper').attr('class');
    $('div#map_wrapper ol li#'+map_class).removeClass('flag').addClass('flag_cur');
    
    $('div#map_image ol li.flag, div#map_image ol li.flag_cur').hover(
        
        function(){
            $(this).addClass('hover');
            $('div#map_image ol li.hover div.flag_hover').show();
        },
        

        function(){
            $('div#map_image ol li.hover div.flag_hover').hide();
            $(this).removeClass('hover');
        }
        
    );
}
