///// common functions
function OpenNewSized(url, width, height) {  
  // alert(url);
  NWin = open (url, 'additional'+Math.floor(Math.random()*1000), 'menubar=0,location=0,status=0,toolbar=0,scrollbars=1,resizable=1,height='+height+',width='+width);
}

function getSelectIndex(obj,value) {
  for (i=0;i<obj.options.length;i++) {
     if (obj.options[i].value==value) {
        return(i);
     }
  } 
  return(-1);
}

Array.prototype.inArray = function ( search_phrase )
{
  for( var i = 0; i < this.length; i++ )
  {
    if( search_phrase == this[i] )
    {
      return i;
    }
  }
  return false;
}
/////////////// page coordinates

function getOffset(elem) {
    if ( (elem.getBoundingClientRect) ) {
        t=getOffsetRect(elem)
        // if (t.top+t.left>0) {
        if (elem.offsetWidth+elem.offsetHeight>0) {
            return t;
        } else {
            return getOffsetSum(elem)
        }
    } else {
        return getOffsetSum(elem)
    }
}

function getOffsetRect(elem) {
    // (1)
    var box = elem.getBoundingClientRect()
 
    // (2)
    var body = document.body
    var docElem = document.documentElement
 
    // (3)
    var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
    var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft
 
    // (4)
    var clientTop = docElem.clientTop || body.clientTop || 0
    var clientLeft = docElem.clientLeft || body.clientLeft || 0
 
    // (5)
    var top  = box.top +  scrollTop - clientTop
    var left = box.left + scrollLeft - clientLeft
 
    return { top: Math.round(top), left: Math.round(left) }
}

function getOffsetSum(elem) {
    var top=0, left=0
    while(elem) {
        top = top + parseInt(elem.offsetTop)
        left = left + parseInt(elem.offsetLeft)
        elem = elem.offsetParent        
    }
 
    return {top: top, left: left}
}

/////////////////////////////////////////////////

///// menu
function getViewPort() {
    // return {width: document.body.offsetWidth, height: document.body.offsetHeight}; 
    // return {width: document.body.clientWidth, height: document.body.clientHeight}; 
    return {width: document.body.scrollWidth, height: document.body.scrollHeight}; 
}
function moveToScreen(elem,oldViewPort,rpNode) {
    // get element box
    box = getOffset(elem);
    // get screen height,width
    // oldViewPort=getViewPort();
    view_width=oldViewPort.width;
    view_height=oldViewPort.height; 
    
    // element height
    el_height=elem.offsetHeight;
    el_width=elem.offsetWidth;

    if (box.top+el_height+10>=view_height) {
        // alert((elem.style.top*1)+"-"+(view_height-(box.top+el_height+10)));
        if (rpNode) {
            nbox=getOffset(rpNode);
            elem.style.top=nbox.top-el_height-20;
        } else {
            elem.style.top=parseInt(elem.style.top)+view_height-(box.top+el_height+20);
        }
        // alert(elem.style.top);
    }
    
    if (box.left+el_width>=view_width) {
        // alert((elem.style.top*1)+"-"+(view_height-(box.top+el_height+10)));
        // elem.style.left=parseInt(elem.style.left)+view_width-(box.left+el_width+20);
        elem.style.left=view_width-el_width-20;
        // alert(elem.style.top);
    }
    
    // alert('coord: '+elem.style.top+','+elem.style.left+'');
    // alert('viewPort: '+view_height+','+view_width+'');
    
    //
    
}

