$(function(){
	
	$('a.tofavorite').livequery('click', function(event){
		event.preventDefault();
		event.stopPropagation();
		var elm = $(this);
		var url = window.location.href;
		var title = elm.attr('title');
/*        if (document.all) window.external.AddFavorite(url,title);
        else window.sidebar.addPanel(title,url,'');
*/        
        
		if(window.sidebar){ // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
			return false;
		} else if(window.external){ // IE Favorite
			window.external.AddFavorite( url, title);
			return false;
		} else if(window.opera && window.print){ //Opera Hotlist
			elm.attr('href', url).attr('title', title).attr('rel', 'sidebar');
			return false;
		}
	});
	
	$('a.toggleMenu').livequery('click', function(event){
		event.preventDefault();
		event.stopPropagation();
		var elm = $(this).toggleClass('menuiconopen').removeClass('hidden');
		$('.cmenu')
			.slideToggle()
			.css('top', (parseInt(elm.offset().top + elm.height()))+'px')
			.css('left', parseInt(elm.offset().top + 10) + 'px');
	});
	
	/*$('td.helpText').each(function(index, elm){
		$(this).html('<div class="box">'+$(this).html()+'</div>');
	});*/
	
	$('#mcap').hide();
	
	$('div.inner h1.title').each(function(index, title){
		$(title).html($(title).text().replace('->', '<img src="/images/next.gif" />'));
	});

	
	$('div.box').each(function(index, div){
		var text = '<div class="notice"><div class="tl"></div><div class="tr"></div><div class="middle"><div class="noticeText">';
		text+=$(div).html();
		text+='</div></div><div class="noticeFooter"><div class="bl"></div><div class="br"></div></div></div>';
		$(div).replaceWith($(text));
		
	});
	

	$("a[rel^='prettyPhoto']").prettyPhoto();

	
	
	$('.cmenu H3.slide A').livequery('click', function(event){
		event.preventDefault();
		event.stopPropagation();
		$(this).toggleClass('menuiconopen').parents('H3.slide').next('UL').toggle();
	});
	$('a.author').click(function(){
		$.ajax({url: '/author', success: function(data){
			$(data).filter('.dialog').dialog({
				bgiframe: true,
				resizable: false,
				height:360,
				modal: true,
				title: $(data).filter('span'),
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				},
				buttons: {
					'ОК': function() {
						$(this).dialog('close');
					}
				}				
			});
		}});
		return false;
	});

	
	$('form#searchForm input').focus(function(){
		$(this).addClass('black');
		if (this.value=='Введите запрос') this.value = ''
	}).blur(function(){
		$(this).removeClass('black');
		if (this.value=='') this.value = 'Введите запрос';
	});
	$('a.addcart').click(function(){
		$.ajax({url: $(this).attr('href'), success: function(html){
			$(html).filter("#dialog").dialog({
				bgiframe: true,
				resizable: false,
				height:160,
				modal: true,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				},
				buttons: {
					'ОК': function() {
						$(this).dialog('close');
					}
				}
			});
			$("#cartInfo").html($(html).filter("#informationText"));
		}});
		return false;
	});
})

var attachCartEvents = function(){
	$('a.cartitem').click(function(){
		if ($(this).attr('qty')==1){
			if (!confirm('Вы желаете удалить данный продукт из корзины?')) return false;
		}	
		$.ajax({url: $(this).attr('href')+"?rnd="+Math.random(), success: function(html){
			$("#cartInfo").html($(html).filter("#informationText"));
			$("#cartData").html($(html).filter("#itemsInfo"));
		}});
		return false;
	});
}
