function AddToLog(val)
{
    var divLog = document.getElementById("log");
    if (! divLog)
    {
        return;
    }
    divLog.innerHTML = val + "</br>" + divLog.innerHTML;
}
        
function FilterMouseOverLeaveEvent (evt)
{   

    var parentFromId = "";
    var parentToId = "";
    
    var fromElement = evt.fromElement;
    if (! fromElement) fromElement = evt.currentTarget;
    
    var toElement = evt.toElement;
    if (! toElement) toElement = evt.relatedTarget;    
    
    if (fromElement != null && fromElement.parentElement != null)
    {
        parentFromId = fromElement.parentElement.id;
    }
    if (fromElement != null && fromElement.parentNode != null)
    {
        parentFromId = fromElement.parentNode.id;
    }
    
    if (toElement != null && toElement.parentElement  != null)
    {
        parentToId = toElement.parentElement.id;
    }
    if (toElement != null && toElement.parentNode  != null)
    {
        parentToId = toElement.parentNode.id;
    }
    
    if (parentFromId != "" && parentFromId == toElement.id) 
    {
        return true;
    }
    if (parentToId != "" && parentToId == fromElement.id) 
    {
        return true;
    }
    
    if (parentToId != "" && parentToId == parentFromId) 
    {
        return true;
    }
    
    if (fromElement.id != "" && fromElement.id == toElement.id) 
    {
        return true;
    }    
    
    return false;
}

function HasParent (obj, parentID)
{
    for (;obj != null;)
    {
        if (obj.id == parentID)
        {
            return true;
        }        
        
        obj = GetParent(obj);        
    }
    return false;
}

function GetParent (obj)
{
    if (obj != null && obj.parentElement != null)
    {
        return obj.parentElement;
    }
    if (obj != null && obj.parentNode != null)
    {
        return obj.parentNode;
    }
    return null;
}

function FilterMouseOverLeaveEvent (evt, commonParentID)
{   
    var fromElement = evt.fromElement;
    if (! fromElement) fromElement = evt.currentTarget;
    
    var toElement = evt.toElement;
    if (! toElement) toElement = evt.relatedTarget;    
    
    if (HasParent (fromElement, commonParentID) && HasParent (toElement, commonParentID))
    {   
        return true;
    }
    
    return false;
    
}

/*function FilterMouseOverLeaveEvent (evt, commonParent)
{   

    var parentFromId = "";
    var parentToId = "";
    
    var fromElement = evt.fromElement;
    if (! fromElement) fromElement = evt.currentTarget;
    
    var toElement = evt.toElement;
    if (! toElement) toElement = evt.relatedTarget;    
    
    if (fromElement != null && fromElement.parentElement != null)
    {
        parentFromId = fromElement.parentElement.id;
    }
    if (fromElement != null && fromElement.parentNode != null)
    {
        parentFromId = fromElement.parentNode.id;
    }
    
    
    if (toElement != null && toElement.parentElement  != null)
    {
        parentToId = toElement.parentElement.id;
    }
    if (toElement != null && toElement.parentNode  != null)
    {
        parentToId = toElement.parentNode.id;
    }
    
    if (parentFromId != "" && parentFromId == toElement.id) 
    {
        return true;
    }
    if (parentToId != "" && parentToId == fromElement.id) 
    {
        return true;
    }
    
    if (parentToId != "" && parentToId == parentFromId) 
    {
        return true;
    }
    
    if (fromElement.id != "" && fromElement.id == toElement.id) 
    {
        return true;
    }    
    
    return false;
}
*/        

function textLinkMouseHandler(evt, objID, arrowID,from,to,imgFrom,imgTo,count,delay)
{        
    ChangeWidth(objID,arrowID,from,to,imgFrom,imgTo,count*1.0,delay)
    //ChangeWidth (objID, arrowID, 0, 100, -3, 0, 10.0, 10);    
}

function ChangeWidth(objID,arrowID,from,to,imgFrom,imgTo,count,delay)
{
    var obj = document.getElementById(objID);
    var img = document.getElementById(arrowID);
    var delta=(to-from)/(count * 1.0);
    var newfrom=from+delta;
    var newcount=count-1;

    var imgDelta=(imgTo-imgFrom)/count;
    var newImgFrom=imgFrom+imgDelta;

    obj.style.width=from + "%";
    img.style.left=imgFrom + "px";

    if (count==0)
    {
        return;
    }

    var command="ChangeWidth('"+objID+"','"+arrowID+"',"+newfrom+","+to+","+newImgFrom+","+imgTo+","+newcount+","+ delay+")";
    window.setTimeout(command, delay);
    
}