function moveToScreenBlock(elem,oldViewPort,rpNode) {

    if ((!document.getElementById('myDebug')) && (false)){
        novo = document.createElement("div");
        document.body.appendChild(novo);
        novo.id = "myDebug";
        novo.className = "popup";
        novo.style.position = "absolute";
        novo.style.top = 10;
        novo.style.left = 10;
        novo.style.display = "block";
        novo.style.width = 300;
        novo.innerHTML = '<DIV><A HREF="" onClick="document.getElementById(\'debugText\').innerHTML=\'\'; return(false);">Debug:</A></DIV><DIV ID="debugText"></DIV>';
    }




    // get element box
    box = getOffset(elem);
    // get screen height,width
    // oldViewPort=getViewPort();
    view_width=oldViewPort.width;
    view_height=oldViewPort.height; 
    
    // element height
    // el_height=elem.offsetHeight;
    // el_width=elem.offsetWidth;
    // elem.style.display="inline-block";
    el_height=elem.clientHeight;
    el_width=((elem.clientWidth>=0)?elem.clientWidth:parseInt(elem.style.width));
    
    
    // get parent info
        // height
        pel_h=elem.parentNode;
        while (pel_h.offsetHeight==0) {pel_h=pel_h.parentNode;}

        pbox_h=getOffset(pel_h);
        p_height=pel_h.offsetHeight;

        // wifdth
        pel_w=elem.parentNode;
        while (pel_w.offsetWidth==0) {pel_w=pel_w.parentNode;}

        p_width=pel_w.offsetWidth;
    
    
    if (document.getElementById('debugText')) {
        document.getElementById('debugText').innerHTML+='<DIV> in ID:'+elem.id+'('+vp.id+')['+elem.style.display+'], l:'+box.left+'/'+box.top+', w:'+el_width+' h:'+el_height+'</DIV>';
    }

    if (box.top+el_height+10>=view_height) {
        // alert((elem.style.top*1)+"-"+(view_height-(box.top+el_height+10)));
        if (!elem.style.top) {
            // alert('_'+elem.style.top+'_');
            elem.style.top=0;
        }
        
        if (    
               ( (box.left>=pbox_h.left) && (box.left>=pbox_h.left+p_width) )
               // ||
               // ( (box.left+el_width>=pbox_h.left) && (box.left+el_width>=pbox_h.left+p_width) )
           ) {
            // move over parent
            elem.style.top=parseInt(elem.style.top)-( (box.top+el_height+20) - (pbox_h.top+p_height) );
        } else {
            // move up to get to the viewPort
            elem.style.top=parseInt(elem.style.top)-( (box.top+el_height+20) - view_height );
        }
        // elem.style.top=parseInt(elem.style.top)+view_height-(box.top+el_height+p_height+20);
    }
    
    if (box.left+el_width>=view_width) {
        // alert((elem.style.top*1)+"-"+(view_height-(box.top+el_height+10)));
        // elem.style.left=parseInt(elem.style.left)+view_width-(box.left+el_width+20);
        if (!elem.style.left) {
            elem.style.left=0;
        }
        if (elem.style.left=='') {elem.style.left=0;}
        if (elem.style.left==' ') {elem.style.left=0;}
        elem.style.left=parseInt(elem.style.left)+(view_width-box.left-el_width-20);
        /*
        if (elem==document.getElementById('tm93')) {
            alert(elem.style.left);
        }
        */
        // elem.innerHTML+="left: "+elem.style.left;
        // alert(elem.style.left);
    }
    
    // alert('box: '+box.top+','+box.left+'');
    // alert('element: '+el_height+','+el_width+'');
    // alert('coord: '+elem.style.top+','+elem.style.left+'');
    // alert('viewPort: '+view_height+','+view_width+'');
    
    //
    
}


function switchDIVWindow(id) {

  if (document.getElementById(id)) {
    // if ( (!document.getElementById(id).style.display) || (document.getElementById(id).style.display=="none") ) {
    if ( (!document.getElementById(id+"_js")) ) {
         // document.getElementById(id).style.display="block";
        vp=getViewPort(); 
        box=getOffset(document.getElementById(id).parentNode);
        novo = document.createElement("div");
        document.body.appendChild(novo);
        novo.id = id+"_js";
        novo.className = document.getElementById(id).className;
        novo.style.position = "absolute";
        novo.style.top = box.top;
        novo.style.left = box.left;
        novo.style.display = "block";
        novo.style.width = document.getElementById(id).style.width;
        novo.innerHTML = document.getElementById(id).innerHTML;
        moveToScreen(novo,vp,document.getElementById(id).parentNode);
    } else {
         elem=document.getElementById(id+"_js");
         // elem.style.display="none";
         elem.parentNode.removeChild(elem);
    }
  } 
}

function switchDIVBlock(id) {
  if (document.getElementById(id)) {
    if ( (!document.getElementById(id).style.display) || (document.getElementById(id).style.display=="none") ) {
        vp=getViewPort(); 
        box=getOffset(document.getElementById(id).parentNode);
        document.getElementById(id).style.display="block";
        moveToScreenBlock(document.getElementById(id),vp);
    } else {
        document.getElementById(id).style.display="none";
    }
  } 
}

function switchDIV(id) {
  if ( (document.getElementById(id).className=="hiddenBlock1") && (false) ){
    switchDIVWindow(id);
  } else {
    switchDIVBlock(id)
  }
}


function show(id) {
  if (document.getElementById(id)) {
      if ( ((document.getElementById(id).className=="hiddenBlock1") || (document.getElementById(id).className=="popup") ) && (false) ){
      // if ( ((document.getElementById(id).className=="subMenu") || (document.getElementById(id).className=="subTMMenu") ) && (true) ){
        showWindow(id);
      } else {
        showBlock(id)
      }
  }
}

function hide(id) {
  if (document.getElementById(id)) {
      if ( ((document.getElementById(id).className=="hiddenBlock1") || (document.getElementById(id).className=="popup") ) && (false) ){
      // if ( ((document.getElementById(id).className=="subMenu") || (document.getElementById(id).className=="subTMMenu") ) && (true) ){
        hideWindow(id);
      } else {
        hideBlock(id)
      }
      if (document.getElementById('debugText')) {
        document.getElementById('debugText').innerHTML+='<DIV> out ID:'+id+'</DIV>';
      }
          // alert ('hide '+ID);
  }
}

