(function($) {
	$.fn.outerHTML = function(s) {
		return (s) 
			? this.before(s).remove() 
			: $('<p>').append(this.eq(0).clone()).html();
	}
})(jQuery);


jQuery.fn.inputVal = function(title) {

	var obj = $(this);

	$(this).closest('form').submit(function() { $(obj).val($(obj).inputValGetVal());  })

	$(this).attr('firstText', title);
	$(this).val(title);
		
	$(this).click(function(){
		if ($(this).attr('firstText') == $(this).val())
		{
			$(this).val('');
		}
	});
	
	$(this).blur(function(){
		if ($(this).attr('firstText') != '' && $(this).val() == '')
		{
			$(this).val($(this).attr('firstText'));
		}
		else if ($(this).attr('firstText') != '' && $(this).val() != '')
		{
			$(this).attr('firstText', '');
		}
	});
};

jQuery.fn.inputValGetVal = function(){
	if ($(this).attr('firstText') != '' && $(this).val() == $(this).attr('firstText')) return "";
	else return $(this).val();
};

function MyAlert(str)
{
	$.fancybox(
		str,
		{
			backgroundColor: '#fff',
			scrolling: 'no'
		}
	);
}

jQuery.fn.FadeSlide = function(he){
	var obj = $(this);
	
	var numLi = $(obj).find('li').length;
	var currLi = 1;
	
	if (numLi < 2) return;
	
	var mouseOver = false;
	var isMouseOverNow = false;
	
	$(obj).mouseover(function() {
		mouseOver = true;
		isMouseOverNow = true;
	});
	$(obj).mouseout(function() {
		isMouseOverNow = false;
		setTimeout(function() { if (!isMouseOverNow) { mouseOver = false; } }, 5000);
	});
	
	$(obj).find('li').not(':eq(0)').hide();
	$(obj).css({position: 'relative', height: he + "px"});
	$(obj).find('li').css({position: 'absolute'});
	
	
	setInterval(function() {
		if (mouseOver) {return true;}
		$(obj).find('li').fadeOut('slow');
		$(obj).find('li:eq(' + currLi + ')').fadeIn('slow');
		currLi++;
		if (numLi == currLi) { currLi = 0;}
	}, 5000);
	
	setTimeout(function() { $(obj).find('li:not(:eq(0))').hide(); }, 500);
};


$(function() {
	$('*[rel=fancy], .fancy').each(function(){
		var title = $(this).attr('title');
		$(this).fancybox({showNavArrows: false, enableKeyboardNavigation: false, href: this.href + "&pmode=empg", title: title });
	});
	
	$('a[rel=blank]').attr('target', '_blank');
	
	/* HomePage */
	$('#header li.first a').append("<span id='numOfProducts'></span>");
	refreshOrder();
	
	/* Shop */
});

jQuery.fn.MyCheckbox = function() {
	
	var obj = $(this);
	
	$(this).wrap('<div class="checkbox" />');
	$(this).after('<span></span>');
	
	$(this).closest('.checkbox').find('span').click(function(){
		if($(obj).is(':checked')){ $(obj).attr('checked', false); $(this).removeClass('checked'); }
		else { $(obj).attr('checked', true); $(this).addClass('checked'); }
	});
	
};

function refreshOrder()
{
	
	$.get('index.php?module=Shop&pname=getOrderData&pmode=empg', function(msg) {
		ex = msg.split('_');
		//$('#orderPrice').html(ex[0]);
		$('#numOfProducts').html('(' + ex[1] + ')');
	});
}




var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

function jsddm_open()
{
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').eq(0).css('display', 'block');

}

function jsddm_close()
{
	if(ddmenuitem) ddmenuitem.css('display', 'none');
}

function jsddm_timer()
{
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

$(document).ready(function(){
	$('#header > ul > li').bind('mouseover', jsddm_open);
	$('#header > ul > li').bind('mouseout',  jsddm_timer);
});

document.onclick = jsddm_close;


$(function() {
	$('#quats ul').FadeSlide('20')
	
});
