var SITE_ROOT = 'http://www.hotelesparaiso.com/';
 
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}
 
function id(id) {
	var elem = document.getElementById(id);
	return elem;
}		

function close_message(linku) {
	linku.parentNode.parentNode.style.display = 'none';
}

function contact_form(form) {
	var form_obj = id(form);
	/*alert(form_obj);*/
	var poststr = "nume=" + form_obj.nume.value
	+ "&companie=" + form_obj.companie.value
	+ "&email=" + form_obj.email.value
	+ "&mesaj=" + form_obj.mesaj.value
	+ "&sent=" + form_obj.sent.value
	+ "&contact=1&ajax=1"
	load_content('contact_div', SITE_ROOT, poststr);
}

function rezervare(form) {
	form_obj = document.getElementById(form);
	if(!form_obj.sent.value) {
		form_obj.sent.value = 0;
	}
	var poststr = "nume=" + form_obj.nume.value
	+ "&data_sosirii=" + form_obj.data_sosirii.value
	+ "&data_plecarii=" + form_obj.data_plecarii.value
	+ "&tip_camera=" + form_obj.tip_camera.value
	+ "&telefon=" + form_obj.telefon.value
	+ "&nr_camere=" + form_obj.nr_camere.value
	+ "&nr_persoane=" + form_obj.nr_persoane.value
	+ "&email=" + form_obj.email.value
	+ "&sent=" + form_obj.sent.value
	+ "&rezervari=1&ajax=1"
	load_content('rezervari_div', SITE_ROOT, poststr);
}

function selectReplacement(obj) {
	obj.className += ' replaced';
	var ul = document.createElement('ul');
	ul.className = 'selectReplacement';
	ul.setAttribute('id','selectReplacement');
	var opts = obj.options;
	for (var i=0; i<opts.length; i++) {
		var selectedOpt;
		if (opts[i].selected) {
			selectedOpt = i;
			break;
		} else {
			selectedOpt = 0;
		}
	}
	for (var i=0; i<opts.length; i++) {
		var li = document.createElement('li');
		var txt = document.createTextNode(opts[i].text);
		li.appendChild(txt);
		li.selIndex = opts[i].index;
		li.selectID = obj.id;
		li.onclick = function() {
			selectMe(this);
		}
	
		if (i == selectedOpt) {
			li.className = 'selected';
			li.onclick = function() {
				this.parentNode.className += ' selectOpen';
				this.onclick = function() {
					selectMe(this);
				}
			}
		}
		if (window.attachEvent) {
			li.onmouseover = function() {
				this.className += ' hover';
			}
			li.onmouseout = function() {
				this.className = 
				this.className.replace(new RegExp(" hover\\b"), '');
			}
		}
		ul.appendChild(li);
	}
	obj.parentNode.insertBefore(ul,obj);
}

function selectMe(obj) {
	var lis = obj.parentNode.getElementsByTagName('li');
	for (var i=0; i<lis.length; i++) {
		if (lis[i] != obj) {
			lis[i].className='';
			lis[i].onclick = function() {
				selectMe(this);
			}
		} else {
			setVal(obj.selectID, obj.selIndex);
			obj.className='selected';
			obj.parentNode.className = 
			obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
			obj.onclick = function() {
				obj.parentNode.className += ' selectOpen';
				this.onclick = function() {
					selectMe(this);
				}
			}
		}
	}
}

function setVal(objID, selIndex) {
	var obj = document.getElementById(objID);
	obj.selectedIndex = selIndex;
}

function setForm() {
	var s = document.getElementsByTagName('select');
	for (var i=0; i<s.length; i++) {
		selectReplacement(s[i]);
	}
}

function fotos_big_replace() {
 var fotos_big = $$('.foto');
 for(m=0; m<fotos_big.length; m++) {
		fotos_big[m].setAttribute('src',fotos_array[m]);
	}
}	
  
function timer_img() {
	var data2 = new Date();
	window.setTimeout(fotos_big_replace,data2-data1);	
}
	
