var IE='\v'=='v';
var g_paysystem = "none";
var click_link_search = true;
var timeoutId = -1;

if(IE)
 {
  $(window).load(
   function()
    {
     $('#slider1').nivoSlider({
      pauseTime:5000,
      pauseOnHover:false ,
      effect:'fade',
      controlNav:false,
      captionOpacity:0.8 });
     $('#login').css('background', 'url(images/login_bg_ie.png) no-repeat');
    }
  );
 }
  else
 {
  $(window).load(
   function()
    {
	 $('#slider1').nivoSlider({
	  pauseTime:5000, pauseOnHover:false ,
	  effect:'fade', controlNav:false,
	  captionOpacity:1.0});
    }
  );
 }

   function ajax_search(search)
    {
     return $.ajax({url: 'http://' + document.domain + '/ajax.php?action=search&s='+search, async: false}).responseText;
    }
   function ajax_save_to_session(value)
    {
     return $.ajax({url: 'http://' + document.domain + '/ajax.php?action=save_to_session&value='+value, async: false}).responseText;
    }
   function ajax_pass(uid, pass)
    {
     return $.ajax({url: 'http://' + document.domain + '/ajax.php?action=check_pass&uid='+uid+'&pass='+pass, async: false}).responseText;
    }	
   function ajax_mail(mail)
    {
     return $.ajax({url: 'http://' + document.domain + '/ajax.php?action=check_mail&mail='+mail, async: false}).responseText;
    }
   function ajax_login(login)
    {
     return $.ajax({url: 'http://' + document.domain + '/ajax.php?action=check_login&login='+login, async: false}).responseText;
    }	
	
	
   function hh(obj)
	{
	 $('#'+obj).removeClass().addClass('form-submit-hover');
	}
   function hh2(obj)
	{
	 $('#'+obj).removeClass().addClass('form-submit');
	}
   function hhm(obj)
	{
	 $('#'+obj).css('background', 'url(/images/nav_left_h.gif) no-repeat');
    }
   function hhm2(obj)
	{
	 $('#'+obj).css('background', 'url(/images/nav_left.gif) no-repeat');
    }

	
  function save_changes (uid)
   {
    var pass = document.getElementById('old_pass').value;
    var error = 0;

    if (pass != '')
	 {
      var my_pass = ajax_pass(uid, pass);
      if(my_pass == 0)
	   {
        error = 1;
        alert('Неверный старый пароль!');
       }
	 }
      else
	 {
      error = 1;
      alert('Неверный старый пароль!');
     }
    if(error == 0) document.getElementById('account_form').submit();
   }
	

function perclick()
 {
  var agt = navigator.userAgent.toLowerCase();
  if(agt.indexOf("msie") != -1)
   if(agt.substr(agt.indexOf("msie")+5,1) == '6')
    location.replace("/login_ie.htm"); 
	 else
    return false;	
 }

function fill_wallet(u)
 {

  var paysystem = g_paysystem;
  if ( paysystem == "none" )
   {
    alert ("Выберите платёжную систему!");
    return false
   }
  var summ = document.getElementById("summ").value;
  if ( ( summ <= 0 ) ||  ( summ == "" ) )
   {
    alert ("Введите сумму, большую 0");
    return false
   }
	
	switch (paysystem) {
		
		case 'pay_MobilCommerce_Beeline':
			location.href = 'http://' + document.domain + '/_billing/magaz_pay.php?u='+u+'&system=pay_MobilCommerce&sum=' + summ + '&provider=Beeline';
		break;

		case 'pay_MobilCommerce_Megafon':
			location.href = 'http://' + document.domain + '/_billing/magaz_pay.php?u='+u+'&system=pay_MobilCommerce&sum=' + summ + '&provider=Megafon';
		break;

		case 'pay_MobilCommerce_MTS':
			location.href = 'http://' + document.domain + '/_billing/magaz_pay.php?u='+u+'&system=pay_MobilCommerce&sum=' + summ + '&provider=MTS';
		break;

		default:
			location.href = 'http://' + document.domain + '/_billing/magaz_pay.php?u='+u+'&system=' + paysystem + '&sum=' + summ;
	}

  
 }

function my_search()
 {
  $('#smart').html(ajax_search($('#inp_search').val()));
 }

function off_timer()
 {
  if (timeoutId == -1)
  {
   return 0;
  }
   else
  {
   clearTimeout(timeoutId);
  }
 }

function smart_search()
 {
  off_timer();
  timeoutId = setTimeout(my_search, 500);
 }
function hide()
 {
  $('#1, #2, #3').removeClass().addClass('non_sel');
  $('#popular, #new, #cheap, #carousel, #wallet, #account, #cart').hide();
 }
