var is_ie = (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
function GID(id) { return (is_ie) ? document.all[id] : document.getElementById(id); }

$(document).ready(function() {
   $(document).pngFix();
   checkFlash();
   checkGallery();

   GetTabsHeight();

    if ($('.scroll-pane').length) {
        $('.scroll-pane').each(
            function()
            {
                $(this).jScrollPane(
                    {
                        showArrows: $(this).is('.arrow')
                    }
                );
                var api = $(this).data('jsp');
                var throttleTimeout;
                $(window).resize(function()
                    {
                        if ($.browser.msie) {
                            if (!throttleTimeout) {
                                throttleTimeout = setTimeout(
                                    function()
                                    {
                                        api.reinitialise();
                                        throttleTimeout = null;
                                    },
                                    50
                                );
                            }
                        } else {
                            api.reinitialise();
                        }
                    }
                );
            }
        )
    }


   $('.closeBut').click(function(){
    $('#SHNav').slideUp('fast');
    $('table.mt23').attr('id','NoTop');
    $('div.topIcons').attr('id','NoTop');
   });

    if ($('.jCarouselLite').length){
        $(".jCarouselLite").jCarouselLite({
            btnNext: ".next",
            btnPrev: ".prev",
            visible:1,
            vertical: true
        });
    }

    $('.TWrap').mouseover(function(){
        $(this).addClass('ActiveTabHover');
    });

    $('.TWrap').mouseout(function(){
        $(this).removeClass('ActiveTabHover');
    });

   $.fn.alignCenter = function() {
      var marginLeft = - $(this).width()/2 + 'px';
      var marginTop = - $(this).height()/2 + 'px';
      return $(this).css({'margin-left': marginLeft, 'margin-top': marginTop});
   };
   $.fn.togglePopup = function(){
      if ($('#popup').hasClass('hidden')) {
         if ($.browser.msie) {
            $('#opaco').height($(document).height()).toggleClass('hidden').click(function() { $(this).togglePopup(); } );
            $('#popup').css({'position': 'absolute', 'left': ($(document).width() / 2), 'top': ($(window).height() / 2)});
         }
         else {
            $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7).click(function() { $(this).togglePopup(); } );
            $('#popup').css({'position': 'fixed', 'left': '50%', 'top': '50%'});
         }
         $('#popup').alignCenter().toggleClass('hidden');
      }
      else {
         $('#opaco').toggleClass('hidden').removeAttr('style').unbind('click');
         $('#popup').toggleClass('hidden');
      }
   };
});

function GetTabsHeight(){
    if ($('.TWrap').length){
        $('.TWrap').each(function(i){
            if (i == 0) MaxHeight = $('#TWrap_1').height();
            else {
                if ($('#TWrap_'+(i+1)).height() > MaxHeight) MaxHeight = $('#TWrap_'+(i+1)).height();
            }
        })
        if($.browser.msie){
            var PaddingTop = $('.TWrap').css('padding-top');
            var PaddingBot = $('.TWrap').css('padding-bottom');
            PaddingTop = Number(PaddingTop.replace('px', ''));
            PaddingBot = Number(PaddingBot.replace('px', ''));
            $('.TWrap').css({'height':(MaxHeight+PaddingTop+PaddingBot)+'px'});
        }
        else $('.TWrap').css({'height':MaxHeight+'px'});
    }
    else return false;
}

function GetBigImage(Name) {
    $('.BigImage').attr('src', 'photoimages_big_'+Name);
    $('.FLink').attr('href', 'photoimages_real_'+Name);
}

function ShowTab(TabId){
    $('.TabDiv').each(function(){
        ID = $(this).attr('id');
        ID = ID.replace('Tab_', '');
        if (ID == TabId) $(this).show();
        else $(this).hide();
    })

    $('.TWrap').each(function(){
        ID2 = $(this).attr('id');
        ID2 = ID2.replace('TWrap_', '');
        if (ID2 == TabId) $(this).addClass('ActiveTab');
        else  $(this).removeClass('ActiveTab');
    })
    if($('.scroll-pane').length){
    $('.scroll-pane').each(
		function()
		{
			$(this).jScrollPane(
				{
					showArrows: $(this).is('.arrow')
				}
			);
			var api = $(this).data('jsp');
			var throttleTimeout;
		}
	)
    }

}

function commitFlashObject(_obj, _container) {
   var _output = "";
   var _paramoutput = "";
   for (_cO in _obj) {
      if (_cO != "movie" && _cO != "FlashVars" && _cO != "wmode" && _cO != "quality") _output += _cO+"=\""+_obj[_cO]+"\" ";
      if (_cO != "data" && _cO != "width" && _cO != "height") _paramoutput += "<param name=\""+_cO+"\" value=\""+_obj[_cO]+"\" />\n";
   }
   var ihtm = "<object type=\"application/x-shockwave-flash\" "+_output+">\n";
   ihtm += _paramoutput;
   ihtm += "</object>\n";
   GID(_container).innerHTML = ihtm;
}