function imgLinkFrameMouseEnter(evt, parentWidth, parentHeight, topID, bottomID, leftID, rightID, width, offset, count, delay) 
{
    var top=document.getElementById(topID); 
    var parent = GetParent(top);
    if (null != parent)
    {
        if (FilterMouseOverLeaveEvent (evt, parent.id)) return;
    }     
    
    var bottom=document.getElementById(bottomID);
    var left=document.getElementById(leftID);
    var right=document.getElementById(rightID);    

    top.style.display='block';
    bottom.style.display='block';
    left.style.display='block';
    right.style.display='block';

    ChangeFrameWidth(parentWidth, parentHeight, topID, bottomID, leftID, rightID,width,offset + width,offset, count*1.0, delay);
}

function imgLinkFrameMouseLeave(evt, topID, bottomID, leftID, rightID) 
{
    var top=document.getElementById(topID); 
    var parent = GetParent(top);
    if (null != parent)
    {
        if (FilterMouseOverLeaveEvent (evt, parent.id)) return;
    }
    
    var bottom=document.getElementById(bottomID);
    var left=document.getElementById(leftID);
    var right=document.getElementById(rightID);   
    top.style.display='none';
    bottom.style.display='none';
    left.style.display='none';
    right.style.display='none';
}

function arrangeImgLinkFrame(parentWidth, parentHeight, top, bottom, left, right, width, offset) 
{
    top.style.top=offset+"px";
    top.style.height=width+"px";
    top.style.width=(parentWidth - 2*offset)+"px";

    bottom.style.top=offset+"px";
    bottom.style.height=width+"px";
    bottom.style.width=(parentWidth - 2*offset)+"px";

    left.style.top=offset+"px";
    left.style.left=offset+"px";
    left.style.width=width+"px";
    left.style.height=(parentHeight - 2*(offset+width))+"px";

    right.style.top=offset+"px";
    right.style.width=width+"px";
    right.style.height=(parentHeight - 2*(offset+width))+"px";
    right.style.left=-offset+"px";
}


function ChangeFrameWidth(parentWidth, parentHeight, topID, bottomID, leftID, rightID,width,from,to,count,delay)
{       
    //alert("to=" + to + " from=" + from);
    var delta=(to-from)/(count * 1.0);
    var newfrom=from+delta;
    var newcount=count-1;
    
    var top=document.getElementById(topID); 
    var bottom=document.getElementById(bottomID);
    var left=document.getElementById(leftID);
    var right=document.getElementById(rightID);   

    arrangeImgLinkFrame(parentWidth, parentHeight, top, bottom, left, right, width, Math.round(from));

    if (count==0)
    {
        arrangeImgLinkFrame(parentWidth, parentHeight, top, bottom, left, right, width, to);
        return;
    }
                                                                                                                           
    var command="ChangeFrameWidth("+parentWidth+","+parentHeight+",'"+topID+"','"+bottomID+"','"+leftID+"','"+rightID+"',"+width+","+newfrom+","+to+","+newcount+","+ delay+")";
    window.setTimeout(command, delay);    
}

function MoveBackgroundNocycle(cellID, width, height, posX, finalPosX, posY, finalPosY, count, delay)
{
    var cell=document.getElementById(cellID); 
    
    var deltaX=(finalPosX-posX);
    posX=posX+(deltaX/(count * 1.0));
        
    var deltaY=(finalPosY-posY);
    posY=posY+(deltaY/(count * 1.0));
    
    var count=count-1;
    cell.style.backgroundPosition = posX + "px " + posY + "px";
    if (count==0)
    {
        cell.style.backgroundPosition = finalPosX + "px " + finalPosY + "px";
        return;
    }
                                                                                                                           
    var command="MoveBackgroundNocycle('"+cellID+"',"+width+","+height+","+posX+","+finalPosX+","+posY+","+finalPosY+","+count+","+delay+")";
    window.setTimeout(command, delay);    
}

