jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++){
    jQuery("<img>").attr("src", arguments[i]);
  }
}
$(document).ready(function(){
		if ($.browser.msie) { //IE bug fixes
   			$('hr').css('margin','-7px 0 !important');
		} else if($.browser.safari) {
			sIFR.prefetch('../swf/century.swf');
			new Image().src = '../swf/century.swf';
		}

		if($('#aboutNav img').attr("alt") == "Декларация Фонда") {
			$('body p').css('font-size','115%');
			$('#caption').css('font-size','90%');
			$.preloadImages("../img/nav/ru/about_rollover.gif", "../img/nav/ru/activities_rollover.gif");
		} else {
			$.preloadImages("../img/nav/en/about_rollover.gif", "../img/nav/en/activities_rollover.gif");
		
		}
		$("#nav a img").hover(function(){
				if($(this).attr("src").indexOf("-active") == -1) {
					var newSrc = $(this).attr("src").replace("default.gif","rollover.gif");
					$(this).attr("src",newSrc);
				}
			},
			function(){
				if($(this).attr("src").indexOf("rollover.gif") != -1) {
					var oldSrc = $(this).attr("src").replace("rollover.gif","default.gif");
					$(this).attr("src",oldSrc);
				}
			}
		);

		$("#contentBucket3 .info .title a").click(function(event){
			$('#contentBucket3 .info .details:visible').slideUp(380, function() {
				$("#contentBucket3 .info .title a").css({ color:"#898989" });
			});
			//$(this).css({ color:"#000" });
			$(this).parent().parent().children('.details:hidden').slideDown(420, function(){
				$(this).parent().children().children().css({ color:"#000" });
			});
		});
		prepGallery();
});

/********
 * Displays the signup text for the stay connected email submission form
 *******/
