$(document).ready( function(){

$('.select-box ul').hide();

$('.select-box:not(".disabled") .select-value').click(function(){
	$(this).parents('.select-box').addClass('active').find('ul').show();
	return false;
});

$('.city .cusel span').live('click', function(){
	var par = $(this).parents('.city');
	var name = par.find('.cuselText').text();
	par.find('.selected_city').html(name);
	return false;
})

/*	
$('.select-box').hover(null, function(){
	if ($(this).find('ul').is(':visible')) $(this).find('ul').hide();
	$(this).find('.select-value').blur();
	$(this).removeClass('active');
});
*/

$('ul.sf-menu').superfish();

$('.show_all').click(function(){
		$(this).parent().find('.cat_list.hidden').slideToggle(500);
		$(this).parent().find('.all').slideToggle(0);
		$(this).parent().find('.no_all').slideToggle(0);
		$(this).toggleClass('hide_all');
	});
	
$('.special li:last-child').addClass('last');

$('.catalog li a.sort').click(function(){
		$('.catalog li').removeClass('active');
		$(this).parent().addClass('active');
	});
/*inputPlaceholder( document.getElementById('inp1') );*/

	$('section.price .with-price.pseudolink').mouseenter(function(){
		var pos = getElementPosition(this);
		var popup = $("#" + $(this).attr('rel'));
		popup.css('left', pos.left - 0 + 'px');
		popup.css('top', pos.top + 20 + 'px');
		$('[id^=price]').not(popup).stopTime().fadeOut('slow');
		popup.show();
	})

	$('section.price .with-price.pseudolink').mouseleave(function(){
		
		var popup = $("#" + $(this).attr('rel'));
		if (popup.mouseover()) {
			popup.mouseleave(function(){
				rand_num = Math.floor(Math.random() * 11);
				popup.oneTime(1000, rand_num, function(){popup.fadeOut('slow')});
			})
			return;
		} 
		rand_num = Math.floor( Math.random() * 11 );
		popup.oneTime(1000, rand_num, function(){popup.fadeOut('slow')});
	});


	var params = {
		changedEl: ".select-box select",
		visRows: 11,
		scrollArrows: false
	}
	cuSel(params);
});

function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent)
		obj.attachEvent( "on"+type, fn );
}

function removeEvent(obj, type, fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(type, fn, false);
	else if (obj.detachEvent)
		obj.detachEvent( "on"+type, fn );
}

function isChildNode(elem, sell) {
	for (var childItem in elem.childNodes) {
		if (elem.childNodes[childItem].nodeType == 1) {
			if (elem.childNodes[childItem] == sell)
				return true;
			else if (isChildNode(elem.childNodes[childItem], sell))
				return true;
		}
	}
	return false;
}


function getElementPosition(elem) {
	var w = elem.offsetWidth;
	var h = elem.offsetHeight;

	var l = 0;
	var t = 0;

	while (elem) {
		l += elem.offsetLeft;
		t += elem.offsetTop;
		elem = elem.offsetParent;
	}

	return { "left": l, "top": t, "width": w, "height": h };
}