function showFlash(path, width, height, div, vars, wmode) {
   var myFlashObject = new Object;
   myFlashObject.movie = path;
   myFlashObject.data = path;
   myFlashObject.quality = "high";
   if (width) myFlashObject.width = width;
   if (height) myFlashObject.height = height;
   if (vars != "") myFlashObject.FlashVars = vars;
   if (wmode != "") myFlashObject.wmode = wmode;
   commitFlashObject(myFlashObject, div);
}

function checkFlash() {
   var flash_versions = 20; var flash_installed = 0; var flash_version = '0.0'; var java_installed = 0;
   if (navigator.plugins && navigator.plugins.length) {  // Netscape style plugin detection
      for (x = 0; x < navigator.plugins.length; x++) {
         if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {
            flash_version = navigator.plugins[x].description.split('Shockwave Flash ')[1];
            flash_installed = 1;
            break;
         }
      }
      for (x = 0; x < navigator.plugins.length; x++) {
         if (navigator.plugins[x].name.indexOf('Java(TM)') != -1) {
            java_installed = 1;
            break;
         }
      }
   }
   else if (window.ActiveXObject) {  // ActiveX style plugin detection
      for (x = 2; x <= flash_versions; x++) {
         try {
            oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
            if (oFlash) {
               flash_installed = 1;
               flash_version = x + '.0';
            }
         }
         catch(e) { }
      }
   }
   return flash_installed;
}

var isFlash = checkFlash();

var carton = new Image ();
carton.src = "image/new/m-deliver-a.gif";
var cartoff = new Image ();
cartoff.src = "image/new/m-deliver.gif";
var cartonL = new Image ();
cartonL.src = "image/new/m-left-a.gif";
var cartoffL = new Image ();
cartoffL.src = "image/new/m-left.gif";
var cartonR = new Image ();
cartonR.src = "image/new/m-right-a.gif";
var cartoffR = new Image ();
cartoffR.src = "image/new/m-right.gif";

function turnOn(imageName, left, right) {
   if (document.images) {
      var srcI = carton.src;
      if (left > 0) srcI = cartonL.src;
      else if (right > 0) srcI = cartonR.src;
      document.images[imageName].src = srcI;
   }
}
function turnOff(imageName, left, right) {
   if (document.images) {
      var srcI = cartoff.src;
      if (left > 0) srcI = cartoffL.src;
      else if (right > 0) srcI = cartoffR.src;
      document.images[imageName].src = srcI;
   }
}

function SelMenu(e, idM, left, right, flag) {
   obj = (flag == true ? e : GID(e));
   obj.style.background = 'url(image/new/m-bgr-a.gif)';
   if (idM > 0) {
      var strI =  'deliver'+idM.toString();
      turnOn(strI, 0, 0);
   }
   if (left > 0) turnOn('deliverLeft', left, right);
   if (right > 0) turnOn('deliverRight', left, right);
}

function UnSelMenu(e, idM, left, right, flag) {
   obj = (flag == true ? e : GID(e));
   obj.style.background = 'url(image/new/m-bgr.gif)';
   if (idM > 0) {
      var strI =  'deliver'+idM.toString();
      turnOff(strI, 0, 0);
   }
   if (left > 0) turnOff('deliverLeft', left, right);
   if (right > 0) turnOff('deliverRight', left, right);
}

function SelSubMenu(obj) { obj.style.background='#FFEADA'; }
function UnSelSubMenu(obj) { obj.style.background='#E8EBD0'; }

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

function fnCheckSubscribeForm(f) {
   if (!f.email.value.match(/^[0-9A-Za-z\._-]+@([0-9a-z\._-]+\.)+[a-z]{2,4}$/)) {
      alert("Укажите правильно Ваш e-mail!");
      f.email.focus();
      return false;
   }
   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 fnCheckOrders(obj) {
   for (i = 0; i < obj.length; i++) {
      if (obj[i].type == "text") {
         if (!obj[i].value.match(/^([0-9]+)?$/)) {
            alert("Введено некорректное значение!");
            obj[i].focus();
            return false;
         }
      }
   }
   return true;
}

function fnCheckOrdersFreeForm(f) {
   if (!f.name.value.match(/(.+)/)) {
      alert('Укажите наименование товара!');
      f.name.focus();
      return false;
   }
   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.code.value.match(/[0-9]+/)) {
      alert('Укажите код!');
      f.code.focus();
      return false;
   }
   return true;
}

function fnCheckOrdersForm(f) {
   if (!f.code.value.match(/[0-9]+/)) {
      alert('Укажите код!');
      f.code.focus();
      return false;
   }
   return true;
}
