
var clicked = 0;
var url = window.location.href;
if ( url.match(/\?s=[^\&]+/) ) {
	var clicked2 = 1;
} else {
	var clicked2 = 0;
}

function ClearSearch(it){
	if (clicked2 == 0) {
		it.value = '';
		clicked2 = 1;
	}
	return true;
}

function bookmark(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; 
} 

function addToCompare(id) {
	var form = document.getElementById('compareForm');
	var inp = document.getElementById('compareId');
	var act = document.getElementById('compareAction');
	inp.value = id;
	act.value = 'add';
	form.submit();
	return false;
}

function purgeCompare() {
	var form = document.getElementById('compareForm');
	var act = document.getElementById('compareAction');
	act.value = 'purge';
	form.submit();
	return false;
}

function removeFromCompare(id) {
	var form = document.getElementById('compareForm');
	var inp = document.getElementById('compareId');
	var act = document.getElementById('compareAction');
	inp.value = id;
	act.value = 'remove';
	form.submit();
	return false;
}

$(document).ready(function() {
	/***  tabs switcher ***/
	$(".switchers a").click(function() {
		var sw_class = $(this).parent().attr('class');
		var swc = sw_class.replace(' ','.');
		if (!$(this).hasClass('active')) {
			$('.'+swc+" a").removeClass('active');
			$(this).addClass('active');
			
			var content = $(this).attr('id');
			
			$('.'+swc).nextAll(".switchcontent").removeClass('active');
			$('#'+content+'_switch').addClass('active');
		}
		return false;
	});

	$('#callrequest').click(function(){
			requestOnClick('call',$(this));
		return false;
	});

	requestOnClick = function(type, that) {
		if ( $("body > .call-request-block").length ) return false;
			var $this = that;
			if (type == 'price') {
					var model = $this.attr('data-model');
					var pid = $this.attr('data-pid');
					var prWindow = $('#hiddendiv .price-request-block').clone();
					prWindow.find('.productid').val(pid);
					prWindow.find('.model').html(model);
			} else if (type == 'call') {
					var prWindow = $('#hiddendiv .call-request-block').clone();
			}
			var ci = prWindow.find('.input.capt img');
			ci.attr('src',ci.attr('src').split('?')[0]+'?'+parseInt(Math.random()*10000));
			$over = $('#overlay').clone();
			$over.click(function(){ prWindow.remove(); $(this).remove(); })
							.appendTo('body');
			prWindow.find('.close').click(function(){ prWindow.remove(); $over.remove(); return false; });
			prWindow.find('.sendPR').click(function(){ ajaxRequest(ci,$(this).closest('.'+type+'-request-block'),$over); return false; });
			prWindow.appendTo('body');
			return false;
	};

	ajaxRequest = function(ci,$this,$over) {
			ci = $this.find('.input.capt img');
			$.post(
					__WWWHOST+'action.php',
					$this.find('form').serialize(),
					function(d){
							ci.attr('src',ci.attr('src').split('?')[0]+'?'+parseInt(Math.random()*10000));
							var errs = [];
							if ( $(d).find('error').length > 0 ) {
									$(d).find('error').each(function(){
											errs.push($(this).text());
									});
									$this.find('.errors').css('padding','5px').html(errs.join('<br>'));
							} else {
									var header = $this.find('.header');
									var close = $this.find('.close').html();
									header.html('<a href="#" class="close">'+close+'</a>Ваш запрос успешно отправлен!<br>');
									$this.html(header);
									$this.find('.close').click(function(){ $this.remove(); $over.remove(); return false; });
							}
					}
			);
	};

});
