// (c) z3n - R1V1@110624 - www.overflow.biz - rodrigo.orph@gmail.com

var _dbg = function(x) { if (window.console) console.log(x); },
		lib = "/lib/",
		
		keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
		
		is_array = function(mixed_var) {
				// http://kevin.vanzonneveld.net
				// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
				// +   improved by: Legaev Andrey
				// +   bugfixed by: Cord
				// +   bugfixed by: Manish
				// +   improved by: Onno Marsman
				// +   improved by: Brett Zamir (http://brett-zamir.me)
				// +   bugfixed by: Brett Zamir (http://brett-zamir.me)
				// %        note 1: In php.js, javascript objects are like php associative arrays, thus JavaScript objects will also
				// %        note 1: return true  in this function (except for objects which inherit properties, being thus used as objects),
				// %        note 1: unless you do ini_set('phpjs.objectsAsArrays', true), in which case only genuine JavaScript arrays
				// %        note 1: will return true
				// *     example 1: is_array(['Kevin', 'van', 'Zonneveld']);
				// *     returns 1: true
				// *     example 2: is_array('Kevin van Zonneveld');
				// *     returns 2: false
				// *     example 3: is_array({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});
				// *     returns 3: true
				// *     example 4: is_array(function tmp_a(){this.name = 'Kevin'});
				// *     returns 4: false
				
				var key = '';
				var getFuncName = function (fn) {
						var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
						if (!name) {
								return '(Anonymous)';
						}
						return name[1];
				};
				
				if (!mixed_var) {
						return false;
				}
				
				// BEGIN REDUNDANT
				this.php_js = this.php_js || {};
				this.php_js.ini = this.php_js.ini || {};
				// END REDUNDANT
				
				if (typeof mixed_var === 'object') {
						
						if (this.php_js.ini['phpjs.objectsAsArrays'] &&  // Strict checking for being a JavaScript array (only check this way if call ini_set('phpjs.objectsAsArrays', 0) to disallow objects as arrays)
								(
								(this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase &&
												this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase() === 'off') ||
										parseInt(this.php_js.ini['phpjs.objectsAsArrays'].local_value, 10) === 0)
								) {
								return mixed_var.hasOwnProperty('length') && // Not non-enumerable because of being on parent class
																!mixed_var.propertyIsEnumerable('length') && // Since is own property, if not enumerable, it must be a built-in function
																		getFuncName(mixed_var.constructor) !== 'String'; // exclude String()
						}
						
						if (mixed_var.hasOwnProperty) {
								for (key in mixed_var) {
										// Checks whether the object has the specified property
										// if not, we figure it's not an object in the sense of a php-associative-array.
										if (false === mixed_var.hasOwnProperty(key)) {
												return false;
										}
								}
						}
						// Read discussion at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_is_array/
						return true;
				}
				return false;
		},
		
		_enc = function(input) { // base 64 encode -- v1.02
			var output = "";
			var chr1, chr2, chr3;
			var enc1, enc2, enc3, enc4;
			var i = 0;
			if ((typeof input != 'undefined') && (input != 'undefined') && (input != 'null') && (input != null) && (input != '')) {
				if (is_array(input)) {
					output=[];
					for (i in input) {
						output[i]=_enc(input[i]);
					}
				} else {
					do {
						chr1 = input.charCodeAt(i++);
						chr2 = input.charCodeAt(i++);
						chr3 = input.charCodeAt(i++);
						
						enc1 = chr1 >> 2;
						enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
						enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
						enc4 = chr3 & 63;
						
						if (isNaN(chr2)) {
							 enc3 = enc4 = 64;
						} else if (isNaN(chr3)) {
							 enc4 = 64;
						}
						
						output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
							 keyStr.charAt(enc3) + keyStr.charAt(enc4);
					} while (i < input.length);
				}
			}
			return output;
		},
		
		_dec = function(input) { // base 64 decode -- v1.02
			var output = "";
			var chr1, chr2, chr3;
			var enc1, enc2, enc3, enc4;
			var i = 0;
			if ((typeof input != 'undefined') && (input != 'undefined') && (input != 'null') && (input != null) && (input != '')) {
				if (is_array(input)) {
					output=[];
					for (i in input) {
						output[i]=_dec(input[i]);
					}
				} else {
					input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
					do {
						enc1 = keyStr.indexOf(input.charAt(i++));
						enc2 = keyStr.indexOf(input.charAt(i++));
						enc3 = keyStr.indexOf(input.charAt(i++));
						enc4 = keyStr.indexOf(input.charAt(i++));
						
						chr1 = (enc1 << 2) | (enc2 >> 4);
						chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
						chr3 = ((enc3 & 3) << 6) | enc4;
						
						output = output + String.fromCharCode(chr1);
						
						if (enc3 != 64) {
							output = output + String.fromCharCode(chr2);
						}
						if (enc4 != 64) {
							output = output + String.fromCharCode(chr3);
						}
					} while (i < input.length);
				}
			}
			return output;
		},
		
		paginator = {
			change: function(id, i) {
				$("[id=" + id + "]").each(function(){
					$(this).hide();
				});
				$("[id=" + id + "]:eq(" + i + ")").show();
				this.clearSel(id);
				$("#" + id + "_pg a:eq(" + i + ")").attr("id","sel");
				return false;
			},
			
			scroll: function(id, i, stepping, el, skip) {
				if (typeof stepping == 'undefined')
					stepping = 1;
				if (typeof el == 'undefined')
					el = "div";
				if (typeof skip == 'undefined')
					skip = 0;
				
				this.clearSel(id);
				
				$("#" + id + "_pg a:eq(" + (skip + Math.round(i / stepping)) + ")").attr("id","sel");
				$("#" + id).scrollTo(
					$("#" + id).find(el + ":eq(" + (i == 0 ? i : (skip + i)) + ")").length > 0 ?
							el + ":eq(" + (i == 0 ? i : (skip + i)) + ")"
						:
							el + ":last",
					1000
				);
				
				return false;
			},
			
			prev: function(id, stepping, el) {
				if (typeof stepping == 'undefined')
					stepping = 1;
				if (typeof el == 'undefined')
					el = "div";
				
				var i = parseInt(parseInt(parseInt($("#" + id + "_pg").find("a#sel").index()) * stepping) - (2 * stepping));
				if (i < 0)
					i = parseInt(parseInt($("#" + id + "_pg").find("a:last").index() - 2) * stepping);
				
				return this.scroll(id, i, stepping, el, 1);
			},
			
			next: function(id, stepping, el, compensation, skip) {
				if (typeof stepping == 'undefined')
					stepping = 1;
				if (typeof el == 'undefined')
					el = "div";
				if (typeof compensation == 'undefined')
					compensation = 2;
				if (typeof skip == 'undefined')
					skip = 1;
				
				var i = parseInt(parseInt($("#" + id + "_pg").find("a#sel").index()) * stepping);
				if (i > parseInt(parseInt($("#" + id + "_pg").find("a:last").index() - compensation) * stepping))
					i = 0;
				
				return this.scroll(id, i, stepping, el, skip);
			},
			
			clearSel: function(id) {
				$("#" + id + "_pg").find("a").each(function(){
					$(this).removeAttr("id");
				});
			}
		},
		
		hotelaria = {
			save: function(type) {
				if ($("#hotelaria_sol_" + type + "_form").valid()) {
					$("#hotelaria_sol_" + type + "_bt").hide();
					$("#hotelaria_sol_" + type + "_ld").show();
					
					// send ajax
					var data = {
						"hotel": $("#hotelaria_sol_" + type + "_form").find("[name=nome_hotel]").val(),
						"cidade": $("#hotelaria_sol_" + type + "_form").find("[name=cidade]").val(),
						"email": $("#hotelaria_sol_" + type + "_form").find("[name=email]").val(),
						"compl": $("#hotelaria_sol_" + type + "_form").find("[name=compl]").val()
					};
					
					if (type == "int")
						data.pais = $("#hotelaria_sol_" + type + "_form").find("[name=pais]").val();
					else
						data.estado = $("#hotelaria_sol_" + type + "_form").find("[name=estado]").val();
					
					$.ajax({
						url:lib + 'gtf_hotelaria_sol.php',
						"data":"d=" + _enc($.toJSON(data)),
						success: function(r, s) {
							$("#hotelaria_sol_" + type + "_ld").hide();
							$("#hotelaria_sol_" + type + "_done").show();
						}
					});
				}
				
				return false;
			}
		},
		
		transfers = {
			save: function() {
				if ($("#transfers_sol_form").valid()) {
					$("#transfers_sol_bt").hide();
					$("#transfers_sol_ld").show();
					
					// send ajax
					$.ajax({
						url:lib + 'gtf_transfers_sol.php',
						"data":"d=" + _enc($.toJSON({
										"pessoas": $("#transfers_sol_form").find("[name=pessoas]").val(),
										"data": $("#transfers_sol_form").find("[name=data]").val(),
										"email": $("#transfers_sol_form").find("[name=email]").val(),
										"compl": $("#transfers_sol_form").find("[name=compl]").val()
									})),
						success: function(r, s) {
							$("#transfers_sol_ld").hide();
							$("#transfers_sol_done").show();
						}
					});
				}
				
				return false;
			},
			
			browse: function(id) {
				$("#destinos").find("a").each(function(){ $(this).removeAttr("id"); });
				$("#transfers").find(".item").each(function(){ $(this).hide(0); });
				
				$(".destino_" + id).attr("id", "sel");
				$(".transfer_" + id).show(0);
				
				return false;
			}
		},
		
		eventos = {
			ev: 0,
			type: null,
			show: function(ev, type) {
				if (typeof type == 'undefined')
					type = "eventos";
				
				$('#eventos_sol').overlay().load();
				
				this.ev = ev;
				this.type = type;
			},
			save: function() {
				if ($("#eventos_sol_form").valid()) {
					var $this = this;
					
					$("#eventos_sol_bt").hide();
					$("#eventos_sol_ld").show();
					
					// send ajax
					$.ajax({
						url:lib + 'gtf_eventos_sol.php',
						"data":"d=" + _enc($.toJSON({
										"email": $("#eventos_sol_form").find("[name=email]").val(),
										"compl": $("#eventos_sol_form").find("[name=compl]").val(),
										"ev": $this.ev,
										"type": $this.type
									})),
						success: function(r, s) {
							$("#eventos_sol_ld").hide();
							$("#eventos_sol_done").show();
						}
					});
				}
				
				return false;
			}
		},
		
		newsletter = {
			expose: function() {
				$.scrollTo("#box_newsletter", 500);
				$("#box_newsletter").expose({color:'#3aaedd'});
				
				return false;
			},
			
			save: function() {
				if ($("#box_newsletter_form").valid()) {
					var $this = this;
					
					$("#box_newsletter_bt").hide();
					$("#box_newsletter_ld").show();
					
					// send ajax
					$.ajax({
						url:lib + 'gtf_newsletter.php',
						"data":"d=" + _enc($.toJSON({
										"email": $("#box_newsletter_form").find("[name=email]").val()
									})),
						success: function(r, s) {
							$("#box_newsletter_ld").hide();
							$("#box_newsletter_done").show();
						}
					});
				}
				
				return false;
			}
		},
		
		contato = {
			save: function() {
				if ($("#contato_form").valid()) {
					var $this = this;
					
					$("#contato_bt").hide();
					$("#contato_ld").show();
					
					// send ajax
					$.ajax({
						url:lib + 'gtf_contato.php',
						"data":"d=" + _enc($.toJSON({
										"nome": $("#contato_form").find("[name=nome]").val(),
										"telefone": $("#contato_form").find("[name=telefone]").val(),
										"email": $("#contato_form").find("[name=email]").val(),
										"msg": $("#contato_form").find("[name=msg]").val()
									})),
						success: function(r, s) {
							$("#contato_ld").hide();
							$("#contato_done").show();
						}
					});
				}
				
				return false;
			}
		},
		
		banner = {
			st: null,
			
			start: function(){
				this.stop();
				this.st = setInterval(function(){
					var idx = parseInt($("#links").find("a#sel").index());
					if (idx > 0)
						idx /= 2;
					
					idx++;
					
					if ($("#links").find("a:eq(" + idx + ")").length == 0)
						idx = 0;
					
					$("#links").find("a:eq(" + idx + ")").click();
					
					paginator.scroll("links", idx > 2 ? (Math.floor(idx / 3) * 9) + 1 : 0, 9, undefined, 1);
				}, 6000);
			},
			
			stop: function(){
				clearInterval(this.st);
			}
		},
		
		parceiros = {
			st: null,
			
			start: function(){
				this.stop();
				this.st = setInterval(function(){
					var idx = $("#parceiros_pg").find("a#sel").index();
					if ($("#parceiros_pg").find("a:eq(" + (idx + 1) + ")").length > 0)
						idx++;
					else
						idx = 0;
					
					$("#parceiros_pg").find("a:eq(" + idx + ")").click();
				}, 4000);
			},
			
			stop: function(){
				clearInterval(this.st);
			}
		};

