/*
 * Mikhail Kozlov
 * http://www.315design.com/
 * 
*/
	var C_YEAR = new Date();
	C_YEAR.setFullYear(2010,01,01);
	var lastTip = '';
$(document).ready(function(){
	if($.browser.msie && $.browser.version == '6.0') {
//		window.location.href="/siteinfo/ie6/";
	}
	$(".ToolTipAjax").click(function(){
		return false;
	});
	$('.ToolTipAjax').hover(function(e) {
		$caller = $(this);
		$caller.css("cursor","progress");
		if (lastTip != $caller.attr('ref')) {
			lastTip = $caller.attr('ref');
			$("#info").load(this.href, {}, function(){
				$(this).fadeIn(200);
				$caller.css("cursor", "pointer");
			});
		}else{
			$("#info").fadeIn(200);
			$caller.css("cursor", "pointer");
		}
		$('#info').css('top', e.pageY + +20).css('left', e.pageX -250);
			
	}, function() {
		$('#info').hide();
	});
	$('.ToolTipAjax').mousemove(function(e) {
		$('#info').css('top', e.pageY + +20).css('left', e.pageX -250);
	});
	$('#info').live("click",function(){
		$(this).hide();
	});


	function closeSplash(){
		$('#splash').dialog('close');
	}
	
	$("input, select, textarea").live("click",function(){
		if($(this).parent().hasClass("inputRed")){
			$(this).parent().removeClass("inputRed");
		}
	});
	$("input, select, textarea").focus(function(){
		if($(this).parent().hasClass("inputRed")){
			$(this).parent().removeClass("inputRed");
		}
	});

	$("#control li a").live("click",function(){
		$("#control li").removeClass("selected");
		$(this).parent().addClass("selected");
		$('#tab-content > div').hide();
		$($(this).attr('href'),'#tab-content').show();
		return false;
	});
	$("#control li").each(function(i){
		if($(this).hasClass('selected')){
			$($('a:first',this).attr('href'),'#tab-content').show();
		}
	});
	// featured slider
	$("#scroll_control a").live("click",function(){
		if($(this).hasClass("next")){
			var left = (parseInt($("#scroll_box div.f_boxes").css("left"))-400);
			$("#scroll_box div.f_boxes").animate({ 
				left:left+"px"
			},200);

			if(-left == (parseInt($("#scroll_box div.f_boxes").css("width"))-400)){
				$(this).hide();				
			}else{
				$("#scroll_control a.prev").show();
			}
		}else{
			var left = (parseInt($("#scroll_box div.f_boxes").css("left"))+400);
			$("#scroll_box div.f_boxes").animate({ 
				left:left+"px"
			},200);
			if(left < 0){
				$("#scroll_control a.next").show();
			}else{
				$("#scroll_control a.prev").hide();
			}
		}
		return false;
	});
	
	$("#maillist_join").live("click",function(){
		$res = $(this);
		$(this).val("Loading...").attr("disabled","disabled");
		var data = $("#maillist").serialize();
		$.getJSON("/_postup.php?Show=Subscribe&ToDo=Visitor",data,function(r){
			if(parseInt(r.res) == 1){
				$("#maillist input:text").val('');
				$res.prev().text(r.msg);
			}else{
				$res.prev().text(r.msg);
			}
			$res.val("Join").removeAttr("disabled");
		});
	});
	$("#defaultInlineDatepicker").datepick({
		onSelect: function(value, date) { 
			if(date.getMonth() < 10){
				month = "0"+(date.getMonth()+1);
			}else{
				month = (date.getMonth()+1);
			}
			if(date.getDate() < 10){
				day = "0"+date.getDate();
			}else{
				day = date.getDate();
			}

			location.href = "/events/"+date.getFullYear()+"/"+month+"/"+day+"/";
		},minDate:"01/01/2009", maxDate:"+1Y"
	});

	$("#smallSearch").submit(function(){
		var path =location.pathname; 
		$form = $(this);
		if($(this).find("#arrival").val().length < 10){
			alert('Please select check in date!\n\nNote: Correct date format is mm/dd/yyyy.\n');
			return false;
		}else{
			$("#splash").prepend($("#ad_holder").find("iframe").css({
				top: "50px",
				left: "50px"
			})).dialog({
				title:"We are looking for availability...",
				closeOnEscape:false,
				draggable:false,
				modal:true,
				resizable:false,
				width:600,
				height:300,
				close:function(){
					return false;
				}
			}).dialog('open');
			$(".ui-dialog-titlebar-close").css("display","none");
			$.post("/rentals/search/_postup.php",$form.serializeArray(), function(){
				window.location.href ="/rentals/search/";
			});
			return false;
		}
	});
	
	$("#arrival","#smallSearch").datepick({minDate: new Date(),maxDate:new Date(C_YEAR.getFullYear(),12 - 1, 31) ,beforeShowDay: nationalDays,onSelect: setDept,
    showOn: 'both', buttonImageOnly: true, buttonImage: '/images/calendar/calendar.gif'}); 

	
	function setDept(value, date){
		var day = new Date();
		day.setMonth(date.getMonth());
		day.setDate(date.getDate()+parseInt($("option:selected","#duration").val()));
		$("#departure","#smallSearch").text(day.getMonth()+1+'/'+day.getDate()+'/'+day.getFullYear()); 

	}
	function nationalDays(date) {
		var week = parseInt($("option:selected","#duration").val());
		var type = $("option:selected","#duration").attr('rel');
		switch(week){
			default:
				switch(type){
					default:
						var natDays = [2,3,4];
					break;
					case 'sun':
						var natDays = [1,2,3,4,5,6];
					break;	
					case 'sat':
						var natDays = [0,1,2,3,4,5];
					break;
					case 'min':
						var natDays = [0,2,3,4,6];
					break;	
					case 'full':
						var natDays = [1,2,3,4,5];
					break;	

				}
			break;
			case 10:
			case 3:
				var natDays = [0,1,2,3,4,6];
			break;
			case 4:
			case 11:
				var natDays = [0,2,3,4,5,6];
			break;
			case 14:
				var natDays = [0,1,2,3,4];
			break;
	
		}
	    for (i = 0; i < natDays.length; i++) {   
	        if (date.getDay() == natDays[i]) {   
	            return [false, 'hide_day'];   
	        }   
	    }   
	    return [true, ''];   
	}
	$("#duration","#smallSearch").change(function(){
		$("#arrival, #departure","#smallSearch").val('');
//		alert($("option:selected","#duration").val());
//		$("#arrival","#smallSearch").load("/_postup.php",{"ToDo":$(this).find("option:selected").val(),"Show":"Dates"});
	});
	$("#duration",".searchForm").change(function(){
//		$("#arrival, #departure","#smallSearch").val('');
//		alert($("option:selected","#duration").val());
		$("#arrival",".searchForm").load("/_postup.php",{"ToDo":$(this).find("option:selected").val(),"Show":"Dates"});
	});
	if (!$.browser.msie) {
		$("#anim").animate({
			left: 910,
			opacity: 1
		}, 5000, "linear", function(){
			$(this).animate({
				left: 100
			}, 50000, "linear", function(){
				$(this).animate({
					top: 255,
					left: -50,
					opacity: 0
				}, 10000);
			});
		});
	}
});

	function roundNumber(number,decimal_points) {
		if(!decimal_points) return Math.round(number);
		if(number == 0) {
			var decimals = "";
			for(var i=0;i<decimal_points;i++) decimals += "0";
			return "0."+decimals;
		}
	
		var exponent = Math.pow(10,decimal_points);
		var num = Math.round((number * exponent)).toString();
		return num.slice(0,-1*decimal_points) + "." + num.slice(-1*decimal_points)
	}
	function onSubmit(source,req) {
		var flag = true;
		$("input:text, select, textarea").each(function (i) {
			for (x in req){
				minlength = 2;
				if(parseInt($(this).attr("minlength")) > minlength){
					minlength = parseInt($(this).attr("minlength")) - 1;
				}
				if(req[x] == $(this).attr("id") && $(this).val().length < minlength){
					flag=false;
					$(this).parent().addClass("inputRed");
				}
			}
		});
		if(flag){
			return true;
		}
		else{
			alert("Required fields are empty!\nPlease correct fields marked with red border and submit this form again.");
			return false;
		}
		return false;      	
	}
	function resetFrom(){
		$("input:text, textarea").val("");
	}
	function submitForm(id){
		$(id).submit();
	}
	function showSplash(title){
			$("#splash").dialog({
				title:title,
				closeOnEscape:false,
				draggable:false,
				modal:true,
				resizable:false,
				width:600,
				height:300,
				close:function(){
					return false;
				}
			}).dialog('open');
			$(".ui-dialog-titlebar-close").css("display","none");
	}
