jQuery(document).ready(function(){
	
	//счетчкии
	function BM_Draw(oBM_STAT){
		$('#bigmir').append('<table cellpadding="0" cellspacing="0" border="0" style="display:inline;margin-right:4px;"><tr><td><div style="margin:0px;padding:0px;font-size:1px;width:88px;"><div style="background:url(\'http://i.bigmir.net/cnt/samples/diagonal/b60_top.gif\') no-repeat bottom;">&nbsp;</div><div style="font:10px Tahoma;background:url(\'http://i.bigmir.net/cnt/samples/diagonal/b60_center.gif\');"><div style="text-align:center;"><a href="http://www.bigmir.net/" target="_blank" style="color:#0000ab;text-decoration:none;font:10px Tahoma;">bigmir<span style="color:#ff0000;">)</span>net</a></div><div style="margin-top:3px;padding: 0px 6px 0px 6px;color:#426ed2;"><div style="float:left;font:10px Tahoma;">'+oBM_STAT.hosts+'</div><div style="float:right;font:10px Tahoma;">'+oBM_STAT.hits+'</div></div><br clear="all"/></div><div style="background:url(\'http://i.bigmir.net/cnt/samples/diagonal/b60_bottom.gif\') no-repeat top;">&nbsp;</div></div></td></tr></table>');
	}
	
	$('#chng-country').click(function(){
		if ($('#countries-cont').is(':hidden')) {
			$('#countries-cont').show('slide', {direction: "up"} , 500, callback);
		} else {
			$('#countries-cont').hide('slide', {direction: "up"} , 500, callback);
		}
		return false;
	});
	
	$('.countryid').click(function(event){
		idFull = jQuery(event.target).attr('id');
		id = idFull.substr(10, 100);
		$('#chng-country').load('/ajax/setcountry/id/' + id);
		$('#countries-cont').hide('slide', {direction: "up"} , 500, callback);
	});
	
	$('#chng-region').click(function(){
		if ($('#regions-cont').is(':hidden')) {
			$('#regions-cont').show();
			$('#regions-cont').css('height', '16px');
			$('#regions-cont').html('Загрузка...');
			$.getJSON('/ajax/regions/country/', function(data) {
				$('#regions-cont').hide();
				$('#regions-cont').html('');
				$.each(data, function(i, item) {
					$('#regions-cont').append('<label><input type="radio" class="regionid" value="' + item.region_id + '" name="region">' + item.region_name + '</label><br />');
				});
				$('#regions-cont').css('height', '300px');
				$('#regions-cont').show('slide', {direction: "up"} , 500, callback);
				updateDomRegions();
			});			
		} else {
			$('#regions-cont').hide('slide', {direction: "up"} , 500, callback);
		}
		return false;
	});

	function updateDomRegions() {
		$('.regionid').click(function(event){
			var val = jQuery(event.target).val();
			$('.region-span').load('/ajax/setregion/id/' + val);
			$('#regions-cont').hide('slide', {direction: "up"} , 500, callback);
		});
	}
	
	jQuery('.exp-catalog').click(function(event){
		id = jQuery(event.target).parent().attr('id');
		jQuery('#c' + id).slideDown();
		return false;
	});
	
	jQuery('.mylink').click(function(event) {
		id = jQuery(event.target).attr('id');
		name = jQuery(event.target).attr('title');
		jQuery('#favorites-cont').load('/ajax/favorites/url/' + id + '/name/' + escape(name) + '/');
		return false;
	});
	
	$('#catalog-list').submit(function() {
		word = $('#word').val();
		region = $('#region-id').val();
		catalog = $('#catalog').val();
		var location = $('#catalog-list').attr('action');
		if (word != '') {
			location = location + '/search/' + word;
		}
		if (region != 0) {
			location = location + '/region/' + region;
		}
		if (catalog != 0) {
			location = location + '/catalog/' + catalog;
		}

		document.location = location;
		return false;		
	});	

	$('#discount-list').submit(function() {
		dmin = $('#dmin').val();
		dmax = $('#dmax').val();
		region = $('#region-id').val();
		catalog = $('#catalog').val();
		var location = '/content/discount';
		if (dmin != '') {
			location = location + '/dmin/' + dmin;
		}
		if (dmax != '') {
			location = location + '/dmax/' + dmax;
		}
		if (region != 0) {
			location = location + '/region/' + region;
		}
		if (catalog != 0) {
			location = location + '/catalog/' + catalog;
		}

		document.location = location;
		return false;		
	});		
	
	jQuery('.category-main').click(function(event) {
		element = jQuery(event.target);
		paragraph = element.parent();
		subCats = element.parent().next();
		if (subCats.is(':hidden')) {
			subCats.slideDown();	
			paragraph.css('background-image', 'url(/images/cat_m2_r.jpg)');
		} else {
			subCats.slideUp();
			paragraph.css('background-image', 'url(/images/cat_m2.jpg)');
		}
		return false;
	});
	
	$('#new-companies').click(function() {
		$('#ctnew').attr('class', 'tab-active-r');
		$('#ct10').attr('class', 'tab-inactive-r');
		$('#new10c_cont').show();
		$('#top10c_cont').hide();
		return false;
	});

	$('#top10-companies').click(function() {
		$('#ctnew').attr('class', 'tab-inactive-r');
		$('#ct10').attr('class', 'tab-active-r');
		$('#new10c_cont').hide();
		$('#top10c_cont').show();
		return false;
	});
	
	$('#new-products').click(function() {
		$('#ptnew').attr('class', 'tab-active-r');
		$('#pt10').attr('class', 'tab-inactive-r');
		$('#new10p_cont').show();
		$('#top10p_cont').hide();
		return false;
	});

	$('#top10-products').click(function() {
		$('#ptnew').attr('class', 'tab-inactive-r');
		$('#pt10').attr('class', 'tab-active-r');
		$('#new10p_cont').hide();
		$('#top10p_cont').show();
		return false;
	});	

	
	setTimeout(function(){
			$("#success, #error").show('slide', {direction: "up"} , 1200, callback);
		}, 500);
	
	function callback(){
		setTimeout(function(){
			$("#success:visible, #error:visible").hide('slide',{direction: "up"}, 1200,callback);
		}, 4000);
	};
	
	$('#tologin').click(function(){
		$("#dialog-login").dialog({
			resizable: false,
			modal: true,
			width: 270,
			buttons: {
				'Войти': function() {
					$('#login-form').submit();
				}
			}			
		});
	});
	
	
	

});

