var TopNav = Class.create();
Object.extend(TopNav.prototype, PBase.prototype);
Object.extend(TopNav.prototype, {
	initialize: function(cont, subcont)
	{
		//parent's constructor
		PBase.prototype.initialize.bind(this)();

		this.dom = {};
		this.dom.menuContainer = $(cont);
		this.dom.submenuContainer = $(subcont);
		this.dom.items = this.dom.menuContainer.getElementsBySelector('ul:first-child>li:not(#clock)');
		this.dom.submenus = this.dom.menuContainer.getElementsBySelector('ul ul');
		this.dom.menuContainer.getElementsBySelector('ul>li>div>a:last-child').each(function(element){
			element.style.background = 'none';
		});

		//find default item
		try{this.dom.defaultItem = this.dom.menuContainer.getElementsBySelector('a.active').first().up('li');}
		catch(e){this.dom.defaultItem = this.dom.items[0];}

		if (!this.dom.defaultItem)
			this.dom.defaultItem = this.dom.items[0];


		this.showSubmenu(this.dom.defaultItem);


		for (var i = 0; i < this.dom.items.length; i++)
		{
			Event.observe(this.dom.items[i], 'mouseover', this.onMouseOver.bindAsEventListener(this, this.dom.items[i]));
			Event.observe(this.dom.items[i], 'mouseout', this.onMouseOut.bindAsEventListener(this, this.dom.items[i]));
		}

	},





	showSubmenu: function(li)
	{
		li.down('a').addClassName('hover');
		var submenu = li.getElementsBySelector('div').first();
		if (submenu)
		{
			this.submenu = submenu;
			this.submenu.style.display = 'block';
		}

		this.dom.activeItem = li;
	},

	hideSubmenu: function(li)
	{
		li.down('a').removeClassName('hover');
		var submenu = li.getElementsBySelector('div').first();
		if (submenu)
		{
			this.submenu.style.display = 'none';
		}
	},

	onMouseOver: function(event, li)
	{
		if (this.timer)
			clearTimeout(this.timer);

		if (this.dom.activeItem)
			this.hideSubmenu(this.dom.activeItem);


		this.showSubmenu(li);
	},

	onMouseOut: function(event, li)
	{
		this.timer = setTimeout(this.onMouseOutTimedOut.bind(this, li), 500);
	},


	onMouseOutTimedOut: function(li)
	{
		if (this.dom.activeItem)
			this.hideSubmenu(this.dom.activeItem);

		this.showSubmenu(this.dom.defaultItem);
	}
});




var NavClock = Class.create();
Object.extend(NavClock.prototype, PBase.prototype);
Object.extend(NavClock.prototype, {
	initialize: function(cont)
	{
		//parent's constructor
		PBase.prototype.initialize.bind(this)();

		this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

		this.cont = $(cont);
		this.colon = true;
		this.executrer = new PeriodicalExecuter(this.update.bind(this), 1);
	},

	update: function()
	{
		var date = new Date();
		var day = date.getDate();
		var month = this.months[date.getMonth()];
		var minutes = date.getMinutes();
		var hours = date.getHours();
		var year = date.getYear();

		//conver yer to 4 digits
		year = year%100;
		year += (year < 38) ? 2000 : 1900;


		//converto to am/pm
		if (hours >= 1 && hours <= 12)
		{
			var ampm = 'am';
		}
		else
		{
			var ampm = 'pm';
			if (hours == 0)
				hours = 12;
			else
				hours = hours - 12;
		}

		if (this.colon)
			var colon = ':';
		else
			var colon = ' ';

		this.colon = !this.colon;



		minutes += '';
		if (minutes.length == 1)
			minutes = '0'+minutes;

		var str = month+' '+day+', '+year+' '+hours+colon+minutes+' '+ampm;


		this.cont.innerHTML = str;
	}
});


function formatPrice(price)
{
	return '$'+PUtil.numberFormat(price, 0, '', ',');
}

function setHiddenField(form, name, value)
{
	form = $(form);
	var hiddens = form.getElementsBySelector('input[type=hidden]');
	var hidden = null;
	hiddens.each(function(e){
		if (e.name == name)
		{
			hidden = e;
			throw $break;
		}
	});

	if (!hidden)
	{
		hidden = document.createElement('input');
		hidden.setAttribute('type', 'hidden');
		hidden.setAttribute('name', name);
		form.appendChild(hidden);
	}

	hidden.value = value;
}




