
$(function() {
    //preload images
	//$(".gallery_images a").each(function(){
    //    $('<img/>')[0].src = $(this).attr("href");
    //});
	
	//set active class
    var $first = $('.gallery_images a:last');
    $first.addClass('active');
    
    //set opacity for slider right button
    $("#slide_right").animate({ opacity: "0.3" }, 100);
    
    //start slideshow
    slideSwitch();
    stop_interval = setInterval("slideSwitch()", 1000000);
  
    $(".image").click(function() {
            
        clearInterval(stop_interval);
        $("#image").stop();
        
        var $active = $('.gallery_images a.active');
        if ($active.length == 0) $active = $('.gallery_images a:first');
        var $next = $(this);
        
        clearInterval(stop_interval);
        
        changePic($active,$next);
        return false;
    });
    
   
    $("#slide_right").mouseover(function() {
        stop_interval_slideMoveRight = setInterval("slideMoveRight()", 100);
        return false;
    });

    $("#slide_right").mouseout(function() {
        clearInterval(stop_interval_slideMoveRight);
        return false;
    });

    $("#slide_right").click(function() {
        return false;
    });

    $("#slide_left").mouseover(function() {
        stop_interval_slideMoveLeft = setInterval("slideMoveLeft()", 100);
        return false;
    });

    $("#slide_left").mouseout(function() {
        clearInterval(stop_interval_slideMoveLeft);
        return false;
    });

    $("#slide_left").click(function() {
        return false;
    }); 
	
	$(".gallery_right").click(function() {
		slideSwitch();
        return false;
    }); 
	$(".gallery_left").click(function() {
		
		var $active = $('.gallery_images a.active');
		if ($active.length == 0) $active = $('.gallery_images a:first');
		var $next = $active.prev().length ? $active.prev() : $('.gallery_images a:last');
		changePic($active,$next);
        
		return false;
    }); 
	
	
});


function slideSwitch() {
    var $active = $('.gallery_images a.active');
    if ($active.length == 0) $active = $('.gallery_images a:last');
    var $next = $active.next().length ? $active.next() : $('.gallery_images a:first');
    changePic($active,$next);
    return false;
}

function changePic($active,$next) {
    
    $("#image").stop();
    $("#image_bg").stop();
    $active.addClass('last-active');
    $next.addClass('active');
    $active.removeClass('active last-active');
    
    var rel = $next.attr("href");
    
    $("#image").html($("#image_bg").html());
    $("#image").css('opacity', "1.0");
    $("#image_bg").html("<img width='558px' src='" + rel + "' alt='' />");
    $("#image").animate({ opacity: "0.0" }, 1, function() { });
    $active.removeClass('active last-active');
    return false;
}

function slideMoveLeft() {
    var currentTop = parseInt($(".gallery_images").css('top'));
    var maxHeight = parseInt($(".gallery_images").height());

    if (currentTop > - maxHeight + 378)
    {
        currentTop = currentTop - 10;
        $(".gallery_images").css('top', currentTop + "px");
        $("#slide_right").animate({ opacity: "1" }, 100);
    }
    else 
    {
        $("#slide_left").animate({ opacity: "0.3" }, 100);
    }
    return false;
}

function slideMoveRight() {
    var currentTop = parseInt($(".gallery_images").css('top'));
    var maxHeight = parseInt($(".gallery_images").height());

    if (currentTop < 0)
    {
        currentTop = currentTop + 10;
        $(".gallery_images").css('top', currentTop + "px");
        $("#slide_left").animate({ opacity: "1" }, 100);
    }
    else 
    {
        $("#slide_right").animate({ opacity: "0.3" }, 100);
    }
    return false;
}

