/***
 * javascript helper functions and classes for iPath
 *
 */


$(document).ready(function(){
  $("label.formlabel").append(":");
  
  // on window resize, send size to server and reset miage size
  $(window).resize( function(){
    _AJAX_setPageSize();
    setImageSize(); 
  });
  
});


// send page size to server
function _AJAX_setPageSize(){
  var w = _getScreenWidth();
  var h = _getScreenHeight();
  // alert( "window: "+w+"x"+h );
  
  $.ajax({ type: "POST",
	   url: "ajax.php",
	   data: "fn=setPageSize&w="+w+"&h="+h
  });
}
 

function _getScreenWidth(){
  if( window.innerWidth ){
    return window.innerWidth;
  }
  if( document.body.clientWidth ){
    return document.body.clientWidth;
  }
  if( document.documentElement.clientWidth ){
    return document.documentElement.clientWidth;
  }
  return false;
}

function _getScreenHeight(){
  if( window.innerHeight ){
    return window.innerHeight;
  }
  if( document.body.clientHeight ){
    return document.body.clientHeight;
  }
  if( document.documentElement.clientHeight ){
    return document.documentElement.clientHeight;
  }
  return false;
}

function _getScreenWidthX(x){
  return (Math.floor(_getScreenWidth() / x) * 50 ) - x;
}
function _getScreenHeightX(x){
  return (Math.floor(_getScreenHeight() / x) * 50 ) - x;
}

/**
* focus cursor on initial field
*/

/*
if( $('focussedfield') ){
  $('focussedfield').focus();
}
*/


function show_card( url ){

  var myX = window.screenX + 50;
  var myY = window.screenY + 100;
  var w = "dependent=yes,resize=yes,height=240,width=360,screenX="+myX+",screenY="+myY;
  user_w = open( url, "UserInfo", w );
}


function open_window( url, title, w, h ){

  var myX = window.screenX + 50;
  var myY = window.screenY + 100;

  var w = "dependent=yes,resize=yes,height="+h+",width="+w+",screenX="+myX+",screenY="+myY;
  parentWindow = window.open( url, title, w );
}




function set_lang(baseurl){
    var lang = document.getElementsByName("langsel")[0].value;
    
    var re = /\?/;
    var sep = "?";
    if( re.test(location) ){
	sep = "&";
    }
    var newurl = location + sep + "langset="+lang;

    location.replace( newurl );
}


function toggleHidden( id ){
    var toggle = document.getElementById("toggle"+id);
    var hidden = document.getElementById("hidden"+id);
    var nonhidden = document.getElementById("nonhidden"+id);

    if( hidden.style.display != "block" ){
	toggle.src = "images/arrow-down.png";
	if( hidden ){
	  hidden.style.display = "block"; }
	if( nonhidden ){
	  nonhidden.style.display = "none"; }
    } else {
	toggle.src = "images/arrow-right.png";
	if( hidden ){
	  hidden.style.display = "none"; }
	if( nonhidden ){
	  nonhidden.style.display = "block"; }
    }
}

	
function progressWindow( url ) {

  var myX = window.screenX + 150;
  var myY = window.screenY + 200;

  var opt = "dependent=yes,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,height=120,width=280,screenX="+myX+",screenY="+myY;
	
  theUniqueID = (new Date()).getTime() % 1000000000;
  window.open( url, theUniqueID , opt );

  document.uf.uniq.value = theUniqueID;
  document.uf.submit();  
}


function user_window(obj, uid){
  x = findPosX(obj);
  y = findPosY(obj);

  var w = $("#windowdiv");
  if( w ){
    if( y>50 ){
      w.css('top', (y-30)+"px");
    } else {
      w.css('top', "10px");
    }
    if( x>500 ){
      w.css('left', (x-340)+"px" );
    } else {
      w.css('left', (x+100)+"px" );
    }
    w.show();
    $("#windowdiv-body").append( " loading ... " );

    $.ajax({
  type: "GET",
	  url: "index.php",
	  data: "q=user/card/"+uid+"&plain=1",
	  success: function(msg){
    $("#windowdiv-body").html(msg);
  }
    });
    
  } else {
    alert("HH");
  }
}

function windowdiv_hide(){
  $("#windowdiv").hide();
}



function setImageSize(){
  if( $("#objectimage") ){
    var w = _getScreenWidth();
    var h = _getScreenHeight();

    var x = Math.floor((w-20)/50) * 50;
    var y = Math.floor((h-150)/50) * 50;

    var url = $("#objectimage").attr('src') + "&imagesize="+x+"x"+y;
    // alert( "src = "+url);
    $("#objectimage").attr('src', url);
  }
}




function findPosX(obj){
  var curleft = 0;
  if (obj.offsetParent)
    {
      while (obj.offsetParent)
	{
	  curleft += obj.offsetLeft
	    obj = obj.offsetParent;
	}
    }
  else if (obj.x)
    curleft += obj.x;
  return curleft;
}

function findPosY(obj){
  var curtop = 0;
  if (obj.offsetParent)
    {
      while (obj.offsetParent)
	{
	  curtop += obj.offsetTop
	    obj = obj.offsetParent;
	}
	}
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}



function gotoPage(url){
	window.location = url;
}

