jQuery.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            return (
                key == 8 || 
                key == 9 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};

$(document).ready(function()
{


	$(".photos_group").fancybox(
	{
		'centerOnScroll'	: true,
		'titlePosition' 	: 'over',
		'cyclic'			: true,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
	});

	$(".map").fancybox(
	{
		'centerOnScroll'	: true,
		'width'				: '100%',
		'height'			: '100%',
		'autoScale'			: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titleShow'			: false,
		'type'				: 'iframe'
	});

	$(".cycle").each(
		function(intIndex)
		{
			var n = Math.floor(Math.random()*10);
			var s = (((intIndex+1)+3)/2)*1000;
			$(this).cycle(
			{ 
				fx		: 'fade', 
				speed	: s, 
				timeout	: 2500, 
				pause	: 0
			});
		}
	);

	$('.flash-images').cycle(
	{ 
		fx		: 'fade', 
		speed	: 5000,
		timeout	: 2500,
    	pager	: '#flash-preview',
		pagerAnchorBuilder	: function(idx, slide)
		{
			return '<a href="#"><img src="' + slide.src + '" width="20" height="20" alt="' + (idx+1) + '" title="' + (idx+1) + '" /></a>'; 
		}
	});

	$(".date-pick").datePicker();


	$('.horizontal_scroller').SetScroller(
	{
		velocity	: 60,
		direction	: 'horizontal',
		startfrom	: 'right',
		loop		: 'infinite',
		movetype	: 'linear',
		onmouseover	: 'pause',
		onmouseout	: 'play',
		onstartup	: 'play',
		cursor		: 'pointer'
	});

	$("#simple").click(function()
	{
		if ($('#simple').length && $('#simple').is(':checked'))
		{
			$('#end').attr('disabled', 'disabled');
			$('#end').css({'background-color': '#D4D0C8'});
			$('#end').dpSetDisabled(true);
		}
		else
		{
			$('#end').removeAttr('disabled');
			$('#end').css({'background-color': '#ffffff'});
			$('#end').dpSetDisabled(false);
		}
	});

	$("#tickets").ForceNumericOnly();

});

function init_search(word, img, src, nam)
{
	$(document).ready(function()
	{
		$("#search").focusout( function() { if ($("#search").val() == word) { $("#search").val(''); } else if ($("#search").val() == "") { $("#search").val(word); }; } );
		$("#search").focusin( function() { if ($("#search").val() == word) { $("#search").val(''); } else { $("#search").select(); }; } );
		$(".weather").fancybox(
		{
			'scrolling'			: 'no',
			'centerOnScroll'	: true,
			'width'				: 350,
			'height'			: 350,
			'margin'			: 5,
			'autoScale'			: false,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'titlePosition'		: 'inside',
			'type'				: 'iframe',
			'titleFormat'		: function()
			{
				return '<span id="fancybox-title-over">'+src+': <a href="http://www.eweather.gr" target="_blank" title="'+src+'">http://www.eweather.gr</a></span>';
			}
		});

		$("a[rel*='photos_group']").fancybox(
		{
			'centerOnScroll'	: true,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'titlePosition' 	: 'over',
			'cyclic'			: true,
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts)
			{
				return '<' + 'span' + ' id="fancybox-title-over">'+img+' ' +  (currentIndex + 1) + ' / ' + currentArray.length + '<' + 'br' + ' />' + title + '</' + 'span>';
			}
		});

		$('#tickets').keyup(function()
		{
			var i = 0;
			var n = ($('#tickets').val() - $("input[id*='passid']").length);
			for (i = 0; i < n; i++) { add_passenger(nam); }
		});

		$("form#ticket input, form#ticket textarea[id='text_ifr']").each(function()
		{
			$(this).keyup(function()
			{
				if (jQuery.trim($(this).val()) == '') { $(this).css({'border': '1px solid #ff0000', 'background-color': '#FA7E16'}); }
				else { $(this).css({'border': '1px solid #2678C1', 'background-color': '#ffffff'}); }
			});
		}); 

		$('#send_ticket').click(function(event)
		{
			var ok = true;
			$("form#ticket input:not([id*='passid']), form#ticket textarea[id='text_ifr']").each(function()
			{
				if (jQuery.trim($(this).val()) == '')
				{
					$(this).css({'border': '1px solid #ff0000', 'background-color': '#FA7E16'});
					ok = false;
				}
			}); 

			if (ok == false)
			{
				var s = location.href; s = s.replace('#cell', "");
				location.href = s.replace('#', "")+"#cell";
				event.preventDefault();
				show_mes($('#no_data').val(), 0);
				return false;
			}
			else { return true; }
		});

	});
}


