/*  responsible for scrolling tabs */
function initTabs(size){
    	tmp = document.getElementById("tabs");
		tLeft = document.getElementById("tabsScrollLeft");
		tRight = document.getElementById("tabsScrollRight");		
			tmp.style.left = 0;
	    	tmp.style.clip = 'rect(auto auto auto auto)';
		    tabsLeft = 0;
			var tabsScroll = false;

    	if( size < tmp.scrollWidth || navigator.appName == 'Opera'){ 
    	    tmpSize = size - 40;
		    tmp.style.width= tmpSize.toString() + 'px';
		    tabsRight = size - 40;
            deactivateImage(false,'tabsScrollLeftImg');
			deactivateImage(true,'tabsScrollRightImg');
			tLeft.style.width='20px';
			tRight.style.width='20px';			
		} else {
    		tmp.style.width= size.toString() + 'px';
			tLeft.style.width='0px';
			tRight.style.width='0px';			
			//alert('zalozky se vejdou');
		}
		//alert(size + 'new clip ' + tmp.style.clip);
}
/*  responsible for scrolling tabs */
function scrollTabs(step){
  el = document.getElementById('tabs');
  if( (tabsLeft+step) < 0 ){
     tabsScroll=false;
     tLeft = document.getElementById("tabsScrollLeft");
     deactivateImage(true,'tabsScrollRightImg');
     return;
  } else {
     deactivateImage(false,'tabsScrollRightImg');
  }
  if( tabsRight >= el.scrollWidth && step>0 ){
     tabsScroll=false;
     deactivateImage(true,'tabsScrollLeftImg');     
     return;
  }else{
     deactivateImage(false,'tabsScrollLeftImg');       
  }
  tabsLeft += step;
  tabsRight += step;
  out = "rect(auto, " + tabsRight + "px, auto " + tabsLeft + "px)";

  el.style.clip = out;

  el.style.left = (-tabsLeft).toString() + 'px';
  el.style.width = tabsRight.toString() + 'px';
  if(tabsScroll){
     setTimeout("scrollTabs("+step+")", 1);
  }
}

