var typeVid = '';
var myRe = /menu([1-9][0-9]*)/;
var mySubRe = /submenu([1-9][0-9]*)/;
var sm = /submenu([1-9][0-9]*)/;
var tmL = /topmenuL([1-9][0-9]*)/;
var tm = /topmenu([1-9][0-9]*)/;
var tmR = /topmenuR([1-9][0-9]*)/;
var lev = /level([0-9]+)/;
var catReg = /(catalogf(_([1-9][0-9]*))?)((_(art|name|namef|price)_(asc|desc))?(_page_([1-9][0-9]*))?\.htm)/;
var myArr;
var tmpObj;

$(document).ready(function() {
   $(".pngFix").pngFix();
   checkFlash();
   checkGallery();
   checkKeys();
   Carusel();
   $('#menuLeft').ptMenu({vertical:true});
   $(".smLeft").hover(
      function() {
         $(this).next().removeClass('smLeftB').addClass('smLeftBSel');
         tmpObj = $(this).parent().parent().prev();
         while (tmpObj && tmpObj.hasClass('smLeftB')) {
            tmpObj.removeClass('smLeftB').addClass('smLeftBSel');
            tmpObj = tmpObj.parent().parent().prev();
         }
      },
      function() {
         $(this).next().removeClass('smLeftBSel').addClass('smLeftB');
         tmpObj = $(this).parent().parent().prev();
         while (tmpObj && tmpObj.hasClass('smLeft')) {
            tmpObj.removeClass('smLeftBSel').addClass('smLeftB');
            tmpObj = tmpObj.parent().parent().prev();
         }
      }
      ).
      click(function() { document.location.href = $(this).children().children().attr('href'); }
   );
   $(".submenuTop").hover(
      function() {
         myArr = sm.exec($(this).attr('id'));
         displayMenu2(myArr[1]);
      },
      function() {
         myArr = sm.exec($(this).attr('id'));
         hideMenu2(myArr[1]);
      }
   );
   $(".smCenter").hover(
      function() { $(this).css('background-color', '#777E83'); },
      function() { $(this).css('background-color', '#8D9392'); }
   );
   $(".mTopC").hover(
      function() {
         $(this).prev().removeClass('mTop').addClass('mTopSel').children().attr('src', 'image/m-left-a.gif');
         $(this).removeClass('mTop').addClass('mTopSel');
         $(this).next().removeClass('mTop').addClass('mTopSel').children().attr('src', 'image/m-right-a.gif');
         myArr = tm.exec($(this).attr('id'));
         displayMenu2(myArr[1]);
      },
      function() {
         $(this).prev().removeClass('mTopSel').addClass('mTop').children().attr('src', 'image/m-left.gif');
         $(this).removeClass('mTopSel').addClass('mTop');
         $(this).next().removeClass('mTopSel').addClass('mTop').children().attr('src', 'image/m-right.gif');
         myArr = tm.exec($(this).attr('id'));
         hideMenu2(myArr[1]);
      });
   $(".mTopL").hover(
      function() {
         $(this).removeClass('mTop').addClass('mTopSel').children().attr('src', 'image/m-left-a.gif');
         $(this).next().removeClass('mTop').addClass('mTopSel');
         $(this).next().next().removeClass('mTop').addClass('mTopSel').children().attr('src', 'image/m-right-a.gif');
         myArr = tmL.exec($(this).attr('id'));
         displayMenu2(myArr[1]);
      },
      function() {
         $(this).removeClass('mTopSel').addClass('mTop').children().attr('src', 'image/m-left.gif');
         $(this).next().removeClass('mTopSel').addClass('mTop');
         $(this).next().next().removeClass('mTopSel').addClass('mTop').children().attr('src', 'image/m-right.gif');
         myArr = tmL.exec($(this).attr('id'));
         hideMenu2(myArr[1]);
      });
   $(".mTopR").hover(
      function() {
         $(this).removeClass('mTop').addClass('mTopSel').children().attr('src', 'image/m-right-a.gif');
         $(this).prev().removeClass('mTop').addClass('mTopSel');
         $(this).prev().prev().removeClass('mTop').addClass('mTopSel').children().attr('src', 'image/m-left-a.gif');
         myArr = tmR.exec($(this).attr('id'));
         displayMenu2(myArr[1]);
      },
      function() {
         $(this).removeClass('mTopSel').addClass('mTop').children().attr('src', 'image/m-right.gif');
         $(this).prev().removeClass('mTopSel').addClass('mTop');
         $(this).prev().prev().removeClass('mTopSel').addClass('mTop').children().attr('src', 'image/m-left.gif');
         myArr = tmR.exec($(this).attr('id'));
         hideMenu2(myArr[1]);
      });
   $(".mTopCSel").hover(
      function() {
         myArr = tm.exec($(this).attr('id'));
         displayMenu2(myArr[1]);
      },
      function() {
         myArr = tm.exec($(this).attr('id'));
         hideMenu2(myArr[1]);
      });
   $(".mTopLSel").hover(
      function() {
         myArr = tmL.exec($(this).attr('id'));
         displayMenu2(myArr[1]);
      },
      function() {
         myArr = tmL.exec($(this).attr('id'));
         hideMenu2(myArr[1]);
      });
   $(".mTopRSel").hover(
      function() {
         myArr = tmR.exec($(this).attr('id'));
         displayMenu2(myArr[1]);
      },
      function() {
         myArr = tmR.exec($(this).attr('id'));
         hideMenu2(myArr[1]);
      });
});

