﻿function rWin(url,type) {
  if(type=='booking') {
    window.open(url,'booking','width=640,height=480,menubar,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40,modal=1,dialog=1,dependent=1');
  } else if(type=='priceDetail') {
    window.open(url,'pricedetail','width=640,height=340,menubar,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40,modal=1,dialog=1,dependent=1');
  } else {
    window.open(url,'win','width=640,height=480,menubar,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40,modal=1,dialog=1,dependent=1');
  }
}

function otevrit(druh) {
	var wWidth = window.screen.availWidth;
	var wHeight = window.screen.availHeight;
  	var xpos, ypos = 0;
	var w = 750, h = 550;
  
  	if(wWidth > w) {
    	xpos = (wWidth - w)/2;
  	}
  	if(wHeight > h) {
    	ypos = (wHeight - h)/2;
  	}
window.open(druh + ".html", "_blank", "status=yes, top=0, left=0, width=475, height=485, status=no, top=" + ypos + ", left=" + xpos);
}

function getDateElementName (formName, whichDate) {
	var elementName = whichDate;
	return elementName;
}

function wday (d, m, y) {
	var tage = new Array('Ne','Po','Út','St','Čt','Pá','So');
	if (m < 3) { m = Number(m) + 12; y = Number(y) - 1; }
	w = (Number(y) + Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400) + Math.floor(2.6 * Number(m) + 1.6) + Number(d)) % 7;
	return tage[w];
}

function time (d, m, y) {
	var datum = new Date(y, m, d);
	return datum.getTime();
}

function initWday (form, which, selectNameSuffix) {
	with (document.forms[form]) {
		elements[getDateElementName(form,which+'dayw')+selectNameSuffix].value = wday(elements[getDateElementName(form,which+'day')+selectNameSuffix].value, elements[getDateElementName(form,which+'month')+selectNameSuffix].value, elements[getDateElementName(form,which+'year')+selectNameSuffix].value);
	}
}

function fixDay (y, indexOfMonth, d) {
	var m = indexOfMonth+1;

	if ((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12))	dMax = 31;
	else if ((m == 4) || (m == 6) || (m == 9) || (m == 11))	dMax = 30;
	else if (m == 2) { if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) d = 29; else dMax = 28;	}
	if (d>dMax) d=dMax;
	return d;
}

//TODO: problemy s posunem pri korekcich
function checkDate (form, selectNameSuffix, dateType) {		

	var formName = 'filterform';
	var prim = 'arrival', sec = 'departure';

			
	var goBackwardsAllowed = false;
	// ve scriptu py je dane do JS pole actualDate = ['day': '11', 'month': '0', 'year': '2007'] s aktualnim datumem podle serveru
	// TODO: vyzkouset, zda se ma actualDate['year'] zadavat v rozmezi 0-11 nebo 1-12
	// values of day fields: 1-31, values of month fields: 1-12
	// TODO pracovat s posunem mesic+1
	var startD = form.elements[getDateElementName(formName,prim+'_day')+selectNameSuffix].selectedIndex+1;
	var startM = form.elements[getDateElementName(formName,prim+'_month')+selectNameSuffix].selectedIndex;
	var startY = form.elements[getDateElementName(formName,prim+'_year')+selectNameSuffix].selectedIndex+actualDate['year'];

	var endD = form.elements[getDateElementName(formName,sec+'_day')+selectNameSuffix].selectedIndex+1;
	var endM = form.elements[getDateElementName(formName,sec+'_month')+selectNameSuffix].selectedIndex;
	var endY = form.elements[getDateElementName(formName,sec+'_year')+selectNameSuffix].selectedIndex+actualDate['year'];

	var start = time(startD, startM , startY);
	var ende = time(endD, endM, endY);
	var startDate = new Date(start);
	var endDate = new Date();
	var today = new Date(actualDate['year'], actualDate['month'], actualDate['day']);

	startD = fixDay(startY, startM, startD)
	endD = fixDay(endY, endM, endD)
                
	if (start < today) {
		if ((startM == today.getUTCMonth()) && (startY == today.getUTCFullYear())) {
			if (startM == 11) {
				startY++;
				startM = 0;
			} else {
				startM++;
			}
		} else {
			startY++;
		}
		start = time(startD, startM, startY);
	} 

	if (start >= ende) {
		if ('end' == dateType.substr(dateType.length-3, 3)) {

			if(goBackwardsAllowed == true){
				if (startY > endY){
					endY = startY;
				}
				if (((startY == endY) && (startM >= endM))){
					endM = startM;
					endD = startD+1;
				} else {
					endD++;
				}
				ende = time(endD, endM, endY);
			} else if (startM == endM) {	
				if (endM == 11) {
					endY++;
					endM = 0;
				} else {
					endM++;
				}
				ende = time(endD, endM, endY);
			}
		} else {
			ende = (60*60*26*1000) + start;
		}
	}

   	startDate.setTime(start);
   	endDate.setTime(ende);
				
	form.elements[getDateElementName(formName,prim+'_day')+selectNameSuffix].selectedIndex = startDate.getDate()-1;
	form.elements[getDateElementName(formName,prim+'_month')+selectNameSuffix].selectedIndex = startDate.getMonth();
	form.elements[getDateElementName(formName,prim+'_year')+selectNameSuffix].selectedIndex = startDate.getFullYear()-actualDate['year'];
	form.elements[getDateElementName(formName,sec+'_day')+selectNameSuffix].selectedIndex = endDate.getDate()-1;
	form.elements[getDateElementName(formName,sec+'_month')+selectNameSuffix].selectedIndex = endDate.getMonth();
	form.elements[getDateElementName(formName,sec+'_year')+selectNameSuffix].selectedIndex = endDate.getFullYear()-actualDate['year'];

	//initWday(formName, 'arrival_', selectNameSuffix);
	//initWday(formName, 'departure_', selectNameSuffix);

	return true;
}



