(function($){
	function Period(o)
	{	
		this.__string = 'Date period plugin v1.1';
		
		var _s = this;
		
		$.datepicker.setDefaults({dateFormat: o.dateFormat});
		
		this.sD = o.sD.change(function(e) {_s.changeSelect(e);});
		this.sMY = o.sMY.change(function(e) {_s.changeSelect(e);});
		this.eD = o.eD.change(function(e) {_s.changeSelect(e);});
		this.eMY = o.eMY.change(function(e) {_s.changeSelect(e);});
		this.onSelect = o.onSelect;
		this.interval;
		
		this.sEl = o.start.datepicker({	
			showOn: "button",
			buttonImage: "/i/calendar.png",
			maxDate: '+1y',
			buttonImageOnly: true,
			onSelect: function(s){	
				var date = new Date($.datepicker.parseDate(o.dateFormat,s));
				date.setDate(date.getDate() + 1);
				_s.eEl.datepicker("setDate", date).datepicker("option", "minDate", date);
				_s.change();
			},
			beforeShow: function(input, inst) { _s.timeout(input, inst); },
			onClose: function() { _s.clear(); }
		}).datepicker("setDate",o.sDate).datepicker("option", "minDate", o.sDateMin);
		this.eEl = o.end.datepicker({
			showOn: "button",
			buttonImage: "/i/calendar.png",
			maxDate: '+1y',
			buttonImageOnly: true,
			onSelect: function() { _s.change(); },
			beforeShow: function(input, inst) { _s.timeout(input, inst); },
			onClose: function() { _s.clear(); },
			onChangeMonthYear: function(year, month, inst) { 
				
				
			}

			
			
		}).datepicker("setDate",o.eDate).datepicker("option", "minDate", o.eDateMin);	
	
		
		$.extend (
			Period.prototype,
			{
				getDates: function() {
					return {s:this.sEl.datepicker("getDate"),e:this.eEl.datepicker("getDate")};
				},
				formatSQL: function(d) {
					return $.datepicker.formatDate('yy-mm-dd',d);
				},
				formatDate: function(d) {
					return $.datepicker.formatDate(o.dateFormat,d);
				},
				timeout: function(input, inst) {
					var _s = this;
					$('#ui-datepicker-div').mouseover(function(){
						clearInterval(_s.interval);
					})
					$('#ui-datepicker-div').mouseout(function(){
						clearInterval(_s.interval);
						_s.interval = setTimeout(function(){
							_s.sEl.datepicker( "hide" );
							_s.eEl.datepicker( "hide" );
						}, 500)
					})					
				},
				clear: function() {
					clearInterval(this.interval);
				},
				change: function () {
					var d = this.getDates();
					this.sD.val($.datepicker.formatDate('dd',d.s));
					this.sMY.val($.datepicker.formatDate('mm-yy',d.s));						
					this.eD.val($.datepicker.formatDate('dd',d.e));
					this.eMY.val($.datepicker.formatDate('mm-yy',d.e));

					this.onSelect(d.s,d.e);
				},	
				__check: function(date) {
					try {
						return new Date($.datepicker.parseDate(o.dateFormat,date))
					}
					catch(e) {
						return false;
					}
				}, 	
				__getDate: function(date) {
					var date = this.__check(date);					
					if(!date) {
						var _date = parseInt(this.sD.val());					
						do {
							_date--;
						}
						while (
							(date = this.__check(_date+'-'+this.sMY.val())	) === false
						)
					}
					return date;
				},
				
				changeSelect: function (e) {
					var sDate = this.__getDate(this.sD.val()+'-'+this.sMY.val());
					this.sEl.datepicker("setDate", sDate);
					sDate = this.sEl.datepicker("getDate");
					if(e.target.id == this.sD.attr("id") || e.target.id == this.sMY.attr("id")) {
						sDate.setDate(sDate.getDate() + 1);
						this.eEl.datepicker("option", "minDate", sDate).datepicker("setDate", sDate);
					}
					else {
						var eDate =  this.__getDate(this.eD.val()+'-'+this.eMY.val());
						this.eEl.datepicker("setDate", eDate);
					}	
					this.change();
				}
			}
		);
		this.change();
	};
	$.fn.extend({
	    period :  function(o) {
			return new Period(
				$.extend({
					start: $('#start'),
					end: $('#end'),
					sD:  $('#sdselect'),
					sMY:  $('#smyselect'),
					eD:  $('#edselect'),
					eMY:  $('#emyselect'),
					sDate: '+1m',
					eDate: '+1m +1d',
					sDateMin:  'today',
					eDateMin:  '+1d',
					dateFormat: 'dd-mm-yy',
					onSelect: function(s,e) {}
				}, o)
			); 
	    }
  	});
})(jQuery);

////////////////////////////////////////////////

