jQuery.blockUI.defaults.fadeIn = 0;
jQuery.blockUI.defaults.fadeInTime = 0;
jQuery.blockUI.defaults.fadeOut = 0;
jQuery.blockUI.defaults.fadeOutTime = 0;

var blockBtn = false;

/**
 * Tablica zawierająca nazwy oraz opisy wszystkich zdjęć oferty
 */
var photos = new Array();

//okresla czy mozna rezerwowac
var allow = true;

/**
 * Nr najechania myszki na tabele terminow
 */
var termOver = 0;

/**
 * Nr zjechania myszki z tabeli terminow
 */
var termOut = 0;

var isOverTerm = 0;

/**
 * Czas opóźnienia przed powrotem do wybranego menu, w milisekundach (1000ms == 1s)
 */
var termTimeOut = 200;

/**
 * ID wybranego terminu
 */
var selectedTerm = 0;

/**
 * Kolor wybranego terminu
 */
var selectedColor = '#DDDDDD';

var detailBoxActive = false;

function hoverTerms()
{
  var bgcolor1 = '#F8F6E7'; //light color
  var bgcolor2 = '#F3F1E2'; //dark color
  var bgcolor3 = '#E7E5D6'; //clicked color
  var bg = bgcolor1;

  jQuery(document).ready(function(){

      jQuery("tr.term").hover(
          function ()
          {
            if (!jQuery(this).is(".checked") && !jQuery(this).is(".nohover"))
            {
                jQuery("td",this).css("background-color","#FFFFF3");
            }
          },
          function ()
          {
            if (!jQuery(this).is(".checked") && !jQuery(this).is(".nohover"))
            {
              var rel = jQuery(this).attr('rel');
              if (rel%2) bg = bgcolor2;
              else bg = bgcolor1;
              jQuery("td",this).css("background-color",bg);
            }
          }
      );

      jQuery("tr.term").click( function ()
      {
          jQuery("tr").removeClass("checked");
          jQuery(this).addClass("checked");
          jQuery("tr").removeClass("clicked");
          jQuery(this).addClass("clicked");

          jQuery("tr.term").each(
            function(intIndex) {
                if (!jQuery(this).is(".clicked") && !jQuery(this).is(".nohover"))
                {
                  var rel = jQuery(this).attr('rel');
                  if (rel%2) bg = bgcolor2;
                  else bg = bgcolor1;
                  jQuery("td",this).css("background-color",bg);
                }
                else if(!jQuery(this).is(".nohover"))
                {
                  jQuery("td",this).css("background-color",bgcolor3);
                }
            }
          );

          jQuery("input:radio",this).attr("checked","checked");
          updateTermsForm();
          var scope = jQuery("tr.checked");
          var term_id = jQuery("input:radio",scope).val();
          if (detailBoxActive != jQuery(this).attr('rel'))
          {
            jQuery(".termDetailsBox").fadeOut(200);
          }
      });

      jQuery(".tableOffer img.details").click( function ()
      {
          var id = jQuery(this).attr('rel');
          var detailsBox = '#termDetailsBox' + id;
          detailBoxActive = id;
          var p = jQuery(this).parent().parent().position();
          jQuery('.termDetailsBox').hide();
          jQuery(detailsBox).css('top',p.top+1).css('left',p.left).fadeIn(400);
      });

      jQuery(".termDetailsBox img.close").click( function ()
      {
          jQuery(".termDetailsBox").fadeOut(200);
          detailBoxActive = false;
      });

  });
}

function updateTermsForm() {
      var scope = jQuery("tr.checked");
      jQuery("input[@name=id]").val(jQuery("td[@i]",scope).attr("i"));
      jQuery("input[@name=dni]").val(jQuery("td[@d]",scope).attr("d"));
      jQuery("input[@name=wyzywienie]").val(jQuery("td[@w]",scope).attr("w"));
      jQuery("input[@name=zakwaterowanie]").val(jQuery("td[@z]",scope).attr("z"));
      jQuery("input[@name=cena]").val(jQuery("td[@c]",scope).attr("c"));
      jQuery("#termsView input[@name=price_min]").val(jQuery("td[@c]",scope).attr("c"));
      jQuery("input[@name=cena2]").val(jQuery("td[@c2]",scope).attr("c2"));
      jQuery("input[@name=miasto]").val(jQuery("td[@m]",scope).attr("m"));
      jQuery("input[@name=termin]").val(jQuery("#selectTerms").val());


}

