/***
 * AllSeasons Front JS Interface
 * Rev. Date: Dec 16, 2008
 * Rev. Author: Justin Valceanu
 * Company: C&D Solutions
 */ 
 
$(document).ready(function(){
  $(".numeric").numeric();
  overlay.build();
});


/* Very Very OLD CODE */
function SetCookie(cookieName,cookieValue,nDays) {
 	var today  = new Date();
 	var expire = new Date();
 	if (nDays==null || nDays==0) 
 		nDays=1;
 	expire.setTime(today.getTime() + 3600000*24*nDays);
 	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function ReadCookie(cookieName) {
  var theCookie = ""+document.cookie;
  var ind = theCookie.indexOf(cookieName);
  if (ind==-1 || cookieName=="") 
  	return ""; 
  var ind1=theCookie.indexOf(';',ind);
  if (ind1==-1) 
  	ind1=theCookie.length; 
  return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
	
//////////////////////////////////////////////////////
// Fader script
//////////////////////////////////////////////////////
 
var imageId     = 'thephoto';
var containerId = 'header';

var i=0;
var step;

function initImage() {
		
	currentState = ReadCookie('headerState');
	currentState = currentState == 2 ? false : true;
	searchTabState = ReadCookie('searchTabState');
	if (searchTabState != 'cauta-cazare' && searchTabState != 'cauta-site') {
		searchTabState = 'cauta-cazare';
	}
	rezTabState = ReadCookie('rezTabState');
	if (rezTabState != 'rez-pj' && rezTabState != 'rez-pf') {
		rezTabState = 'rez-pf';
	}
	newsletterForm = ReadCookie('newsletter');
	if (newsletterForm == 1) {
		$('#newsletter').toggle();
	}
	suggestForm = ReadCookie('suggest-form');
	if (suggestForm == 1) {
		$('#suggest-form').toggle();
	}
	if (searchTabState == 'cauta-site'){
		
		$('#cauta-cazare').removeClass('tab-activ');		
		$('#cauta-site').addClass('tab-activ');
		$('#cauta-cazare-content, #cauta-site-content').toggle();
	}
	if (rezTabState == 'rez-pj'){
		
		$('#rez-pf').removeClass('tab-activ');		
		$('#rez-pj').addClass('tab-activ');
		$('#rez-pj-content, #rez-pf-content').toggle();
	}
	if ( currentState == false) {
		$('#header a').css('display', 'block');
		$('#header div, #header object').css('display', 'none');
	}
	
	$('#servicii_unitate span').ToolTip(
		{
			className: 'linksTooltip',
			position: 'mouse',
			delay: 200
		}
	);
	$('.preturi_unitate a').ToolTip(
		{
			className: 'linksTooltip',
			position: 'mouse',
			delay: 200
		}
	);
	
}

function nextImage() 
{
	
	if (i == imaginile.length-1){
		step = -1;
	}
	
	if (i == 0){
		step = 1;
	}
	
	if(i<imaginile.length){
		i = i + step;
	}else if (i>0){
		i = i + step;
	}
	document.getElementById(imageId).src = imaginile[i];
	document.getElementById(containerId).style.backgroundImage = 'url('+imaginile[i-step]+')';
	initImage();
}

function fadeIn(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 150);
		}
	}
}
function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}


function hide_show(curentHead){
    		
	if(curentHead == 'vizibil'){
		
		SetCookie('veziHeader','vizibil',10);   
		document.getElementById('header').style.height = "138px";
		document.getElementById('header').style.backgroundImage = "url(http://www.all-seasons.ro/images/common/loader.gif)";
		document.getElementById('header').style.border = "2px solid #FFFFFF";
		document.getElementById('header').innerHTML = '<div id="header_close"><img src="http://www.all-seasons.ro/images/common/header_face.gif" alt="" usemap="#Map" /></div><img src="http://www.all-seasons.ro/images/common/header_image.jpg" alt="" id="thephoto" /><map name="Map" id="Map"><area shape="rect" coords="330,0,379,12" href="#" onclick="hide_show(\'invizibil\')"/></map>';
				
	}else{
		
		SetCookie('veziHeader','invizibil',10);
		document.getElementById('header').style.backgroundImage = "url('')";
		document.getElementById('header').style.height = "20px";
		document.getElementById('header').style.border = "0px";
		document.getElementById('header').innerHTML = '<img src="http://www.all-seasons.ro/images/common/header_image.jpg" alt="" id="thephoto" style=\"display:none;\"/><img src="http://www.all-seasons.ro/images/common/header_face_small.gif" alt="" border="0" usemap="#Map2" /><map name="Map2" id="Map2"><area shape="rect" coords="334,5,373,19" href="#" onclick="hide_show(\'vizibil\')"/></map>';
						
	}
	
}

