var Site = new Class({
	ldr: null,
	initialize: function()
	{
		this.ldr = new Loader();
		this.load_chat();
	},
	set_cabin: function(ship, cabin)
	{
		var ld = $('loading');
		ld.setStyle('display', 'block');
		$('name').set('text', 'Cabine ' + cabin.toUpperCase());
		var img = Asset.image('/images/ships/' + ship + '/cabines/t/' + cabin + '.jpg', {onload: function(){
			$('imagen').src = img.src;
			ld.setStyle('display', 'none');
			for (var i = 0; i < cabins_.length; i++)
			{
				if (cabins_[i][0] == cabin.toUpperCase())
				{
					$('tabela').set('text', cabin.toUpperCase()).setStyle('background-color', '#'+ cabins_[i][2]);
					$('desc').set('html', cabins_[i][1]);
					break;
				}
			}
			this.set_decks(ship, cabin);
		}.bind(this)});
	},
	set_decks: function(ship, cabin)
	{
		$$('a.deckprintdyna').destroy();
		var d = decks_[ship];
		var c = cabin.toLowerCase();
		for (var i = 0; i < d.cabs[c].length; i++)
		{
			var a = new Element('a', {'href': '#deck', 'class': 'deckprintdyna'});
			a.setStyles({'background': 'url(/images/ships/' + ship + '/blueprint_b.gif) 0 -' + d.coords[d.cabs[c][i]][0] +'px no-repeat', 'width': d.width, 'height': d.coords[d.cabs[c][i]][1]});
			$(document.body).grab(a);
			a.position({'relativeTo': $('conves'), 'edge': 'topLeft', 'position': 'topLeft', 'offset': {'x': 0, 'y': d.coords[d.cabs[c][i]][0]}});
			this.set_deck_event(a, ship, d.cabs[c][i]);
		}
	},
	set_deck_event: function(a, ship, deck)
	{
		a.addEvent('click', function(){
			var p = new PopUp();
			p.win.setStyles({'width': 900, 'height': 500});
			var swf = new Swiff('/swf/zoom.swf', {'width': 900, 'height': 500 - 25, 'container': p.dat, 'params': {'flashvars': 'image=/images/ships/' + ship + '/decks/' + deck + '.jpg'}});
			$(document.body).grab(p.win);
			p.win.position();
			/*var s = window.getSize();
			var div = new Element('div');
			var span = new Element('span');
			var a = new Element('a', {'href': '#close', 'text': 'x Fechar'});
			var swfdiv = new Element('div');
			//
			div.setStyles({'background-color': '#fff', 'width': s.x, 'height': s.y});
			span.setStyles({'background-color': '#002d9b', 'display': 'block', 'padding': '5px 10px', 'text-align': 'right'});
			a.setStyles({'color': '#fff', 'font-weight': 'bold', 'font-size': 14});
			//
			span.grab(a);
			div.grab(span);
			div.grab(swfdiv);
			//
			var swf = new Swiff('/swf/zoom.swf', {'width': s.x, 'height': s.y - 20, 'container': swfdiv, 'params': {'flashvars': 'image=/images/ships/' + ship + '/decks/' + deck + '.jpg'}});
			$(document.body).grab(div);
			div.position();
			//
			a.addEvent('click', function(){div.destroy();});*/
		});
	},
	zoom_cabin: function()
	{
		var src = $('imagen').src.replace('/t/', '/');
		this.zoom_image(src);
	},
	zoom_mapa: function()
	{
		var src = $('mapa').src.replace('/t_', '/');
		this.zoom_image(src);
	},
	zoom_casa: function(pos)
	{
		img = $('casa' + pos);
		src = img.get('src').replace('/s', '');
		this.zoom_image(src);
	},
	zoom_image: function(src)
	{
		var img = Asset.image(src, {onload: function(){
			var bg = new Element('div');
			var div = new Element('div');
			var p = new Element('p');
			var aa = new Element('a', {'href': '#close', 'text': 'fechar'});
			var ab = new Element('a', {'href': '#close'});
			var s = window.getScrollSize();
			//
			bg.setStyles({'background-color': '#000', 'opacity': .8, 'width': s.x, 'height': s.y, 'z-index': 9998});
			p.setStyles({'text-align': 'right', 'padding': '3px 0'});
			aa.setStyle('color', '#fff');
			img.setStyle('border', '1px #aaa solid');
			div.setStyle('z-index', 9999);
			//
			p.grab(aa);
			div.grab(p);
			ab.grab(img);
			div.grab(ab);
			$(document.body).grab(bg);
			$(document.body).grab(div);
			bg.position();
			div.position();
			//
			aa.addEvent('click', function(){
				div.destroy();
				bg.destroy();
			});
			ab.addEvent('click', function(){
				div.destroy();
				bg.destroy();
			});
		}});
	},
	show_img: function(a)
	{
		var ld = $('loading');
		ld.setStyle('display', 'block');
		//alert(a.childNodes[0].src);
		var img = Asset.image(a.childNodes[0].src.replace('/t', ''), {onload: function(){
			ld.setStyle('display', 'none');
			$('imagen').src = img.src;
		}});
	},
	init_packages_menu: function()
	{
		if (menu_pack_.length == 0) return;
		for (var i = 0; i < menu_pack_.length; i++) site.set_package_menu_events($('a_' + menu_pack_[i]), menu_pack_[i]);
	},
	set_package_menu_events: function(a, str)
	{
		a.addEvent('mouseover', function(){if ((active_a_) != str) this.setStyle('background', 'url(/images/btn_pac_' + str + '_on.gif) no-repeat');});
		a.addEvent('mouseout', function(){if ((active_a_) != str) this.setStyle('background', 'url(/images/btn_pac_' + str + '_off.gif) no-repeat');});
		a.addEvent('click', function(){
			if ((active_a_) == str) return;
			this.setStyles('background', 'url(/images/btn_pac_' + str + '_on.gif) no-repeat');
			$(str).setStyle('display', 'block');
			$('a_' + active_a_).setStyle('background', 'url(/images/btn_pac_' + active_a_ + '_off.gif) no-repeat');
			$(active_a_).setStyle('display', 'none');
			active_a_ = str;
			// resize map so to display correctly...
			if (str == 'mapa' && typeof(pack_map_resized) == 'undefined')
			{
				google.maps.event.trigger(site.pack_gmap.gmap, 'resize');
				site.pack_gmap.set_center();
				pack_map_resized = true;
			}
			// attach reserve form
			if (str == 'reserva' && typeof(pack_form_attached) == 'undefined')
			{
				$(str).grab($('reserva-html'));
				$('reserva-html').setStyle('display', 'block');
				pack_form_attached = true;
			}
		});
	},
	init_itinerary_map: function(canvas)
	{
		if (places_pack_.length == 0) return;
		//
		this.pack_gmap = new GMap();
		this.pack_gmap.load($(canvas));
		//
		var latlng = null;
		var arr = [];
		for (var i = 0; i < places_pack_.length; i++)
		{
			latlng = new google.maps.LatLng(places_pack_[i][1], places_pack_[i][2]);
			var ib = new InfoBox(latlng, i + 1, places_pack_[i][0]);
			ib.setMap(this.pack_gmap.gmap);
			arr.push(latlng);
		}
		arr.push(new google.maps.LatLng(places_pack_[0][1], places_pack_[0][2]));
		var c = Math.round(i / 2);
		//var pl = new google.maps.Polyline({'path': arr, 'strokeColor': '#000', 'strokeWeight': 2});
		//pl.setMap(this.pack_gmap.gmap);
		//
		this.pack_gmap.center = new google.maps.LatLng(places_pack_[c][1], places_pack_[c][2]);
		this.pack_gmap.set_center();
	},
	do_package_request: function(pname)
	{
		var fname = $('fname');
		var lname = $('lname');
		var email = $('email');
		var cemail = $('cemail');
		var country = $('country');
		var msg = $('msg');
		//
		if (fname.value == '' || lname.value == '' || email.value == '' || cemail.value == '' || country.value == 0 || msg.value == '')
		{
			alert('Por favor, preencha todos os campos');
			return;
		}
		if (email.value != cemail.value)
		{
			alert('Campos de endereço de e-mail não coincidem');
			return;
		}
		var str = 'pname=' + this.urlencode(pname) + '&fname=' + this.urlencode(fname.value) + '&lname=' + this.urlencode(lname.value) + '&email=' + this.urlencode(email.value) + '&cemail=' + this.urlencode(cemail.value) + '&country=' + country.value + '&msg=' + this.urlencode(msg.value);
		this.ldr.show();
		var json = new Request.JSON({'url': '/reserve/async-pacote', onSuccess: function(j, t){
			if (j.errno != 0)
			{
				this.ldr.say(j.error);
			}
			else
			{
				this.ldr.hide();
				$('reserva-html').setStyle('display', 'none');
				var div = new Element('div', {'class': 'success', 'html': '<p>Agradecidos! Sua solicitação foi enviada. Nos estudaremos atenciosamente seu pedido e responderemos dentro de 2 ou 4 horas</p>'});
				div.setStyles({'margin': '15px', 'width': 400});
				$('reserva').grab(div);
			}
		}.bind(this)}).send(str);
	},
	popup_hotel: function(a, id)
	{
		this.ldr.show();
		var json = new Request.JSON({'url': '/hoteis/get-popup', onSuccess: function(j, t){
			if (j.errno != 0)
			{
				this.ldr.say(j.error);
			}
			else
			{
				this.ldr.hide();
				var p = new PopUp();
				p.dat.grab(new Element('div', {'html': j.out.hotel.html}));
				p.show(a);
				/*$$('a.thumb').each(function(el, i){
					el.addEvent('click', function(){site.set_img(el);});
				});*/
			}
		}.bind(this)}).send('id=' + id);
	},
	load_chat: function()
	{
		$('livechat').position({'relativeTo': $(document.body), 'position': 'topRight', 'edge': 'topRight', 'offset': {'x': 0, 'y': 270}});
		$('livechat').setStyle('position', 'fixed');
	},
	urlencode: function(str)
	{
		return (str == '') ? '' : encodeURIComponent(str);
	},
	urldecode: function(str)
	{
		return (str == '') ? '' : decodeURIComponent(str);
	},
	guide_selector: function(e)
	{
		$(e).setStyle('color', '#ff6e11');
	},
	maps:function(country)
	{
		if (country == 'turquia')
		{
			var turquia = new google.maps.LatLng(39.53, 32.91);
			var myOptions = {zoom:6,center: turquia, mapTypeId:google.maps.MapTypeId.ROADMAP};
			var gmap = new google.maps.Map($('mapas'), myOptions);
		}
		else
		{
			var grecia = new google.maps.LatLng(38.07, 23.59);
			var myOptions = {zoom:6,center: grecia, mapTypeId:google.maps.MapTypeId.ROADMAP};
			var gmap = new google.maps.Map($('mapas'), myOptions);
		}
	}
});
var PopUp = new Class({
	win: null,
	dat: null,
	initialize: function()
	{
		this.win = new Element('div', {'class': 'pop-up'});
		var span = new Element('span', {'class': 'bar'});
		var a = new Element('a', {'href': '#close', 'text': 'Fechar', 'class': 'close'});
		this.dat = new Element('div', {'class': 'content'});
		span.grab(a);
		this.win.grab(span);
		this.win.grab(this.dat);
		a.addEvent('click', function(){this.close();}.bind(this));
	},
	show: function(el)
	{
		$(document.body).grab(this.win);
		this.win.position({'relativeTo': el, 'position': 'topMiddle', 'edge': 'bottomMiddle', 'offset': {'x': 0, 'y': -25}});
	},
	close: function()
	{
		var fx = new Fx.Tween(this.win, {'property': 'opacity', 'duration': 1000, 'onComplete': function(){
			this.win.destroy();
		}.bind(this)}).start(1, 0);
	}
});
var InfoBox = new Class({
	Extends: google.maps.OverlayView,
	div: null,
	latlng: null,
	pos: null,
	place: null,
	initialize: function(latlng, pos, place)
	{
		this.latlng = latlng;
		this.pos = pos;
		this.place = place;
	},
	onAdd: function()
	{
		this.div = new Element('div', {'class': 'ib'});
		this.div.grab(new Element('span', {'class': 'pos', 'text': this.pos}));
		this.div.grab(new Element('span', {'class': 'place', 'text': this.place}));
		var panes = this.getPanes();
		panes.floatPane.appendChild(this.div);
	},
	draw: function()
	{
		var proj = this.getProjection();
		if (proj == null) return;
		var pos = proj.fromLatLngToDivPixel(this.latlng);
		this.div.setStyles({'top': pos.y - 40, 'left': pos.x - 10});
	},
	onRemove: function()
	{
		this.div.destroy();
	}
});
var GMap = new Class({
	gmap: null,
	canvas: null,
	m: null,
	center: null,
	load: function(el,country)
	{
		this.canvas = el;
		this.init_gmap(country);
	},
	init_gmap: function(country)
	{
		if (country == 'turquia')
		{
			if (this.gmap == null) this.gmap = new google.maps.Map(this.canvas, {'zoom': 6, 'center': new google.maps.LatLng(39.53, 32.91), 'mapTypeId': google.maps.MapTypeId.ROADMAP, 'zoomControlOptions': {'style': google.maps.ZoomControlStyle.SMALL}});
		}
		else
		{
			if (this.gmap == null) this.gmap = new google.maps.Map(this.canvas, {'zoom': 6, 'center': new google.maps.LatLng(38.07, 23.59), 'mapTypeId': google.maps.MapTypeId.ROADMAP, 'zoomControlOptions': {'style': google.maps.ZoomControlStyle.SMALL}});
		}
	},
	add_place_marker: function(lat, lng, place)
	{
		var latlng = new google.maps.LatLng(lat, lng);
		if (this.m == null)
		{
			this.m = new google.maps.Marker({'map': this.gmap, 'position': latlng});
		}
		else
		{
			this.m.setPosition(latlng);
		}
		this.gmap.panTo(latlng);
	},
	set_center: function()
	{
		this.gmap.setCenter(this.center);
	},
	show_destinations:function(places)
	{
		var str = '';
		for (var i = 0; i < places.length; i++)
         {
		  str += '<a href="#p' + i + '" onclick="tmap.add_place_marker(' + places[i][0] + ', ' + places[i][1] + ',\'' + places[i][2] + '\');">' + places[i][2] + '</a>';
		  $('places').set('html', str);
		 }
	}
});
var Slideshow = new Class({
	Implements: Options,
	options: {
		id: 'slideshow',
		folder: 'slideshow',
		startimg: 1,
		endimg: 10,
		duration: 2000,
		fade_dur: 3000,
		width: 640,
		height: 312,
		el: null,
		img: null,
		currimg: null,
		imgs: [null, null],
		fx: null
	},
	initialize: function(options){
		this.setOptions(options);
		this.options.el = $(this.options.id);
		this.options.currimg = this.options.startimg;
	},
	start: function(){
		if (this.set_elements())
		{
			setTimeout(this.progress.bind(this), this.options.duration);
		}
	},
	progress: function()
	{
		if (this.options.currimg == this.options.endimg) this.options.currimg = this.options.startimg - 1;
		this.options.currimg++;
		var i = this.options.currimg;
		if (i < 10) i = '0' + i;
		//
		me = this;
		var img = new Asset.images(['/images/' + me.options.folder + '/' + i + '.jpg'], {onComplete: function(){
			//
			me.options.imgs[1].src = img[0].src;
			me.options.fx.start(1, 0).chain(function(){
				me.options.fx.set(1);
				me.options.imgs[0].src = me.options.imgs[1].src;
				me.progress();
			});
		}});
	},
	set_elements: function()
	{
		if (this.options.el == null) return this.error('Missing HTML container element!');
		// get first img el
		this.options.imgs[0] = this.options.el.getFirst();
		if (this.options.imgs[0] == null) return this.error('Missing container\'s image element');
		// create second image element
		this.options.imgs[1] = new Element('img', {'src': '', 'alt': '', 'width': this.options.width, 'height': this.options.height});
		this.options.el.grab(this.options.imgs[1]);
		this.set_styles();
		this.options.fx = new Fx.Tween(this.options.imgs[0], {property: 'opacity', duration: this.options.fade_dur});
		return true;
	},
	set_styles: function()
	{
		if (this.options.imgs[0] != null && this.options.imgs[1] != null)
		{
			this.options.imgs[0].setStyle('position', 'absolute');
			this.options.imgs[1].setStyle('position', 'absolute');
			this.options.imgs[0].setStyle('z-index', 99);
		}
	},
	error: function(msg)
	{
		alert(msg);
		return false;
	}
});
var Loader = new Class({
	el: null,
	p: null,
	a: null,
	initialize: function()
	{
	},
	create: function()
	{
		me = this;
		this.el = new Element('div', {'id': 'loader'});
		this.p = new Element('p');
		this.a = new Element('a', {'href': '#close', 'text': 'Close'});
		this.a.addEvent('click', function(){me.hide();});
		this.el.grab(this.p);
		this.el.grab(this.a);
		$(document.body).grab(this.el);
		this.el.position();
	},
	show: function()
	{
		if (this.el == null) this.create();
		this.say('Please wait...');
		this.el.setStyle('display', 'block');
	},
	hide: function()
	{
		this.el.setStyle('display', 'none');
	},
	say: function(str)
	{
		this.p.set('text', str);
	}
});
var AutoComplete = new Class({
	selected_id: null,
	initialize: function()
	{
		this.selected_id = 0;
	},
	attach: function(el, arr, msg, callback)
	{
		me = this;
		el.addEvent('focus', function(){
			if (this.value == msg)
			{
				this.value = '';
				this.setStyle('color', '#000');
			}
			else
			{
				this.select();
			}
		});
		el.addEvent('blur', function(){
			if (this.value == '')
			{
				this.value = msg;
				this.setStyle('color', '#999');
			}
		});
		el.value = msg;
		el.setStyle('color', '#999');
		var s = el.getDimensions();
		var div = new Element('div', {'id': 'autocomplete', 'class': 'autocomplete'});
		//
		el.addEvent('keyup', function(){
			if (el.value == '') return;
			if (div.getStyle('display') == 'none') div.setStyle('display', 'block');
			div.set('html', '');
			var txt = el.value.toLowerCase();
			var found = false;
			var rx = null;
			for (var i = 0; i < arr.length; i++)
			{
				if (arr[i][1].toLowerCase().indexOf(txt, 0) != -1)
				{
					var a = new Element('a', {'id': 'aci_'+ arr[i][0], 'href': '#' + arr[i][0], 'html': arr[i][1].replace(new RegExp('(' + txt + ')', 'ig'), '<span>$1</span>')});
					a.addEvent('click', function(){
						div.setStyle('display', 'none');
						el.value = this.get('html').replace(/<\/?span>/ig, '');
						me.selected_id = this.id.replace('aci_', '');
						callback();
					});
					div.grab(a);
					var found = true;
				}
			}
			if (!found) div.setStyle('display', 'none'); else {div.setStyle('display', 'block');div.position({'relativeTo': el, 'position': 'bottomLeft', 'edge': 'upperLeft', 'offset': {'x': -1, 'y': 1}});}
		});
		//
		div.setStyles({'width': s.x, 'display': 'none'});
		$('content').grab(div);
		div.position({'relativeTo': el, 'position': 'bottomLeft', 'edge': 'upperLeft', 'offset': {'x': 0, 'y': 1}});
	}
});
var ImageSlider = new Class({
	
	Implements: Options,
	current_pos: null,
	div: null,
	html: null,
	timer: null,
	initialize: function(options){
		this.setOptions(options);
		this.current_pos = 0;
		this.html = {};
		this.setup();
	},
	setup: function()
	{
		// get main element
		if ((this.div = $(this.options.div)) == null) return;
		this.div.setStyles({'width': this.options.width, 'height': this.options.height});
		// check for items
		if (this.options.items.length <= 0) return;
		//make pager
	    this.create_pager();
		// make item html
		this.create_html();
		// add first
		this.move();
		// start timer
		this.start();
	},
	start: function()
	{
		var duration = this.options.duration;
		this.timer = this.move.periodical(duration, this);
	},
	move: function()
	{
		if (this.current_pos >= this.options.items.length) this.current_pos = 0;
		var image = new Asset.image(this.options.items[this.current_pos].img, {onload: function(){
			this.html.img.set('src', image.src);
			this.html.a.set('href', this.options.items[this.current_pos].url);
			this.html.span.set('html', '<span class="price">Á partir de&nbsp;' + this.options.items[this.current_pos].price + '&euro;</span><span class="name">' + this.options.items[this.current_pos].name + '</span>');
			if(this.current_pos != 0){
				this.html.span.setStyles({'display':'block'});
				$('databg').setStyles({'display':'block'});
			}
			else{
				$('databg').setStyles({'display':'none'});
				this.html.span.setStyles({'display':'none'});
			}

			this.current_pos++;	
		}.bind(this)});
	},
	create_pager: function()
	{
		var p = new Element('div', {'id': 'pager', 'visibility': 'visible', 'opacity': 0.8});
		var a = null;
		for (var i = 0; i < this.options.items.length; i++)
		{
			a = new Element('a', {'href': '#is-' + (i + 1), 'html': i + 1});
			this.add_pager_events(a, i);
			p.grab(a);
		}
		this.div.grab(p);
		p.position({'relativeTo': this.div, 'position': 'centerBottom', 'edge': 'centerTop', 'offset': {'x': 0, 'y': -20}});
		
	},
	add_pager_events: function(a, i)
	{
		a.addEvent('mouseover', function(){
			this.current_pos = i;
			this.move();
			if (this.timer != null)
			{
				clearInterval(this.timer);
				this.timer = null;
			}
		}.bind(this));
		a.addEvent('mouseout', function(){
			if (this.timer == null) this.start();
		}.bind(this));
	},
	create_html: function()
	{
		
		this.html.div = new Element('div');
		this.html.a = new Element('a');
		this.html.img = new Element('img');
		this.html.a.grab(this.html.img);
		this.html.div.grab(this.html.a);
		this.div.grab(this.html.div);
		
		this.html.div = new Element('div', {'id': 'databg', 'visibility': 'visible', 'opacity': 0.6});
		this.div.grab(this.html.div);
		
		this.html.div = new Element('div', {'id': 'data'});
		this.html.span = new Element('span', {'class': 'name'});
		this.html.div.grab(this.html.span);
		this.div.grab(this.html.div);
	
	}
});

var site = null;
window.addEvent('domready', function(){site = new Site();_load();});
