$(document).ready(function(){

	if (jQuery.browser.msie) { if(parseInt(jQuery.browser.version) < 9) var ie=0}
	//icons
	$('#icons').hover(function(){
		$('#bigimg img').stop().fadeTo(300,0,function(){$(this).hide()});
	},function(){
		$('#bigimg img').stop().fadeTo(300,1);
	})

	if(ie!=0){
		$('#icons a').live('mouseover mouseout', function(event){
			if (event.type == 'mouseover') grayscale($(this))
			else grayscale.reset($(this))
		})
	}

	//menu
	$('#scrolltop').click(function(){
		$('#right ul').stop().animate({scrollTop:$('#right ul').scrollTop()-100},500);
		return false
	})
	$('#scrollbt').click(function(){
		$('#right ul').stop().animate({scrollTop:$('#right ul').scrollTop()+100},500);
		return false
	})

	$('#showform').click(function(){
		popup(614,597,'/faq/popup.php');
		return false;
	})

	$('#popup .btn').live('click', function(){
		var form=$('#popup form').serialize()+'&iblock_submit=Надіслати';
		$.post("/faq/popup.php",form,function(data) {
			$('#popuptext').html(data);
		});
		return false;
	})

	$('#right ul a').click(function(){
		var li=$(this).parent();
		if(li.hasClass('selected')){
			if(li.children('ul').length){
				//$('#right li').removeClass('selected');
				li.find('ul').slideUp();
				li.removeClass('selected');
				return false
			}
		}
		else{
			if(li.children('ul').length){
				$('#right li').removeClass('selected');
				li.find('ul').slideDown();
				li.addClass('selected');
				return false
			}
		}
	})
	
	$('#hideall').click(function(){
		$('#right li ul').slideUp();
		return false;
	})	

	//left slider
	var loading=$('<img src="/bitrix/templates/main/images/loading.gif" id="loading" />').hide().appendTo('#icons');
	$('.header .link').click(function(){
		$('.header div:visible').animate({
			height:0,
			paddingTop:0
		},500,function(){
			$(this).hide()
		});
		$(this)
			.prev()
			.css('padding-top','0')
			.show()
			.animate({
				height:188,
				paddingTop:27
			},500);
		$('#bigimg img').fadeTo(500,0,function(){$(this).hide()});
	
		$('#icons a').remove();
		$('#loading').show()
	
		if($(this).parent()[0].id=='cherkass'){
			$.get('/mainmenu.php', function(data) {
				$('#icons').append(data);
				$('#loading').hide();
			})
		}
		if($(this).parent()[0].id=='apteka'){
			$.get('/apteka-sadivnyka/mainmenu.php', function(data) {
				$('#icons').append(data);
				$('#loading').hide();
			})
		}
		if($(this).parent()[0].id=='ukravit'){
			$.get('/ukravit-company/mainmenu.php', function(data) {
				$('#icons').append(data);
				$('#loading').hide();
			})
		}		

		return false
	})

	//misc
	$('#gallery .item a').click(function(){
		var id=$(this).attr('rel');
		popup(548,742,'/photo-gallery/image.php?id='+id);
		return false
	})

	if($.browser.webkit) scroller=$('body');
	else scroller=$('html');
	$('#totop').attr('href','#back-to-top').live('click', function(){
		$(scroller).animate({scrollTop:0},1500);
		return false
	})

	$('#section-list strong a').click(function(){
		var str=$(this).parent();
		if(str.hasClass('selected')){
			str.removeClass('selected');
			str.nextAll('.sec:first').hide()
		}
		else{
			str.addClass('selected');
			str.nextAll('.sec:first').show()
		}
		return false;
	})
	
	$('#tov').live('change',function(){
		$('#content').html('<img src="/bitrix/templates/main/images/loading.gif" id="loading" />')
		$.post("/bitrix/templates/main/search.php",{id:$(this).val()},function(url) {
			$.get(url,function(cont) {
				$('#content').html($(cont).find('#content').html());
			})
		});
	})
	
	$('#product table:first tr:first').find('td').each(function(){
		$(this).addClass('th')
		$(this).html($(this).text())
	})
	$('#product > span, #product p span, #product font span').each(function(){
		if($(this).css('font-weight')=='bold' || $(this).css('font-weight')>100) $(this).wrap('<b></b>')
	})
	
	//options
	$('#qq input[type="radio"]').each(function(){
		$(this).hide().wrap('<span class="opt" />')
	})

	$('.opt, #qq label').live('mouseover mouseout click', function(event){
		if (event.type == 'mouseover')
			$(this).parent().find('span').css('background-position','0 -17px')
		else 
			if (event.type == 'mouseout')
				$(this).parent().find('span').css('background-position','0 0')
			else{
				var sp=$(this).parent().find('span');
				$('.opt').removeClass('selected').find('input').removeAttr('checked');
				sp.addClass('selected');
				sp.children('input').attr('checked','checked');
			}
	})
	
	$('#searchform select').live('change',function(){
		$('#searchform').fadeTo(200,0.5)
		$vars=$('#searchform').serialize();
		$.post("/bitrix/templates/main/search.php",$vars,function(data) {
			$('#searchform').replaceWith(data);
		});
		
		return false;
	})
	
	$('#q').click(function(){
		$(this).val('')
	})
$('#icons').supersleight({shim: '/bitrix/templates/main/images/x.gif'});
})