function MoveBackground(cellID, width, height, posX, finalPosX, posY, finalPosY, count, delay)
{
    var cell=document.getElementById(cellID); 
    
    var deltaX=(finalPosX-posX);
    if (deltaX <= 0) deltaX += width;
    posX=posX+(deltaX/(count * 1.0));
    if (posX>=width) posX-=width;
    
    var deltaY=(finalPosY-posY);
    if (deltaY <= 0) deltaY += height;    
    posY=posY+(deltaY/(count * 1.0));
    if (posY>=height) posY-=height;    
    
    var count=count-1;
    cell.style.backgroundPosition = posX + "px " + posY + "px";
    if (count==0)
    {
        cell.style.backgroundPosition = finalPosX + "px " + finalPosY + "px";
        return;
    }
                                                                                                                           
    var command="MoveBackground('"+cellID+"',"+width+","+height+","+posX+","+finalPosX+","+posY+","+finalPosY+","+count+","+delay+")";
    window.setTimeout(command, delay);    
}

function ChangeFontSize(objID,from,to,suffix,count,delay)
{
    var obj = document.getElementById(objID);
    
    var delta=(to-from)/(count * 1.0);
    var newfrom=from+delta;
    var newcount=count-1;

    obj.style.fontSize=from + suffix;    

    if (count==0)
    {
        obj.style.fontSize=to + suffix;    
        return;
    }

    var command="ChangeFontSize('"+objID+"',"+newfrom+","+to+",'"+suffix+"',"+newcount+","+ delay+")";
    window.setTimeout(command, delay);    
}

function curtain (curtainId,
                  tdArrowId, imgArrowId, arrowWidth,
                  firstTopId, firstMiddleId, firstBottomId,
                  secondTopId, secondMiddleId, secondBottomId,
                  thridTopId, thridMiddleId, thridBottomId, 
                  titleId, titleTextId, titleUnderlineId, titleHeight, underlineOffset,
                  count1,delay1,
                  count2,delay2,
                  count3,delay3,
                  count4,delay4,
                  count5,delay5)
    {
        this.param=null;
        this.oldSectionId=null;
        this.curtainId = curtainId;
        this.tdArrowId = tdArrowId; 
        this.imgArrowId = imgArrowId;
        this.arrowWidth = arrowWidth;
        this.firstTopId = firstTopId; 
        this.firstMiddleId = firstMiddleId; 
        this.firstBottomId = firstBottomId;
        this.secondTopId = secondTopId; 
        this.secondMiddleId = secondMiddleId; 
        this.secondBottomId = secondBottomId;
        this.thridTopId = thridTopId; 
        this.thridMiddleId  = thridMiddleId;
        this.thridBottomId = thridBottomId;
        this.titleId = titleId; 
        this.titleTextId = titleTextId; 
        this.titleUnderlineId = titleUnderlineId;
        this.underlineOffset = underlineOffset;
        this.titleHeight = titleHeight;
        this.count1 = count1;
        this.delay1 = delay1;
        this.count2 = count2;
        this.delay2 = delay2;
        this.count3 = count3;
        this.delay3 = delay3;
        this.count4 = count4;
        this.delay4 = delay4;
        this.count5 = count5;
        this.delay5 = delay5;
        
        
        this.curtain_step1 = curtain_step1;
        this.curtain_step2 = curtain_step2;
        this.curtain_step3 = curtain_step3;
        this.curtain_step4 = curtain_step4;
        this.curtain_step5 = curtain_step5;
    }

