(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: '#686868'
		}
	);
}

$(function() {
	$('*[rel=fancy], .fancy').fancybox({showNavArrows: false, enableKeyboardNavigation: false});
	
	$('a[rel=blank]').attr('target', '_blank');

	// Sub
	$('#jsddm img').hover(function(){

		newImg = $(this).attr('src');
		newImg = newImg.replace('.gif', 'h.gif');
								   
		$(this).attr('src', newImg);
	}, function(){

		newImg = $(this).attr('src');
		newImg = newImg.replace('h.gif', '.gif');
								   
		$(this).attr('src', newImg);
	})



	$('#sideBar input[name=yourName]').inputVal('שם:');
	$('#sideBar input[name=yourEmail]').inputVal('דוא"ל:');
	$('#sideBar input[name=yourPhone]').inputVal('טלפון:');
	
	$('#sideBar').submit(function(){
		
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if ($('#sideBar input[name=yourName]').val() == "") { alert("יש למלא שם"); $('#sideBar input[name=yourName]').focus(); return false; }
		if (!filter.test($('#sideBar input[name=yourEmail]').val())) { alert("יש למלא אימייל תקין"); $('#sideBar input[name=yourEmail]').focus(); return false; }
		if ($('#sideBar input[name=yourPhone]').val() == "") { alert("יש למלא מספר טלפון"); $('#sideBar input[name=yourPhone]').focus(); return false; }
		
		data = $('#sideBar form').serialize();
		$.post('index.php?module=Contact',data, function(msg){
			MyAlert("תודה רבה.<br />ניצור איתך קשר בהקדם.");
			
			$('#sideBar input').attr('disabled', true);
		});
		return false;
	});
	
	
	$('#newsletter').submit(function(){
		
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

		if (!filter.test($('#newsletter input[name=yourEmail]').val())) { alert("יש למלא אימייל תקין"); $('#newsletter input[name=yourEmail]').focus(); return false; }
		
		data = $(this).serialize();
		$.post('index.php?module=Newsletter&pname=send&pmode=empg',data, function(msg){

			MyAlert("תודה רבה.<br />ניצור איתך קשר בהקדם.");
			
			$('#newsletter input').attr('disabled', true);
		});
		return false;
	});
});
