var shop = {};
jQuery(document).ready(function(){

	
	jQuery('.top-menu li').hover(function() {
		jQuery('a:first', this).addClass("active");
		jQuery('ul:first', this).show();
		jQuery('a div.adim-btns', this).show();
	}, function() {
		jQuery('a', this).removeClass("hover");
		jQuery('ul', this).hide();
		jQuery('a div.adim-btns', this).hide();
	}); 

	
	if(jQuery('#slider').length) jQuery('#slider').baslider({
		'numeric': true,
		'numericId': 'numControl',
		'auto':true,
		'animatePropertiesXML': '/direct/js/options.xml',
		'afterslide' : function(a,b,c,d){
			/*console.log(a,b,c,d);*/
		}
	});
	
	if(jQuery('.newsScroller').length) jQuery('.newsScroller').innerFade({
		speed: 500,
		timeout: 10000,
		type: 'sequence',
		prevLink: jQuery('#nControls .left'),
		nextLink: jQuery('#nControls .right')
	}); 
	
	function shopClass(sD)
	{
		this.shopDiv = jQuery(sD);
		this.init = function()
		{
			var self = this;
			jQuery('.button', this.shopDiv).click(function(){
				//alert('Сначало заплоти!');
			})
			jQuery('select[name="card.uid"]', this.shopDiv).change(
				this.refresh
			);
			jQuery('input[name="card.quantity"]', this.shopDiv).keypress(function(event){
				var r = self.setCnt(event);
				if(r) self.refresh();
				return r;
			});
			this.refresh();
		}
		this.refresh = function(){ 
		
			var s = jQuery('select[name="card.uid"] option:selected');
			var v = s.attr('rel');
			var t = s.html();
			var c = parseInt(jQuery('input[name="card.quantity"]').val());
			jQuery('#bigCardPrice span').html(v);

			jQuery('div.grey.w85', self.shopDiv).html( t*c + ',<span>00</span>');
			
		}
		this.getCnt = function(){
			return parseInt(jQuery('input[name="card.quantity"]').val());
		}
		this.setCnt = function(evt){
			var charCode = (evt.which) ? evt.which : evt.keyCode;
			if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
			return true;
		}
		this.init();
	}
	var shop = new shopClass('.buycard');
});

var animeSubmenu, animeArrow;

function ChangeActiveSiteGroup(el)
{
	
	if (MEL.dom.hasClass(el, "active")) return;

	if (animeSubmenu) animeSubmenu.finish();
	if (animeArrow) animeArrow.stop();

	var firstSubsiteMenu;

	// get prevActiveBlock, nextActiveBlock
		var i, box = el.parentNode;
		var prevActiveBlock, nextActiveBlock = el;

		for (i=0; i<box.childNodes.length; i++)
		{
			if (!firstSubsiteMenu && MEL.dom.hasClass(box.childNodes[i], "subsite_menu"))
				firstSubsiteMenu = box.childNodes[i];
			if (!prevActiveBlock && MEL.dom.hasClass(box.childNodes[i], "active"))
				prevActiveBlock = box.childNodes[i];
		}

	// get prevActiveName, nextActiveName
		var prevActiveName = (MEL.dom.hasClass(prevActiveBlock, "media") ? "media" :
								(MEL.dom.hasClass(prevActiveBlock, "community") ? "community" :
									"ads"));
		var nextActiveName = (MEL.dom.hasClass(nextActiveBlock, "media") ? "media" :
								(MEL.dom.hasClass(nextActiveBlock, "community") ? "community" :
									"ads"));

	// get prevActiveDescr, nextActiveDescr
		var prevActiveDescr, nextActiveDescr;
		var descrBox = MEL.d.getEl("underline");
		for (i=0; i<descrBox.childNodes.length; i++)
		{
			if (!prevActiveDescr && MEL.dom.hasClass(descrBox.childNodes[i], prevActiveName))
				prevActiveDescr = descrBox.childNodes[i];
			if (!nextActiveDescr && MEL.dom.hasClass(descrBox.childNodes[i], nextActiveName))
				nextActiveDescr = descrBox.childNodes[i];
		}

	// get prevActiveMenu, nextActiveMenu
		var prevActiveMenu, nextActiveMenu;
		for (i=0; i<prevActiveBlock.childNodes.length; i++)
			if (MEL.dom.hasClass(prevActiveBlock.childNodes[i], "menu"))
			{
				prevActiveMenu = prevActiveBlock.childNodes[i];
				break;
			}
		for (i=0; i<nextActiveBlock.childNodes.length; i++)
			if (MEL.dom.hasClass(nextActiveBlock.childNodes[i], "menu"))
			{
				nextActiveMenu = nextActiveBlock.childNodes[i];
				break;
			}
	//

	MEL.dom.removeClass(prevActiveBlock, "active");
	MEL.dom.appendClass(nextActiveBlock, "active");

	animeSubmenu = MEL.animation.Create();
		animeSubmenu.animateOpacity(prevActiveMenu, 1, 0, 0.1);
		animeSubmenu.animateOpacity(nextActiveMenu, 0, 1, 0.1);

		var firstPos = MEL.dom.getAbsolutePos(firstSubsiteMenu);
		var pos = MEL.dom.getAbsolutePos(nextActiveBlock);
		nextActiveDescr.style.left = pos.x+"px";

		animeSubmenu.animateOpacity(prevActiveDescr, 1, 0, 0.1);
	animeSubmenu.start();

	animeArrow = MEL.animation.Create();
		animeArrow.cfg.TIME = 0;

		var arrow = descrBox.getElementsByTagName("P")[0];
		var from = parseInt(MEL.dom.getStyle(arrow, "margin-left"));
		var to = (pos.x - firstPos.x) + 65;
		animeArrow.addParam(arrow, "style.marginLeft", from, to, "10px", null,
								[to + (from < to ? -50 : 50), to + (from < to ? -10 : 10)]);

		animeSubmenu.animateOpacity(nextActiveDescr, 0, 1, 0.1);

	animeArrow.start();
}


jQuery(document).ready(function(){
	
	var fillForm = jQuery('#main_fill_number');
	
	jQuery('a#sort_button_main', fillForm).click(function(event){
		event.preventDefault();
		
		var inp = jQuery('input[name=fill_number]', fillForm);
		var number = jQuery('input[name=fill_numberprefix]', fillForm).val() + inp.val()
		jQuery.ajax({
			url: '/' + current_lang + '/action/check_number_existance?n=' + number,
			success: function( res ) { 
				if(res=='ok')
				{
					fillForm.submit();
				}
				else
				{
					var f = new formValidatorClass(jQuery('<form></form>'),null);
					f.clearErrors();
					f.showError(inp, 'No such number');
				}
			}
		});
	});
	
	
	jQuery('#sendSMS').click(function(event){
		event.preventDefault();
		var form = jQuery('#form_send_sms');
		var inp = jQuery('input[name="number.value"]', form);
		var number = jQuery('input[name="number.valueprefix"]', form).val() + inp.val()
		jQuery.ajax({
			url: '/' + current_lang + '/action/check_number_existance?n=' + number,
			success: function( res ) { 
				if(res=='ok')
				{
					form.submit();
				}
				else
				{
					var f = new formValidatorClass(jQuery('<form></form>'),null);
					f.clearErrors();
					f.showError(inp, 'No such number');
				}
			}
		});
	});

	
});
