var curImgID = 0;

function makeGalleryImage(container,j) {
	j.each(function() {
	 var link = this;
	  $(this).hover(function() {
	   $("span",this).addClass("jsover");
    },function() {
	   $("span",this).removeClass("jsover");
    });
		$(link).click(function() {
			var img_index = $("a",container).index(link);
			$("#cur_photo > img.full").attr({ "image_index":img_index });
			moveToImage(0);
			return false;        
		});
	});
}

function moveToImage(direction) {
	var full_image  = $("#cur_photo > img.full")[0];
	var container = $("#right_photos")[0];
	if (!container)
	 return;
	var img_index = parseInt($(full_image).attr("image_index"))+direction;
//	var row_index = parseInt($(full_image).attr("row_index"));
	var newimg = $("a:eq("+img_index+") img",container)[0];
//	alert(newimg);
	if (!newimg) {
	 newimg = $("a:eq("+img_index+") div",container)[0];
  }
	if (!newimg)
	 return;
	$(">.selected",container).removeClass("selected");
	$(newimg).parent().parent().addClass("selected");
	if (container.scrollTo)
	 container.scrollTo("li:eq("+img_index+")");
	var flag = 0;
	if (img_index<=0) {
		$("#prev_fullimage").hide();
		flag++;
	}	else {
		$("#prev_fullimage").show();
	}
 	if (img_index>=$("a",container).length-1) {
		$("#next_fullimage").hide();
		flag++;
	} else {
		$("#next_fullimage").show();
	}
	$("#cur_photo_num").text(img_index+1);
	$("#cur_photo .loading").hide();
	var imgval = setInterval(function() { 
			$("#cur_photo .loading").show();
			$(full_image).hide();
			clearInterval(imgval); 
			imgval = null; 
		},100);
	$.preload([ $(newimg).attr("fullsrc") ], {
		onFinish: function(data) {
			clearInterval(imgval);
			imgval = null;
			$("#cur_photo > .loading").hide(); 
			$(full_image).attr({ "image_index":img_index,"src":data.image }).fadeIn(300);
		}
	});
	return false;
}


var ival = null;

function getPositionLite(el)
{
	var x = 0, y = 0;
	while(el) {
		x += el.offsetLeft || 0;
		y += el.offsetTop || 0;
		el = el.offsetParent;
	}
	return { x:x, y:y };
}

function getSizeLite(el){
		return {
			wb:el.offsetWidth||0,
			hb:el.offsetHeight||0
		};
}

function hideChildMenu() { 
  $("#childs_cont ul.childsmenu").hide();
  if (!ival)
    return;
  clearInterval(ival); 
  ival = null; 
}

function bindMenu() {
  var cc = $("#childs_cont")[0];
  if (!cc)
    return;  
  var mainpos = getPositionLite(cc);
  var mainsize = getSizeLite(cc);
//  alert(mainpos.x+" "+mainsize.wb);;
  $("#mainmenu a[showChilds]").click(function() {
    return false;
  }).hover(function() {
    hideChildMenu();
    var child_name = $(this).attr("showChilds");
    pos = getPositionLite(this);
    size = getSizeLite(this)
//    alert(child_name+" "+$("#childs_cont ul#child_"+child_name).html());
    $("#childs_cont ul#child_"+child_name).css("right",mainsize.wb-pos.x+mainpos.x-size.wb).show();    
  },function() {
    ival = setInterval(hideChildMenu,400);
  });
  $("#childs_cont").hover(function() {
    if (!ival)
      return;
    clearInterval(ival);
    ival = null;
  },function() {
    ival = setInterval(hideChildMenu,400);
  });
}

function onEmailFocus() {
    if (this.value=="Ваш e-mail" || this.value=="Your e-mail") {
      this.defValue = this.value; 
      this.value='';
    }
}

function onEmailBlur() {
    if (this.value==this.defValue || this.value=='' || !this.value)
      this.value=this.defValue;
}

$(document).ready(function() {
  bindMenu();
  $("#subscribe_form input[name='subscriber_email']").bind("focus",onEmailFocus).blur(onEmailBlur);
  $("#contact_email").bind("focus",onEmailFocus).blur(onEmailBlur);

  var time_cont = $("#region-time")[0];
  if (time_cont) {
    var elapsed = 10;
    rval = setInterval(function() {
      elapsed--;
      $(time_cont).text("00:0"+elapsed);
      if (!elapsed) {
        clearInterval(rval);
        rval = null;
        var cur_region = $("#cur_region")[0].value ? $("#cur_region")[0].value : 1; 
        document.location.href="/choose-region/"+cur_region+"/";
      }
    },1000);
  }
  var gallery_cont = $("#right_photos")[0];
  if (gallery_cont) {
    $(gallery_cont).jScrollPane({ scrollbarWidth:15,scrollbarMargin:20 });
  	$.preload( '#cur_photo img');
  	var ival = setInterval(function() { 
  	    $(gallery_cont).jScrollPane({ scrollbarWidth:15,scrollbarMargin:20 })
  		},1000);
  	
  	$.preload( '#right_photos img',{ 
      placeholder:'/i/blank.gif',onFinish:function() {
        ival = clearInterval(ival); 
  			ival = null; 
        setTimeout(function() { $(gallery_cont).jScrollPane({ scrollbarWidth:15,scrollbarMargin:20 }) },1000);
      }
    });
    makeGalleryImage(gallery_cont,$("a",gallery_cont));
  	$("#prev_fullimage").click(function() { moveToImage(-1); return false; });
  	$("#next_fullimage").click(function() { moveToImage(+1); return false; });
	}
});