function qDeschideImagine(imagine){
	win = window.open( "", "_blank", "resizable=0,HEIGHT=200,WIDTH=200"); 
	win.document.write("<html><head style=\"margin: 0; padding: 0;\"></head><body style=\"background: #0076D4 url(http://www.all-seasons.ro/images/common/loader.gif) center no-repeat; margin: 0; padding: 0;\">");
	obiect = new Image();
	obiect.src = imagine;
	obiect.onload = function(){
			win.document.write("<img src=\""+imagine+"\" alt=\"\" /></body></html>");
			latime = document.all?win.document.body.clientWidth:win.window.innerWidth; 
			inaltime = document.all?win.document.body.clientHeight:win.window.innerHeight;
			win.resizeBy((obiect.width - latime), (obiect.height - inaltime));
			win.focus();
	}
}

function qShowImage(imagine, latime, inaltime){
	window.open( "http://www.all-seasons.ro/files/show_image.php?img="+imagine+"", "_blank", "resizable=0,HEIGHT="+inaltime+",WIDTH="+latime+""); 
}

function qOpenFile(file, latime, inaltime){
	window.open( "http://www.all-seasons.ro/files/"+file+"", "_blank", "resizable=0,HEIGHT="+inaltime+",WIDTH="+latime+""); 
}

function verificaCifre(care){
 	care = (care) ? care : window.event;
    var charCode = (care.which) ? care.which : care.keyCode;
    if(charCode>31 && (charCode<48 || charCode>57)){ 
    	return false; 
    }else 
    	return true;
}


function DaysDiff(D1, D2) {
    return Math.round((D1 - D2) / 86400000);
}

function DiffDays(S1, S2) {
    var X = ReadISO8601date(S1);
    if (X < 0) {
        return "Date 1 bad";
    } 
    var Y = ReadISO8601date(S2);
    if (Y < 0) {
        return "Date 2 bad";
    }
    var Dx = Date.UTC(X[0], X[1] - 1, X[2]);
    var Dy = Date.UTC(Y[0], Y[1] - 1, Y[2]);
    return (Dx - Dy) / 86400000;
}

function calculeazaNopti(startID, stopID, nopti){
	
	var dataStart = document.getElementById(startID);
	var dataStop  = document.getElementById(stopID);
		
	if(dataStart.value == null || dataStop.value == null){
		
		document.getElementById(nopti).value = 0;
		
	}else{
		
		if(isNaN(DaysDiff(new Date(dataStop.value.replace(/-/g, "/")), new Date(dataStart.value.replace(/-/g, "/"))))){
	    	
	    	document.getElementById(nopti).value = 0;
	    	
	    }else{
		
			duratanopti = Math.abs(DaysDiff(new Date(dataStop.value.replace(/-/g, "/")), new Date(dataStart.value.replace(/-/g, "/"))));
			document.getElementById(nopti).value = duratanopti;
			$.post("getMinSejur.php", { checkIN: $("#check_in").val(), checkOUT: $("#check_out").val(), numarNopti: $("#nr_nopti").val() }, function (data) {
				/*aici o sa afisam mesajul*/
				if (data.length > 0) {
					 $("#minimSejur").fadeIn(400, function () {
						$("#minimSejur").html(data);
					  });
				}
			});	
	    }
        		
	}
	
}