(function($){
	function Room(o)
	{	
		var _s = this;
		
		this.__name = 'Room';
		this.enable = true;
	
		this.holder = o.holder
		
		this.id = o.id;
		this.instanse = o.instanse;
		this.adult = 1;
		this.child = 0;
		this.baby = 0;	
		this.w_child = 0;
		this.w_baby = 0;
		this.a_child = 0;
		this.a_baby = 0;
			
		this.koef = 0;
		this.w_adult = false;
		
		this.$adult = this.holder.find("#adult"); 
		this.$child = this.holder.find("#child"); 		
		this.$baby = this.holder.find("#baby"); 	
		this.$w_child = this.holder.find("#w_child"); 	
		this.$w_baby = this.holder.find("#w_baby"); 	
		this.$a_child = this.holder.find("#a_child"); 	
		this.$a_baby = this.holder.find("#a_baby");
		
		this.$stime = this.holder.find("#stime"); 	
		this.$etime = this.holder.find("#etime"); 
		this.$price_room = this.holder.find("#price_room");
		this.$days = this.holder.find("input.days");
		this.$room_title = this.holder.find("#room_title");
		this.$ad = this.holder.find("#layer__adult_add"+this.instanse);
		this.$ch = this.holder.find("#chidren"+this.instanse);
		
		
		this.holder.find("#chlds_add"+this.instanse).click(function(){
			if($(this).is(":checked")) {				
				_s._adult = _s.adult;
				_s.adult = 0;
				_s.w_adult = true;
			}
			else {
				_s.adult = _s._adult;
				_s.w_adult = false;
			}
			_s.updatePrice();
		});
		
		
		this.holder.find("input.view").click(function(){
			_s.updatePrice();
		});
		this.holder.find("#adult,#child,#baby,#w_child,#w_baby,#a_child,#a_baby, input.__children").change(function(){
			_s.getPeople();
		});
		this.holder.find("div.option").click(function(){
			_s.id = $(this).attr("rel");
			_s.showRoom(_s.id, true);
		}).mouseover(function(){
			$(this).addClass("option-hover");
			_s.showRoom($(this).attr("rel"),false);
		}).mouseout(function(){
			$(this).removeClass("option-hover");
			_s.showRoom(_s.id, false);
		});
		
		this.$stime.change(function(){
			_s.updateDays(parseInt($(this).val()));
		});
		this.$etime.change(function(){
			_s.updateKoef(parseInt($(this).val()));
		});
		
		$.extend (
			Room.prototype,
			{
				updateKoef: function(etime) {
					if(etime<=12) {
						this.koef = 0;
					}
					else if(etime>12&&etime<=18) {
						this.koef =(1/24) *(etime-12)
					}
					else if(etime>18) {
						this.koef = 0.5;
					}
				  	this.updatePrice();
				},
				updateDays: function(stime) {
					if(stime<14)
						this.$days.val(this.days+1);
					else
						this.$days.val(this.days);
					this.updatePrice();
				},
				updatePrice: function() {
					if(this.w_adult) {						
						var a_child = this.a_child;
						if(a_child) {							
							a_child--;
						}
						this.countedPrice = (parseInt(this.$days.val())+this.koef)*(
							//GET PRICE ROOM
							parseInt(window.prices[this.id+'-'+this.holder.find("input[name=view"+this.id+"-"+this.instanse+"]:checked").val()])+
							//CHILD ON w/ADD_PLACE PRICE
							a_child*window.prices.breakfast
						);
					}
					else {
						this.countedPrice = (parseInt(this.$days.val())+this.koef)*(
							//GET PRICE ROOM
							parseInt(window.prices[this.id+'-'+this.holder.find("input[name=view"+this.id+"-"+this.instanse+"]:checked").val()])+
							//ADULT_PRICE
							(this.adult-1)*window.prices.breakfast +
							//BABY PRICE
							this.baby*window.prices.add_place+
								//CHILD ON ADD_PLACE PRICE
							this.child*(parseInt(window.prices.add_place)+parseInt(window.prices.breakfast))+
							//CHILD ON w/ADD_PLACE PRICE
							this.w_child*window.prices.breakfast
						);
						
					}
										
					this.$price_room.html(Math.round(this.countedPrice));
					o.onChangePrice();
				},
				getPeople: function (contin) {						
					if(!contin) {						
						this.adult = 	!this.w_adult ? parseInt(this.$adult.val()) : 0;
						
						if(this.$ch.is(':checked')) { 						
							this.baby  = 	parseInt(this.$baby.val());	
							this.w_baby  = 	parseInt(this.$w_baby.val());
							this.child = 	parseInt(this.$child.val());
							this.w_child  = parseInt(this.$w_child.val());
						}
						else {
							this.baby  = this.w_baby  = this.child = this.w_child  = 0;
						}
											
						this.a_baby  = parseInt(this.$a_baby.val());
						this.a_child  = parseInt(this.$a_child.val());
					}

					var dif = this.ap_max - (this.baby+this.child);
					var w_dif = this.w_ap_max - (this.w_baby+this.w_child);
					var a_dif = this.w_ap_max+this.a_max - (this.a_baby+this.a_child);
					
					this.$baby.html(this.getOptions( this.baby, dif, true ));
					this.$child.html(this.getOptions( this.child, dif, true ));
					
					this.$w_baby.html(this.getOptions( this.w_baby, w_dif, true ));
					this.$w_child.html(this.getOptions( this.w_child, w_dif, true ));	

					this.$a_baby.html(this.getOptions( this.a_baby, a_dif, true ));	
					this.$a_child.html(this.getOptions( this.a_child, a_dif, true ));
					
					this.updatePrice();
				},
				getOptions: function(cur, more, nulled) {
					//alert(num)
					c = nulled ? '<option selected="true" value="0"></option>' : '';
					for(var i=1; i<=cur+more; i++) {
						selected = i==cur ? 'selected="true"' : '';
						c+='<option '+selected+' value="'+i+'">'+i+'</option>';
					}
					return c;
					
				},
				showRoom: function (id,c) {
					var el = this.holder.find("div.option[rel="+id+"]");
					var title = el.find("div.title").html();
					
					var words = title.split(/\s/);
					if(words.length> 3)
						title = '«'+ words[1]+" "+words[3];
					
					this.holder.find("div.desc:visible").hide();
					this.holder.find("div.desc[rel="+id+"]").show();
					
					if(id>2) {
						this.holder.find("div.bed").hide();
					}
					else {
						this.holder.find("div.bed").show();
					}
					
					if(c) {
						this.a_max = parseInt(window.rooms[id].adult);
						this.ap_max = parseInt(window.rooms[id].add_place);
						this.w_ap_max = parseInt(window.rooms[id].w_add_place);
																					
						this.holder.find("div.option-active").removeClass("option-active");	
						el.addClass("option-active");		
						this.holder.find("div.header > span").html(title);	
						
						if(this.adult>=this.a_max)	{
							this.adult = this.a_max
						}					

						if(this.child>=this.ap_max) {
							this.baby = 0;
							this.child = this.ap_max;
						}
						else if(this.baby>= this.ap_max) {
							this.baby =  this.ap_max;
							this.child = 0;
						}
						
						if(this.w_child>=this.w_ap_max) {
							this.w_baby = 0;
							this.w_child = this.w_ap_max;
						}
						else if(this.w_baby>= this.w_ap_max) {
							this.w_baby =  this.w_ap_max;
							this.w_child = 0;
						}
						
						if(this.a_child>=this.w_ap_max+this.a_max) {
							this.a_baby = 0;
							this.a_child = this.w_ap_max+this.a_max;
						}
						else if(this.a_baby>= this.w_ap_max+this.a_max) {
							this.a_baby =  this.w_ap_max+this.a_max;
							this.a_child = 0;
						}
						
						if(!this.ap_max) {
							this.holder.find('div.add_place').hide();							
						}
						else {
							this.holder.find('div.add_place').show();							
						}
					
						this.$adult.html(this.getOptions( this.adult, this.a_max-this.adult, false ))	
							
						this.getPeople(true);
						this.updatePrice();
						
						this.$room_title.html(words.join(" "));
						
						
						this.holder.find("textarea.defaulted").defaultValue();
					}
					else {
						this.holder.find("div.header > span").html(title)
					}				
				}
			}
			
		);
		
		this.showRoom(this.id, true);
		
		$.fn.period({
			start: this.holder.find("#start"),
			end: this.holder.find("#end"),
			sD:  this.holder.find("#sdselect"),
			sMY:  this.holder.find("#smyselect"),
			eD:  this.holder.find("#edselect"),
			eMY:  this.holder.find("#emyselect"),
			sDate: o.sDate,
			eDate: o.eDate,
			onSelect: function(sdate, edate) {				
				_s.days = Math.round((edate.getTime()-sdate.getTime())/(1000*60*60*24));
				_s.$days.val(_s.days);
				_s.updatePrice();
			}
		});
		
	};
	$.fn.extend({
	    room :  function(o) {		
			return new Room(
				$.extend({
					holder: this,
					id: 1,
					sDate: "+1d",
					eDate: "+2d",
					onChangePrice: function() {}
				}, o)	
			); 	
	    }
  	});
})(jQuery);

(function() {
	jQuery.fn.defaultValue = function() {
		return this.each(function() {	
			var _default = $(this).attr("default");
			$(this).val(_default).addClass("default")		
			.bind("focus", function() {
				
				
				if($(this).val() == _default)
					$(this).val("").removeClass("default").removeClass("error");
				
			
			})
			.bind("blur", function() {
				if($(this).val()=="")
					$(this).val(_default).addClass("default").addClass("error");
			});
		});
	}
})(jQuery);

String.prototype.replaceTPL = function(pattern,str) {
	if(typeof pattern == 'array' || typeof pattern == 'object') {
		var _text = this.toString();
		for(i=0;i<pattern.length;i++) {
			_text = _text.replace('{'+pattern[i]+'}',str[i]);
		}
		return _text;
	}
	else {	
		return this.replace('{'+pattern+'}',str);
	}
}
String.prototype.printr = function (_hamper,_prefix,_postfix,_face) {
  _hamper = _prefix+"@"+this+(_postfix || '');
  document.write((_face||_hamper).link("mailto:"+_hamper));
}

$().ready(function() {
	if($.browser.msie && $.browser.version<=6)
		DD_belatedPNG.fix('.png');	
})

