$(document).ready(function(){
    
    if ($('#accordion')){        
        $('#accordion').accordion({ autoHeight: false ,collapsible: false});
    }
    if ($('#time-line')){
        $('#accordion2').accordion({ autoHeight: true ,collapsible: true, navigation:true});
        $('.h-year').each(function(index){

             $(this).click(function(){
                $('.h-year').removeClass('active');                
                $(this).addClass('active');
                $('#accordion2').accordion('activate',index);
             });
        });                    
    }
    var $zoom = $("a.zoom1");
    if ($zoom.length){
        $("a.zoom1").fancybox({
            'overlayOpacity'	:	0.7,
            'overlayColor'		:	'#000',
            'padding'           :  '25'
        });
    }
    if ($("a.fancybox").length){
			$("a.fancybox").fancybox({
				'overlayOpacity'	:	0.7,
				'overlayColor'		:	'#000',                
                'hideOnOverlayClick':   false,
                'hideOnContentClick':   false,
                'frameWidth'        :   500,
                'frameHeihgt'       :   100
			});
    }
    if($('#slideshow')){
        $(function() {
            setInterval( "slideSwitch()", 5000 );
        });
    }

    if($(".page-title").length){
       $(".page-title").append('<div class="box"></div>');
       $(".page-title .box").css('opacity',0.8);
    }



    $('#searchform').submit(function() {
        loadResult('ajax.search.php?'+$("#searchform").serialize());
        return false;
        
    });

    initPreview();
   
    
});
function initPreview(){
   $("a.preview").each(function(i){
        $("body").append("<div class='pdf-preview' id='pdf-preview-"+i+"'><img src='pdf-preview.php?file_id="+ this.rel +"' alt='Image preview' /></div>");
        var my_preview = $("#pdf-preview-"+i);
        var left_pos;
        var top_pos;
        xOffset = 320+25;
        yOffset = 453-100;

        top_pos =  $(this).offset().top;
        left_pos = $(this).offset().left;

        top_pos  -= yOffset;
        left_pos -= xOffset;

        $(this).removeAttr("title").mouseover(function(){
                my_preview.css({display:"block", top:top_pos,left:left_pos}).fadeIn(400);

        }).mouseout(function(){
                my_preview.css({display:"none"});
        });

   });

}
function initLink(){
    $('#search-result .page-list li a').click(function(e){
        e.preventDefault();
        loadResult($(this).attr('href'));
    });
}
function loadResult(data){
    $('#search-result').load(data, function(){
        initLink();
        initPreview();
    });
}
function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}