onload = bodySize;
onresize = bodyReSize;

function bodySize() {
   if (($.browser.msie || $.browser.safari) && (document.documentElement.clientWidth || document.body.clientWidth)) {
      $("#tablemain").css('width', ((document.documentElement.clientWidth || document.body.clientWidth) > 1258) ? 1258 : "100%");
   }
	if($('#logos').innerWidth() > 1109) {
		$('#logos').css('width', 1109);
	}
}


function bodyReSize() {
   var newWidth;
   if ($.browser.msie || $.browser.safari) {
	if(document.documentElement.clientWidth >= 1258 || document.body.clientWidth >= 1258) {
		$("#tablemain").css('width', 1258);//for ie7
		newWidth = $("#tablemain").innerWidth() - 61;
	}
	else 
	newWidth = $("body").innerWidth() - 61;
   }
   else {
	if(document.documentElement.clientWidth >= 1258 || document.body.clientWidth >= 1258) newWidth = $("#tablemain").innerWidth() - 65;
	else newWidth = $("body").innerWidth() - 65;
   }
   if ($("#logos").length) {
      var widthL = newWidth - 84;
      var n = Math.round(widthL / 167);
      $("#logos").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: n
      }).css('width', widthL);
   }
   $(".carusel").css('width', newWidth);
   if (($.browser.msie || $.browser.safari) && (document.documentElement.clientWidth || document.body.clientWidth)) {
      $("#tablemain").css('width', ((document.documentElement.clientWidth || document.body.clientWidth) > 1258) ? 1258 : document.body.clientWidth);
   }
}

function viewItems(flag, page) {
   showLoading();
   typeVid = (flag == 1 ? 'table' : 'vitrina');
   dataSend = 'type='+(flag == 1 ? 'table' : 'vitrina')+'&page='+(page ? page : 1);
   $.ajax({
    type: "POST",
    url: "scripts/viewitems.php",
    data: dataSend,
    success: function(response) {
       if (response) $("#contentItems").html(response);
       closeProgress();
    }
   });
   $("a").each(function() {
      var href = $(this).attr('href');
      if (href.match(/^catalogf.+/) && !href.match(/_vitrina/)) {
         myArr = catReg.exec(href);
         $(this).attr('href', myArr[1]+'_vitrina'+myArr[4]);
      }
      else if (href.match(/^catalogf.+/) && href.match(/_vitrina/)) $(this).attr('href', href.replace(/_vitrina/, ''));
   });
   if (flag == 1) {
      $("#typeTable").attr('src', 'image/form-table-a.gif');
      $("#typeVitrina").attr('src', 'image/form-thumbnaisl.gif');
   }
   else if (flag == 2) {
      $("#typeTable").attr('src', 'image/form-table.gif');
      $("#typeVitrina").attr('src', 'image/form-thumbnaisl-a.gif');
   }
}

function fnCheckSearchForm(f) {
   if (!f.search.value.match(/^.+$/) || f.search.value == searchText) {
      alert("Укажите поисковый запрос!");
      f.search.focus();
      return false;
   }
   return true;
}

function checkFilterForm(f) {
   if (f.typeStr.value > 0) f.action = 'catalogf_'+f.typeStr.value+(typeVid == 'vitrina' ? '_vitrina' : '')+'.htm';
   else f.action = 'catalogf'+(typeVid == 'vitrina' ? '_vitrina' : '')+'.htm';
   f.submit();
   return true;
}

function fnCheckRegistrationForm(f) {
   if (!f.fio.value.match(/(.+)/)) {
      alert("Укажите ФИО!");
      f.fio.focus();
      return false;
   }
   if (!f.email.value.match(/^[0-9A-Za-z._-]+@([0-9a-z_-]+\.)+[a-z]{2,4}$/)) {
      alert("Укажите правильный E-mail!");
      f.email.focus();
      return false;
   }
   if (!f.password.value.match(/(.+)/)) {
      alert("Укажите пароль!");
      f.password.focus();
      return false;
   }
   if (f.password.value != f.re_password.value) {
      alert("Подтверждение пароля не совпадает с основным паролем!");
      f.re_password.focus();
      return false;
   }
   return true;
}

