// pozice mysi
var x = 0;
var y = 0;

document.onmousemove = alertCoord;

function alertCoord(e) {
  var xcoord, ycoord;
  if( !e ) { e = window.event; }
  if( !e ) { return; }
  if( typeof( e.pageX ) == 'number' ) {
    xcoord = e.pageX;
    ycoord = e.pageY;
  } else if( typeof( e.clientX ) == 'number' ) {
    xcoord = e.clientX;
    ycoord = e.clientY;
    if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
      xcoord += document.body.scrollLeft;
      ycoord += document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
      xcoord += document.documentElement.scrollLeft;
      ycoord += document.documentElement.scrollTop;
    }
  //} else { return; }
  }

  x = xcoord;
  y = ycoord;
  
  // zmena pozicTabulky
  if (currenttable && currenttable.dragObject) {
      e   = e || window.event;
      var mousePos = currenttable.mouseCoords(e);
      var yOfset = mousePos.y - currenttable.mouseOffset.y;
      if (y != currenttable.oldY) {
          // work out if we're going up or down...
          var movingDown = yOfset > currenttable.oldY;
          // update the old value
          currenttable.oldY = yOfset;
          // update the style to show we're dragging
          currenttable.dragObject.style.backgroundColor = "red";
          // If we're over a row then move the dragged row to there so that the user sees the
          // effect dynamically
          var currentRow = currenttable.findDropTargetRow(yOfset);
          if (currentRow) {
              if (movingDown && currenttable.dragObject != currentRow) {
                  currenttable.dragObject.parentNode.insertBefore(currenttable.dragObject, currentRow.nextSibling);
              } else if (! movingDown && currenttable.dragObject != currentRow) {
                  currenttable.dragObject.parentNode.insertBefore(currenttable.dragObject, currentRow);
              }
          }
      }

      return false;
  }

  
}

/*
window.onload = init;

function init() {
  if(window.Event){
    document.captureEvents(Event.MOUSEMOVE);
  }
  document.onmousemove = getXY;
}

function getXY(e) {
  x = (window.Event) ? e.pageX : event.clientX;
  y = (window.Event) ? e.pageY : event.clientY;
}
*/
// konec pozice mysi


// univerzalni zobrazeni - skryti objektu
function sci_showelm(elm){
  if(document.getElementById(elm)){
    if(document.getElementById(elm).style.display == "none"){
      document.getElementById(elm).style.display = "";
    } else {
      document.getElementById(elm).style.display = "none";
    }
  }
}

// zaskrtnuti regionu z mapky
function checkreg(elm){
  if(document.getElementById('ch_' + elm)){
    if(document.getElementById('ch_' + elm).checked == true){
      document.getElementById('ch_' + elm).checked = false;
    } else {
      document.getElementById('ch_' + elm).checked = true;
    }
  }
}


// Benefit Program a Obsazenost
function bpshow(stav, recid){
  var editpos = 20;
  var ie = document.all?true:false;
  var opera = navigator.userAgent.indexOf('Opera') != -1 ? true : false;

  if(!editpos){editpos = 0}

  if((ie == true) && (opera == false)) {
    Y_pos=(y - editpos);
  } else {
    Y_pos=(y + 10 - editpos);
  }

  X_pos=(x - 310);
  if(X_pos < 0){
    X_pos = (x + 20);
  }

  document.getElementById('pbdivshow').style.top = Y_pos + 'px';
  document.getElementById('pbdivshow').style.left = X_pos + 'px';

  if(stav == "on"){

    document.getElementById('pbdivshow_info').innerHTML = document.getElementById(recid).innerHTML;

    document.getElementById('pbdivshow').style.display = "";
  } else {
    document.getElementById('pbdivshow').style.display = "none";
  }

}

// titulky obrazku
function title2script(stav, recid){
  var editpos = 20;
  var ie = document.all?true:false;
  var opera = navigator.userAgent.indexOf('Opera') != -1 ? true : false;

  if(!editpos){editpos = 0}

  if((ie == true) && (opera == false)) {
    Y_pos=(y - editpos);
  } else {
    Y_pos=(y + 10 - editpos);
  }
  X_pos=(x - 310);
  if(X_pos < 0){
    X_pos = (x + 20);
  }


  document.getElementById('pbdivshow').style.top = Y_pos + 'px';
  document.getElementById('pbdivshow').style.left = X_pos + 'px';

  if(stav == "on"){

    document.getElementById('pbdivshow_info').innerHTML = document.getElementById(recid).title;

    document.getElementById('pbdivshow').style.display = "";
  } else {
    document.getElementById('pbdivshow').style.display = "none";
  }

}


