function preloadImg (imgSRC, ImgTitle){
    $('#img_ind').html("");
    //alert(imgSRC);
  var img = new Image();
  // wrap our new image in jQuery, then:
  $(img)
    // once the image has loaded, execute this code
    .load(function () {
      // set the image hidden by default    
      $(this).hide();
      $("#top_gallery_title").hide();
      // with the holding div #loader, apply:
      $('#img_ind').append(this);
      // fade our image in to create a nice effect
      $(this).fadeIn();
      $("#top_gallery_title").fadeIn();
    })
    
    // if there was an error loading the image, react accordingly
    .error(function () {
      // notify the user that the image could not be loaded
    })
    .attr('src', imgSRC);
    $("#top_gallery_title").text(ImgTitle);
};



        // ***
	// Scrolling background
	// ***
		
	// height of background image in pixels
	var backgroundheight = 1967;
	
	// get the current minute/hour of the day
	var now = new Date();
	var hour = now.getHours();
	var minute = now.getMinutes();
	
	// work out how far through the day we are as a percentage - e.g. 6pm = 75%
	var hourpercent = hour / 24 * 100;
	var minutepercent = minute / 60 / 24 * 100;
	var percentofday = Math.round(hourpercent + minutepercent);
	
	// calculate which pixel row to start graphic from based on how far through the day we are
 	var offset = backgroundheight / 100 * percentofday;

	// graphic starts at approx 6am, so adjust offset by 1/4
	var offset = offset - (backgroundheight / 4);

	function scrollbackground() {
		// decrease the offset by 1, or if its less than 1 increase it by the background height minus 1
		//if (offset>backgroundheight) {
		//    offset = 0;
		//    }else{
		//    offset++;
		//};
   		offset = (offset < 1) ? offset + (backgroundheight - 1) : offset - 1;
		// apply the background position
   		$('#wrap').css("background-position", -offset + "px 121px");
   		// call self to continue animation
   		setTimeout(function() {
			scrollbackground();
			}, 100
		);
   	}
    


$(document).ready(function(){
	// Start the animation
	scrollbackground();
//$("#wrap").animate({backgroundPosition: '+1px 121px'},1000);
$("#menu_top_right a").animate({opacity: 0.75},300);
$("#menu_top_right a").hover(function(){
	$(this).stop().animate({opacity: 1},300);
    },function(){
	$(this).animate({opacity: 0.75},300);
	});


//    Galery
//    show title


rand_eq = Math.floor(Math.random()*$(".slide").length);
eq = rand_eq;
eq_count = ($(".slide").length-1);
$("a[rel^='prettyPhoto']").colorbox();
//$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
    $("#img_ind").click(function(){
	//alert($(".slide").eq(eq).children("a:first").attr('rel'));
	$(".slide").eq(eq).children("a:first").click();
	if ($(".slide").eq(eq).children("a:first").attr('rel')=='')
	{
	    location.href = $(".slide").eq(eq).children("a:first").attr('href');
	}
	return false;
	});
    
if ($("#first_top_gal").length){
    eq=$("#first_top_gal").attr("count");
}


$("#top_gallery_title").text($(".slide img").eq(eq).attr("title"));

$(".slide").fadeOut();
preloadImg ($(".slide").eq(eq).attr("fotopath"),$(".slide").eq(eq).attr("title"));


$("#top_galery .next").animate({opacity: 0.0},0);
$("#top_galery .prev").animate({opacity: 0.0},0);
    $("#img_ind").hover(function(){
	    //$("#top_gallery_title").stop().animate({height: '0px', top: '250px', opacity: 0},1000);
	    $("#top_galery .next").stop().animate({opacity: 0.5},1000);
	    $("#top_galery .prev").stop().animate({opacity: 0.5},1000);
	},function(){
	    $("#top_galery .next").stop().animate({opacity: 0.0},1000);
	    $("#top_galery .prev").stop().animate({opacity: 0.0},1000);
	    });
    
    $("#top_gallery_title").hover(function(){
	    $("#top_galery .next").stop().animate({opacity: 0.5},1000);
	    $("#top_galery .prev").stop().animate({opacity: 0.5},1000);
	},function(){
	    $("#top_galery .next").stop().animate({opacity: 0.0},1000);
	    $("#top_galery .prev").stop().animate({opacity: 0.0},1000);
	    });
    
    $("#top_galery .next").hover(function(){
	    $(this).stop().animate({opacity: 1},300);
	},function(){
	    $(this).stop().animate({opacity: 0.5},1000);
	    });
    $("#top_galery .prev").hover(function(){
	    $(this).stop().animate({opacity: 1},300);
	},function(){
	    $(this).stop().animate({opacity: 0.5},1000);
	    });
    
//    click
    $("#top_galery .next").click(function(){
	    if (eq>=eq_count){
		eq=0;
	    }else{
		eq++;
	    };
	    //alert($(".slide").eq(eq).attr("fotopath"));
	    preloadImg ($(".slide").eq(eq).attr("fotopath"),$(".slide").eq(eq).attr("title"));
	    return false;
	    });
    $("#top_galery .prev").click(function(){
	    if (eq<=0){
		eq=eq_count;
	    }else{
		eq--;
	    };
	    preloadImg ($(".slide").eq(eq).attr("fotopath"),$(".slide").eq(eq).attr("title"));
	    return false;
	    });
    

    
    
    });