var stayingConnected = false;
function displayEmailForm(lang) {
		if(stayingConnected == false) {
		string = "";
		if(lang=="en") {
			string = '<hr /><div id="stayConnected" class="contentBucket1 hidden">'+
				"<h2 class='sc'>Stay Connected</h2>"+
	            "<p>Stay up-to-date about the Russian Avantgarde Foundation by subscribing to our mailing list. You will receive updates on special events, and important news regarding our foundation and the avantgarde community.</p>"+
	            '<form id="submitEmail" name="submitEmail" method="post" action="functions/submitmail.php">'+
	            '<input name="lang" type="hidden" id="lang" value="en" />'+
				'<input name="name" type="text" id="name" value="Name" onfocus="if(document.submitEmail.name.value==\'Name\') document.submitEmail.name.value=\'\';" onblur="if(document.submitEmail.name.value==\'\') document.submitEmail.name.value=\'Name\';" />'+
	            '<input name="email" type="text" id="email2" value="Email" onfocus="if(document.submitEmail.email.value==\'Email\') document.submitEmail.email.value=\'\';" onblur="if(document.submitEmail.email.value==\'\') document.submitEmail.email.value=\'Email\';" />'+
	            '<input type="image" name="submit" id="submit" value="Submit" src="/firstborn/avangard-ru/img/btn_submit_en.gif" />'+"</form></div>";
		} else {
			string = '<hr /><div id="stayConnected" class="contentBucket1 hidden">'+
				'<h2 class="sc">Подписка</h2>'+
	            '<p>Если вы хотите получить уведомление о запуске нашего нового сайта и подписаться на новости о предстоящих мероприятиях, оставьте, пожалуйста, ваш электронный адрес.</p>'+
	            '<form id="submitEmail" name="submitEmail" method="post" action="functions/submitmail.php">'+
	            '<input name="lang" type="hidden" id="lang" value="ru" />'+
				'<input name="name" type="text" id="name" value="Имя" onfocus="if(document.submitEmail.name.value==\'Имя\') document.submitEmail.name.value=\'\';" onblur="if(document.submitEmail.name.value==\'\') document.submitEmail.name.value=\'Имя\';" />'+
	            '<input name="email" type="text" id="email2" value="Электронный адрес" onfocus="if(document.submitEmail.email.value==\'Электронный адрес\') document.submitEmail.email.value=\'\';" onblur="if(document.submitEmail.email.value==\'\') document.submitEmail.email.value=\'Электронный адрес\';" />'+
	            '<input type="image" name="submit" id="submit" value="Submit" src="/firstborn/avangard-ru/img/btn_submit_ru.gif" />'+
	          '</form></div>';
		}
		$('#leftContent').append(string);
		
		$('#stayConnected').slideDown(400,function() {
			sIFR.replace(century, {selector: '.sc',css: ['.sIFR-root {color:#ee2a28;font-weight:bold;}'],wmode: 'transparent'});
			$('#submitEmail').ajaxForm({ 
				dataType:  'json', 
				success:   processJson
			});
		}).removeClass('hidden');
		//$('#stayConnected').scrollTo(1000);
		stayingConnected = true;
	}
	//$.scrollTo($('#stayConnected'),800);
	

	return false;
}
function processJson(data) { 
   
   if(data.isValid == "true") {
	   $('#submitEmail').slideUp("slow",function() {
		   $('#submitEmail').before("<p class=\"error\">"+data.message+"</p>");
		});
   } else {
	  $('#submitEmail').append("<p class=\"error\" style=\"margin-left:0px;\">"+data.message+"</p>");
   }
}
function prepGallery() {
		$('#imageGallery #meds img').hide();
		$('#imageGallery #larges img').hide();
		$('#imageGallery #meds img.med_0').show();
		
		$('#imageGallery .nextImgArrow').css('cursor','pointer');
		$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(0); });
		$('#imageGallery img.nextImgArrow').bind("mouseover", function() { $('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_rd.gif'}); });
		$('#imageGallery img.nextImgArrow').bind("mouseout", function() { $('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_wt.gif'}); });
		var caption = $('#imageGallery img.med_0').attr("alt");
		$('#imageGallery #caption p').text(caption);
		$('#enlarge').css('cursor','pointer');
		$('#enlarge').bind("click", function() { enlargeImage(0); });
		$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_rd.gif'}); });
		$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_wt.gif'}); });
		$('#imageGallery #thumbs img.thumb').hover(function(event) {
			$('#imageGallery #thumbs img.thumb').clearQueue();
			$('#imageGallery #gridCaption p').clearQueue();
			
			var copy = this.alt;
			$('#imageGallery #gridCaption p').show();
			$('#imageGallery #gridCaption p').animate({'color':'#000'},200, function () {
				$('#imageGallery #gridCaption p').text(copy);
				$('#imageGallery #gridCaption p').animate({'color':'#fff'},200);
			});
			
			$('#imageGallery #thumbs img.thumb').fadeTo(200,0.4);
			$(this).stop().fadeTo(200,1.0);
			
			//show red bar
			var thumbLeft = $(this).offset().left;
			var thumbTop = $(this).offset().top;
			$('#redHooverBar').css({position:'absolute',top:thumbTop+82,left:thumbLeft}).show();
			
		}, function(event) {
			$('#imageGallery #thumbs img.thumb').pause(300).fadeTo("fast",1);
			$('#redHooverBar').hide();
			$('#imageGallery #gridCaption p').animate({'color':'#000'},200);
		});
		$('#imageGallery #thumbs img.thumb').click(function(event){
			var copy = this.alt;
			var href = this.parentNode.href;
			var id = href.substr(href.indexOf('#')+1);
			enlargeGridImage(id,copy);
		});
		$('#linearIcn').css('cursor','pointer');
		$('#linearIcn').click(function() {
			$('#imageGallery #gridCaption').slideUp(300);
			$('#imageGallery #thumbs').SlideOutUp(400, function() {
				$('#imageGallery img.nextImgArrow').unbind();
				
				$('#imageGallery #meds img').hide();
				$('#imageGallery #larges img').hide();
				$('#imageGallery #meds img.med_0').show();
				
				$('#imageGallery img.prevImgArrow').unbind().css('cursor','default').attr({src:'../img/btn_arrow_lft_gry.gif'});
				$('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_wt.gif'}).bind("click", function() { nextImage(0,"foo",false); });
				$('#imageGallery img.nextImgArrow').bind("mouseover", function() { $('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_rd.gif'}); });
				$('#imageGallery img.nextImgArrow').bind("mouseout", function() { $('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_wt.gif'}); });

				var caption = $('#imageGallery img.med_0').attr("alt");
				$('#imageGallery #caption p').text(caption);
				
				$('#imgNo .imgOf').empty().text("1");
				$('#toggles').show();
				$('#enlarge').show().unbind().bind("click", function() { enlargeImage(0); });
				$('#enlarge img').attr({src:'../img/icn_enlarge_wt.gif'});
				$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_rd.gif'}); });
				$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_wt.gif'}); });
				
				$('#imageGallery #meds').SlideInUp(400);
				$('#imageGallery #caption').slideDown(300);
			});
		});
		$('#linearIcn').bind("mouseover", function() { $('#linearIcn').attr({src:'../img/galleries/icn_linear_rd.gif'}); });
		$('#linearIcn').bind("mouseout", function() { $('#linearIcn').attr({src:'../img/galleries/icn_linear_wht.gif'}); });
		
		$('#gridIcn').css('cursor','pointer');
		$('#gridIcn').click(function() {
			$('#imageGallery #caption').slideUp(300);
			$('#imageGallery #meds').SlideOutUp(400, function() {
				$('#toggles').hide();
				$('#imageGallery #thumbs').SlideInUp(400);
				$('#imageGallery #gridCaption').slideDown(300);
			});
		});
		$('#gridIcn').bind("mouseover", function() { $('#gridIcn').attr({src:'../img/galleries/icn_grid_rd.gif'}); });
		$('#gridIcn').bind("mouseout", function() { $('#gridIcn').attr({src:'../img/galleries/icn_grid_wht.gif'}); });
		
		$('a.bookThumb img').bind("mouseover", function(data) {
			//alert("test"+$(this).height());
			//show red bar
			var thumbLeft = $(this).offset().left;
			var thumbTop = $(this).offset().top;
			var thumbHeight = $(this).height();
			var thumbWidth = $(this).width();
			$('#redHooverBar').css({position:'absolute',top:thumbTop+thumbHeight-3,left:thumbLeft,width:thumbWidth}).show();
		});
		$('a.bookThumb img').bind("mouseout", function(data) {
			$('#redHooverBar').hide();
		});
}

