﻿/* 0
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* included in tattva\application\layout\layout.phtml */ 

function AddClass(Element,Clss){
    Element.className += ' ' + Clss;
}

function DelClass(Element,Clss){
    with (Element)
    {
        if (className.search(Clss) != -1) {
           className = className - Clss;
        }
    }
}

function ChangeClass(Element,Clss){
    Element.className = Clss;
}

function getElementPosition(elemId){
    var elem = typeof elemId == 'object' ? elemId : document.getElementById(elemId);
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
    var l = 0;
    var t = 0;
 
    while (elem){
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }
    return {
        "left":l, 
        "top":t, 
        "width": w, 
        "height":h
    };
}
// SM = SubMenu - позволяет скрыть или раскрыть меню при наведении, called from views\scripts\content\index.phtml
function SMShow(id){
    var E = document.getElementById('SM_' + id);
    if (E != null){
       E.style.cssText = 'display: block; '+
        'position: absolute; '+
        'background: url(../img/menu1px.png); '+
        'background-position: top left; '+
        'background-repeat: repeat-y; '+
        'width: 130px; '+
        'margin-left: 40px; '+
        'border: 1px solid #DCE; '+
        'box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3); '+
        '-webkit-box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3); '+
        '-moz-box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3); '+
        'padding: 1px 10px 1px 5px;';
    }
    /*E.style.display = 'block';
    E.style.position = 'absolute';
    E.style.background = 'url(../img/menutop_bg.png)';
    E.style.backgroundPosition = 'top left';
    E.style.backgroundRepeat = 'repeat-y';
    E.style.width = '140px';
    E.style.paddingLeft = '15px';
    E.style.marginLeft = '50px';
    E.style.border = '1px solid #DCE';
    E.style.boxShadow = '0px 0px 4px rgba(0, 0, 0, 0.2)';
    E.style.padding = '1px 1px 1px 3px';*/

}
function SMHide(id){
    var E = document.getElementById('SM_' + id);
    if (E != null){
      E.style.display = 'none';
      E.style.position = 'relative';
    }  
}
function HideElementStyle(IdName){
    var E = document.getElementById(IdName);
    if (E != null){
       E.style.display = 'none';
    }   
}
function ShowElementStyle(IdName){
    var E = document.getElementById(IdName);
    if (E != null){
        E.style.display = 'block';
    }    
}

function ChangeElementDisplayStyle(IdName){
    var E = document.getElementById(IdName);
    if (E != null){
       if (E.style.display == 'none') {E.style.display = 'block';}
       else {E.style.display = 'none';}
    }   
}
 
function HideElementStyleAll(IdNamePart,FromId,ToId){
    for (var i = FromId; i <= ToId; i++){
        HideElementStyle(IdNamePart+i);
    }
}

function ShowElementStyleAll(IdNamePart,FromId,ToId){
    for (var i = FromId; i <= ToId; i++){
        ShowElementStyle(IdNamePart+i);
    }    
} 

function ShowElementStyleWithHideAll(CurrId,IdNamePart,FromId,ToId){
    HideElementStyleAll(IdNamePart,FromId,ToId);
    ShowElementStyle(IdNamePart+CurrId);
}

function SchedDaySetStyleText(IdName,Txt,Clss){
    var E = document.getElementById(IdName);
    if (E != null){
    with (E)
    {
        innerHTML=Txt;
        className=Clss;
    }}
}

function SchedDaySetStyleTextDef(IdNamePart,FromId,ToId,DefTextArr,DefClass){
    for (var i = FromId; i <= ToId; i++){
        SchedDaySetStyleText(IdNamePart+i, DefTextArr[i-1], DefClass);}
    
}

function SchedTabClick(CurrIdName, CurrText, CurrClass, IdNamePart, FromId, ToId, DefTextArr, DefClass){
    SchedDaySetStyleTextDef(IdNamePart, FromId, ToId, DefTextArr, DefClass);
    SchedDaySetStyleText(CurrIdName, CurrText, CurrClass);
}


function rand( min, max ) {	// Generate a random integer
	// +   original by: Leslie Hoare
	if( max ) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	} else {
		return Math.floor(Math.random() * (min + 1));
	}
}

function changeSrc(ElemId,ImgSrc){
    var E = document.getElementById(ElemId);
    if (E != null){
        E.src = ImgSrc;
    }    
}

function printElemAtPos(ElemId,sText,withClass,withStyle){
    var E = document.getElementById(ElemId);
    if (E != null){
        if (withClass == ''){
           if (withStyle != ''){ 
                E.style.cssText = withStyle;
           }
        }
        else{
           E.className = withClass;
        }
        E.innerHTML = sText;
    }    
}

