var lang="ru";
var interval_scroll=500;
//var amount_scroll=350;
var amount_scroll=154*2+28;
var ordering=0;

function show_cart() {
   hs_alert();
}

function make_order(u) {
   if (ordering) return;
   ordering=1;
   $.ajax({
      url: u,
      type: "GET",
      data: "",
      success : function (v) {
         show_cart();
         ordering=0;
      }
   });
}

function zoom_image(url, big, t) {
  //alert(url+" "+big);
  var img;
  var big;
  url="/upload/"+url;
  //alert(url);
  $("#preview_image").fadeOut("slow", function() {
    $("#preview_image").show();
    img="<img src=\""+url+"\" id=\"preview\" width=\"220\" height=\"313\" />";
    if (big) {
      big="/upload/"+big;
      $("#preview_image").html("<div class=\"highslide\"><a href=\""+big+"\" onclick=\"hs.expand(this, {align:'center'}); return false;\">"+img+"</a>"+($(t).parent().find(".descr").length ? "<div class=\"highslide-caption hs_descr\">"+$(t).parent().find(".descr").html()+"</div>" : "")+"</div>");
    }
    else {
      $("#preview_image").html(img);
    }
    $("#preview").css("visibility", "hidden");
    $("#preview").bind("load", function() {
      //alert();
      $("#preview").hide();
      $("#preview").css("visibility", "visible");
      $("#preview").fadeIn("slow");
      //$("#preview").css("visibility", "visible");
      //$("#preview_image").fadeIn();
    });
  });
}

var hovered=new Array();
var timeouts=new Array();
var showing=new Array();
var speed_show=200;
function hide_elem(id,p) {
   if (!p) {
      hovered[id]=0;
      if (timeouts[id]) clearTimeout(timeouts[id]);
      timeouts[id]=setTimeout('hide_elem("'+id+'",1);', 500);
      return;
   }
   if (!hovered[id]) {
      $("#"+id).hide();
      showing[id]=0;
   }
}

var mouseX = 0;
var mouseY = 0;

$(document).ready(function() {
   var io;
   var e;
   $("a.highslide").click(function() {
      return hs.expand(this);
   });
   if ($.validationEngine) {
      $(".valForm").validationEngine({
         beforeSend : function () {
            $(".valForm").find("input[type=submit]").val( (lang=="ru"?"Пожалуйста, подождите":lang=="en"?"Please wait":"Будь ласка, зачекайте")+"...").attr('disabled', 'disabled');
         },
         async: true,
         ajaxSubmit: true,
         ajaxSubmitFile: $(".valForm").attr('action'),
         failure : function() {}
      });
   }

   io="content";
   if ($("#rightCol_gall").length) {
      io="rightCol_gall";
   }
   if ($("#rightCol_gall_shop").length) {
      io="rightCol_gall_shop";
   }
   else {
      amount_scroll=313;
   }
   $("#down").click(function() {
    e=$('#'+io);
    v=e.scrollTop()+amount_scroll;
    //alert(v+" "+e.attr('scroullHeight'));
    /*m=2;
    if (v>e.height()+amount_scroll) {
      v=e.attr('scrollHeight')-amount_scroll;
      v-=m;
    }*/
    e.animate({
      scrollTop: v
      }, interval_scroll); 
   });

   $("#up").click(function() {
    v=$('#'+io).scrollTop()-amount_scroll;
    $('#'+io).animate({
      scrollTop: v
      }, interval_scroll); 
   });
   
   if ($("#preview_image").length) {
      e=$(".thumb:first");
      e.find("a").click();
   }
   
   $(".expanding").hover(function() {
         var id;
         id=$(this).attr('id');
         /*if (showing[id] && ( $(this).is(':animated') || $(this).is(':visible')) ) {
            //alert("showing");
            return;
         }*/
         showing[id]=1;
         hovered[id]=1;
         submenu=$("#submenu_"+id);
         submenu.css({
            top: $(this).height()
         });
         submenu.unbind('hover');
         submenu.hover(
            function () {
               id=$(this).attr('id');
               hovered[id]=1;
            },
            function () {
               id=$(this).attr('id');
               hide_elem(id);
            }
         );
         submenu.slideDown(speed_show, function() {
            showing[id]=0;
         });
      },
      function() {
         var id;
         id=$(this).attr('id');
         hide_elem("submenu_"+id);
      }
   );
   
   $(".level2").hover(
      function() {
         var id;
         //
         id=$(this).attr('id');
         //if (showing[id]) return;
         showing[id]=1;
         hovered[id]=1;
         submenu=$("#submenu_"+id);
         //alert("#submenu_"+id);
         //alert(submenu.parent().html());
         v=$(this).offset().top-$(this).parent().parent().offset().top;
         //submenu.find("a").html(v);
         submenu.css({
            top: v-$(this).height()+2
         });
         submenu.unbind('hover');
         submenu.hover(
            function () {
               id=$(this).attr('id');
               hovered[id]=1;
            },
            function () {
               id=$(this).attr('id');
               hide_elem(id,1);
            }
         );
         w=submenu.width();
         submenu.css('width', 0);
         submenu.animate({
            width: w
         },speed_show, function() {
            showing[id]=0;
            $(".level2").find("ul").each(function () {
               //document.write($(this).attr('id')+" submenu_"+id);
               if ($(this).attr('id')!=submenu.attr('id')) {
                  $(this).hide();
               }
            });
         });         
      },
      function() {
         var id;
         id=$(this).attr('id');
         $("#submenu_"+id).hide();
         //$(".level2").find("ul").hide();
      }
   );

   $(".level3").hover(
      function() {
      },
      function() {
         var id;
         id=$(this).attr('id');
      }
   );
 
});
var idd='ss';

function close_hs(t) {
   hs.close(t);
}

var opened=0;
function hs_alert(msg, uu) {
   idd=new Date().getTime();
   $(".highslide-html-content").parent().fadeIn('normal');
   msg='<img src="/usr/system/progress-circle.gif" />';
   $(".hs_cont").html(msg);
   if (!$(".hs_cont:visible")[0]) {
      hs.htmlExpand(null, {width: '500', height: '350',
         contentId: "sss",
         transitions: ['fade'],
         objectType: 'iframe',
         useControls: false,
         slideshowGroup: 'group2'
      });
      $(".highslide-html-content").parent().parent().css('border', '2px solid #dec27c');
   }
   if (!hs.htmlExpand) {
      alert(msg);
      return;
   }
   //alert($("#s"+id).html());
   //alert(msg);
   $.ajax({
      url: (uu ? uu+"&ajax=1" : "/"+lang+"/cart/?ajax=1"),
      type: "GET",
      data: "",
      success : function (v) {
         $(".hs_cont:visible").html(v);
      }
   });
}

// contacts

function sendMsg(win){
window.open('',''+win+'','width=500,height=350,left='+(screen.availWidth/2-350)+',top='+(screen.availHeight/2-250)+',resizable=0,scrollbars=0,status=0');
}
function Process(form) {

   e1="email";   
   e2="phone";
   if ($('#'+e1).length && $('#'+e2).length) {
   if(!$('#'+e1).val() && !$('#'+e2).val()){
      alert('Оставьте Ваш e-mail или телефон.');
      $('#'+e1).focus();
      window.event.returnValue=false;
      return false;
   }
   }

   elem_name="comment";
   if(!$('#'+elem_name).val()){
      alert('Оставьте Ваше сообщение.');
      $('#'+elem_name).focus();
      window.event.returnValue=false;
      return false;
   }

   sendMsg('newwin');
   $("#order_form").submit();
   setTimeout("document.forms['order_form'].reset();", 1000);
}