//used for destination's pages and left navbar filter
var DestinationSelector = Class.create();
DestinationSelector.prototype = {
	initialize: function(cont, infoJSON, selection)
	{
		var info = infoJSON.evalJSON(true);
		this.destinations = new Hash(info.destinations);
		this.destinations.map();
		this.rootId = info.rootId;

		this.domContainer = $(cont);
		this.domSelects = this.domContainer.getElementsBySelector('select.p_dst');
		//this.initSelect(this.domSelects[0], this.rootId);
		for (var i = 0; i < this.domSelects.length; i++)
		{
			this.domSelects[i].defaultVal = $F(this.domSelects[i]);
			this.domSelects[i].observe('change', this.onChange.bindAsEventListener(this));
		}

		this.domForm = this.domSelects[0].up('form');
		this.setSelection(selection);

		//set observer for hidden field representing destination
		//new Form.Element.Observer(this.domForm.down('[name=destination]'), 0.1, this.onHiddenFieldChanged.bind(this))
	},

	setSelection: function(id)
	{
		if (!id || id == "0")
			id = this.rootId;


		this.selection = id;
		setHiddenField(this.domForm, 'destination', id);

		if (id == this.rootId)
		{
			this.initSelect(this.domSelects[0], id);
			for (var i = 1; i < this.domSelects.length; i++)
				this.clearSelect(this.domSelects[i]);

			return;
		}


		var dstArr = [-1];		
		var dst = this.destinations.get(id);
		while (1)
		{
			if (dst.id == this.rootId)
				break;

			dstArr.push(dst);
			dst =  this.destinations.get(dst.parentId);
		}


		for (var i = 0; i < this.domSelects.length; i++)
		{
			var curSelect = this.domSelects[i];
			var curNode = dstArr.pop();

			if (curNode == -1 && prevNode)
			{
				this.initSelect(curSelect, prevNode.id);
				continue;
			}

			var prevNode = curNode;

			if (!curNode)
			{
				this.clearSelect(curSelect);
				continue;
			}

			curSelect.style.visibility = 'visible';
			var option = curSelect.getElementsBySelector('option[value='+curNode.id+']').first();
			if (!option)
			{
				this.initSelect(curSelect, curNode.parentId);
				var option = curSelect.getElementsBySelector('option[value='+curNode.id+']').first();
			}

			option.selected = true;
		}
	},

	clearSelect: function(select)
	{
		select.style.visibility = 'hidden';
		select.innerHTML = '';
	},

	onChange: function(event)
	{
		var select = Event.element(event);
		var pos = -1;
		if ($F(select))
		{
			this.setSelection($F(select));
		}
		else if (select.previous('select.p_dst'))
		{
			this.setSelection($F(select.previous('select.p_dst')));
		}
		else
			this.setSelection(this.rootId);
	},

	initSelect: function(select, parentId)
	{
		select.innerHTML = '';

		var option = document.createElement('option');
		option.value = parentId;
		option.innerHTML = select.defaultVal;
		select.appendChild(option);

		this.destinations.each(function(pair){
			var dst = pair.value;
			if (dst.parentId == parentId)
			{
				var option = document.createElement('option');
				option.value = dst.id;
				option.innerHTML = dst.title.escapeHTML();
				select.appendChild(option);

			}
		});

		if (select.getElementsBySelector('option').length == 1)
			this.clearSelect(select);
		else
			select.style.visibility = 'visible';
	}

	/*
	onHiddenFieldChanged: function(el, value)
	{

		//console.log(value);
		return;
		if (!el)
			return;

		if (value != this.selection)
			this.setSelection(value);
	}
	*/
}


var RequestList =
{
	add: function(id)
	{
		url = urlBaseRelative+'my-request-list/?action=add&id='+id;
		new Ajax.Request(url, {onSuccess: this.onAjax.bind(this)});
		urchinTracker(url);
	},

	rent: function(id)
	{
		url = urlBaseRelative+'my-request-list/?action=add&id='+id;
		new Ajax.Request(url, {onSuccess: this.onRent.bind(this)});
		urchinTracker(url);
	},

	remove: function(id)
	{
		url = urlBaseRelative+'my-request-list/?action=remove&id='+id;
		new Ajax.Request(url, {onSuccess: this.onAjax.bind(this)});
		urchinTracker(url);
	},

	removeAll: function()
	{
		url = urlBaseRelative+'my-request-list/?action=removeAll';
		new Ajax.Request(url, {onSuccess: this.onAjax.bind(this)});
		urchinTracker(url);
	},

	setService: function(name, remove)
	{
		if (!remove)
			remove = 0;
		else
			remove = 1;


		url = urlBaseRelative+'my-request-list/?action=set_service&service='+name+'&remove='+remove;
		new Ajax.Request(url, {onSuccess: this.onAjax.bind(this)})
		urchinTracker(url);
	},

	onAjax: function(req)
	{
		var data = req.responseText.evalJSON();
		$$('.mrl_size').each(function(e)
		{
			e.update('('+data.size+')');
		});



		new InfoBox(data.message, data.type);
	},

	onRent: function(req)
	{
		var data = req.responseText.evalJSON();
		$$('.mrl_size').each(function(e)
		{
			e.update('('+data.size+')');
		});

		document.location.href = urlBaseRelative+'contact-us/';
	}
}






var PopupWindow;

function raw_popup(url, target, features)
{
	// pops up a window containing url optionally named target, optionally having features
	features = 'location=0,statusbar=0,menubar=0,width=640,height=544';
	target = '_blank';
	PopupWindow = window.open(url, target, features);
	PopupWindow.focus();
}

function raw_popup_sized(url, width, height, scroll)
{
	if (scroll != 'yes')
		scroll = 'no';

	// pops up a window containing url optionally named target, optionally having features
	features = 'location=0,statusbar=0,menubar=0,width='+width+',height='+height+',scrollbars='+scroll;
	target = '_blank';
	PopupWindow = window.open(url, target, features);
	PopupWindow.focus();


}