function hide_calendar() {
	var calendar= $$('.calendar');
	for(m=0; m<calendar.length; m++) {
		document.body.removeChild(calendar[m]);
	}
}	

 function navigation() {
 
 	var scrollDos = new Fx.Scroll('fotos_chuchu', {
		wait: false, duration: 1900, transition: Fx.Transitions.Quart.easeOut
	});

	var galleryImgs = $$('#lins2 a');
	if (galleryImgs.length > 0) galleryImgs.each(function(image, index) {
		$('fot' + index).addEvent('click',
		function(event) {
			event = new Event(event).stop();
			scrollDos.toElement('li' + index);
		});
	});
	var current_foto = 0;
	var foto_nav2_lks = $$('#fotos_nav2 a');
	//var fotos_nav = document.createElement('ul');
	//fotos_nav.setAttribute('id','fotos_nav')
	var fotos_nav = $('fotos_nav');
	/*for(i=0; i<2; i++) {
		var li = document.createElement('li');
		var a = document.createElement('a');
		a.setAttribute('href','#');
		a.setAttribute('id','fot'+i);
		fotos_nav.appendChild(li);
		li.appendChild(a);
	}
	$('galerie').appendChild(fotos_nav);*/
	/*var fotos_nav_lks = fotos_nav.getElementsByTagName('a');
	fotos_nav_lks[0].setAttribute('id','foto_inapoi');
	fotos_nav_lks[1].setAttribute('id','foto_inainte');*/

	//var foto_inapoi = fotos_nav_lks[0];
	var foto_inapoi = $('foto_inapoi');
	//var foto_inainte = fotos_nav_lks[1];
	var foto_inainte = $('foto_inainte');
	foto_inapoi.onclick = function () { 
		if(current_foto==1) {
			this.style.visibility = 'hidden';
		}
		if(current_foto>0) {
			current_foto--;
		}
		var selectat = $('selectat');
		if(selectat) {
			selectat.removeAttribute('id');
		}
		foto_nav2_lks[current_foto].setAttribute('id','selectat');
		foto_inainte.style.visibility  = 'visible';
		scrollDos.cancel().toElement($('li'+(current_foto)));
		// fotos_nav_h4.innerHTML = fotos_span[current_foto].innerHTML;
		return false;
	}
	foto_inainte.onclick = function () { 
		if(current_foto==foto_nav2_lks.length-2) {
			this.style.visibility = 'hidden';
		}
		if(current_foto<foto_nav2_lks.length-1) {
			current_foto++;
		}
		var selectat = $('selectat');
		if(selectat) {
			selectat.removeAttribute('id');
		}
		foto_nav2_lks[current_foto].setAttribute('id','selectat');
		foto_inapoi.style.visibility  = 'visible';
		scrollDos.cancel().toElement($('li'+(current_foto)));
		// fotos_nav_h4.innerHTML = fotos_span[current_foto].innerHTML;
		return false;
	}
	for(x=0; x<foto_nav2_lks.length; x++) {
		foto_nav2_lks[x].onclick = function () {
			var selectat = $('selectat');
			if(selectat) {
				selectat.removeAttribute('id');
			}
			this.setAttribute('id','selectat');
			for(z=0; z<foto_nav2_lks.length; z++) {
				if(foto_nav2_lks[z].getAttribute('id')=='selectat')	{
					current_foto = z;
					if(current_foto==0) {
						foto_inapoi.style.visibility = 'hidden';
						foto_inainte.style.visibility = 'visible';
					}	else if (current_foto==(foto_nav2_lks.length-1)) {
						foto_inainte.style.visibility = 'hidden';
						foto_inapoi.style.visibility = 'visible';
					} else {
						foto_inapoi.style.visibility = 'visible';	
						foto_inainte.style.visibility = 'visible';
					}
					scrollDos.cancel().toElement($('li'+(current_foto)));
					// fotos_nav_h4.innerHTML = fotos_span[current_foto].innerHTML;
				}
			}
			return false;
		}
	}
} 

window.addEvent('domready', function() {
	// addDOMLoadEvent(path_images);
	 navigation();
	setForm();
	externalLinks();
	// addLoadEvent(timer_img);
	date_chooser_init();
	date_chooser_call();
}); 
