$(document).ready(function(){	
	try {
		if ($("#promoContainer")) {
			//promo bg rollover functions
			$("#promoContainer").each(function(){								
				$("#promoBg").hover(function(){
					StopPromoTimer();
					$(this).children("img").bind("click", function(){
						var sPromoIndex = this.getAttribute("src").substring(this.getAttribute("src").lastIndexOf(".") - 1, this.getAttribute("src").lastIndexOf("."));
						$(".promo" + sPromoIndex).children("a").trigger("click");
					});
			}, function(){
					StartPromoTimer();
					//$(this).unbind("click");
				});
				
				var parentDiv = $("#promoBg img");
				$("#promoBg").find("img").each(function(i){
					parentDiv = this;
				});
				var BGa = $("#promoBg a");
				$("#promoBg").find("a").each(function(i){
					BGa = this;
				});	
				$("#promo").find("a").each(function(i){
					hsrc[i] = this.getAttribute("href");
				});	
				BGa.setAttribute("href", hsrc[3]);
				$("#promo .promo1 img").hover(function(){
					StopPromoTimer();
					nPromoTimerIndex = 2;
					parentDiv.setAttribute("src", "/bitrix/templates/opengate/promo/promo1.jpg");
					BGa.setAttribute("href", hsrc[0]);
				});
				$("#promo .promo2 img").hover(function(){
					StopPromoTimer();
					nPromoTimerIndex = 3;
					parentDiv.setAttribute("src", "/bitrix/templates/opengate/promo/promo2.jpg");
					BGa.setAttribute("href", hsrc[1]);
				});
				$("#promo .promo3 img").hover(function(){
					StopPromoTimer();
					nPromoTimerIndex = 4;
					parentDiv.setAttribute("src", "/bitrix/templates/opengate/promo/promo3.jpg");
					BGa.setAttribute("href", hsrc[2]);
				});
				$("#promo .promo4 img").hover(function(){
					StopPromoTimer();
					nPromoTimerIndex = 1;
					parentDiv.setAttribute("src", "/bitrix/templates/opengate/promo/promo4.jpg");
					BGa.setAttribute("href", hsrc[3]);
				});

			});

			StartPromoTimer();
		}
	} catch(e) {
	}
});

var nPromoTimerIndex = 2;
var ptrPromoTimer = null;
hsrc = new Array(4);

function StartPromoTimer() {
	StopPromoTimer();
	ptrPromoTimer = setInterval('PromoTimerCallback();', 5000);
}
//$(".promo" + nPromoTimerIndex).children("a").getAttribute("href")
function PromoTimerCallback() {
	$("#promoContainer").each(function(){
		$("#promoBg").find("img").each(function(i){
			this.setAttribute("src", "/bitrix/templates/opengate/promo/promo" + nPromoTimerIndex + ".jpg");
		});
		$("#promoBg").find("a").each(function(i){
			this.setAttribute("href",  hsrc[nPromoTimerIndex-1]);
		});
	});
	if (nPromoTimerIndex == 4) nPromoTimerIndex = 1;
	else nPromoTimerIndex++;
}

function StopPromoTimer() {
	if (ptrPromoTimer) clearInterval(ptrPromoTimer);
	ptrPromoTimer = null;
}
