
$(document).ready(function(){
	loader = new ajax(lang);
	imagegallery = new gallery();
	reserverequest = new request();
	recommendto = new recommend();
	$('.stripbox .strip').serialScroll({
		items:'div.boxm',
		prev:'.stripbox .controls a.prev',
		next:'.stripbox .controls a.next',
		offset:0,
		start:0,
		duration:1200,
		force:true,
		stop:true,
		lock:false,
		cycle:false,
		easing:'easeOutQuart',
		jump: true,
		exclude: 1,
		onAfter:function(elem){ $('npanel').value = $(".scroller .boxm", $(this)).index(elem); }
	});	
	reservecalendar = new reserveCalendar(localtime);		
	$('a.xgallery').each(function(i){ $(this).click(function(){ imagegallery.open(i); }) })
	$('a.xrequest').click(function(){ reserverequest.open(); });
	$('a.xrecommend').click(function(){ recommendto.open(); });
	
	var coords = $('latt', 'long', 'pname');
	if(typeof(GMap2) != "undefined" && GBrowserIsCompatible() && coords[0] && coords[1]){
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		if(coords[0].value == '0' || coords[1].value == '0'){
			var point = new GLatLng('39.974489161874125', '4.0779876708984375');
			var marker = new GMarker(point);	
			map.setCenter(point, 10);			
		}else{
			var point = new GLatLng(coords[0].value, coords[1].value);
			var marker = new GMarker(point);	
			map.setCenter(point, 14);			
			map.addOverlay(marker);	
			GEvent.addListener(marker, 'click', function() {
				marker.openInfoWindowHtml(coords[2].value);		
			});			
		}
  	}
  	
  	// comments
  	getComments($('#uacomment').val(), 1);
 	$('div#textlimit textarea.comment').keyup(function(){ limitChars('div#textlimit', 250); });  	
	$('a#showform').click(function(){ 
		$(this).hide();  
		$('div#sendcomment').slideDown(600, function(){ 
			$('a#hideform').show(); 
		}); 
	});
	$('a#hideform').click(function(){ 
		$(this).hide(); 
		$('div#sendcomment').slideUp(600, function(){ 
				$('a#showform').show(); 
			}); 
		});
	$('div#sendcomment div#failed a#tryagain').click(function(){ 
		$('div#sendcomment div#failed').css('display', 'none'); 
		$('#addcomment').css('display', 'block'); 
	});
	$('button#submitcomment').click(function(){
		$name = $('#name');
		$comment = $('#comment');
		if($name.val().length < 1){ 
			$name.focus();
			return false;
		}
		if($comment.val().length < 1){
			$comment.focus();
			return false;
		}
		if($('#accept:checked').val() == null){
			$('div.lopd').css('border', '1px solid #DC0000');
			setTimeout("$('div.lopd').css('border', '1px solid #ffffff')", 500);
			setTimeout("$('div.lopd').css('border', '1px solid #DC0000')", 1000);
			setTimeout("$('div.lopd').css('border', '1px solid #ffffff')", 1500);
			setTimeout("$('div.lopd').css('border', '1px solid #DC0000')", 2000);
			setTimeout("$('div.lopd').css('border', '1px solid #ffffff')", 2500);
			setTimeout("$('#accept').focus()", 2501);
			return false;
		}
		$.ajax({
			url: '/ajxdata/sendcomment.php?pid=' + $('#uacomment').val() + '&lang=' + $('#hlang').val(), type: 'POST', dataType: 'text', cache: false, contentType: 'application/x-www-form-urlencoded', processData: true, 
			data: '&pid=' + $('#uacomment').val() + '&name=' + $('#name').val() + '&email=' + $('#email').val() + '&comment=' + $('#comment').val() + '&accept=' + $('#accept:checked').val(), 
			dataFilter: function(data){ return parseInt(data); },
			beforeSend: function(){ 
				$('div#sendcomment').attr('class', 'showprogress'); 
				$('form#addcomment').css('visibility', 'hiden'); 
			},
			success: function(data){ 
				$('div#sendcomment').attr('class', 'hideprogress');
				if(typeof(data) == "undefined" || data != 1){
					$('#addcomment').css('display', 'none').css('visibility', 'visible');
					$('div#sendcomment div#failed').css('display', 'block');
				}else{
					$('#addcomment').css('display', 'none').css('visibility', 'visible');
					$('div#sendcomment div#success').css('display', 'block');
				}
			}
		});
	});
});

function getComments(pid, pag) {
	$.ajax({
		url: '/ajxdata/comments.php?pid=' + pid + '&pag=' + pag, 
		type: 'GET', 
		dataType: 'html', 
		cache: false, 
		processData: false,
		success: function(data){ $('#comments-list').html(data); }
	});	
}

function limitChars(textid, limit, color){
	var textcont = $(textid);
	var textarea = textcont.find('textarea');
	var text = textarea.val();
	var textlength = text.length;
	if(textlength > limit){
		textarea.val(text.substr(0, limit));
		return false;
	}
	textcont.find('span.chars').html((limit - textlength) + ' / ' + limit);
	return true;
}