// strankovani v titulce regionu
function minikategoryregion(kategorie, oblast, stranka, lang, mesto){

  myDiv = document.getElementById('minikat' + kategorie);

  if (xmlHttp)
  {
    try
    {

      var params = "&kategorie=" + kategorie +
                   "&stranka=" + stranka +
                   "&oblast=" + oblast +
                   "&mesto=" + mesto +
                   "&lanG=" + lang +
                   "&subakce=minikat";
      xmlHttp.open('GET', 'http://' + document.location.host + '/redakce/ajax.php?akce=lbckraj' + params, true);
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(null);


    }
    catch (e)
    {
      alert("Can't connect to server 2:\n" + e.toString());
    }
  }

}

function ajaxshowrecord(record, lang){

  var ie = document.all?true:false;
  var opera = navigator.userAgent.indexOf('Opera') != -1 ? true : false;
  var verze = parseInt(navigator.appVersion);
  var vyska = 600;
  var odzhora = 50;

  if (document.documentElement && document.documentElement.scrollTop) {
    sScroll = document.documentElement.scrollTop;
  } else if (document.body) {
    sScroll = document.body.scrollTop;
  } else {
    sScroll = 0;
  }

  if(parseInt(navigator.appVersion)>3){
    if (navigator.appName=="Netscape") {
      sirkao = window.innerWidth;
    }
    if (navigator.appName.indexOf("Microsoft")!=-1) {
      sirkao = document.body.offsetWidth;
    }
  }

  if (verze >= 4){
    vyskao = screen.height;
    vyska = vyskao - 400;
  }
  var sirka = 550;
  var odleva = (sirkao - 550) / 2;

//  if((ie == true) && (opera == false)) {
    odzhora = (odzhora + sScroll);
//  }


  document.getElementById('divshowajax').style.top = odzhora + 'px';
  document.getElementById('divshowajax').style.left = odleva + 'px';
//  document.getElementById('divshowajax').style.height = vyska + 'px';
  document.getElementById('divshowajax').style.width = sirka + 'px';

  myDiv = document.getElementById('divshowajaxobs');

  if (xmlHttp)
  {
    try
    {

      var params = "&record=" + record +
                   "&lanG=" + lang +
                   "&subakce=showrecord";
      xmlHttp.open('GET', 'http://' + document.location.host + '/redakce/ajax.php?akce=scinfo' + params, true);
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(null);


    }
    catch (e)
    {
      alert("Can't connect to server 2:\n" + e.toString());
    }
  }

  document.getElementById('divshowajax').style.display = "";

}

var oldchange;
function changemapreg(changto){

  if(changto == 'sluknovsko'){
    var newimage = 'http://' + document.location.host + '/galerie/obrazky/image.php?img=63524&x=260&y=130';
    document.getElementById('a_' + changto).style.fontWeight = "bold";
    oldchange = 'a_' + changto;
  } else if(changto == 'ceske_svycarsko'){
    var newimage = 'http://' + document.location.host + '/galerie/obrazky/image.php?img=63517&x=260&y=130';
    document.getElementById('a_' + changto).style.fontWeight = "bold";
    oldchange = 'a_' + changto;
  } else if(changto == 'machuv_kraj'){
    var newimage = 'http://' + document.location.host + '/galerie/obrazky/image.php?img=63523&x=260&y=130';
    document.getElementById('a_' + changto).style.fontWeight = "bold";
    oldchange = 'a_' + changto;
  } else if(changto == 'luzicke_hory'){
    var newimage = 'http://' + document.location.host + '/galerie/obrazky/image.php?img=63522&x=260&y=130';
    document.getElementById('a_' + changto).style.fontWeight = "bold";
    oldchange = 'a_' + changto;
  } else if(changto == 'frydlantsko'){
    var newimage = 'http://' + document.location.host + '/galerie/obrazky/image.php?img=63519&x=260&y=130';
    document.getElementById('a_' + changto).style.fontWeight = "bold";
    oldchange = 'a_' + changto;
  } else if(changto == 'jizerske_hory'){
    var newimage = 'http://' + document.location.host + '/galerie/obrazky/image.php?img=63520&x=260&y=130';
    document.getElementById('a_' + changto).style.fontWeight = "bold";
    oldchange = 'a_' + changto;
  } else if(changto == 'cesky_raj'){
    var newimage = 'http://' + document.location.host + '/galerie/obrazky/image.php?img=63518&x=260&y=130';
    document.getElementById('a_' + changto).style.fontWeight = "bold";
    oldchange = 'a_' + changto;
  } else if(changto == 'krkonose'){
    var newimage = 'http://' + document.location.host + '/galerie/obrazky/image.php?img=63521&x=260&y=130';
    document.getElementById('a_' + changto).style.fontWeight = "bold";
    oldchange = 'a_' + changto;
  } else {
    var newimage = 'http://' + document.location.host + '/galerie/obrazky/image.php?img=63516&x=260&y=130';
    if(document.getElementById(oldchange)){
      document.getElementById(oldchange).style.fontWeight = "normal";
    }
    oldchange = '';
  }
  document.getElementById('mapa_regionu').src = newimage;

}