function prevImage(n,type,grid) {
	$('#imageGallery .prevImgArrow').unbind();
	$('#imageGallery .nextImgArrow').unbind();
	$('#enlarge').unbind();
	$(document).unbind();
	
		$('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_wt.gif'});
		$('#imageGallery .nextImgArrow').bind("mouseover", function() { $('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_rd.gif'}); });
		$('#imageGallery .nextImgArrow').bind("mouseout", function() { $('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_wt.gif'}); });

		n--;
		if(n > 0) {
			$('#imageGallery img.prevImgArrow').css('cursor','pointer');
			if(type == "large") {
				$('#imageGallery .prevImgArrow').bind("click", function() { prevImage(n,"large",grid); });
				
			} else {
				$('#imageGallery .prevImgArrow').bind("click", function() { prevImage(n); });
			}
			$('#imageGallery .prevImgArrow').bind("mouseover", function() { $('#imageGallery img.prevImgArrow').attr({src:'../img/btn_arrow_lft_rd.gif'}); });
			$('#imageGallery .prevImgArrow').bind("mouseout", function() { $('#imageGallery img.prevImgArrow').attr({src:'../img/btn_arrow_lft_wt.gif'}); });
		} else {
			//swap its source with the grey arrow
			$('#imageGallery .prevImgArrow').attr({src:'../img/btn_arrow_lft_gry.gif'});
			$('#imageGallery .prevImgArrow').css('cursor','default');
		}
		$('#imgNo .imgOf').empty().text(n+1);
		$('#imageGallery .nextImgArrow').css('cursor','pointer');
		
		//determine which image is currently active in list
		if(type == "large") {
			var activeImg = $('#imageGallery img.large:visible');
			$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n,"large",grid); });
			if(grid) {
				$('#enlarge').bind("click", function() { closeGridEnlarge(n); });
			} else {
				$('#enlarge').bind("click", function() { closeEnlarge(n); });
			}
			
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_close_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_close_wt.gif'}); });
			
			activeImg.fadeOut(250, function() {
				$('#imageGallery img.lg_'+n).fadeIn(250);
			});
		} else {
			var activeImg = $('#imageGallery img.med:visible');
			$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n); });
			$('#enlarge').bind("click", function() { enlargeImage(n); });
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_wt.gif'}); });
			
			activeImg.fadeOut(300, function() {
				$('#imageGallery img.med_'+n).fadeIn(300);
			});
		}
		activeImg.fadeOut(250, function() {
			activeImg.parent().prev().chid().fadeIn(250);
		});
		
		$('#imageGallery #caption p').animate({'color':'#000'},250,function() {
			var caption = "";
			caption = $('#imageGallery img.med_'+n).attr("alt");
			if(caption!=undefined) {
				$('#imageGallery #caption p').text(caption);
				$('#imageGallery #caption p').animate({'color':'#fff'},250);
			}
		});
}
function nextImage(n,type,grid) {
	$('#imageGallery .prevImgArrow').unbind();
	$('#imageGallery .nextImgArrow').unbind();
	$('#enlarge').unbind();
	$(document).unbind();
		if(n==0) {
			$('#imageGallery img.prevImgArrow').attr({src:'../img/btn_arrow_lft_wt.gif'});
		} 
		n++;
		$('#imageGallery img.prevImgArrow').bind("mouseover", function() { $('#imageGallery img.prevImgArrow').attr({src:'../img/btn_arrow_lft_rd.gif'}); });
		$('#imageGallery img.prevImgArrow').bind("mouseout", function() { $('#imageGallery img.prevImgArrow').attr({src:'../img/btn_arrow_lft_wt.gif'}); });
		
		if(n+1 == $('#imgNo .imgTotal').text()) {
			$('#imageGallery .nextImgArrow').css('cursor','default');
			$('#imageGallery .nextImgArrow').attr({src:'../img/btn_arrow_rt_gry.gif'});
			
		} else {
			$('#imageGallery .nextImgArrow').css('cursor','pointer');
			if(type == "large") {
				$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n,"large",grid); });
				
			} else {
				$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n); });
			}
			$('#imageGallery .nextImgArrow').bind("mouseover", function() { $('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_rd.gif'}); });
			$('#imageGallery .nextImgArrow').bind("mouseout", function() { $('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_wt.gif'}); });
		}
		$('#imgNo .imgOf').empty().text(n+1);
		$('#imageGallery .prevImgArrow').css('cursor','pointer');
		if(type == "large") {
			var activeImg = $('#imageGallery img.large:visible');
			$('#imageGallery .prevImgArrow').bind("click", function() { prevImage(n,"large",grid); });
			if(grid) {
				$('#enlarge').bind("click", function() { closeGridEnlarge(n); });
			} else {
				$('#enlarge').bind("click", function() { closeEnlarge(n); });
			}
			
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_close_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_close_wt.gif'}); });
			
			activeImg.fadeOut(300, function() {
				$('#imageGallery img.lg_'+n).fadeIn(300);
			});
			
		} else {
			var activeImg = $('#imageGallery img.med:visible');
			$('#imageGallery .prevImgArrow').bind("click", function() { prevImage(n); });
			$('#enlarge').bind("click", function() { enlargeImage(n); });
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_wt.gif'}); });
			
			activeImg.fadeOut(250, function() {
				$('#imageGallery img.med_'+n).fadeIn(250);
			});
		}
		
		$('#imageGallery #caption p').animate({'color':'#000'},250,function() {
			var caption = "";
			caption = $('#imageGallery img.med_'+n).attr("alt");
			if(caption!=undefined) {
				$('#imageGallery #caption p').text(caption);
				$('#imageGallery #caption p').animate({'color':'#fff'},250);
			}
		});
}
function enlargeImage(n) {
	
	$('#imageGallery #meds img.med_'+n).SlideOutUp(500, function() {
		$('#imageGallery .prevImgArrow').unbind();
		$('#imageGallery .nextImgArrow').unbind();
		$('#enlarge').unbind();
		
		$('#imageGallery #caption').slideUp(250, function() {
			if(n > 0) {
				$('#imageGallery img.prevImgArrow').css('cursor','pointer');
				$('#imageGallery img.prevImgArrow').bind("click", function() { prevImage(n,"large"); });
			} 
			if(n < $('#imgNo .imgTotal').text()) {
				$('#imageGallery .nextImgArrow').css('cursor','pointer');
				$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n,"large"); });
			}
			$('#toggles').hide(10);
			//replace enlarge button with X and bind it to closeEnlarge
			$('#enlarge img').attr({src:'../img/icn_close_wt.gif'});
			$('#enlarge').bind("click", function() { closeEnlarge(n); });
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_close_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_close_wt.gif'}); });
		});
		
		$('#imageGallery #larges img.lg_'+n).SlideInUp(550, function() {
			$('#imageGallery #caption').slideDown(350);
		});
	});
}
function enlargeGridImage(n,copy) {
	$('#enlarge').unbind();
	$('#imageGallery .prevImgArrow').unbind();
	$('#imageGallery .nextImgArrow').unbind();
	
	$('#redHooverBar').fadeOut(150,function() {
		
		$('#imageGallery #gridCaption p').fadeOut(150, function() {
		
			$('#imageGallery #thumbs').slideUp(400, function() {
				$('#imageGallery #gridCaption').slideUp(200);
				if(n > 0) {
					$('#imageGallery img.prevImgArrow').css('cursor','pointer').attr({src:'../img/btn_arrow_lft_wt.gif'}).bind("click", function() { prevImage(n,"large",true); });
					$('#imageGallery img.prevImgArrow').bind("mouseover", function() { $('#imageGallery img.prevImgArrow').attr({src:'../img/btn_arrow_lft_rd.gif'}); });
					$('#imageGallery img.prevImgArrow').bind("mouseout", function() { $('#imageGallery img.prevImgArrow').attr({src:'../img/btn_arrow_lft_wt.gif'}); });
				} 
				if(parseInt(n) < parseInt($('#imgNo .imgTotal').text())) {
					$('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_wt.gif'});
					$('#imageGallery .nextImgArrow').css('cursor','pointer').bind("click", function() { nextImage(n,"large",true); });
				} else {
					$('#imageGallery .nextImgArrow').css('cursor','default');
					$('#imageGallery img.nextImgArrow').attr({src:'../img/btn_arrow_rt_gry.gif'});
				}
				$('#enlarge img').attr({src:'../img/icn_close_wt.gif'});
				$('#enlarge').show().bind("click", function() { closeGridEnlarge(n); });
				$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_close_rd.gif'}); });
				$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_close_wt.gif'}); });
				
				$('#toggles').hide();
				$('#grid').hide();
				
				$('#imageGallery #caption p').empty().text(copy);
				var num = parseInt(n)+1;
				$('#imgNo .imgOf').empty().text(num);
				
				$('#imageGallery #larges img.lg_'+n).SlideInUp(550, function() {
					$('#imageGallery #caption').slideDown(350);
				});
			});
		});
	});	
}

