var url =  new String(document.location)
var urlels =  url.split('/');
var lang = 'en';
var txtmore = 'Read more';

$(document).ready(function(){


	$('.button').click(function(){this.blur();});

	if ((urlels[3].charAt(0) == '?') || (urlels[3] == 'inverno') || (urlels[3] == 'estate') || (urlels[3]=='index.php')  || (urlels[3]=='')){
		lang = 'it';
		txtmore = 'Ulteriori informazioni';
	}


	/*______________________________________________
	 * Slideshow
	 */
	if ($('#slideshow').length>0){
		var dir = $('#slideshow div').attr('class');
		if (dir!==''){
			$('#slideshow').html('').flash(
				{
					src: '/img/slide.swf?xmlfile='+dir,
					width: 652,
					height: 340,
					border:0,
					flashvars: { xmlfile:dir}
				},
				{ version: 8 }
			);
		}
	}

	$('form input,form.contact textarea').hover(
		function(){$(this).addClass('over');},
		function(){$(this).removeClass('over');}
	).focus(function(){$(this).addClass('focus');})
	 .blur(function(){$(this).removeClass('focus');});


	var options = {};
	if ($('#dateFrom').attr('alt')!=''){
		var options = eval('('+$('#dateFrom').attr('alt')+')');
	}

	// Date picker
	var dateoptions = $.extend({
		beforeShow: customRange,
		showOn: "both",
		dateFormat: "dd/mm/yy",
		onSelect: function(InputDate){
			var id = new String($(this).attr('id'));
			var date = InputDate.split('/');
			var pre = (id == 'dateFrom') ? 'arrival_' : 'departure_';

			if (date[0].charAt(0) == '0' ) date[0] = date[0].charAt(1);
			if (date[1].charAt(0) == '0' ) date[1] = date[1].charAt(1);

			$('#'+pre+'day').val(parseInt(date[0]));
			$('#'+pre+'month').val(parseInt(date[1])-1);
			$('#'+pre+'year').val(parseInt(date[2]));

		},
		buttonImage: "/reservation/media/res_calendar.gif",
		buttonImageOnly: true
	}, options);


	$('#dateFrom, #dateTo').datepicker(dateoptions)//.keypress(function(){});


	// Rss Smart
	if( $('#RSSsmart').length > 0)
		rssSmart();

	if ($('#offerta').length > 0){
		var msg = '';



		msg += $.ajax({url: "/CI/last_mini/"+lang, async: false}).responseText;
		msg += $.ajax({url: "/CI/offers_mini/"+lang, async: false}).responseText;
		msg += $.ajax({url: "/CI/wellness_mini/"+lang, async: false}).responseText;


		$('#offerta').html(msg);
		//cycle offers
		$('#offerta').cycle({
			fx:		'pinch',
			timeout:18000,
			speed:	1000,
			//height:	220,
			pause:	true,
			random:	true,
			fit:	true
		});
		$('#offerta .contenutoofferta')
			.hover(
				function(){
					$('img', this).addClass('over').css({opacity:0.5});
				},
				function(){
					$('img', this).removeClass('over').css({opacity:1});
				})
			.click(function(){document.location = $('a', this).attr('href');})
			.css({cursor:'pointer'});



	}


	$('form#NL').submit(function(){
		var email = $('#email', this).attr('value');
		var name = $('#name', this).attr('value');
		if (! /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(email) ){
			alert('Email non valida');
			return false;
		}
		else if(name==''){
			alert('Devi inserire un nome');
			return false;
		}
		else{
			return true;
		}
	});

	$('form#NL #name').focus(function(){
		if ($(this).attr('value') == 'nome')
			$(this).attr('value', '');
	});
	$('form#NL #email').focus(function(){
		if ($(this).attr('value') == 'e-mail')
			$(this).attr('value', '');
	});



});

$.fn.cycle.transitions.pinch = function($cont, $slides, opts) {
    var $el = $($slides[0]);
    var w = $el.width();
    var h = $el.height();
    opts.cssBefore = { top: 5, left: 5, width: 0, height: 0, zIndex: 1 };
    opts.animIn    = { width: w, height: h };
    opts.animOut   = { top: 0, left: w, width: 0, height: h };
    opts.cssAfter  = { zIndex: 0, display:'none'};
};

function rssSmart(){
	caricaRSS_RSSsmart();
	$('#RSSsmart a').each(function(){
		var href="/includes/parseSmart.php?url=";
		href += $(this).attr('href');
		href += '?KeepThis=false&TB_iframe=true&height=450&width=800';
		$(this).attr('href',href).attr('onclick','').attr('title','News prelevata da www.sanmartino.com');
		$(this).parent().parent().attr('onclick','').append('<a href="'+href+'">'+txtmore+' »</a>');
	});
	tb_init('#RSSsmart a');
}

function customRange(input) {
	if($('#dateFrom').attr('alt') =='')
		var opt = {};
	else
		var opt = eval('('+$('#dateFrom').attr('alt')+')')

	var from = $("#dateFrom").datepicker("getDate") == null ?  opt.minDate :$("#dateFrom").datepicker("getDate") ;
	var to = $("#dateTo").datepicker("getDate") == null ?  opt.maxDate :$("#dateTo").datepicker("getDate") ;
  return {
	 minDate: (input.id == "dateTo"   ? from : opt.minDate),
	 maxDate: (input.id == "dateFrom" ? to: opt.maxDate)
	};
}

function include(script_filename) {
	document.write('<' + 'script');
	document.write(' language="javascript"');
	document.write(' type="text/javascript"');
	document.write(' src="' + script_filename + '">');
	document.write('</' + 'script' + '>');
}


/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Updated by: Mike Weiner :: http://www.wearebent.com
Original author: Eric King (eric_andrew_king@hotmail.com)
Last Updated: May 2006 */

function newWindow(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen) {
  var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
  var int_windowTop = (screen.height - a_int_windowHeight) / 2;
  var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
  var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
    if (parseInt(navigator.appVersion) >= 4) {
      obj_window.window.focus();
    }
}