//
// ##Выезд шторки
//    
function curtain_step1 (var_name, from,to,suffix,count,delay)
{
    var firstTop = document.getElementById(this.firstTopId);
    var firstMiddle = document.getElementById(this.firstMiddleId);
    var firstBottom = document.getElementById(this.firstBottomId);
    var secondTop = document.getElementById(this.secondTopId);
    var secondMiddle = document.getElementById(this.secondMiddleId);
    var secondBottom = document.getElementById(this.secondBottomId);
    var thridTop = document.getElementById(this.thridTopId);
    var thridMiddle = document.getElementById(this.thridMiddleId);
    var thridBottom = document.getElementById(this.thridBottomId);
    firstTop.style.height= "50%";
    firstTop.style.display="";
    firstMiddle.style.height= "0%";
    firstMiddle.style.display= "none";
    firstBottom.style.height= "50%";
    firstBottom.style.display="";
    
    secondTop.style.height= "50%";
    secondTop.style.display="";
    secondMiddle.style.height= "0%";
    secondMiddle.style.display= "none";
    secondBottom.style.height= "50%";
    secondBottom.style.display="";
    
    thridTop.style.height= "50%";
    thridTop.style.display="";
    thridMiddle.style.height= "0%";
    thridMiddle.style.display= "none";
    thridBottom.style.height= "50%";
    thridBottom.style.display="";
    
    var title = document.getElementById(this.titleId);
    var titleUnderline = document.getElementById(this.titleUnderlineId);
    title.style.display= "none"; 
    
    var obj = document.getElementById(this.curtainId);    
    var delta=(to-from)/(count * 1.0);
    var newfrom=from+delta;
    var newcount=count-1;

    var imgObj = document.getElementById(this.imgArrowId);
    var tdImgObj = document.getElementById(this.tdArrowId);
    imgObj.style.visibility="visible";
    
    imgObj.style.width=this.arrowWidth + "px";
    
    imgObj.style.display="";
    tdImgObj.style.display="";
    tdImgObj.style.width=this.arrowWidth + "px";
    obj.style.visibility="visible";
        
    obj.style.width=from + suffix;
    obj.width=from + suffix;
    if (count==0)
    {
        obj.style.width=to + suffix;
        obj.width=to + suffix;
        this.curtain_step2 (var_name, this.arrowWidth,0,"px",this.count2,this.delay2);
        
        return;
    }

    var command=var_name+".curtain_step1('"+var_name+"',"+newfrom+","+to+",'"+suffix+"',"+newcount+","+ delay+")";
    window.setTimeout(command, delay);    
}    

//
// ##Сжатие стрелки
//
function curtain_step2 (var_name,from,to,suffix,count,delay)
{
    var obj = document.getElementById(this.curtainId);
    var imgObj = document.getElementById(this.imgArrowId);
    var tdImgObj = document.getElementById(this.tdArrowId);
    var delta=(to-from)/(count * 1.0);
    var newfrom=from+delta;
    var newcount=count-1;

    imgObj.style.display="";
    tdImgObj.style.display="";    
    
    imgObj.style.width=from + suffix;
    tdImgObj.style.width=from + suffix;
    imgObj.width = from + suffix;
    //if (tdImgObj.width != null) tdImgObj.width = from + suffix;
    
    if (count==0)
    {
        imgObj.style.width=to + suffix;
        tdImgObj.style.width = to + suffix;
        
        if (navigator.userAgent.indexOf("Opera")!=-1)        
        {
            imgObj.style.display="none";        
            tdImgObj.style.display="none";       
        }   
        
        var secondTop = document.getElementById(this.secondTopId);
        var secondMiddle = document.getElementById(this.secondMiddleId);
        var secondBottom = document.getElementById(this.secondBottomId);
        var title = document.getElementById(this.titleId);
        var titleUnderline = document.getElementById(this.titleUnderlineId);
        
        title.style.display="";
        titleUnderline.style.top = (-this.titleHeight + this.underlineOffset) + "px";
        secondMiddle.style.display="none";
        secondTop.style.height="40%";
        secondBottom.style.height="60%";
    
        this.curtain_step3 (var_name, (-this.titleHeight + this.underlineOffset),this.underlineOffset,this.count3,this.delay3);
        
        return;
    }

    var command=var_name+".curtain_step2('"+var_name+"',"+newfrom+","+to+",'"+suffix+"',"+newcount+","+ delay+")";
    window.setTimeout(command, delay);    
}

//
// ##Вывод заголовка
//
function curtain_step3 (var_name,from,to,count,delay)
{
    var titleUnderline = document.getElementById(this.titleUnderlineId);
    var titleText = document.getElementById(this.titleTextId);
    titleText.innerHTML = this.param.title;
    
    var delta=(to-from)/(count * 1.0);
    var newfrom=from+delta;
    var newcount=count-1;
    
    titleUnderline.style.top = from + "px";
  
    if (count==0)
    {
        titleUnderline.style.top = to + "px";
        
        //var command=var_name+".curtain_step4('"+var_name+"',50,0,"+this.count4+","+ this.delay4+")";
        //window.setTimeout(command, this.delay4 * this.count4);
    
        this.curtain_step4 (var_name,50,0,this.count4,this.delay4);        
        
        return;
           
    }
    var command=var_name+".curtain_step3('"+var_name+"',"+newfrom+","+to+","+newcount+","+ delay+")";
    window.setTimeout(command, delay);    
}