function showBlock(ID) {
      if (document.getElementById(ID)) {
        vp=getViewPort(); 
        vp.id=ID;
        box=getOffset(document.getElementById(ID).parentNode);
        // document.getElementById(ID).style.visibility="visible";
        document.getElementById(ID).style.display="block";
        
        // show all parents
        tel=document.getElementById(ID).parentNode;
        while (tel.tagName!='BODY') {
            if (tel.style.display=='none') {
                tel.style.display='block';
            }
            tel=tel.parentNode;
        }
        
        moveToScreenBlock(document.getElementById(ID),vp);
      }
      // alert ('show '+ID);
}

function hideBlock(ID) {
      if (document.getElementById(ID)) {
              // document.getElementById(ID).style.visibility="hidden";
              document.getElementById(ID).style.display="none";
      }
      // alert ('hide '+ID);
}

function showWindow(id) {
    if ( (!document.getElementById(id+"_js")) ) {
         // document.getElementById(id).style.display="block";
        vp=getViewPort(); 
        box=getOffset(document.getElementById(id).parentNode);
        novo = document.createElement("div");
        document.body.appendChild(novo);
        novo.id = id+"_js";
        novo.className = document.getElementById(id).className;
        novo.style.position = "absolute";
        novo.style.top = box.top;
        novo.style.left = box.left;
        novo.style.display = "block";
        novo.style.width = document.getElementById(id).style.width;
        // novo.style.height = "14px";
        // novo.style.padding = "3px";
        // novo.onmouseover = function() { select(varPrefix,this,true); }
        // novo.onmouseout = function() { unselect(varPrefix,this,true); }
        // novo.onclick = function() { selectchoice(varPrefix,this); }
        novo.innerHTML = document.getElementById(id).innerHTML;
        moveToScreen(novo,vp,document.getElementById(id).parentNode);
    }      // alert ('show '+ID);
}

function hideWindow(id) {
    if ( (document.getElementById(id+"_js")) ) {
         elem=document.getElementById(id+"_js");
         // elem.style.display="none";
         elem.parentNode.removeChild(elem);
    }
      // alert ('hide '+ID);
}

function color(obj) {
       obj.bgColor='7EC892';
       if (!obj.childNodes[0].childNodes[0].style.oldColor) {
            obj.childNodes[0].childNodes[0].style.oldColor=obj.childNodes[0].childNodes[0].style.color;
       }
       obj.childNodes[0].childNodes[0].style.color='FFFFFF';
}

function uncolor(obj) {
       obj.bgColor='FFFFFF';
       if ( (obj.childNodes[0].childNodes[0]) ){
          obj.childNodes[0].childNodes[0].style.color=obj.childNodes[0].childNodes[0].style.oldColor;
       }
}



function switchSubmenu(id) {
        if (!document.getElementById(id))  {return;}
        var obj = document.getElementById(id);
        // alert(id+'.display='+obj.display);
        if (obj.style.display!='block') {
                document.getElementById(id).style.display='block';
        } else {
                document.getElementById(id).style.display='none';
        }
}

function show1(ID) {
      if (document.getElementById(ID)) {
              document.getElementById(ID).style.visibility="visible";
              document.getElementById(ID).style.display="";
      }
      // alert ('show '+ID);
}

function hide1(ID) {
      if (document.getElementById(ID)) {
              document.getElementById(ID).style.visibility="hidden";
              document.getElementById(ID).style.display="none";
              // alert(document.getElementById(ID).style.display);
      }
      // alert ('hide '+ID);
}








///////////////////////// music //////////////////////////////////

function getCookie (name) {
        var prefix = name + "=";
        var cookieStartIndex = document.cookie.indexOf(prefix);
        if (cookieStartIndex == -1)
                return null;
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length;
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function setCookie(name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "")
        if ((name + "=" + escape(value)).length <= 4000)
                document.cookie = curCookie
        else
                if (confirm("Cookie превышает 4KB и будет вырезан !"))
                        document.cookie = curCookie
}
var music_status='play';
function switchMusic() {
   // http://dotupdate.wordpress.com/2007/08/15/using-wmp-and-quicktime-with-silverlight/
   if (music_status=='play') {
        if ( (document.getElementById('WMPlayer')) && (document.getElementById('WMPlayer').controls)) {document.getElementById('WMPlayer').controls.pause();}
        document.getElementById('musPicture').src="/img/mus-off.gif";
        music_status='pause';
        setCookie('music_play',0,'','/');
   } else {
        if ((document.getElementById('WMPlayer')) && (document.getElementById('WMPlayer').controls)) {document.getElementById('WMPlayer').controls.play();}
        document.getElementById('musPicture').src="/img/mus-on.gif";
        music_status='play';
        setCookie('music_play',1,'','/');
   }
}










function viewObject(name)
{
var obj = eval(name), i;

if(!obj)
{
alert("\""+name+"\" ia not an object");
return;
}

var w_Test = open("","Test","width=600,height=500,scrollbars=1");

if(!w_Test)
{
alert("Cannot open window for "+name);
return;
}

w_Test.document.open();

for(i in obj)
w_Test.document.write(name+"."+i+"="+obj[i]+"<br>");

w_Test.document.close();
}

