var Calendar2 = {
	self: "Calendar2",
	
	cal_clicked: function(e){
		Calendar2.override=true;
	},
	
	window_clicked: function(e){
		if(!Calendar2.override){Calendar2.close();}
		Calendar2.override = false;
	},
	
	init: function(){
		this.override = true;
		this.setOptions({noInit:true});
		this.cal = document.getElementById(this.divId);
		this.cal.style.display = "none";
		this.cal.style.zIndex = "1500";
		this.addEvent(this.cal,'click',Calendar2.cal_clicked);
		this.addEvent(document,'click',Calendar2.window_clicked);
	},
	
	checkOverride: function(){
		if(!this.override){this.close();}
		this.override = false;
	},
	
	addEvent: function(obj,type,fn){
		if(obj.addEventListener){
			obj.addEventListener(type,fn,false);
		}else if(obj.attachEvent){
			obj["e"+type+fn]=fn;
			obj[type+fn]=function(){obj["e"+type+fn](window.event);};
			obj.attachEvent("on"+type,obj[type+fn]);
		}
	},
	
	removeEvent: function(obj,type,fn){
		if(obj.removeEventListener){
			obj.removeEventListener(type,fn,false);
		}else if(obj.detachEvent){
			obj.detachEvent("on"+type,obj[type+fn]);
			obj[type+fn]=null;
			obj["e"+type+fn]=null;
		}
	},
	
	setOptions: function(options){
		if(!options){options = {};}
		this.header = (options.header)?options.header:"Please Select a Date";
		this.current = options.input;
		this.next = (options.next)?options.next:"";
		this.previous = (options.previous)?options.previous:"";
		this.date_fmt = 'mm/dd/yyyy';
		this.anchor = (options.anchor)?options.anchor:options.input;
	//	var floatingTP = document.getElementById('floatingTP');

		this.total_pages = (options.pages)?options.pages:2;
		this.divId = (options.div)?options.div:"calendarbox";
		// the offsets require a check explicitly for 0 and not just set since if it's 0, it checks as unset
		this.xOffset = (options.xOffset>=0 || options.xOffset<0)?options.xOffset:15;
		this.yOffset = (options.yOffset>=0 || options.yOffset<0)?options.yOffset:15;
		this.page = 0;
		if(!options.noInit){
			this.prev_date = new Date();
			if(!this.cal){this.init();}
		}
		if(this.previous!=""){
			this.prev_date = new Date();
			var prev = document.getElementById(this.previous).value;
			if(prev != ""){
				prev = prev.replace(/\/([0-9]{2})$/,"/20$1"); // compensate for 0x -> 190x issue
				document.getElementById(this.previous).value = prev;
				this.prev_date.setTime(Date.parse(prev));
			}
			this.prev_date.setDate(this.prev_date.getDate());
		}
		if(this.current){
			this.curr_date = new Date();
			var curr = document.getElementById(this.current).value;
			if(curr != ""){
				// make sure date is in m/d/yyyy format as opposed to m/d/yy format
				curr = curr.replace(/\/([0-9]{2})$/,"/20$1"); // compensate for 0x -> 190x issue
				document.getElementById(this.current).value = curr;
				this.curr_date.setTime(Date.parse(curr));
				if(this.curr_date < this.prev_date){
					document.getElementById(this.current).value = "";
					this.curr_date = this.prev_date;
				}
			}else if(this.prev_date){
				this.curr_date = this.prev_date;
			}
		}
		
	},
	
	turnPage: function(page){
		this.page = this.page+page;
		this.show();
	},
	
	show: function(options){
		this.override = true;
		if(!this.cal){this.init();}
		if(options){
			this.setOptions(options);
		}
		var date = new Date();
		var toChange = ($(this.current).value != "") ? this.curr_date : this.prev_date;
		date.setTime(toChange);

		var content = "";
		//content += "<a id=\"gc_close\" href=\"javascript:"+this.self+".close();\">[x] - Close</a>";
		content += "<div id=\"cal_header\">"+this.header+"</div>";
		$("#calendarbox").draggable({ handle: '#cal_header' });
		this.cal.innerHTML = content;
		this.cur_page = 1;
		date.setMonth(date.getMonth()+this.page);
		this.move(this.current);
		for(var x=0;x<this.total_pages;x++){
			this.cal.innerHTML+= this.generateTable(date);
			date.setMonth(date.getMonth()+1);
			this.cur_page++;
		}
		this.cal.style.display = "block";
		
	},
	
	toggle: function(options){
		if(!this.cal){this.init();}
		if(this.cal.style.display == "none"){
			this.show(options);
		}else{
			if(this.current != options.input){
				this.show(options);
			}else{
				this.close();
			}
		}
	},
	
	close: function(){
		//this.override = true;
		this.page = 0;
		if(!this.cal){this.init();}
		this.cal.style.display = "none";
		
	},
	
	generateTable: function(date){
			
		var prev = "<a id=\"cal_prev\" href=\"javascript:"+this.self+".turnPage(-1);\">&lt;</a>";
		var next = "<a id=\"cal_next\" href=\"javascript:"+this.self+".turnPage(1);\">&gt;</a>";
		if(this.cur_page!=1){prev="";}
		if(this.cur_page!=this.total_pages){next="";}
		result = "<table id=\"cal_table_"+this.cur_page+"\" class=\"cal_table\"><tr><th>"+prev+"</th><th colspan=\"5\">"+this.getMonth(date.getMonth())+" "+date.getFullYear()+"</th><th>"+next+"</th></tr>";
		result += "<tr class=\"dow\"><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr>";
		result += this.getTableCode(date);
		result += "</table>";
		return result;
	},
	
	getTableCode: function(date){
		var first_day = date;
		var today = new Date();
		first_day.setDate(1);
		var counter = 1;
		var startDays = false;
		var result = "";
		var finished = false;

		for(x=0;x<6;x++){
			result += "<tr>";
			for(y=0;y<7;y++){
				if(y==first_day.getDay()){
					startDays = true;
				}
				if(startDays){
					if(!finished){
						// Class for handling whether or not this is the 'selected' element
						//var selected = (this.dateCompare(date,this.curr_date)==0 && ($(this.current).value != "" || this.previous != "")) ? " class=\"selected\" " : "";
						var selected = (this.dateCompare(date,this.curr_date)==0 && ($(this.current).value != "" || this.previous != "")) ? " class=\"selected\" " : "";
					    if (this.dateCompare(today,this.curr_date) == 0 && this.previous == '') selected = '';
						// See if the previous date is the current iteration date minus a day, keeping in mind that we don't want it to happen if it's not on a "next" field (the 'To' part of a 'From'-'To' sequence
						var isNotPrevDate = (this.prev_date.getTime() != (date.getTime()) || this.previous == "");
						
						// For handling showing a nice date range color when we have a previously selected date (onmouseover)
						var omo = (this.previous != "" && ($(this.current).value == "" || $(this.current).value == null)) ? ' onmouseover="' + this.self + '.showDateRange(' + this.cur_page + ', ' + date.getDate() + ');" ' : "";
						
						// Generate the ID for this field for ease in using our range script
						var cur_id = "site_cal_" + this.cur_page + "_" + date.getDate();
						
						if(this.dateCompare(date,this.prev_date)<=0 && isNotPrevDate){
							result += '<td'+selected+omo+' onmouseout="this.style.backgroundColor=\'#FFFFFF\';" id="' + cur_id + '"><a href="javascript:'+this.self+'.insertDate(\''+this.getFormattedDate(date)+'\');">'+date.getDate()+'</a></td>';
						}else{
							result += "<td"+selected+omo+" id='" + cur_id + "'>"+date.getDate()+"</td>";
						}
					}else{
						result += "<td>&nbsp;</td>";
					}
					date.setDate(date.getDate()+1);
					if(date.getDate()==1){date.setMonth(date.getMonth()-1);finished=true;}
				}else{
					result += "<td>&nbsp;</td>";
				}
			}
			result += "</tr>";
		}
		return result;
	},
	
	showDateRange: function(page, num) {
		// This variable is used to determine which item is selected; what we'll do is leave empty until it's found
		var selectedItem = {
			page: 0,
			date: 0
		};
		
		// Go through both pages
		for (i = 1; i <= 2; ++i) {
			// Go through from 1 - 31, ignoring elements that don't exist (obviously)
			for (j = 1; j <= 31; ++j) {
				// Get the current ID
				curItemid = 'site_cal_' + i + '_' + j;
				curItem = document.getElementById(curItemid);
				if (curItem == null || curItem == "undefined")
					continue;
					
				// See if this is the selected item
				if (selectedItem.page == 0 && curItem.className == "selected") {
					selectedItem.page = i;
					selectedItem.date = j;
					continue;
				}
				
				// See if this is the one we're hovering over
				if (page == i && num == j && curItem.innerHTML.length > 3) {
					curItem.style.backgroundColor = "#9a7d6c";
					continue;
				}

				// If we don't yet have the selection, continue on
				if (selectedItem.page == 0) {
					curItem.style.backgroundColor = "#FFFFFF";
					continue;
				}				
					
				//See if it falls between the selected date and the one we were given
				if (
					(selectedItem.page < page && j > selectedItem.date && i < page) ||				// See if the selected page < page we're hovering on and the date is > the selected date and we're on the lesser page
					(selectedItem.page < page && j < num && i >= page) ||							// See if the selected page < page we're hovering on and the date is < hover date and we're on the greater page
					(selectedItem.page == page && j > selectedItem.date && j < num && i == page)	// See if the selected page = page we're on and the date > selected date and the date < the hovering date
				) {
					curItem.style.backgroundColor = "#9a7d6c";
				}
				else 
					curItem.style.backgroundColor = "#FFFFFF";
			}
		}
	},
	
	getFormattedDate: function(date){
		var str = this.date_fmt;
		var yyyy = date.getFullYear();
		var yy = date.getYear()%100;
		yy = (yy.length>1)?yy:"0"+yy;
		var m = date.getMonth()+1;
		var mm = (m>9)?m:"0"+m;
		var d = date.getDate();
		var dd = (d>9)?d:"0"+d;
		str = str.replace(/y{4}/,yyyy);
		str = str.replace(/y{2}/,yy);
		str = str.replace(/m{2}/,mm);
		str = str.replace(/d{2}/,dd);
		str = str.replace(/m{1}/,m);
		str = str.replace(/d{1}/,d);
		return str;
	},

	dateCompare: function(x,y){
		var result = 0;
		if(x.getYear()<y.getYear()){
			result = 1;
		}else if(x.getYear()>y.getYear()){
			result = -1;
		}else if(x.getMonth()<y.getMonth()){
			result = 1;
		}else if(x.getMonth()>y.getMonth()){
			result = -1;
		}else if(x.getDate()<y.getDate()){
			result = 1;
		}else if(x.getDate()>y.getDate()){
			result = -1;
		}
		return result;
	},
	
	getMonth: function(x){
		var result;
		switch(x){
			case 0:result="January";break;
			case 1:result="February";break;
			case 2:result="March";break;
			case 3:result="April";break;
			case 4:result="May";break;
			case 5:result="June";break;
			case 6:result="July";break;
			case 7:result="August";break;
			case 8:result="September";break;
			case 9:result="October";break;
			case 10:result="November";break;
			case 11:result="December";break;
		}
		return result;
	},
	
	insertDate: function(date_text){
		document.getElementById(this.current).value = date_text;
		try{
			if(this.next){
				var obj = document.getElementById(this.next);
				var d = obj.onfocus.toString();
				var e = d.match(/\.(show|toggle)\((\{.*?\})\)/);
				if(!e){
					e = d.match(/\{([^\{\}]*?)\}/);
					eval(e[1])
				}
				else{
					this.show(eval("("+e[2]+")"))
				}
			}else{
				this.close();
			}
		}catch(e){this.close();}
	},
	
	move: function(current_field){
		//this.override = true;
		var pos = this.findPosition(document.getElementById(this.anchor));
		/*Condition for use of the calender on the Site Shade*/
				this.cal.style.zIndex = "1000";
				this.cal.style.left = pos[0]+this.xOffset+"px";
				this.cal.style.top = pos[1]+this.yOffset+"px";
				if(pos[0]+this.xOffset<0)
					this.cal.style.left="0px";
				if(pos[1]+this.yOffset<0)
					this.cal.style.top="0px";
				this.cal.style.position = "absolute";
				
	},
	
	findPosition: function(obj){
		//alert('asdf');
		var curleft = 0;
		var curtop = 0;
			if(obj){
				if(obj.offsetParent){
					curleft = obj.offsetLeft;
					curtop = obj.offsetTop;
					while(obj = obj.offsetParent){
						curleft+=obj.offsetLeft;
						curtop+=obj.offsetTop;
					}
				}
			}
			return [curleft,curtop];
	}
}

function show_cal(fd,action,opt){
 eval("Calendar2."+action+"({"+opt+"});");
}

var sbtn = new Image();
sbtn.src = '/images/btn_book_now_over.png';


$(document).ready(inicializarEventos);
function inicializarEventos()
{
  var x;
  x=$("#_fragment-1");
x.click( function(){_show(1);});
x=$("#_fragment-2");
  x.click( function(){_show(2);});
  x=$("#_fragment-3");
  x.click( function(){_show(3);});
  x=$("#_fragment-4");
  x.click( function(){_show(4);});
  x=$("#_fragment-5");
  x.click( function(){_show(5);});
  x=$("#_fragment-6");
  x.click( function(){_show(6);});
}
function _show(numero){
_hidden(numero);
$('#_fragment-'+numero).parents('li:last').addClass("ui-tabs-selected");
var x=$('#fragment-'+numero);
x.removeClass();
x.addClass("ui-tabs-panel");
}
function _hidden(numero){
var id="#reservation-tabs ";
if(numero>3){
	id="#villasuites ";
}
$(id+'.ui-tabs-selected').removeClass();
var x=$(id+'.ui-tabs-panel')
x.removeClass();
x.addClass('ui-tabs-panel ui-tabs-hide');
}