function show(obj, obj2)
 {
  hide();
  $('#'+obj).show();
  $('#'+obj2).removeClass().addClass('item_selected');
  if (obj!='wallet' && obj!='account' && obj!='cart' && obj!='carousel')
   ajax_save_to_session(obj2);
   
  if ( obj == 'account' )
   {
    var params = {changedEl: ".lineForm select", visRows: 5, scrollArrows: true };
    cuSel(params);
   } 
  return false; 
 }

$(document).ready(
 function()
  {
   
   cuSel({changedEl: ".lineForm select", visRows: 5, scrollArrows: true });


	  
   $('.login_iner_v').click(
    function ()
	 {
	  return fperclick();
	 }
   );
	  
   $("#vk_shower").click(
    function ()
     {
      $("#vk_auth").slideDown();
	  VK.init({apiId: $(this).attr('name')});
	  VK.Widgets.Auth.logout;
	  VK.Widgets.Auth("vk_auth", {width: "200px", authUrl: "/lk.html"});
      $(this).slideUp();
     }
   );	 

   $(".info").hover(
    function()
     {
      $('.info_hover:not(:animated)').fadeIn(200);
     },
    function()
     {
	  $('.info_hover:not(:animated)').fadeOut(100);
     }
   );

   $(".nivo-caption").live('mouseover mouseout',
    function(event)
     {
      if (event.type == 'mouseover')
       {
		$('.nivo-directionNav').hide();
	   }
	 }
   );

   $(".info_hover").hover(
    function()
     {
      $('.info_hover:not(:animated)').fadeIn(200);
	 },
	function()
	 {
      $('.info_hover:not(:animated)').fadeOut(100);
	 }
   );


   $("#shops a").hover(
    function()
     {
	  $('#shops a').removeClass('selected');
	  $(this).addClass('selected');
	 },
	function()
	 {
	  $('#shops a').removeClass('selected');
	  $('#inetportal').addClass('selected');
	 }
   );

   $("#inpromo").focus(
    function()
     {
	  if (this.value == 'Введите промокод')
	   {
		 this.value = '';
	   }
     }
   );

   $("#inpromo").blur(
    function()
     {
	  if (this.value == '')
	   this.value = 'Введите промокод';
     }
   );

   $("#menu ul li").hover(
    function()
     {
	  $('.arr',this).addClass("arr2");
	 },
	function()
	 {
	  $('.arr',this).removeClass("arr2");
	 }
   );

   $(".pay_item").click(
    function ()
     {
	  $(".pay_item").removeClass('selected');
	  $(this).addClass('selected');
      g_paysystem = $(this).attr("id");
			$('#summ').keyup();
     }
   );


	$('#summ').keyup(
		function(){

			if ($('#ya_discount_text').css('display') == 'none') return;

			if ($('.pay_item.selected').attr('id') == 'pay_YandexMoney') {
				var summ = parseFloat( $(this).attr('value') );

				if (summ > 0) {
					var bonus = summ * 0.25;
					$('#mess').html( 'В ваш кошелек дополнительно будет зачислен бонус в размере ' + bonus + ' руб.' );
					$('#mess').css('display', 'block');
				}
				else
					$('#mess').css('display', 'none');
			}
			else
				$('#mess').css('display', 'none');
		}
	);	



   $("#inp_search").mouseup(
    function ()
     {
      click_link_search = true;
     }
   ).mousedown(
    function ()
     {
      click_link_search = false;
     }
   ).focus(
    function ()
     {
	  var left = $(this).offset().left;
	  var top = $(this).offset().top;
	  $("#smart").css({'left':(left-82), 'top': (top+26)}).slideDown(500);
     }
   ).blur(
    function ()
     {
	  if (click_link_search==false ) return false;
	  $("#smart").slideUp(700);
     }
   );

   $(".non_sel").hover(
    function()
     {
	  $('.item_left',this).addClass("item_left_hover");
	  $('.item_right',this).addClass("item_right_hover");
	  $('.item',this).addClass("item_hover");
	 },
    function()
     {
	  $('.item_left',this).removeClass("item_left_hover");
	  $('.item_right',this).removeClass("item_right_hover");
	  $('.item',this).removeClass("item_hover");
	 }
   );

   $(".fancy_box_img").fancybox();

   $('a[href=/login.html]').click(
    function ()
     {
      $('#showlogin').fadeOut();
      $('#fon_bg_log').remove();

      $('body').prepend('<div id="fon_bg_log" style="opacity: 0.5; background-color: #333333; position:fixed; z-index:700; filter: alpha(opacity = 50);">&nbsp</div>');
      $('#fon_bg_log').css({'width':$(window).width(), 'height':$(window).height(), 'top':0, 'left':0}).click(
       function ()
        {
         $('#showlogin').fadeOut();
         $('#fon_bg_log').remove();
        }
      );
      $('#showlogin').css({'position':'fixed', 'z-index':702, 'top':($(window).height()-204)/2, 'left':($('html').width()-381)/2}).fadeIn();
      return false;
     }
   );   
  }
);