function closeEnlarge(n) {
	
	$('#imageGallery #larges img.lg_'+n).SlideOutUp(500, function() {
		$('#imageGallery #caption').slideUp(200, function() {
			$('#imageGallery .prevImgArrow').unbind();
			$('#imageGallery .nextImgArrow').unbind();
			$('#enlarge').unbind();
			if(n > 0) {
				$('#imageGallery img.prevImgArrow').css('cursor','pointer');
				$('#imageGallery img.prevImgArrow').bind("click", function() { prevImage(n); });
			} 
			if(n < $('#imgNo .imgTotal').text()) {
				$('#imageGallery .nextImgArrow').css('cursor','pointer');
				$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n); });
			}
			$('#toggles').show();
			$('#enlarge img').attr({src:'../img/icn_enlarge_wt.gif'});
			$('#enlarge').bind("click", function() { enlargeImage(n); });
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_wt.gif'}); });
			
			$('#imageGallery #meds img.med_'+n).SlideInUp(550, function() {
				$('#imageGallery #caption').slideDown(350);
			});
		});
	});
}

function closeGridEnlarge(n) {
	$('#enlarge').unbind();
	$('#imageGallery #larges img.lg_'+n).SlideOutUp(550, function() {
		$('#imageGallery #caption').slideUp(350);
		$('#enlarge').hide();
		$('#grid').show();
		$('#imageGallery #thumbs').SlideInUp(550, function() {
			$('#imageGallery #gridCaption').slideDown(350);
		});
	});
}