$(document).ready(function(){
	if ($("#parceiros").length > 0) {
		var pi = 0;
		$("#parceiros").find("div").each(function(){ pi++; });
		
		for (var i = 0;i < pi;i++)
			$("#parceiros_pg").append("<a href='#' onClick='return paginator.scroll(\"parceiros\"," + i + ");' class='p bg6'></a>");
		
		paginator.scroll("parceiros",0);
		$("#parceiros").hover(function(){parceiros.stop();},function(){parceiros.start();});
		parceiros.start();
	}
	
	if ($("#noticias").length > 0) {
		var pi = 0;
		$("#noticias").find("div").each(function(){
			pi++;
		});
		
		pi = Math.ceil(pi / 4);
		
		if (pi > 0)
			$("#noticias_pg").append("<a href='#' onClick='return paginator.prev(\"noticias\",4);' class='pg_prev'><div>&nbsp;</div>ANTERIOR</a>");
		
		for (var i = 0;i < pi;i++)
			$("#noticias_pg").append("<a href='#' onClick='return paginator.scroll(\"noticias\"," + (i * 4) + ",4,undefined,1);' class='p bg5'></a>");
		
		if (pi > 0)
			$("#noticias_pg").append("<a href='#' onClick='return paginator.next(\"noticias\",4);' class='pg_next'><div>&nbsp;</div>PRÓXIMA</a>");
		
		paginator.scroll("noticias",0,4,undefined,1);
	}
	
	if ($("#eventos").length > 0) {
		var pi = 0;
		$("[id=eventos]").each(function(){
			$(this).find("a").each(function(){
				pi++;
			});
		});
		
		pi = Math.ceil(pi / 6);
		
		for (var i = 0;i < pi;i++)
			$("#eventos_pg").append("<a href='#' onClick='return paginator.change(\"eventos\"," + i + ");' class='p bg26'></a>");
		
		paginator.scroll("eventos",0);
	}
	
	if ($("#links").length > 0) {
		var pi = 0;
		$("#links").find("div").each(function(){
			pi++;
		});
		
		pi = Math.ceil(pi / 9);
		if (pi > 0)
			$("#links_pg").append("<a href='#' onClick='return paginator.prev(\"links\",9);' class='pg_prev'><div>&nbsp;</div>ANTERIOR</a>");
		
		for (var i = 0;i < pi;i++)
			$("#links_pg").append("<a href='#' onClick='return paginator.scroll(\"links\"," + ((i * 9) + 1) + ",9,undefined,1);' class='p bg14'></a>");
		
		if (pi > 0)
			$("#links_pg").append("<a href='#' onClick='return paginator.next(\"links\",9);' class='pg_next'><div>&nbsp;</div>PRÓXIMA</a>");
		
		$("#links").find("a").each(function(){
			$(this).click(function(){
				$("#banner_cont").scrollTo("div:eq(" + $("#links a").index(this) + ")", 250);
				$("#links").find("a").each(function(){ $(this).removeAttr("id"); });
				$(this).attr("id","sel");
				return false;
			});
		});
		
		paginator.scroll("links",0,9,undefined,1);
	
		$("#banner_big_cont").hover(function(){banner.stop();},function(){banner.start();});
		banner.start();
	}
	
	// load overlays
	$("#hotelaria_sol_br, #hotelaria_sol_int, #transfers_sol, #eventos_sol").overlay({
		'mask': {
			color: '#3aaedd',
			loadSpeed: 200,
			opacity: 0.9
		},
		closeOnClick: true
	});
	
	// form validators
	if ($("#hotelaria_sol_br").length > 0) {
		$("#hotelaria_sol_br_form").validate({
			rules:{
				nome_hotel: "required",
				cidade: "required",
				estado: "required",
				email: {
					email: true,
					required: true
				}
			},
			messages:{
				nome_hotel:"",
				cidade:"",
				estado:"",
				email:""
			},
			errorPlacement:function(error,el) { error.css({display:none}); }
		});
		
		$("#hotelaria_sol_int_form").validate({
			rules:{
				nome_hotel: "required",
				cidade: "required",
				pais: "required",
				email: {
					email: true,
					required: true
				}
			},
			
			messages:{
				nome_hotel:"",
				cidade:"",
				pais:"",
				email:""
			},
			errorPlacement:function(error,el) { error.css({display:none}); }
		});
	}
	
	if ($("#transfers_sol").length > 0)
		$("#transfers_sol_form").validate({
			rules:{
				pessoas: "required",
				"data": "required",
				email: {
					email: true,
					required: true
				}
			},
			messages:{
				pessoas:"",
				"data":"",
				email:""
			},
			errorPlacement:function(error,el) { error.css({display:none}); }
		});
	
	if ($("#eventos_sol").length > 0)
		$("#eventos_sol_form").validate({
			rules:{
				email: {
					email: true,
					required: true
				}
			},
			messages:{
				email:""
			},
			errorPlacement:function(error,el) { error.css({display:none}); }
		});
	
	if ($("#contato_form").length > 0)
		$("#contato_form").validate({
			rules:{
				nome: "required",
				telefone: "required",
				msg: "required",
				email: {
					email: true,
					required: true
				}
			},
			messages:{
				nome:"",
				telefone:"",
				msg:"",
				email:""
			},
			errorPlacement:function(error,el) { error.css({display:none}); }
		});
	
	$("#box_newsletter_form").validate({
		rules:{
			email: {
				email: true,
				required: true
			}
		},
		messages:{
			email:""
		},
		errorPlacement:function(error,el) { error.css({display:none}); }
	});
	
	$.ajaxSetup({
		global:true,
		type:"POST",
		dataType:"json",
		cache:false,
		async:true,
		timeout:12000000,
		scriptCharset:"UTF-8",
		error:function(obj,txt,err){ _dbg("ajax error:" + txt + " thrown error:" + err); }
	});
});
