var book_id = 0;

function toCart(id) {	var book_id = id;
	$.ajax({
	    url: '/shop/add_cart.php',
	    data: {	    	item_id: id,
			noshow: 1	    },
	    type: 'post',
	    dataType : 'json',
	    cache: 'false',
	    success: function (data, status) {	    	if (data.result == 'added') {
	    		var buy_label = $('#buy'+book_id);
	        	if (data.type == 'buy') {
		        	html = '<span class="small">товар в корзине</span><br><a href="/shop/cart.php" class="pay"><b>оформить</b></a>';
		        	if (buy_label)
		        		buy_label.html(html);	        		var basket_label = $('#basket');
	        		if (basket_label) {	        			html = '<div class="big">Корзина</div>товаров: '+data.total_quan+'<br>на сумму: '+data.total_price+' руб.<br><a href="/shop/cart.php"><b>Оформить заказ</b></a>';
	        			basket_label.html(html);
	        		}
	        		alert('Товар помещен в корзину');
				}	        }
	        return false;
	    },
	    error: function (data, status, e) {
		    alert(data.error);
		    return false;
		}
	});
}

function MM_openBrWindow(theURL,winName,features){window.open(theURL,winName,features);}
eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('$(0).1(2(){$("3.4").5()});',6,6,'document|ready|function|span|tag|remove'.split('|'),0,{}));

//------------ карусель ---------------------------//

function carousel_itemAddCallback(carousel, first, last, xml) {
	carousel.size(parseInt(jQuery('total', xml).text()));
	jQuery('book', xml).each(function(i) {
	    carousel.add(first + i, carousel_getItemHTML(
	    	jQuery('id',this).text(),
	    	jQuery('img',this).text(),
	    	jQuery('price',this).text(),
	    	jQuery('alt',this).text(),
	    	jQuery('title',this).text()
	    ));
	});
};

function carousel_getItemHTML(id,img,price,alt,title) {
	return '<div style="height:190px;"><a href="/shop/items.php?id='+id+'" title="'+alt+'" alt="'+alt+'"><img src="'+img+'" width="90" height="115" title="'+alt+'" alt="'+alt+'" /><br>'+title+'</a></div><div class="price">'+price+' руб.</div>';
};

//------------ social button ----------------------//
function NewOdnaknopka3(){
this.url=function(system){
var title=encodeURIComponent(document.title);
var url=encodeURIComponent(location.href.replace(/&section=\d+/g,''));
var description = encodeURIComponent(getDesc());
switch(system){
case 1:return'http://vkontakte.ru/share.php?url='+url;
case 2:return'http://www.facebook.com/sharer.php?u='+url+'&t='+title+'&src=sp';
case 3:return'http://twitter.com/home?status='+title+' '+url;
case 4:return'http://www.livejournal.com/update.bml?event='+url+'&subject='+title;
case 5:return'http://connect.mail.ru/share?share_url='+url;
case 6:return'http://www.google.com/buzz/post?url='+url+'&title='+title+'&srcURL='+pageHost;
case 7:return'http://my.ya.ru/posts_add_link.xml?URL='+url+'&title='+title;
case 8:return'http://www.odnoklassniki.ru/dk?st.cmd=addShare&st._surl='+url;
}
}
this.init=function(){
var titles=new Array('&#1042; &#1050;&#1086;&#1085;&#1090;&#1072;&#1082;&#1090;&#1077;','Facebook','Twitter','LiveJournal','&#1052;&#1086;&#1081; &#1084;&#1080;&#1088;','&#1046;&#1080;&#1074;&#1072;&#1103; &#1083;&#1077;&#1085;&#1090;&#1072; Google','&#1071;.&#1088;&#1091;','&#1054;&#1076;&#1085;&#1086;&#1082;&#1083;&#1072;&#1089;&#1089;&#1085;&#1080;&#1082;&#1080;');
var html='';
html+='';
for(i=0;i<8;i++){
html+='<a href="'+this.url(i+1)+'" target="_blank"><img align="center" src="/images/buttonblank.gif" width="24" height="22" alt=" #" title="'+titles[i]+'" style="border:0;padding:0;margin:0 4px 0 0;background:url(/images/panel.png) no-repeat -263px -'+(i*22)+'px";/></a>';
}
document.write(html);
}
}
function getDesc(){
var metas = document.getElementsByTagName('meta');
for (var i=0;i<metas.length;i++){
if (metas[i].getAttribute('name')) {
if(metas[i].getAttribute('name').toLowerCase()=='description'){
return metas[i].getAttribute('content');
}
}
}
return null;
}

//------------ search  ---------------------------//

var is = new Is();

function Is(){	this.agent=navigator.userAgent.toLowerCase();
	this.opera = (this.agent.indexOf('opera')!=-1) ? true:false;
	this.ie4 = ((this.agent.indexOf('msie')!=-1) && (this.agent.indexOf('opera')==-1)) ? true:false;
	this.ns6 = ((this.agent.indexOf('gecko')!=-1) && (this.agent.indexOf('opera')==-1)) ? true:false;
	this.ns4 = ((this.agent.indexOf('mozilla')!=-1) && (this.agent.indexOf('opera')==-1) && (this.agent.indexOf('msie')==-1) && (this.agent.indexOf('gecko')==-1)) ? true:false;
}

function GetWindowWidth(){	return((is.ie4)? document.body.clientWidth: window.innerWidth);
}

function showSearch() {	if (document.getElementById('msearch').style.visibility == 'hidden') {		var winwid=GetWindowWidth();
		with(document.getElementById('msearch').style){			left = Math.floor(winwid/2);
			visibility = 'visible';
		}
		document.getElementById('sa').className = 'selected';
	}
	else {
		document.getElementById('msearch').style.visibility = 'hidden';
		document.getElementById('sa').className = '';
	}
	return false;
}
