function open_printable_version(link) // opens new window
{
	var win = "menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(link, 'printableWin', win);
	if(newWin){newWin.focus();}
}

  //Новости
  $(document).ready(function(){
  //переносим seo текст вниз
  $('#seo_text2').append( $('#seo_text').remove() );
  // seo-text
  var txt = $(".stext").text();
  if ((txt) && (txt.length >300)) {

    txt = "<div class='stextshort'><p>"+txt.substring(0,300) + '... <a href="/" class="open">Читать полностью</a></p></div>';
	hid = ' <a href="/" class="close">Скрыть</a></p>';
	$(".stext").before(txt).append(hid).hide();
	$(".stextshort a.open").click(function(){
	  $(".stextshort").hide();
	  $(".stext").show();
	  return false;
	});
	$(".stext a.close").click(function(){
	  $(".stext").hide();
	  $(".stextshort").show();
	  return false;
	});
  }

});
//каталог
$(function() {
   $("span.plus, span.minus, span.child_plus, span.child_minus").click(function() {
	
	if ($(this).hasClass('plus')) {
		
		li=$(this).parent();
		li.removeClass('parent');li.addClass('parent_current');
		$(this).removeClass('plus');$(this).addClass('minus');
		
	} else
	
	if ($(this).hasClass('minus')) {
	
		li=$(this).parent();
		li.removeClass('parent_current');li.addClass('parent');
		$(this).removeClass('minus');$(this).addClass('plus');
	
	}
	
	if ($(this).hasClass('child_plus')) {
	
		li=$(this).parent();
		li.removeClass('child');li.addClass('child_current');
		$(this).removeClass('child_plus');$(this).addClass('child_minus');
		
	} else
	
	if ($(this).hasClass('child_minus')) {
	
		li=$(this).parent();
		li.removeClass('child_current');li.addClass('child');
		$(this).removeClass('child_minus');$(this).addClass('child_plus');
	
	}
	
	});
	
	});

	function update_basket() {
	
	$.get ('/',function(data){
	
	search_string='id="basket_body">';
	
	start=data.indexOf(search_string)+search_string.length;
	left_cut=data.substr(start);
	end=left_cut.indexOf('</span>');
	done=left_cut.substr(0,end);
	$('#basket_body').html(done);
	});}
	
	function update_href_checkout() {
	
	a=$('a[href="/checkout/"]');
	a.unbind();
	a.click(function() {if (shopping_cart_min>shopping_cart_value) { alert("Сумма заказа должна быть не менее "+shopping_cart_min+" руб.");return false;}});

	}
	
	function amount_click(elem) {
		
		set=prompt("Введите количество:");
		//ищим форму
		form=$(elem).parents('form');
		//находим скпытый инпут со значением productID
		productID=form.find('input[name=productID]');
		//ищем все дивы после кнопки купить
		all_divs=form.find('input[name=add2cart]+div');
		//организуем ajax запрос
		$.ajax({url: '/basket/session.php', data: { action: "set", productID: productID.val(), amount: set }, all_divs: all_divs, form: form,
		success: function(data) {
		update_basket();
		this.all_divs.replaceWith(data);
		if (data=='') this.form.find('input[name=add2cart]').show(); else {
		
			this.form.find('.amount').click(function() { amount_click(this);});
			this.form.find('.remove_from_cart').click(function() { remove_from_cart_click(this);});
			
		}
	}}); 
	}
	
function remove_from_cart_click(elem) {
		
		//ищим форму
		form=$(elem).parents('form');
		//находим скрытый инпут со значением productID
		productID=form.find('input[name=productID]');
		//ищем все дивы после кнопки купить в форме
		all_divs=form.find('input[name=add2cart]+div');
		//организуем ajax запрос
		$.ajax({url: '/basket/session.php', data: { action: "set", productID: productID.val(), amount: 0 }, form: form, all_divs: all_divs,
		success: function(data) {
		update_basket();
		this.all_divs.replaceWith(data);
		if (data=='') this.form.find('input[name=add2cart]').show(); else {
		
			this.form.find('.amount').click(function() { amount_click(this);});
			this.form.find('.remove_from_cart').click(function() { remove_from_cart_click(this);});
			
		}
	}}); 
	}