function doResize() { // This do change height of iframe to be over all window, but dont overfow
     // http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
     if( typeof( window.innerWidth ) == 'number' ) {
         //Non-IE
          wW = window.innerWidth;
          wH = window.innerHeight;
          } 
     else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
          //IE 6+ in 'standards compliant mode'
          wW = document.documentElement.clientWidth;
          wH = document.documentElement.clientHeight;
          } 
     else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
          //IE 4 compatible + IE 6 in
          wW = document.body.clientWidth;
          wH = document.body.clientHeight;
          }

     // checks if its login page
     standardElements = 0; // elements of present page (header, footer, menu, tabs etc.)
     tmpHeaderSize = 0;
     tmpNavigationSize = 0;
     tmpMenuSize = 0;
     tmpTabsSize = 0;
     tmpInfoSize = 0;
     tmpMainFooterSize = 0;
     tmpFooterSize = 0;
     tmpSettingSize = 0;
     if(document.getElementById("header"))
	  tmpHeaderSize = headerSize;
     if(document.getElementById("navigation"))
	  tmpNavigationSize = navigationSize;
     if(document.getElementById("menu"))
	  tmpMenuSize = menuSize;
     if(document.getElementById("tabs")){
       if(document.getElementById("dialogLeft")){
		  tmpTabsSize = tabsSizeDialog;
       }else{
    	  tmpTabsSize = tabsSize;
       }
	 } 
     if(document.getElementById("info"))
	  tmpInfoSize = infoSize;
     if(document.getElementById("mainFooter"))
	  tmpMainFooterSize = mainFooterSize;
     if(document.getElementById("footer"))
	  tmpFooterSize = footerSize;
     if(document.getElementById("settingsNavigation"))
	  tmpSettingSize = settingSize;
	 standardElements = tmpHeaderSize + tmpNavigationSize + tmpMenuSize + tmpTabsSize + 
	   tmpInfoSize + tmpMainFooterSize + tmpFooterSize + tmpSettingSize;
     try{ 
     // alert("X/Y -> " + wW + "/ " +wH  ); alert("standardElements" +standardElements  );

     // height of desktop
     tmpHeight = wH - standardElements - heightOffset;  // alert("desktop height -> " +tmpHeight );
     document.getElementById("desktop").style.height=tmpHeight.toString()+'px';

     // height of dialog (settings)
     if(document.getElementById("dialogLeft")){
          tmpHeight =  tmpHeight - dialogOffset;   // alert("dialog  height " +  tmpHeight );
          document.getElementById("dialogLeft").style.height=  tmpHeight.toString() + 'px';
          document.getElementById("dialogRight").style.height=  tmpHeight.toString() + 'px';
     }

     //height of function block
     fW = 0;
     if( fDiv = document.getElementById("functions")) {
     //  alert ( "functions -  yes" );
          if( fDiv.className=="opened"){ // functions are opened
               fW = functionsOpened;
          } else {
               fW = functionsClosed;
          }

          tmpHeight =  tmpHeight + tmpTabsSize  + tmpMainFooterSize - functionsOffset; //tabs + mainFooter missing 
          document.getElementById("functions").style.height= tmpHeight.toString()+'px'; // alert("function height -> " + tmpHeight  );
        
          tmpHeight =  tmpHeight - functionsListOffset;
          document.getElementById("functionsList").style.height= tmpHeight.toString()+'px';  // alert("fList height -> " + tmpHeight );     
          } 
    else fw = 0;
    
    // width of desktop
    tmpWidth = wW - fW  -  widthOffset;
    document.getElementById("desktop").style.width= tmpWidth.toString()+'px';   // alert("desktop width -> " + tmpWidth);
	//info("desktopWidth: " + document.getElementById("desktop").style.width);

     // width of crossroadsPaneTable
     if(document.getElementById("crossroadsPaneTable")){
        if(tmpWidth > 700){
          crossroadsPaneWidth = 600;
        }else{
     	  crossroadsPaneWidth = tmpWidth - 100;
        }
        document.getElementById("crossroadsPaneTable").style.width= crossroadsPaneWidth.toString()+'px';    // alert("footer width -> " + tmpWidth);
        document.getElementById("crossroadsPaneTips").style.width= crossroadsPaneWidth.toString()+'px';    // alert("footer width -> " + tmpWidth);
    	//info("crossroadsPaneWidth: " + document.getElementById("crossroadsPaneTable").style.width);
     }

     // width of footer
     if(document.getElementById("mainFooter")){
          document.getElementById("mainFooter").style.width= tmpWidth.toString()+'px';    // alert("footer width -> " + tmpWidth);
     }

     // scrolovani tabu
     if(document.getElementById("tabs")){
          initTabs(tmpWidth);
          }

     var loc = window.location.toString();
     if(loc.indexOf("dummy#") != -1){
          var anchorName = loc.substring(loc.indexOf("dummy#") + 6);
          var targetDateField = document.getElementsByName (anchorName).item(0);
          targetDateField.scrollIntoView();
          focusFirstFieldDo = false;
          }

     }

catch(e){
     info( "Chyba " + e );
     }

}

