$(function(){
     $('ul.topweek').css({'opacity':'0.5'});
     $('ul.topweek').hover(function(){
        $(this).animate({'opacity':'1'});
     },function(){
        $(this).animate({'opacity':'0.5'});
     });
     
     $('#go_login').click(function(){
        $(document).scrollTop(0);
        return false;
     });
     
     $('.textdesc span').click(function(){
		    $(this).hide();
		    $(this).parent().find('input[type="text"], input[type="password"]').focus();
	   });
	   $('.textdesc input').focus(function(){
		    $(this).siblings('span').hide();
	   });
	   $('.textdesc input').blur(function(){
		    if ($(this).val() == ''){
			   $(this).siblings('span').show();
		    }
	   });
     
     $('.q_search span').click(function(){
		    $(this).hide();
		    $(this).parent().find('input[type="text"]').click().focus();
	   });
	   $('.q_search input').focus(function(){
		    $(this).siblings('span').hide();
	   });
	   $('.q_search input').blur(function(){
		    if ($(this).val() == ''){
			   $(this).siblings('span').show();
		    }
	   });
    
    $('a#cp').hover(function(){
      $(this).animate({'top':'0px'},'1000');
    },function(){
      $(this).animate({'top':'-50px'},'1000');
    });
    $('#register, .go_register').click(function(){
      NewWindow({object: '#reg-window'});
      return false;
    });

    $('#lostpass').click(function(){
      NewWindow({object: '#lostpass-window'});
      return false;
    });

    $('#section li').hover(function(){
      $(this).find('ul').show();
    },function(){
      $(this).find('ul').hide();
    });
   
   $('.in_sort').click(function(){
      var value = $(this).attr('id');
      $(this).parents('form').find('input[name="sort"]').val(value);
      $(this).parents('form').submit();
      return false;
   });
  
  
   $('ul.act-tab li a').not('.head').click(function(){
       $($(this).parent().parent().find('li')).each(function(){
           $(this).removeClass('active');
           var href = $(this).find('a').attr('href');
           if(href) $(href).hide();
        });
        $(this).parent().addClass('active');
        var href = $(this).attr('href');
        if(href) $(href).show();
        return false;
   });
   
   $('ul.act-tab li').each(function(){
      var href = $(this).find('a').attr('href');
      if(href && $(this).attr('class')!='active') $(href).hide();
   }); 
   
  var append = true;
  $('[rel=tooltip]').mouseover(function(kmouse){
     var msg = $(this).attr('title');
     if(msg!=""){
      var maxleft = $('body').width()-150;
      var mousl = kmouse.pageX;
      var kmousepageX = mousl>maxleft ? (kmouse.pageX-(mousl-maxleft)) : (kmouse.pageX+15);
      var message = $('<div class="tooltip">'+msg+'</div>');
          $(message).css({
              'top':'-18px',
              'position': 'absolute',
              'padding':'5px',
              'background-color':'#FFFFFF',
              'color':'#333',
              'z-index': '9999'
            });

      if(append){
        if($(this).attr('class')=="share-block"){
          $(this).parent().append(message);
        }else{
          $(this).parent().parent().append(message);
        }
        append = false;
      }
          

     }
  }).mouseout(function(kmouse){
     $('.tooltip').remove();
     append = true;
  });
  var share_other = true;
  $('.share .spec .arrow, .share-block').hover(function(){
     $('.share-block').fadeIn(200);
     share_other = false;
  },function(){
     setTimeout(function(){
        if(share_other) $('.share-block').hide();
     },200);
     share_other = true;
  });
  
  $('.gallery-slider').Slider({limit:3});
  $('.screen-slider').Slider({'limit':4,'step':120});
                          

  $('select[name=country]').change(function(){
    var country = $(this).val();
    var region = is_region>0 ? is_region : 0;
    var city  = is_city>0 ? is_city : 0;
    var region = $('<div class="text-input" style="clear: both;margin-top:5px;"><div class="bg"><select name="region" style="width: 200px;"><option valye="0">-Регион-</option></select><div class="end"></div></div></div>');
    var scity  = $('<div class="text-input" style="clear: both;margin-top:5px;"><div class="bg"><select name="city" style="width: 200px;"><option valye="0">-Город-</option></select><div class="end"></div></div></div>');
    var load  = $('<option selected value="0">загрузка...</option>');
    $(this).parent().parent().parent().find('select[name=region]').parent().parent().remove();
    $(this).parent().parent().parent().find('select[name=city]').parent().parent().remove();
    if(country>0){
      $(region).remove();
      $(scity).remove(); 
      $(region).find('select').attr('disabled',true);
      $(region).find('select').prepend(load);
      $.post('/ajax/country.html',{'country': country},
      function(data){
            var result = data.result;
            for (var key in result) {
              var value = result[key];
              var sel = '';
              if(is_region==value.id) sel = 'selected'
              $(region).find('select[name=region]').append($('<option '+sel+' value="'+value.id+'">'+value.name+'</option>'));
            }
            $(load).remove();
            $(region).find('select').attr('disabled',false);
            if(is_region>0){
              $(region).find('select[name=region]').change();
            }
      },'json');
      $(this).parent().parent().after(region);
      $(region).find('select[name=region]').change(function(){
         $(scity).remove();
         $(scity).find('select').prepend(load);
         $(scity).find('select').attr('disabled',true);  
         region = $(this).val();
         $.post('/ajax/country.html',{'country': country,'region':region},
            function(data){
                var result = data.result;
                for (var key in result) {
                  var value = result[key];
                  var sel = '';
                  if(is_city==value.id) sel = 'selected'
                  $(scity).find('select[name=city]').append($('<option '+sel+' value="'+value.id+'">'+value.name+'</option>'));
                }
                $(load).remove();
                $(scity).find('select').attr('disabled',false);
            },'json'); 
        $(this).parent().parent().after(scity);
      });
    }
  });  
  
  $('.location_go').click(function(){
     var href = $(this).attr('url');
     window.location.href = href;
     return true;
  });
         
});