// calendar 

function bs_load_event_calendar(rok, mesic, den){
  
  var lang = document.getElementById('search_actualLang').value;
  var bsScUrl = "http://" + location.host + "/bs/events.php";
  
  if(rok == undefined){rok = "";}
  if(rok == undefined){mesic = "";}
  if(rok == undefined){den = "";}
  
  if(document.getElementById('calendarbody')){
    
    if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
  
      myDiv = document.getElementById('calendarbody');
      //myDiv.innerHTML = "Calendar load .... please wait";
  
      var params = "&lanG=" + lang +
                   "&bsThisDomain=" + document.location.host +
                   "&rok=" + rok +
                   "&mesic=" + mesic +
                   "&den=" + den +
                   "&type=sort" +
                   "&subakce=calendar";
  
      if (xmlHttp)
      {
        try
        {
  
          xmlHttp.open('GET', bsScUrl + '?akce=events' + params, true);
          xmlHttp.onreadystatechange = handleRequestStateChange;
          xmlHttp.send(null);
  
        }
        catch (e)
  
        {
          alert("Can't connect to server 2:\n" + e.toString());
        }
      }
  
    } else {
      setTimeout('bs_load_event_calendar()', 500);
    }
   }else {
     setTimeout('bs_load_event_calendar()', 500);
   }

}

function bs_load_events_short(rok, mesic, den){

  var lang = document.getElementById('search_actualLang').value;
  var bsScUrl = "http://" + location.host + "/bs/events.php";

  for(var i=1;i<32;i++){
    if(document.getElementById('bs_dic' + i)){
      document.getElementById('bs_dic' + i).className = 'den_vkalendari';
    } else {
      break;
    }
  }
  document.getElementById('bs_dic' + den).className = 'den_vkalendari vybrany_den';

  if(document.getElementById('eventsbody')){

    myDiv = document.getElementById('eventsbody');
    //myDiv.innerHTML = "Events load .... please wait";
    
    if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
      
  
      var params = "&lanG=" + lang +
                   "&bsThisDomain=" + document.location.host +
                   "&zvolenyRok=" + rok +
                   "&zvolenyMesic=" + mesic +
                   "&zvolenyDen=" + den +
                   "&subakce=eventssort";
  
      if (xmlHttp)
      {
        try
        {
  
          xmlHttp.open('GET', bsScUrl + '?akce=events' + params, true);
          xmlHttp.onreadystatechange = handleRequestStateChange;
          xmlHttp.send(null);
  
        }
        catch (e)
  
        {
          alert("Can't connect to server 2:\n" + e.toString());
        }
      }
  

    }

  }

}


function as4uSaveRowsShort(table){
  
  if(table.id == 'vmap_routepoints'){
    saveVmapTableRowsShort(table)
  }

}



// ajaxove okno


function as4uAjaxAlertShow(aaWidth, aaHeight){

  document.getElementById('ajax_transparent').style.display = '';
  document.getElementById('ajax_view').style.display = '';
  as4uAjaxAlertShowSize(aaWidth, aaHeight);

}