function updateTerms() {
  var term = jQuery("#selectTerms").val();
  var miasto = jQuery("#selectMiasto").val();
  var AjaxRequest = jQuery.ajax({
           type: "GET",
           url: "/rpc/wyjazd.rpc.php5",
           data: "termsView="+term+"&miasto="+miasto,
           beforeSend: function() {
             jQuery("div.RezButtons").block();
             jQuery(".tableWylot .loading div").show();
             blockBtn = true;
           },
           success: function(msg)
           {
              jQuery("#termsView").html(msg);
              var price_top = jQuery("#termsView input[@name=price_min]").val();
              if (price_top!="") jQuery("#price_top").html(price_top);
              hoverTerms();
              updateTermsForm();
              var scope = jQuery("tr.checked");
              var term_id = jQuery("input:radio",scope).val();
              jQuery("div.RezButtons").unblock();
              jQuery(".tableWylot .loading div").hide();
           }
  });
}

function submitRezerwacja()
{
    var url = jQuery("input[@name=hotelurl]").val();
    var oid = jQuery("input[@name=oid]").val();
    var act = "/wakacje/rezerwacja/" + url + "-" + oid + ".html";
    if (url && oid)
    {
      document.termForm.action = act;
      document.termForm.submit();
    }
}

function submitPytanie()
{
    var url = jQuery("input[@name=hotelurl]").val();
    var oid = jQuery("input[@name=oid]").val();
    var act = "/wakacje/pytanie/" + url + "-" + oid + ".html";
    if (url && oid)
    {
      document.termForm.action = act;
      document.termForm.submit();
    }
}

function updateTermDesc(id) {
    var AjaxRequest = jQuery.ajax({
           type: "GET",
           url: "/rpc/wyjazd.rpc.php5",
           data: "termDesc="+id,
           dataType: "json",
           beforeSend: function() {
             if (!blockBtn) jQuery("div.RezButtons").block();
           },
           success: function(j){

              if (j.bottom.length==0 || j.bottom[0].name=="" || j.bottom[0].value=="") {
                  jQuery("#termDescBottom").html('');
              } else {
                  var html = '<table class="tableContent" cellpadding="0" cellspacing="0">';
                  for (var i = 0; i < j.bottom.length; i++) {
                    html += '<tr><td class="left">'+j.bottom[i].name+'</td><td class="right">'+j.bottom[i].value+'</td></tr>';
                  }
                  html += '</table>';
                  jQuery("#termDescBottom").html(html);
              }

              if (j.top.length==0 || j.top[0].name=="" || j.top[0].value=="") {
                  jQuery("#termDescTop").html('');
              } else {
                  var html = '<table class="tableContent" cellpadding="0" cellspacing="0">';
                  for (var i = 0; i < j.top.length; i++) {
                    html += '<tr><td class="left">'+j.top[i].name+'</td><td class="right">'+j.top[i].value+'</td></tr>';
                  }
                  html += '</table>';
                  jQuery("#termDescTop").html(html);
              }
              jQuery("div.RezButtons").unblock();
           }
    });
}


