// if(self.location.hostname!=top.location.hostname) {
// top.location.href=self.location.href;
// }

var $j = jQuery;

/* */
jQuery(function($j){
	// do jQuery stuff when DOM is ready

  $j().doPreload();

$j('a[href*="http://"]:not([href*="'+location.hostname+'"])').attr('target','_blank').attr('rel','nofollow');

var demo = false;// || window.location.href.match(/^.*localhost/i) || (NOP == 1);

var _FORM = $j('.jqtransform');
if(_FORM.length) {
 _FORM.get(0).reset();
	if (demo) {
		$j('#e-name').val('ENRICO POSSENTI');
		$j('#e-city').val('CALCI');
		$j('#e-country').val('ITALIA');
		$j('#e-phone').val('+39 349 3324498');
		$j('#e-email').val('test@noveopiu.com');
		$j('#e-from').val('2011-10-01');
		$j('#e-upto').val('2011-10-15');
 }

	if ($j().jqTransform) {
		_FORM.jqTransform();
	}

	if ($j().validationEngine) {
		_FORM.validationEngine();
	}

	_FORM.submit(function(){
	 $j('#e-apartmentname').val($j('option:selected', this).text());
	});


	if ($j().datepicker) {
		$j().getNights();
		$j('.datepicker').datepicker({
			inline: true,
			minDate: '+1',
			altFormat: 'dd-mm-yy',
			dateFormat: 'yy-mm-dd',
			onSelect: function(dateText, inst) {
				$j().getNights();
			}
		});
	}

}

if ($j().nyroModal) {
  if ($j('.nyroModal').length) {
			$j.nyroModalSettings({
     endShowContent: function() {
						if ($j('#nyroModalContent .ajaxed').length) {
       $j('.ajaxed .slider img:first').attr('src',$j('.ajaxed .slider img:first').attr('src')+"?");
       $j().doPreload();
       $j().doSlider();
						};
     },
			  processHandler: function(settings) {
							var from = settings.from;
							$jgoOn = true;
							// LOCAL FLV
							if ($jgoOn) {
								var from = settings.from;
								if (from && from.href) {
									if (from.href.match(/\.flv$j/)) {
										$jgoOn = false;
										$j.nyroModalSettings({
												type: 'swf',
												swf: {
													id: "nyroFlash",
													allowscriptaccess: 'always',
													allowfullscreen: 'true',
													flashvars: '&file='+from.href+'&autoStart=true&allowfullscreen=true&allowscriptaccess=always'
												},
												width: 640,
												height: 512,
												url: 'http://freevideocoding.com/flvplayer.swf'
										});
									};
								}
							};
							// YOUTUBE
							if ($jgoOn) {
								var url = settings.url;
								if (url && url.indexOf('http://www.youtube.com/watch?v=') == 0) {
									url = url.replace(new RegExp("watch\\?v=", "i"), 'v/');
								};
								if (url && url.indexOf('http://www.youtube.com/v/') == 0) {
									$jgoOn = false;
									url += '?';
									url += 'enablejsapi=1&amp;';
									url += 'version=3&amp;';
									url += 'playerapiid=nyroFlash&amp;';
									url += 'fs=1&amp;';
									url += 'autoplay=1&amp;';
									url += 'loop=0&amp;';
									url += 'rel=0&amp;';
									url += 'showinfo=0&amp;';
									url += 'iv_load_policy=3';
									$j.nyroModalSettings({
											type: 'swf',
											swf: {
												id: "nyroFlash",
												allowscriptaccess: 'always',
												allowfullscreen: 'true',
												flashvars: '&file='+from.href+'&autoStart=true&allowfullscreen=true&allowscriptaccess=always'
											},
											width: 640,
											height: 512,
										url: url
									});
								}
							};
							// WEB PAGE
							if ($jgoOn) {
								if (from && from.href) {
										$jgoOn = false;
										$j.nyroModalSettings({
										});
								};
							};
							// IMAGE
							if ($jgoOn) {
								if (from && from.src) {
									$jgoOn = false;
									$j.nyroModalSettings({
											type: 'image',
											width: 100,
											height: 100
									});
							};
						};
					}
			});
	  $j('.nyroModal').nyroModal();
  };
 };

 if ($j().localScroll) {
		$j('a[href]').click(function(e){
			src = location.href;
			tgt = $j(this).attr('href');
			tgx = tgt.split('#');
			if (location.pathname.indexOf(tgx[0])!=-1) {
				if (tgt.indexOf('#')!=-1) {
					// tgt = tgt.substr(tgt.indexOf('#')+1);
					tgt = tgt.substr(tgt.indexOf('#'));
					//if ($j('a[name='+tgt+']').length) {
					if ($j(tgt).length) {
						e.preventDefault();
						// $j('a[name='+tgt+']').localScroll().targetize();
						if ($j(this).parents('#tabs').length) {
							 $j('#tabs').localScroll().targetize();
						} else {
							$j(tgt).localScroll().targetize();
						};
						return false;
					};
				};
			};
			return true;
		});

		if (location.hash.indexOf('#')!=-1) {
			// $j('a[name='+location.hash.substr(1)+']').localScroll().targetize();
			if ($j('#'+location.hash.substr(1)).parents('#tabs').length) {
     $j('#tabs').localScroll().targetize();
			} else {
					$j('#'+location.hash.substr(1)).localScroll().targetize();
			};
		} else {
			// $j('html').localScroll();
		};
	};

	if ($j('.nav ul').length==2) {
  $j('.nav:eq(2)').parent().remove();
  $j('.nav:eq(1)').parent().removeClass('grid_2').addClass('grid_4');
	};

//  if ($j('#gmap').length) {
// 		$j('#gmap').load(function(){
// 		 $j('#gmap-loading').hide();
// 			$j(this).fadeIn();
// 		});
// 	};

		$j().doCarousel();
		$j().doSlider();

	//----------
	$j(window).load(function(){
		// initialize/finalize after images are loaded
	});
	//----------
});
/* */