//
// ##Подъем боковых шторок
//
function curtain_step4 (var_name,from,to,count,delay)
{
    var oldSection = document.getElementById(this.oldSectionId);
    var newSection = document.getElementById(this.param.id);
    oldSection.style.display = "none";
    newSection.style.display = "";
    
    var delta=(to-from)/(count * 1.0);
    var newfrom=from+delta;
    var newcount=count-1;
    
    var firstTop = document.getElementById(this.firstTopId);
    var firstMiddle = document.getElementById(this.firstMiddleId);
    var firstBottom = document.getElementById(this.firstBottomId);
    var thridTop = document.getElementById(this.thridTopId);
    var thridMiddle = document.getElementById(this.thridMiddleId);
    var thridBottom = document.getElementById(this.thridBottomId);
    
    var middle_val = (100 - from - from);
    firstTop.style.height= from+"%";
    firstTop.style.display="";
    firstMiddle.style.height= middle_val + "%";
    if(middle_val > 0) firstMiddle.style.display= "";
    else firstMiddle.style.display= "none";
    firstBottom.style.height= from+"%";
    firstBottom.style.display="";
    
    thridTop.style.height= from+"%";
    thridTop.style.display="";
    thridMiddle.style.height= middle_val + "%";
    if(middle_val > 0) thridMiddle.style.display= "";
    else thridMiddle.style.display= "none";
    thridBottom.style.height= from+"%";
    thridBottom.style.display="";

    if (count==0)
    {
        firstTop.style.height= to+"%";
        firstTop.style.display="none";
        firstMiddle.style.height= "100%";
        firstMiddle.style.display= "";
        firstBottom.style.height= to+"%";
        firstBottom.style.display="none";
        
        thridTop.style.height= to+"%";
        thridTop.style.display="none";
        thridMiddle.style.height= "100%";
        thridMiddle.style.display= "";
        thridBottom.style.height= to+"%";
        thridBottom.style.display="none";       
        
        var command=var_name+".curtain_step5('"+var_name+"',50,0,"+this.count5+","+ this.delay5+")";
        //window.setTimeout(command, this.delay5 * this.count5);
        
        this.curtain_step5 (var_name, 50,0,this.count3,this.delay3);
        return;
    }

    var command=var_name+".curtain_step4('"+var_name+"',"+newfrom+","+to+","+newcount+","+ delay+")";
    window.setTimeout(command, delay);    
}

//
// ##Подъем центральной шторки
//
function curtain_step5 (var_name,from,to,count,delay)
{
    var title = document.getElementById(this.titleId);
    title.style.display="none";    
    
    
    var delta=(to-from)/(count * 1.0);
    var newfrom=from+delta;
    var newcount=count-1;
    
    var secondTop = document.getElementById(this.secondTopId);
    var secondMiddle = document.getElementById(this.secondMiddleId);
    var secondBottom = document.getElementById(this.secondBottomId);
    
    var middle_val = (100 - from - from);
    secondTop.style.height= from+"%";
    secondTop.style.display="";
    secondMiddle.style.height= middle_val + "%";
    if(middle_val > 0) secondMiddle.style.display= "";
    else secondMiddle.style.display= "none";
    secondBottom.style.height= from+"%";
    secondBottom.style.display="";
    
    if (count==0)
    {
        secondTop.style.height= to+"%";
        secondTop.style.display="none";
        secondMiddle.style.height= "100%";
        secondMiddle.style.display= "";
        secondBottom.style.height= to+"%";
        secondBottom.style.display="none";
        
        
        var obj = document.getElementById(this.curtainId);    
        obj.style.visibility="hidden";
        return;
    }

    var command=var_name+".curtain_step5('"+var_name+"',"+newfrom+","+to+","+newcount+","+ delay+")";
    window.setTimeout(command, delay);    
}

function SectionInfo(elementID, title)
{
    this.title = title;
    this.id = elementID;
} 