
function slideSwitch() {
    var $active = $('#news_head img.active');

    if ( $active.length == 0 ) $active = $('#news_head img:last');
    
    var $next =  $active.parent().next().children().length ? $active.parent().next().children()
        : $('#news_head img:first');

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

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

function initGallery()
{
     var baseUrl = $("div#baseUrl").html();

     $('a#nav_prev').bind('click', function() { 
         if ($(this).hasClass("disabled")) return false;
         photo = $("a.photo_link img.active").parent().prev();
         photo.click();

         return false; 
     }); 
     
     $('a#nav_next').bind('click', function() { 
         if ($(this).hasClass("disabled")) return false;
         photo = $("a.photo_link img.active").parent().next();
         photo.click();

         return false; 
     });
     
     $('a.gallery_link').bind('click', function() { 
         activeGallery($(this));
         $(this).next().find('a.photo_link').first().click();
         
         return false; 
     });
         
     $('a.photo_link').bind('click', function() { 
         var filename = $(this).attr("id");
         
         $("div#photo img").addClass("front");
         var image = $("div#photo").image(baseUrl + "/public/galleries/middle/" + filename, $(this).children().attr('title'));
         $("a.photo_link img.active").removeClass("active");
         $(this).children().addClass("active");
         $("div#photo img.front").fadeOut('slow');
         
         $("a.navigator").removeClass("disabled");  
         if ($(this).prev().length == 0) $('a#nav_prev').addClass("disabled");

         if ($(this).next().length == 0) $('a#nav_next').addClass("disabled"); 

               
         
         if ($(this).next().is(':hidden')) 
         {
             photo = $(this).next();
             photo.show();   
             photo.parent().children().not(':hidden').first().hide();
         }
         if ($(this).prev().is(':hidden')) 
         {
             photo = $(this).prev();
             photo.show();   
             photo.parent().children().not(':hidden').last().hide(); 
         }
         
         
         activeGallery($(this).parent().prev()); 
         
        

         return false; 
     });  

     $('#gallery_link_' + $("input#gallery_pk").val()).click(); 
}

function activeGallery(gallery)
{
    // if change 
    if (!gallery.hasClass("active")) 
    {
        // remove classes
        $("a.gallery_link").removeClass("active");
        $("a.gallery_link").addClass("noactive");
        gallery.removeClass("noactive"); 
        gallery.addClass("active");
        $("a.gallery_link").next().find("a").show();

        $("a.gallery_link").filter(".noactive").next().find("a:gt(2)").hide();
        $("a.gallery_link").filter(".active").next().find("a:gt(5)").hide();
    }
    
   // $("a.gallery_link").filter(".active").next().find("a").eq(5).nextAll().hide();
 //  gallery.next().find("a").slice(0,6).show(); 
    
   // gallery.next().find("a").slice(6).hide();
    
    
}

function initScroll()
{
    
     $('.scrollable').serialScroll({
        target:'.scroll',
        items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
        prev:'.first',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
        next:'.second',// Selector to the 'next' button (absolute too)
        axis:'xy',// The default is 'y' scroll on both ways
        navigation:'#navigation li a',
        duration:400,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
        force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
        
        //queue:false,// We scroll on both axes, scroll both at the same time.
        //event:'click',// On which event to react (click is the default, you probably won't need to specify it)
        //stop:false,// Each click will stop any previous animations of the target. (false by default)
        //lock:true, // Ignore events if already animating (true by default)        
        start: 0, // On which element (index) to begin ( 0 is the default, redundant in this case )        
        cycle:true,// Cycle endlessly ( constant velocity, true is the default )
        step:1, // How many items to scroll each time ( 1 is the default, no need to specify )
        //jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
        //lazy:false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
        //interval:6000, // It's the number of milliseconds to automatically go to the next
        //constant:true, // constant speed
        exclude:3,
        onBefore:function( e, elem, $pane, $items, pos ){

            e.preventDefault();
            if( this.blur )
                this.blur();
        },
        onAfter:function( elem ){
            //'this' is the element being scrolled ($pane) not jqueryfied
        }
    });
}

function initPage()
{

    try
    {
        document.execCommand("BackgroundImageCache", false, true);
    }
    catch(err){}
    
    setInterval( "slideSwitch()", 8000 );     
    
    $("a.lightbox").lightBox('', $('#baseUrl').text());
    $('a.reference_link').colorbox({onComplete:initGallery}); 
                  
    $("div.slider_trigger").click(function () {
      
          $(".slider").slideUp("slow");  
        if ($(this).next(".slider").is(":hidden")) {
            $(this).next(".slider").slideDown("slow");
        } else {
            $(this).next(".slider").slideUp("slow");
        }
    });      
    initScroll();

    /* create jquery plugin */
    $.fn.image = function(src, f){
        return this.each(function(){ 
            var i = new Image();
            i.src = src; 
            i.title = f;
            this.appendChild(i);
        });
    }
}







 

