// JavaScript Document

var imgs=new Array();
var cur=0;
var speed_show_card=500;
var speed_show_cards=1000;
var timer=0;

function set_timer() {
   if (timer) {
      clearTimeout(timer);
   }
   timer = setTimeout("prev_or_next(1);", speed_show_cards);
}

function loading() {
   $("#card").html("<div id=\"loading\"><img src=\"/usr/system/loading.gif\" /></div>");
}

function load_card(n) {
   var img;
   var e;
   e=$("#card").find("img");
   if (e.length) {
      e.fadeOut(speed_show_card, function () {
         $(this).remove();
         load_card(cur);
      });
      return;
   }
   cur=n;
   loading();
   img = $("<img>");
   img.load(function () {
      $("#loading").remove();
      $(this).css({
         'visibility': 'visible'
      });
      $(this).hide();
      $(this).fadeIn(speed_show_card, function () {
         set_timer();
      });
   });
   img.css({
      'visibility': 'hidden'
   });
   img.attr('src', imgs[cur]);
   $("#card").append(img);
}

function prev_or_next(p) {
   cur+=p;
   if (cur==imgs.length) cur=0;
   if (cur==-1) cur=imgs.length-1;
   load_card(cur);
}

function p_l() {
   $("#login_error").html("&nbsp;");
   $.ajax({
      url: "/scripts/ajax_stage11.php",
      type: "POST",
      data: "login="+$("#user_login").val()+"&pass="+$("#user_pass").val()+"&act=login",
      success : function (v) {
         v=v.replace(/\<script((.|\n)*)/g, '').replace(/(^\s+)|(\s+$)/g, "");
         if (v) {
            $("#login_error").html(v);
            $("#user_login").focus();
         }
         else window.location.href="/user/";
      }
   });
}

$(document).ready(function () {
   $(".iconz").click(function() {
      im=$(this).find("img");
      s=im.attr('src');
      st=(/\/1\.gif/.test(s) ? 0 : 1);
      //alert(st);
      cid=$(this).attr('id').replace(/a/, '');
      im.attr('src', s.replace(/\/[01]{1}\.gif/, '/'+st+'.gif'));
      r=$(this).attr('rel');
      //alert(r); return false;
      $.ajax({
         url: "/scripts/ajax_iconz.php",
         type: "GET",
         data: "cid="+cid+"&st="+st+"&t="+r,
         success : function (v) {
            //alert(v);
         },
         error : function () {
            alert("Ошибка при сохранении статуса заказа!");
         }
      });
      return false;
   });
   if ($("#card").length) {
      load_card(0);
   }
   if ($.tooltip) {
      $(".ahelp").tooltip({
         showURL: false
      });
      $(".ahelp").click(function () {
         return false;
      });
   }
   $(".show_prev").click(function () {
      if ($(".previous").is(":visible")) {
         $(".previous").hide();
      }
      else {
         $(".previous").show();
      }
      $(this).html("<b>"+($(".previous").is(":visible") ? 'Скрыть предыдущие заказы' : 'Показать предыдущие заказы')+"</b>");
      return false;
   });
   $("#btn_perf_login").click(p_l);
   $("#user_login").keydown(function (e) {
      if (e.keyCode == 13) p_l();
   });
   $("#user_pass").keydown(function (e) {
      if (e.keyCode == 13) p_l();
   });
});

$(document).ready(function() {
   if (window.location.href.match(/\/(product|prices|contacts)\/./)) {
      arr=window.location.href.split("/");
      v="";
      //for (i=arr.length-1; i>=0; i--) if (arr[i]) v=arr[i];
      for (i=0; i<arr.length; i++) if (arr[i]) v=arr[i];
      //alert(v);
      $(".rightBlock_top").find("a").each(function () {
         if ($(this).html().match(/^[a-zA-Zа-яА-Я -]+$/)) {
            if ( $(this).attr('href').indexOf(v)>0) {
            $(this).css({
               'font-weight': 'bold'
            });
            }
         }
      });
   }
// Preload all rollovers and apply active status
  $("a.vt_autorollover").each(function() {
    rollsrc =  $(this).children("img").attr("src");
    rollON = rollsrc.replace(/_off./ig,"_on.");
	$("<img>").attr("src", rollON);
	if ( $(this).hasClass("act") ) {$(this).children("img").attr("src", rollON);};
  });
//Rollover images
  $("a.vt_autorollover img").hover(
    function() {
	  imgsrc = this.src;
      matches = imgsrc.match(/_on./);
      if (!matches) {
        imgsrcON = imgsrc.replace(/_off./ig,"_on.");
        this.src = imgsrcON;
      }
	},
    function() {this.src = imgsrc;}
  );
//Dropdown menu
    $('ul.vt_dropdown li').hover(
        function() {
            $(this).find('ul.submenu').slideDown(200);
        },
        function() {
            $(this).find('ul.submenu').slideUp(1);
        }
    );
});
