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(){
	/* галерея ютуб-роликов */
	$.ajax({
		type: "GET",
		url: "pgsga_channel_feed.xml",
		dataType: "xml",
		success: function(xml) {
			$('<div class="left_arr" style="opacity: 0.7;">' +
				'<a href="#">' +
					'<img title="" alt="" src="/images/larr.gif" />' +
				'</a>' +
			'</div>' +
			'<div class="right_arr" style="opacity: 0.7;">' +
				'<a href="#">' +
					'<img title="" alt="" src="/images/rarr.gif" />' +
				'</a>' +
			'</div>' +
			'<div class="gall"><ul class="ov"></ul></div>').appendTo('#bottom_galery');
			$(xml).find('entry').each(
				function()
				{
					var href = $(this).children('link').text().replace(new RegExp("watch\\?v=", "i"), 'v/');
					var title = $(this).children('title').text();
					var thumbnail_url = $(this).find('thumbnail').text();
					$('<li></li>').html('<a href="'+href+'" class="fancybox" onclick="return false"><img src="'+thumbnail_url+'" title="'+title+'" alt="'+title+'" /><span>'+title+'</span></a>').appendTo('ul.ov');
				}
			);
			$("#bottom_galery a.fancybox").fancybox({
				'padding'             : 0,
				'autoScale'           : false,
				'transitionIn'        : 'none',
				'transitionOut'       : 'none',
				'title'               : this.title,
				'width'               : 680,
				'height'              : 495,
				'href'                : this.href,
				'type'                : 'swf',
				'swf'                 : {'allowfullscreen':'true','wmode':'opaque'}
			});
			
				
			/* карусель */	
			$("#bottom_galery .left_arr").animate({opacity: 0.7},500);
			$("#bottom_galery .right_arr").animate({opacity: 0.7},500);
			$("#bottom_galery ul li a img").animate({opacity: 0.9},500);
			
			$("#bottom_galery .left_arr").hover(function(){
				$("#bottom_galery .left_arr").animate({opacity: 1},500);  
			},function(){
				$("#bottom_galery .left_arr").animate({opacity: 0.7},500);
				});
			
			$("#bottom_galery .right_arr").hover(function(){
				$("#bottom_galery .right_arr").animate({opacity: 1},500);  
			},function(){
				$("#bottom_galery .right_arr").animate({opacity: 0.7},500);
				});
			
			$("#bottom_galery ul li a").hover(function(){
				$(this).children("img").stop().animate({opacity: 1},500);  
			},function(){
				$(this).children("img").stop().animate({opacity: 0.9},500);
				});
			
			c_eq = 0;
			//c_eq_count = ($("#bottom_galery ul li img").length-1);
			c_eq_width = $("#bottom_galery ul li img").length * 161;
			$("#bottom_galery ul").width(c_eq_width);
			
			
			//carusel (c_eq, );
			$("#bottom_galery .left_arr").click(function(){
				c_eq--;
				if((c_eq * 159)<0){
				c_eq = $("#bottom_galery ul li img").length-4;
				};
				$("#bottom_galery ul").animate({marginLeft: -(c_eq * 159)},300);
				return false;
			});
			
			$("#bottom_galery .right_arr").click(function(){
				c_eq++;
				if((c_eq * 159)>(c_eq_width-(159*4))){
				c_eq = 0;
				};
				$("#bottom_galery ul").animate({marginLeft: -(c_eq * 159)},300);
				return false;
			});			
		}});
		/* /карусель */				
		/* /галерея ютуб-роликов */	


	// Start the animation
	scrollbackground();

	$("#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);
	});

	rand_eq = 0;
	eq = rand_eq;
	eq_count = ($(".slide").length-1);
	$("a.fancybox").fancybox();	
	$("#img_ind").click(function(){
		$(".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++;
	    };	    
	    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;
	});
});