function checkFields(){
	
	var mesaj = "";
	if(document.preFormRez.check_in.value == ''){
		mesaj += "Selecteaza data de check-in!\n";
	}
	if(document.preFormRez.check_out.value == ''){
		
		mesaj += "Selecteaza data de check-out!\n";
		
	}
	
	if(document.preFormRez.check_in.value != '' && document.preFormRez.check_out.value != '' && document.preFormRez.check_in.value >= document.preFormRez.check_out.value){
		
		mesaj += "Data de check-in nu poate fi egala sau mai mare cu data de check-out!\n";
		
	}
	
	if(mesaj != ''){
		
		alert(mesaj);
				
	}else{
		
		document.preFormRez.submit();
		
	}
	
}

var http_request = false;
function makePOSTRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
        http_request.overrideMimeType('text/xml');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  
  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents() {
  if (http_request.readyState == 4) {
     if (http_request.status == 200) {
        //alert(http_request.responseText);
        result = http_request.responseText;
        document.getElementById('newsletter-response').innerHTML = result;            
     } else {
        alert('There was a problem with the request.');
     }
  }
}

function get(obj) {
  var poststr = "nume=" + encodeURI( document.getElementById("nume").value ) +
                "&mail=" + encodeURI( document.getElementById("mail").value );
  makePOSTRequest('http://www.all-seasons.ro/newsletter.php', poststr);
}

function swicthHeader()
{
	if (currentState == true) {
		$('#header div, #header a').css('display', 'block');
		$('#header object').css('display', 'none');
		$('#header div').slideUp(400);
		currentState = false;
		SetCookie('headerState', 2, 30);
	} else {
		$('#header div').slideDown(
			400,
			function ()
			{
				$('#header div, #header a').css('display', 'none');
				$('#header object').css('display', 'block');
			}
		);
		currentState = true;
		SetCookie('headerState', 1, 30);
	}
}

function switchForms(oform)
{
	state = $('#' + oform).css('display');
	if (state && state == 'block') {
		$('#' + oform).slideUp(400);
		SetCookie(oform, 0, 30);
	} else {
		$('#' + oform).slideDown(400);
		SetCookie(oform, 1, 30);
	}
	
	return false;
}

function switchSearch(tab){
	if(tab != searchTabState){	
		$('#'+searchTabState+'-content').slideUp(
			300,
			function (){
				$('#'+tab+'-content').slideDown(300)				
			}
		)
		$('#'+searchTabState).removeClass('tab-activ');		
		$('#'+tab).addClass('tab-activ');
		searchTabState = tab;
		SetCookie('searchTabState', tab, 30);		
	}
}
function switchRezTab(tab){
	if(tab != rezTabState){	
		/*$('#'+rezTabState+'-content').slideUp(
			300,
			function (){
				$('#'+tab+'-content').slideDown(300)				
			}
		)*/
		$('#'+rezTabState+'-content, #'+tab+'-content').toggle();
		$('#'+rezTabState).removeClass('tab-activ');		
		$('#'+tab).addClass('tab-activ');
		rezTabState = tab;
		SetCookie('rezTabState', tab, 30);		
	}
}

var overlay = function(){
	var el;
	var message;
	return {
		build: function() {
			el = $('<div id="overlay" style="position: absolute; display: none; top: 0pt; left: 0pt;background: #000; z-index: 5000"></div>')
			.css('opacity', 0.6)
			.appendTo('body').bind('click', overlay.hide);
			message = $('<div id="message" style="position: absolute; left: 50%; top: 200px; margin-left: -200px; background: #fff; display: none; width: 400px; height: 100px;  line-height: 50px; font-size: 18px; z-index: 5002">Sectiune momentan indisponibila!<br />This section is unavailable for the moment!</div>')
			.appendTo('body').bind('click', overlay.hide);
			$('a.notAvailable').bind('click', overlay.show);
		},
		show: function() {
			el.css({
				width: document.body.offsetWidth + 'px',
				height: document.body.offsetHeight + 'px'
			}).show();
			message.show();
			window.scrollTo(0,0);
			return false;
		},
		hide: function() {
			el.hide();
			message.hide();
		}
	}
}();	

function showTableUnitPrices(UnitateId, lang) {
	tb_open_new('/tabelPreturiUnitate.php?modal=true&height=500&width=750&unitateID='+UnitateId+'&lang='+lang);
}
