$(document).ready(function() { 
	$('form.ajax').live('submit', function(){
		$(this).find("input[name='ajax']").val('1');
		$(this).ajaxSubmit({
			dataType: 'json',
	
			beforeSubmit: function(a,form,o) {
				$('.errors').hide();
			},
	
			success: function(response, statusText, xhr, form)
			{
				var update = $(form).find("input[name='update']").val();
				if (response.code)
				{
					response.code = prepare(response.code);
					$(form).find(".code").html(response.code).show();
				}
				if (! update && response.errors)
				{
					$.each(response.errors, function(k, val)
					{
						val = prepare(val);
						$(form).find(".error"+(k != 0 ? "_"+k : '')).html(val).show();
					});
				}
				if (response.success)
				{
					$(form).clearForm();
					$(form).find('input[type=file]').last().addClass('last');
					$(form).find('input[type=file]').each(function(){
						if (! $(this).is('.last'))
						{
							$(this).next('br').remove();
							$(this).remove();
						}
					});
					$(form).find('input[type=file]').removeClass('last');
				}
				if (response.add)
				{
					response.add = prepare(response.add);
					if ($(form).find("input[name='first']").val())
					{
						$(form).before(response.add);
						$(form).find("input[name='first']").val('');
					}
					else
					{
						$('.'+$(form).attr("id")).append(response.add);
						$('.'+$(form).attr("id")).show();
					}
				}
				if (response.basket && response.basketcount)
				{
					$('.basket_block .basket_summ').text(response.basket);
					$('.basket_block  .basket_count').text(response.basketcount);
                                        //$('.basket_last_tr .basket_count').text(response.basketcount);
                                        //$('.basket_last_tr .basket_summ').text(response.basket);
				}
				if (response.form)
				{
					$(form).html(prepare(response.form));
				}
				if (response.form_hide)
				{
					if (response.target && ! response.data)
					{
						$(response.target).hide();
					}
					else
					{
						$(form).hide();
					}
				}
				if (response.redirect)
				{
					window.location = response.redirect;
				}
				if (response.data && response.target)
				{
					$(response.target).html(prepare(response.data));
					$(form).find("input[name=avatar]").val('');
				}
				if (response.hash)
				{
					$('input[name=check_hash_user]').val(response.hash);
				}
				return false;
			}
		});
		return false;
	});

	$("form input[name='attachments[]']").live('change', function() {
		if ($(this).attr("max") && $(this).parents('form').find('input[name="'+$(this).attr("name")+'"]').length >= $(this).attr("max"))
		{
			return false;
		}
		$(this).after($(this).clone(true).val(''));
		$(this).after($('<br>'));
	});
                        
        $("select[name=depend]").change(function() {
		var price_id = $(this).find('option:selected').attr('rel');
		$(this).parents('form').find('.shop-price-depend').hide();
                $(this).parents('form').find('.price_id'+price_id).show();
	});

	$('.inpnum').live('keydown', function(evt) {
		evt = (evt) ? evt : ((window.event) ? event : null);

		if (evt)
		{
			var elem = (evt.target)
			? evt.target
			:(
			  evt.srcElement
			  ? evt.srcElement
			  :null
			);
		 
			if (elem)
			{
				var charCode = evt.charCode
				?evt.charCode
				:(evt.which
				  ?evt.which
				  :evt.keyCode
				  );
		 
				if ((charCode < 32 ) ||
					(charCode > 44 && charCode < 47) || 
					(charCode > 95 && charCode < 106) || 
					(charCode > 47 && charCode < 58) || charCode == 188 || charCode == 191 || charCode == 190  || charCode == 110)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
	});
	$('.code_update').live('click', function(){
		$(this).parents("form").find("input[name=update]").val("1");
		$(this).parents("form").submit();
	});
	$('a[rel=big_image]').live('click', function(){
		var self = $(this);
		window.open(self.attr("href"),'', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+(self.attr("width") * 1 + 40)+',height='+(self.attr("height") * 1 + 40));
		return false;
	});
        
	$('.top_search .input_search').click(function(){
		$(this).val('');
	})
	
	$('.bottom .clear_search').click(function(){
		$('.bottom .input_search').val('');
	})

});

function prepare(string)
{
	string = str_replace('&lt;', '<', string);
	string = str_replace('&gt;', '>', string);
	string = str_replace('&amp;', '&', string);
	return string;
}

function str_replace(search, replace, subject, count)
{
	f = [].concat(search),
	r = [].concat(replace),
	s = subject,
	ra = r instanceof Array, sa = s instanceof Array;
	s = [].concat(s);
	if (count)
	{
		this.window[count] = 0;
	}
	for (i = 0, sl = s.length; i < sl; i++)
	{
		if (s[i] === '')
		{
			continue;
		}
		for (j = 0, fl = f.length; j < fl; j++)
		{
			temp = s[i]+'';
			repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
			s[i] = (temp).split(f[j]).join(repl);
			if (count && s[i] !== temp)
			{
				this.window[count] += (temp.length-s[i].length)/f[j].length;
			}
		}
	}
	return sa ? s : s[0];
}

function add2Fav(a){
    title=document.title;
  url=document.location;
  try {
    // Internet Explorer
    window.external.AddFavorite(url, title);
  }
  catch (e) {
    try {
      // Mozilla
      window.sidebar.addPanel(title, url, "");
    }
    catch (e) {
      // Opera
      if (typeof(opera)=="object") {
        a.rel="sidebar";
        a.title=title;
        a.url=url;
        return true;
      }
      else {
        // Unknown
        alert('Нажмите Ctrl-D чтобы добавить страницу в закладки');
      }
    }
  }
  return false;
}