function changePhoto(direction, photosid, oid, type, hid, name) {
	var imgobj = $('photo_'+photosid);	// element img htmla
	var pnum = $('pnum_'+photosid);		// span - zawiera nr zdjecia
	var pmax = $('pmax_'+photosid);		// span - zawiera liczbe zdjec
	var ptxt = $('ptxt_'+photosid);		// span - opis zdjecia
	var plink = $('plink_'+photosid);	// a - link zdjecia duzego
//	alert(pmax);
	if (imgobj && pnum && ptxt) {	// istnieja obiekty htmla odpowiadajace za zdjecia
		var L1 = 0;
		var L2 = 0;
		if (direction == 1) {	// nastepne zdjecie
			if (pnum.innerHTML < photos[photosid][2]) {	// dane zdjecie nie jest ostatnim zdjeciem
				L1 = pnum.innerHTML;
				L2 = eval(pnum.innerHTML)+1;
			}
			else {	// jest ostatnim zdjeciem - przechodzimy do pierwszego
				L1 = 0;
				L2 = 1;
			}
		}
		else {		// zdjecie poprzednie
			if (eval(pnum.innerHTML) > 1) {	// dane zdjecie nie jest pierwszym zdjeciem
				L1 = pnum.innerHTML-2;
				L2 = eval(pnum.innerHTML)-1;
			}
			else {	// to jest pierwsze zdjecie - przechodzimy do ostatniego
				L1 = eval(pmax.innerHTML)-1;
				L2 = pmax.innerHTML;
			}
		}
		imgobj.src = '/img/'+photos[photosid][0][L1]+'.jpg';
		imgobj.alt = photos[photosid][1][L1];
		imgobj.title = photos[photosid][1][L1];
		plink.onclick = function() {};	// przypisac pusta funkcje zeby nic sie nie dzialo po kliknieciu tylko zeby uruchomil sie link
        plink.href = 'javascript:openPhoto('+ photos[photosid][0][L1] +', '+ oid +', '+ type + ', ' + hid + ', "'+ name +'");';
        ptxt.innerHTML = photos[photosid][1][L1]+'&nbsp;';
		pnum.innerHTML = L2;
	}
}

/**
 * Metoda otwierajaca popup ze zdjeciem
 *
 * var pid int identyfikator zdjecia
 * var oid int identyfikator oferty
 * var type int type popupa: 1 - zdjęcia oferty + lokalizacje; 2 - zdjęcia wycieczek fakultatywnych
 * var hid int indetyfikator hotelu
 */
function openPhoto(pid, oid, type, hid, name) {

	if (hid!=0)
       var openurl = '/zdjecia/' + type + oid + '_' + hid + '_'+ pid + '_' + name +'.html';
    else
       var openurl = '/zdjecia/' + type + oid + '_0' + pid + '_' + name +'.html';

	var okno = window.open(openurl, null, 'height=200,width=400,status=yes,toolbar=no,menubar=no,location=no');
}

function cha(){
var plink = $('plink_1');
plink.href = '#';
}

/**
 *	Wyzwalacz pokazywania tabeli terminów
 */
function showterms() {
	termOver++;
	isOverTerm = 1;
	setTimeout('showtermsTrue('+ termOver +')', termTimeOut);
}

/**
 * Uruchamia wyzwalacz chowania terminow
 */
function hideterms() {
	termOut++;
	isOverTerm = 0;
	setTimeout('hidetermsTrue('+termOut+')', termTimeOut);
}

/**
 * Pokazuje wszystkie terminy po najechaniu myszką na pierwszy termin
 */
function showtermsTrue(overId) {
	if (overId == termOver && isOverTerm) {
		var termlines = $('hiddenlines');
		if (termlines) {
			if (selectedTerm != 0)
				lineOver($(selectedTerm), selectedColor);
			termlines.className = 'overshown';
		}
	}
}

/**
 * Ukrywa terminy do jednej linii po zjechaniu myszką z tabeli terminów
 */
function hidetermsTrue(outId, switched) {
	if (outId == termOut && !isOverTerm || switched) {
		var termlines = $('hiddenlines');
		if (termlines) {
			termlines.className = 'overhidden';
		}
	}
}

/**
 * "Podświetla" pozycję w tabeli terminów
 *
 * var that obiekt htmla, ktorego wszystkim divom zmieniamy kolor
 * var color kolor jaki ustawiamy wszystkim divom danego obiektu
 */
function lineOver(that, color) {
	if (that) {
		var divy = that.getElementsByTagName('div');
		for(var i=0; i<divy.length; i++){
			if (divy[i].style.zIndex != 100)
				divy[i].style.backgroundColor = color; //'#F9EFAD';
		}
	}
}

/**
 * "Gasi" pozycję w tabeli terminów
 *
 * var that obiekt htmla, ktorego wszystkim divom zmieniamy kolor
 * var color kolor jaki ustawiamy wszystkim divom danego obiektu
 */