/** change pics usualy with onMouseOver / onMouseOut
 all pics has its name, second pic has its name_p (as pasive)
*/
function switchImage(what){
    myImage=document.getElementById(what)
    pos1=myImage.src.lastIndexOf('/');
    base=myImage.src.substr(0,pos1+1);
    fullName=myImage.src.substr(pos1+1,myImage.src.length);
    name = fullName.substr(0,fullName.lastIndexOf('.'));
    ext = fullName.substr(fullName.lastIndexOf('.'));
    tmp = name.substr(name.length-2);
    if( tmp == "-o") {
      newImage = base + name.substr(0, name.length-2) + ext;
    } else if( tmp == "-d"){ // zustane stejny
      newImage = base + name + ext;
    }
    else {
      newImage = base + name + "-o" + ext;
    }
    //info("konec " + tmp + " nove " + newImage) ;
    myImage.src=newImage;
}
/** change to -d if image is normal or to normal if image is -d
@deactivate true to switch to -d
@what elementId
*/
function deactivateImage(deactivate , what){
    myImage=document.getElementById(what)
    pos1=myImage.src.lastIndexOf('/');
    base=myImage.src.substr(0,pos1+1);
    fullName=myImage.src.substr(pos1+1,myImage.src.length);
    name = fullName.substr(0,fullName.lastIndexOf('.'));
    ext = fullName.substr(fullName.lastIndexOf('.'));
    tmp = name.substr(name.length-2,1);
    if( tmp == "-") { // is not normal
	  name = name.substr(0, name.length-2);
	}
	if(deactivate){
	  name = name + "-d";
	}
    myImage.src = base + name + ext;
}

function forbidImageControl(what, whatElse){
    myImage=document.getElementById(what)
    if(myImage.src.indexOf("-d") != -1){
		return false;       
	}else{
		deactivateImage(true, what);
		if(arguments.length == 2)
			deactivateImage(true, whatElse);
		return true;
	}
}
/** change color of row selected by checkbox
from - element which called action
*/
function selectRow(whitch){
	tr = whitch.parentNode.parentNode;
	colorsId = tr.id.split('_');
	if( colorsId.length ==2 );
      unselectColor = colorsId[1];
    trNum = colorsId[0];
	if(whitch.checked){
		tr.id= trNum + '_' + tr.className ;
		tr.className="format3";
	}else {
		tr.className = unselectColor;
	}
}
/** change state to all checkboxes on document.data by one control checkbox */ 
function selectAllRows(caller){
	newStatus = caller.checked;
	elements = document.getElementsByTagName('input');
//	alert( elements.length + " " + elements);

	for( i = 1; i < elements.length ; i++){
		if( elements[i].type == 'checkbox'){
			if( elements[i] != caller ){
				if(elements[i].checked != newStatus){
					elements[i].checked = newStatus;
					selectRow(elements[i]);
				}
			}
		}
	}
//	alert('vybiram vsechny podle ' + caller + 'cilovy status ' + caller.checked);
}
/* change bg image for function closer needed for IE - like :hover pseudoclass*/
function hover(elName){
	el = document.getElementById(elName);
	if(el.className == "hover"){
		el.className = "";
	}else{
		el.className = "hover";
	}
}

/* ----------------- FUNKCE --------------- */
function initFunctions(){
  try{
	cook = document.cookie;
	cooks = cook.split(";");
	//alert(cook + "\n pocet:" + cooks.length);
	for(i=0 ; i < cooks.length; i++){
		id=cooks[i].split("=")[0].replace(" ","");
		value=cooks[i].split("=")[1].replace(" ","");
		if( el = document.getElementById(id)){
			//alert("nasle " + el);
			el.className=value;
			if(value == "itemsClosed"){
				el = document.getElementById('functionBlock_' + id) ;
				if(el){
    		        el.className = "functionBlockClosed";
				}
		    }
		}
	}
  }catch(e){
     info( "Chyba " + e );
  }
}
/* show/hide panel with functions
*/
function showFunctions(param){
	document.cookie
	el = document.getElementById('functions');
	//alert(el.className);

	if(el.className == "opened" || el.className == ""){
		el.className = "closed";
		setCookie("functions", "closed");
	}else{
		el.className = "opened";
		setCookie("functions", "opened");
	}
	info("sirka: " + el.style.width);
	doResize();
}
function showFunctionItems(item, kdo){
	el = document.getElementById(item);
	//alert(el.className);
	if( el.className == "items"){
		el.className = "itemsClosed";
		setCookie(item, "itemsClosed");
		kdo.className = "functionBlockClosed";
	} else{
		el.className = "items";
		setCookie(item, "items");
		kdo.className = "functionBlock";		
	}
}