var c = 60; 
function fc(obj_hide,obj_show){
  if(c>0){
		$(obj_hide).hide();
    $(obj_show).find('p').html("<b>Ваша очередь подайдет через</b> <h2>"+c+"</h2> <b>секунд.</b>"); 
		c = c - 1; 
		setTimeout('fc("'+obj_hide+'","'+obj_show+'")', 1000);
  }else{
		$(obj_show).hide();
		$(obj_hide).show();
  }
}   

function ErrorMSG(obj,msg){
  if(msg){
     var obj = $(obj); 
     var text = $('<div class="warning"><span>'+msg+'</span><img src="/skin/images/wrn_arr.png"></div>');
     
      var inerr = 	setInterval(function(){
        $(text).fadeOut(function(){
          $(this).remove();
        });
      },1500);  
      $(text).hover(function(){
         clearInterval(inerr);
      },function(){
         var inerr = 	setInterval(function(){
          $(text).fadeOut(function(){
              $(this).remove();
          });
         },1500);  
      });
     
     $(text).mousedown(function(){
        $(this).fadeOut(function(){
          $(this).remove();
        });
     });
     obj.after(text);
  }
}

var msgWarning = 1;
function MessageWarning(msg){
  var plash   = $('<div></div>');
  var close   = $('<div>[x]</div>');
  var message = $('<div>'+msg+'</div>');
  $(close).css({'display':'block','position':'absolute','top':'5px','right':'5px','cursor':'pointer'});  
  $(plash).css({'position': 'fixed','right': '30px','top': (msgWarning*50)+'px','width': '300px','z-index': '9999','padding':'1px','background':'#FF0000','border-radius':'4px','box-shadow': '0 0 7px 1px #000000'});
  $(message).css({'position': 'relative','padding':'15px','border':'1px solid #000','background':'#5A0000','color':'#fff','border-radius':'4px'});
  $(message).append(close).effect('highlight', { color: "red" }, 1000);;
  $(plash).append(message);
  
  $(close).click(function(){
     msgWarning--;
     $(plash).remove();
     return false;
  });
  
      var inerr = setInterval(function(){
        $(plash).fadeOut(function(){
          $(close).click();
        });
      },1500);  
      $(plash).hover(function(){
         clearInterval(inerr);
      },function(){
         var inerr = 	setInterval(function(){
          $(plash).fadeOut(function(){
          $(close).click(); 
          });
         },1500);  
      });
  
  $('body').append(plash);
  msgWarning++;
}