function checkOrderForm(idF) {
   var f = eval('document.ordersForm'+idF);
   if (fnCheckOrders(f)) {
      dataSend = 'id='+idF+'&count='+$("input[name='items["+idF+"]']").val();
      $.ajax({
       type: "POST",
       url: "scripts/ordersdo.php",
       data: dataSend,
       success: handleServerResponse
      });
      /*$("#dialog").text('В корзину помещена позиция');
      $("#dialog").dialog({
         title: 'Заказ товаров'
      });*/
   }
   return false;
}

function handleServerResponse(response) { $("#basket").html(response); }

function fnCheckOrders(obj) {
   var is_ok = false;
   for (i = 0; i < obj.length; i++) {
      if (obj[i].type == "text") {
         if (!obj[i].value.match(/^([1-9][0-9]*)?$/)) {
            alert("Введено некорректное значение!");
            obj[i].focus();
            return false;
         }
         if (is_ok == false) {
            if (obj[i].value) {
               is_ok = true;
            }
         }
      }
   }
   if (is_ok == false) {
      alert("Укажите количество заказываемых позиций!");
      return false;
   }
   return true;
}

function fnCheckOrderForm(f) {
   if (!f.fio.value.match(/(.+)/)) {
      alert('Укажите ФИО!');
      f.fio.focus();
      return false;
   }
   if (!f.phones.value.match(/(.+)/)) {
      alert('Укажите контактные телефоны!');
      f.phones.focus();
      return false;
   }
   if (!f.email.value.match(/^[a-zA-Z0-9._-]+\@[^\s:,]+\.[^\s:,]+$/i)) {
      alert('Укажите правильный e-mail!');
      f.email.focus();
      return false;
   }
   if (!f.address.value.match(/(.+)/)) {
      alert('Укажите адрес!');
      f.address.focus();
      return false;
   }
   return true;
}

function displayMenu2(idM) {
   if ($("#submenu"+idM).length && $("#submenu"+idM).css('display') == 'none') {
      var obj = $("#topmenuL"+idM);
      var offset = obj.offset();
      $("#submenu"+idM).css({'left': offset['left'], 'top': (offset['top'] + 22), 'display': 'block'});
   }
}

function hideMenu2(idM) {
   if ($("#submenu"+idM).length) $("#submenu"+idM).css('display', 'none');
}

function showBigPic(idP) {
   if ($("#bigPic").children().children().length) $("#bigPic").children().children().attr('src', $("#pic"+idP).children().children().attr('src'));
   if ($("#bigPic").children().length) $("#bigPic").children().attr('href', $("#pic"+idP).children().attr('href'));
   $(".picInFancy").each(function() { $(this).attr('rel', 'fancy'); });
   if ($("#pic"+idP).children().length) $("#pic"+idP).children().attr('rel', 'fancyTmp');
}

function Carusel() {
   if ($("#logos").length) {
      var widthL = ($(".carusel").width() - 84);
      var n = Math.round(widthL / 167);
      $("#logos").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: n
      }).css('width', widthL);

   }
}

function setFabrics(idF) {
   document.logosForm.brand.value = idF;
   document.logosForm.submit();
}

function GetNull(){
	if ($("#brand").length) {
      $("#brand").attr('value', 0);
	   $("#filterForm").submit();
      return false;
   }
   return true;
}

function GetAtherList(id, level) {
   var next = level + 1;
   var tmpStr = '';
   if (id != 0) $('input[name=typeStr]').attr('value', id);
   if (id == 0) {
      tmpStr = "type"+(level - 1);
      $('input[name=typeStr]').attr('value', ($("#"+tmpStr).length ? $("#"+tmpStr).val() : 0));
   }
   $.ajax({
       type: "POST",
       url: "scripts/levels.php",
       data: 'id='+id+'&level='+level+'&brand='+$("#brand").val(),
       success: function(response) { $("#level"+next).html(response).addClass('padb5'); }
   });
   $.ajax({
       type: "POST",
       url: "scripts/getbrands.php",
       data: 'id='+$('input[name=typeStr]').val()+'&brand='+$("#brand").val(),
       success: function(response) { $("#brandDiv").html(response); }
   });
   $(".filterLevel").each(function() {
      myArr = lev.exec($(this).attr('id'));
      if (myArr[1] > level) $(this).removeClass('padb5').empty();
   });
}

function GetTypesList() {
   $.ajax({
       type: "POST",
       url: "scripts/gettypes.php",
       data: 'id='+$('input[name=typeStr]').val()+'&brand='+$("#brand").val(),
       success: function(response) { $("#typesSelect").html(response); }
   });
}