var isDOM = (document.getElementById);
var isIE4 = (document.all && !isDOM);
var isNS4 = (document.layers);
var isDyn = (isDOM || isIE4 || isNS4);

function getRef(id) {
  if (isDOM) return document.getElementById(id);
  if (isIE4) return document.all[id];
  if (isNS4) return document.layers[id];
}

function getVP(x) {
  if (typeof x == 'undefined') {x = 'filterform';}
  return document.forms[x];
}

function htmlShow(obj,displ) {
  switch (displ) {
    case 'inline':
      obj.style.display='inline';
      break;
    case 'block':
      obj.style.display='block';
    default:
      obj.style.display='';
  }
}

function htmlHide(obj) {
  obj.style.display='none';
}

function addChildInputs(room) {
  var j = 1;
  for (var i = 0; i < CHILDREN_COUNT; i++) {
    obj = getRef('room' + room + '_children' + i);
    if (obj.style.display == 'none') {
      htmlShow(obj);
      break;
    }
  }
  showHideAddChild(room, i, CHILDREN_COUNT);
  return false;
}

function delChildInputs(room, k) {
  if (confirm('Are you sure to delete the selected child?')) {
    var p = getVP().elements['room' + room + '_children'];
    for (var j = k; j < CHILDREN_COUNT - 1; j++) {
      p[j].value = p[j + 1].value;
    }
    p[j].value = '';

    j = 0;
    for (var i = CHILDREN_COUNT - 1; i >= 0; i--) {
      obj = getRef('room' + room + '_children' + i);
      if (obj.style.display == '') {
        htmlHide(obj);
        break;
      }
      j++
    }
    showHideAddChild(room, j - 1, CHILDREN_COUNT);
  }
  return false;
}

function showHideAddChild(room, j, max_pocet_osob) {
  if (j == max_pocet_osob - 1) {
   htmlHide(getRef('room' + room + '_add_child')); 
  } else {
   htmlShow(getRef('room' + room + '_add_child'), 'inline');
  }
//  showHideZapati(); // fixace zobrazeni kvuli IE
}

function addRoom() {
  var j = 1;
  for (var i = 1; i < ROOMS_COUNT; i++) {
    obj = getRef('room' + i);
    if (obj.style.display == 'none') {
      htmlShow(obj);
      break;
    }
  }
  showHideAddRoom(i, ROOMS_COUNT);
  return false;
}

function delRoom(k) {
  r=getRef('room' + (k+1)).style.display;
  if(k || r!='none') {
    for (var j = k; j < ROOMS_COUNT - 1; j++) {
      p = getVP().elements['room' + j + '_children'];
      p1 = getVP().elements['room' + (j + 1) + '_children'];
      for (var i = 0; i < CHILDREN_COUNT; i++) {
        p[i].value = p1[i].value;
        p1[i].value = '';
        getRef('room' + j + '_children' + i).style.display = getRef('room' + (j + 1) + '_children' + i).style.display;
      }
      if (getRef('room' + j + '_children' + (CHILDREN_COUNT - 1)).style.display == 'none') showHideAddChild(j, 0, CHILDREN_COUNT);
      getRef('room' + j + '_adults').value = getRef('room' + (j + 1) + '_adults').value;
      getRef('room' + (j + 1) + '_adults').value = '';
    }

    j = 0;
    for (var i = ROOMS_COUNT - 1; i >= 0; i--) {
      obj = getRef('room' + i);
      if (obj.style.display == '') {
        p = getVP().elements['room' + i + '_children'];
        for (var x = 0; x < CHILDREN_COUNT; x++) {
          htmlHide(getRef('room' + i + '_children' + x));
        }
        showHideAddChild(i, 0, CHILDREN_COUNT);
        htmlHide(obj);
        break;
      }
      j++
    }
    showHideAddRoom(0, ROOMS_COUNT);
  }
  return false;
}

function showHideAddRoom(j, max_pocet_mistnosti) {
  if (j == max_pocet_mistnosti - 1) {
   htmlHide(getRef('room_add')); 
  } else {
   htmlShow(getRef('room_add'), 'inline');
  }
//  showHideZapati(); // fixace zobrazeni kvuli IE
}

/* insiders-select */

$(document).ready(function() {
						   
	$('.insiders-select').hover(function(){
		$('.insiders-select-div').css({'display':'block'});
	}, function(){
		$('.insiders-select-div').css({'display':'none'});
	})
});