function as4uAjaxAlertShowSize(aaWidth, aaHeight){


  if(document.getElementById('ajax_transparent').style.display != 'none'){
    
  	if (window.innerHeight){
      wSizeH = window.innerHeight;
      wSizeW = window.innerWidth;
  	} else if (document.documentElement && document.documentElement.clientHeight){
  		wSizeH = document.documentElement.clientHeight;
  		wSizeW = document.documentElement.clientWidth;
  	} else if (document.body && document.body.clientHeight){
  		wSizeH = document.body.clientHeight;
  		wSizeW = document.body.clientWidth;
    }
    if(wSizeH == 0){
    } else {
      document.getElementById('ajax_transparent').style.height = wSizeH + 'px';
      document.getElementById('ajax_transparent').style.width = wSizeW + 'px';

      ajaxSizeObH = (aaHeight * 1) + 20;
      ajaxSizeObW = (aaWidth * 1);

      document.getElementById('ajax_view').style.height = ajaxSizeObH + 'px';
      document.getElementById('ajax_view').style.width = ajaxSizeObW + 'px';
      document.getElementById('ajax_view_close').style.width = ajaxSizeObW + 'px';

      ajaxSizeH = ajaxSizeObH - 20;
      ajaxSizeW = ajaxSizeObW;

      document.getElementById('ajax_content').style.height = ajaxSizeH + 'px';
      document.getElementById('ajax_content').style.width = ajaxSizeW + 'px';

      posAjaxH = ((wSizeH - ajaxSizeH) / 2);
      posAjaxW = ((wSizeW - ajaxSizeW) / 2);

      if (document.documentElement && document.documentElement.scrollTop) {
        winScroll = document.documentElement.scrollTop;
      } else if (document.body) {
        winScroll = document.body.scrollTop;
      } else {
        winScroll = 0;
      }
      
      document.getElementById('ajax_transparent').style.top = (winScroll) + 'px';
      
      document.getElementById('ajax_view').style.top = (winScroll + posAjaxH) + 'px';
      document.getElementById('ajax_view').style.left = (posAjaxW) + 'px';
      
      setTimeout('as4uAjaxAlertShowSize(' + aaWidth + ', ' + aaHeight + ')', 100);
      
    }
  
  }
}

function closeajax(){

  document.getElementById('ajax_transparent').style.display = 'none';
  document.getElementById('ajax_view').style.display = 'none';

}




// posun tabulky

var currenttable = null;

function as4uTableRowsChange(tableObj){
  var table = document.getElementById(tableObj.id);
  var tableDnD = new TableDnD();
  tableDnD.init(table);
}

document.onmouseup   = function(ev){
    if (currenttable && currenttable.dragObject) {
        var droppedRow = currenttable.dragObject;
        droppedRow.style.backgroundColor = 'transparent';
        currenttable.dragObject   = null;

        currenttable.onDrop(currenttable.table, droppedRow);
        currenttable = null; 
        
    }
}


function getEventSource(evt) {
    if (window.event) {
        evt = window.event; 
        return evt.srcElement;
    } else {
        return evt.target; 
    }
}

function TableDnD() {

    this.dragObject = null;
    this.mouseOffset = null;
    this.table = null;
    this.oldY = 0;


    this.init = function(table) {
        this.table = table;
        var rows = table.tBodies[0].rows; 
        for (var i=0; i<rows.length; i++) {
			var nodrag = rows[i].getAttribute("NoDrag")
			if (nodrag == null || nodrag == "undefined") { 
				this.makeDraggable(rows[i]);
			}
        }
    }

    this.onDrop = function(table, droppedRow) {
      
      as4uSaveRowsShort(table);

    }


    this.getPosition = function(e){
        var left = 0;
        var top  = 0;

		if (e.offsetHeight == 0) {

			e = e.firstChild; 
		}

        while (e.offsetParent){
            left += e.offsetLeft;
            top  += e.offsetTop;
            e     = e.offsetParent;
        }

        left += e.offsetLeft;
        top  += e.offsetTop;

        return {x:left, y:top};
    }


    this.mouseCoords = function(ev){
        if(ev.pageX || ev.pageY){
            return {x:ev.pageX, y:ev.pageY};
        }
        return {
            x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
            y:ev.clientY + document.body.scrollTop  - document.body.clientTop
        };
    }

    this.getMouseOffset = function(target, ev){
        ev = ev || window.event;

        var docPos    = this.getPosition(target);
        var mousePos  = this.mouseCoords(ev);
        return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
    }


    this.makeDraggable = function(item) {
        if(!item) return;
        var self = this; 
        item.onmousedown = function(ev) {
            var target = getEventSource(ev);
            if (target.tagName == 'INPUT' || target.tagName == 'SELECT') return true;
            currenttable = self;
            self.dragObject  = this;
            self.mouseOffset = self.getMouseOffset(this, ev);
            return false;
        }
        item.style.cursor = "move";
    }


    this.findDropTargetRow = function(y) {
        var rows = this.table.tBodies[0].rows;
		for (var i=0; i<rows.length; i++) {
			var row = rows[i];
			var nodrop = row.getAttribute("NoDrop");
			if (nodrop == null || nodrop == "undefined") { 
				var rowY    = this.getPosition(row).y;
				var rowHeight = parseInt(row.offsetHeight)/2;
				if (row.offsetHeight == 0) {
					rowY = this.getPosition(row.firstChild).y;
					rowHeight = parseInt(row.firstChild.offsetHeight)/2;
				}
				if ((y > rowY - rowHeight) && (y < (rowY + rowHeight))) {
					return row;
				}
			}
		}
		return null;
	}
}
  