function dateDiffInDays(LeftDate,RightDate){   
    // задаём время с точностью до секунды — это не педантизм, а важная деталь, избавляющая от багов при вычислении разницы между датами
    //LeftDate = new Date(LeftDate);
    // а тут мы вычисляем, сколько же осталось дней — находим разницу в миллисекундах и переводим её в дни
    var nd = LeftDate > RightDate ? Math.ceil((LeftDate - RightDate) / (1000 * 60 * 60 * 24)) : null;
    return nd;
}

var smoothMoveTimeOutVar;
function smoothMove(ElemId,tmOut,iY,toY,stepY,iX,toX,stepX)
{
     var E = document.getElementById(ElemId);
     var doSet = false;
     if (E != null){ 
         if (typeof E.top == 'undefined') {E.top=iY};
         if (typeof E.left == 'undefined') {E.left=iX};
         if ((toY > E.top && stepY > 0) || (toY < E.top && stepY < 0)){
               doSet = true;
               E.top = E.top + stepY; 
               E.style.top = E.top + "px";
         }
         if ((toX > E.left && stepX > 0) || (toX < E.left && stepX < 0)){
              doSet = true;
              E.left = E.left + stepX; 
              E.style.left = E.left + "px";
         }
         if (doSet){
              clearTimeout(smoothMoveTimeOutVar);
              smoothMoveTimeOutVar = setTimeout('smoothMove(\''+ElemId+'\','+tmOut+','+iY+','+toY+','+stepY+','+iX+','+toX+','+stepX+')',tmOut);
         }
     }  
}

var jsBannerChangeTimeoutVar;
function jsBannerChange(CurrId,Banner,Desc,Link,Header){
    if (typeof CurrId ==  'undefined') return;
    var td = document.getElementById('jsbannertabletd');
    var div = document.getElementById('jsbannerdesc');
    var div2 = document.getElementById('jsbannerdescshadow');
    var id = document.getElementById(CurrId);
    //var lnk = document.getElementById('jsbannerhref');
    if (td != null){
        td.style.backgroundImage = "url('../img/jsbanners/"+Banner+"')";
    }
    if (div != null){
        div.innerHTML = "<a id='jsbannerhref' href="+Link+"><p id='jsbannerheader'>"+Header+'<hr style="margin: 0px 0px 5px 0px; padding: 0px;"/></p>'+Desc + "</a>";
    }
    if (div2 != null){
        div2.innerHTML = "<p id='jsbannerheadershadow'>"+Header+'<hr style="margin: 0px 0px 5px 0px; padding: 0px;"/></p>'+Desc;
        div = getElementPosition(div);
        div2.style.top = div.top + 1 + "px";
        div2.style.left = div.left + "px";
    }
    if (id != null){
        jsBannerTimerAvatarClearClass();
        ChangeClass(id, 'jsbanneravatarActive');
    }
    return;
}

var jsBannerArr = new Array();
var jsBanner_i = -1;
var jsBannerTimeoutValue;
var jsBannerAvatarId = new Array();

function jsBannerCreateArrays(MultiArr,AvatarId){
    window.jsBannerArr = MultiArr;
    window.jsBannerAvatarId = AvatarId;
}

function jsBannerTimer(){
    if (window.jsBanner_i >= window.jsBannerArr.length - 1) {window.jsBanner_i = -1};
    window.jsBanner_i += 1;
    var i = window.jsBanner_i;
    with (window.jsBannerArr[i]){jsBannerChange('jsba'+Id,Banner,Description,Link,Header);}
    clearTimeout(jsBannerChangeTimeoutVar);
    jsBannerChangeTimeoutVar = setTimeout(jsBannerTimer,jsBannerTimeoutValue);
}

function jsBannerTimerStart(tmOut){
    if (typeof jsBannerTimeoutValue == 'undefined') jsBannerTimeoutValue = tmOut;
    clearTimeout(jsBannerChangeTimeoutVar);
    jsBannerChangeTimeoutVar = setTimeout(jsBannerTimer,jsBannerTimeoutValue);
}

function jsBannerTimerStop(){
    clearTimeout(jsBannerChangeTimeoutVar);
}

function jsBannerTimerAvatarClearClass(){
     for (var i = 0; i <= jsBannerAvatarId.length-1; i++){
        ChangeClass(document.getElementById(jsBannerAvatarId[i]), 'jsbanneravatar');
    }
}    