/* ++++++++++++++++ */
/* + LOCAL SCROLL + */
/* ++++++++++++++++ */
$j.fn.localScroll = function () {
 var target = $j(this).length ? $j(this) : $j('html');
 var targetOffset = target.offset().top-20;
	$j('html,body').animate({scrollTop: targetOffset}, 1000);
 return $j(this);
}

/* +++++++++++++ */
/* + TARGETIZE + */
/* +++++++++++++ */
$j.fn.targetize = function () {
if ($j(this).is('.offer-item')) {
			$j('.targeted').removeClass('targeted');
			$j(this).fadeOut(200, function(){
			 $j(this).addClass('targeted').fadeIn(500);
			});
		};
}

/* +++++++++++++ */
/* + DO SLIDER + */
/* +++++++++++++ */
$j.fn.doSlider = function () {
		if ($j().cycle) {
		 if ($j('.slider').length) {
				$j('.slider img:first').load(function() {
					h = $j(this).height();
					$j('.slider').height(h);
     var prev, next = null;
					if ($j('.slider img').length>1) {
						prev = '#prev';
						next = '#next';
						$j('.slider').after('<a class="button" id="prev" href="#">prev</a><a class="button" id="next" href="#">next</a><div class="clearfix"></div>');
						$j('.button').css('height', h).css('margin-top', -h);
						$j('.slider').cycle({
							height:        h,      // container height
							prev:          prev,   // id of element to use as click trigger for previous slide
							next:          next,   // id of element to use as click trigger for next slide
							pause:         1,      // true to enable "pause on hover"
							timeout:       8000,   // milliseconds between slide transitions (0 to disable auto advance)
							speed:         1000
						});
					};
 			});
			};
		};
}

/* +++++++++++++++ */
/* + DO CAROUSEL + */
/* +++++++++++++++ */
$j.fn.doCarousel = function () {
		if ($j().jCarouselLite) {
		 if ($j('.carousel').length) {
//		  $j('.carousel img:eq(0)').load(function() {
 		  var w = ($j('.carousel').parent().width()-3)/3;
					$j('.carousel img').each(function(){
						$j(this).width(w).wrap('<li></li>');
					});
					$j('.carousel li').wrapAll('<ul></ul>');
					$j('.carousel').jCarouselLite({
						auto: 3000,
			 		speed: 1000
					});
//		  });
		 }
		}
}

/* ++++++++++++++ */
/* + DO PRELOAD + */
/* ++++++++++++++ */
$j.fn.doPreload = function () {
	 if ($j().imagemonitor) {
		 if ($j('img').length>1) {
		  $j('.slider img:gt(1)').hide();
 	  $j('img').imagemonitor({});
 	 };
	 };
}

/* +++++++++++++++++ */
/* + BROWSER CHECK + */
/* +++++++++++++++++ */
var $buoop = {reminder:0, l:'en', newwindow:true}
$buoop.ol = window.onload;
window.onload=function(){
 if ($buoop.ol) $buoop.ol();
 var e = document.createElement("script");
 e.setAttribute("type", "text/javascript");
 e.setAttribute("src", "http://browser-update.org/update.js");
 document.body.appendChild(e);
}

/* ++++++++++++++++ */
/* + DAYS BETWEEN + */
/* ++++++++++++++++ */
$j.fn.getNights = function () {
	function daysBetween(first, second) {
					// Copy date parts of the timestamps, discarding the time parts.
					var one = new Date(first.getFullYear(), first.getMonth(), first.getDate());
					var two = new Date(second.getFullYear(), second.getMonth(), second.getDate());
					// Do the math.
					var millisecondsPerDay = 1000 * 60 * 60 * 24;
					var millisBetween = two.getTime() - one.getTime();
					var days = millisBetween / millisecondsPerDay;
					// Round down.
				 var x = Math.floor(days);
					return x ? x : 0;
	}
	var upto = new Date($j('#e-upto').val());
	var from = new Date($j('#e-from').val());
	$j('#e-nights').text(daysBetween(from, upto));
}