function get_captcha(id)
{
	var timestamp = new Date().getTime();
	$('#'+id).attr('src', '/captcha.php?time='+timestamp);
}

function select_property(id, table, name)
{
	$(document).ready(function()
	{
		$.get("/classes/select.php", { 'table': table, 'name': name }, 
		function(data)
		{
			$("#"+id).html(data);
			if ($('#properties').length)
			{
				if ($('#properties').val() == '0')
				{
					$('#send_offer').attr('disabled', 'disabled');
					$('#properties').css({'border': '1px solid #ff0000', 'background-color': '#FA7E16'});
				}
				else
				{
					$('#properties').css({'border': '1px solid inherit', 'background-color': '#ffffff'});
					$('#send_offer').removeAttr('disabled');
				}
			}
		});
	});
}

function toggle_div(id)
{
	$(document).ready(function()
	{
		$('#' + id).slideToggle('fast', function()
		{
			if ($('#properties').length)
			{
				if ($('#properties').val() == '0')
				{
					$('#send_offer').attr('disabled', 'disabled');
					$('#properties').css({'border': '1px solid #ff0000', 'background-color': '#FA7E16'});
				}
				else
				{
					$('#properties').css({'border': '1px solid inherit', 'background-color': '#ffffff'});
					$('#send_offer').removeAttr('disabled');
				}
			}
		});
	});
}

function add_passenger(nam, fullname, id)
{
	var rowCount = $('#passengers_table tr').length;
	rowCount++;
	$('#passengers_table tr:last').after('<tr id="pass'+rowCount+'"><td><hr /><img class="person" src="/sbin/img/person.png" height="16" width="16" alt="'+nam+'" title="'+name+'" />&nbsp;<strong>'+nam+'&nbsp;<span id="passn'+rowCount+'">'+($("input[id*='passid']").length+1)+'</span>:</strong><a href="javascript:void(0);" onclick="javascript:remove_item('+rowCount+');" class="remove"></a>'+
	'<br /><label for="passname'+rowCount+'">'+fullname+':</label><input name="pass_name[]" type="text" value="" id="passname'+rowCount+'" />'+
	'<label for="passid'+rowCount+'">'+id+':</label><input name="pass_id[]" type="text" value="" id="passid'+rowCount+'" /></td></tr>');
	$("#tickets").val($("input[id*='passid']").length);
	clear_numbers();
}

function remove_item(id) { $("#pass"+id).remove(); $("#tickets").val($("input[id*='passid']").length); clear_numbers(); }

function clear_numbers()
{
	var i = 1;
	$("span[id*='passn']").each(function() { $(this).html(i); i++; });
}

function remove_all()
{
	$("tr[id*='pass']").each(function()
	{
		$(this).remove();
		$("#tickets").val($("input[id*='passid']").length);
		clear_numbers();
	});
}

function show_mes(message, typ)
{
	var cls; if (typ == 0) { cls = "error"; } else if (typ == 1) { cls = "ok"; };
	$(document).ready(function()
	{
		$.fancybox('<div class="' + cls + '">'+message+'</div>',
		{
			'centerOnScroll'	: true,
			'titleShow'			: 'false',
			'titlePosition' 	: 'outside',
			'width'         	: 'auto',
			'height'        	: 'auto',
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
		});
	});
}

function check_simple()
{
	$(document).ready(function()
	{
		if ($('#simple').length && $('#simple').is(':checked'))
		{
			$('#end').attr('disabled', 'disabled');
			$('#end').css({'background-color': '#D4D0C8'});
			$('#end').dpSetDisabled(true);
		}
		else
		{
			$('#end').removeAttr('disabled');
			$('#end').css({'background-color': '#ffffff'});
			$('#end').dpSetDisabled(false);
		}
	});
}