function lineOut(that, color) {
	if (that) {
		var divy = that.getElementsByTagName('div');
		if (that.id != selectedTerm) {	// zmiana koloru tla niewybranego wiersza
			for(var i=0; i<divy.length; i++){
				if (divy[i].style.zIndex != 100) {
					if (color)
						divy[i].style.backgroundColor = color;
					else
						divy[i].style.backgroundColor = 'transparent';
				}
			}
		}
		else	// zmiana koloru tla dla wybranego wiersza
			for(var i=0; i<divy.length; i++) {
				if (divy[i].style.zIndex != 100)
					divy[i].style.backgroundColor = selectedColor;
			}
	}
}
/**
 * Wybiera konkretny termin (akcja onclick na tabeli terminow)
 *
 * @param object klikniety element html
 */
function switchterm(that,symbol) {  //zmiana EGO

    setSymbol(symbol); // zmiana EGO
    if (symbol==0) {
        jQuery("#rezerwacjaInfo").html("Termin sprzedany.<br>Brak możliwości rezerwacji.").show();
        allow = false;
        //jQuery(".rezerwacja").removeAttr("onclick");
        //jQuery(".pytanie").removeAttr("onclick");
    } else {
        jQuery("#rezerwacjaInfo").hide();
        allow = true;
        //jQuery(".rezerwacja").attr({ onclick:"rezerwuj();blur();", alt: "Rezerwacja" });
        //jQuery(".pytanie").attr({ onclick:"pytanie();blur();", alt: "Pytanie" });
        //jQuery(".rezerwacja").attr('onclick','rezerwuj();blur();');
        //jQuery(".pytanie").attr('onclick','pytanie();blur();');
    }

	var firstLine = $('term_0');				// pobranie obiektu pierwszego wiersza
	color = getLineColor(firstLine);			// pobiera kolor tla pierwszego wiersza
	if (firstLine)
		firstLine.innerHTML = that.innerHTML;		// zapisanie w pierwszym wierszu wartosci wyrbanego wiersza
	var divy = $$('otermtable','div');
	for(var i=0; i<divy.length; i++){
		if (divy[i].className == 'slectedTerm') {
			divy[i].className = 'termline';
		}
	}
	that.className = 'slectedTerm';				// podswietla wybrany termin
	lineOut(firstLine, color);					// "Gasi" pierwszy wiersz
	hidetermsTrue(0, 1);						// Chowa tabelę terminów do jednego wiersza
	isOverTerm = 0;								// zapisuje w zmiennej info o tym, ze kursor nie jest juz nad tabela
	if ($(selectedTerm)) 						// istnieje wiersz juz wczesniej wybrany
		lineOver($(selectedTerm), $(selectedTerm).lang);	// zmieniamy mu kolor tla

	if (that.lang == '#E6E6E6')					// sprawdzamy jaki jest kolor wybranego wiersza
		selectedColor = '#DDDDDD';				// i na tej podstawie okreslamy kolor tla wybranego wiersza (sprzedanego)
	else
		selectedColor = '#F3EFD5';				// lub inny kolor tla dla niewybranego wiersza
	selectedTerm = that.id;						// zapisujemy ID nowo wybranego wiersza

	lineOver(firstLine, that.lang);				// ustawienie koloru tla pierwszego wiersza na kolor tla wybranego wiersza
	if (firstLine) {
		firstLine.onmouseover = that.onmouseover;	// ustawienie zachowan pierwszego (behaviours) wiersza na zachowanie wybranego wiersza
		firstLine.onmouseout = that.onmouseout;		// ustawienie zachowan pierwszego (behaviours) wiersza na zachowanie wybranego wiersza
	}
}

/**
 * Pobiera kolor tla linii terminow
 */
function getLineColor(that) {
	if (that) {
		var divy = that.getElementsByTagName('div');
		for(var i=0; i<divy.length; i++){
			var bgc = divy[i].style.backgroundColor;
			if (bgc != '' && bgc != 'transparent')
				return bgc;
		}
	}
}


/**
 * Otwiera popup z informacji touroperatora takimi jak (warunki ubezpieczenia itp)
 */
function openTour(tid,type){ //type = pozycja pliku np umowa lub warunki //tid = toruoperator id
	var tour = window.open('/warunki/' + tid + '/' + type +'/', null, 'height=550,width=860,resizable=1,scrollbars=yes,toolbar=0');
}