function check_basket(elem,action) {

	//ищим форму
	form=$(elem).parents('form');
	//находим скрытый инпут со значением productID
	productID=form.find('input[name=productID]');
	//ищем все кнопки "купить" в этой форме
	all_inputs=form.find('input[name=add2cart]');
	//скрываем все найденные инпуты
	all_inputs.hide();
	//если это был клик то добавляем продукт инасе просто передает его ID
	if (action=='click') query={ action: "add", productID: productID.val() }; else query={productID: productID.val()};
	//организуем ajax запрос
	$.ajax({url: '/basket/session.php', data: query, form: form, all_inputs: all_inputs, elem: elem,
	error: function() {this.all_inputs.show();},
	success: function(data) {
	//находим див сразу после input
	div=$(this.elem).next();
	//Делаем такое условие так как даже при ошибке он возвращает правильную страницу (проблема реврайт модуля)
	if ((data.length<1000)&&(data!='')) {	
	//вставляем ajax ответ если не присутствует там div
	if (div.length==0) { 
	//передаем в них данные
	update_basket();
	this.all_inputs.after(data);
	this.form.find('.amount').click(function() { amount_click(this);});
	this.form.find('.remove_from_cart').click(function() { remove_from_cart_click(this);});
	//if (query.action=='add') window.location = '/cart/';
			
			} 
	} else {this.all_inputs.show();}
	
 	}
	});

}
	
$(function() {

$("#tooltiper").tooltip({track:true, delay:100,showURL: false});

//Выводим форму подписки

/*if (document.cookie.indexOf('visited=true') === -1) {
    var expires = new Date();
    expires.setDate(expires.getDate()+30);
    document.cookie = "visited=true; expires="+expires.toUTCString();

    $.colorbox({href:'/subscribe/form.html'});
} */

 //Обрезаем текст в каталоге
  $('.prdbrief_brief_description').jTruncate({  
  length: 100,  
  minTrail: 0,  
  moreText: "далее",  
  lessText: "скрыть",  
  ellipsisText: "...",  
  moreAni: "fast",  
  lessAni: 1000  
 });
 
//вешаем событие на нажатие кнопки!!! 
$('input[name=add2cart]').click(function() { check_basket(this,'click')});
$('input[name=add2cart]').each(function() { check_basket(this)});
//вешаем проверку на нажатие checkout
update_href_checkout();
});

function popupform(myform, windowname)
			{
			if (! window.focus)return true;
			window.open('', windowname, 'height=300,width=400');
			myform.target=windowname;
			return true;
			}

function getBrowserInfo() {
    var t,v = undefined;
   
    if (window.chrome) t = 'Chrome';
    else if (window.opera) t = 'Opera';
    else if (document.all) {
        t = 'IE';
        var nv = navigator.appVersion;
        var s = nv.indexOf('MSIE')+5;
        v = nv.substring(s,s+1);
    }
    else if (navigator.appName) t = 'Netscape';
   
    return {type:t,version:v};
}

function bookmark(a){
    var url = window.document.location;
    var title = window.document.title;
    var b = getBrowserInfo();
   
    if (b.type == 'IE' && 8 >= b.version && b.version >= 4) window.external.AddFavorite(url,title);
    else if (b.type == 'Opera') {
        a.href = url;
        a.rel = "sidebar";
        a.title = url+','+title;
        return true;
    }
    else if (b.type == "Netscape") window.sidebar.addPanel(title,url,"");
    else alert("Нажмите CTRL-D, чтобы добавить страницу в закладки.");
    return false;
}

function subscribe() {

popup_email=$("#popup_email").val();
popup_name=$("#popup_name").val();

$.ajax({url: '/subscribe/form_subscribe.php', data: { email: popup_email, name: popup_name }, type: "POST",
		success: function(data) {
		
		$("#popup_msg").html(data);

	}}); 

return false;
	}

