﻿function collapse(obj, div, controlname) {
    with (document.getElementById(div)) {
        //alert(style.display);
        if (style.display == 'none') {
            style.display = 'block';
            obj.getElementsByTagName("IMG")[0].src = "/Style Library/Images/twisty_collapsed.gif";
            obj.getElementsByTagName("IMG")[0].alt = "Hide details for " + controlname;
        } else {
            style.display = 'none';
            obj.getElementsByTagName("IMG")[0].src = "/Style Library/Images/twisty_default.gif";
            obj.getElementsByTagName("IMG")[0].alt = "Show details for " + controlname;
        }
    }
}


//Collaps all divs
function CollapsAll(divId, innerDivPrefix, cityname, clickedDiv) {

    var mainDiv = document.getElementById(divId);

    if (mainDiv != null) {
        var divs = mainDiv.getElementsByTagName("div");
        if (null != divs)
            for (var i = 0; i < divs.length; i++) {
            if ((divs[i].id.substring(0, 26) == innerDivPrefix) && (divs[i].id != clickedDiv))
                divs[i].style.display = 'none';

        }
    }

    var imgs = mainDiv.getElementsByTagName("IMG");
    if (null != imgs) {
        for (var j = 0; j < imgs.length; j++) {
            imgs[j].src = "/Style Library/Images/ico_plus.jpg";
            //imgs[j].alt="Show details for " + cityname;
        }
    }
}


//<----- Tree Expand/Colapse client script START---->
function TreeExpandCollapse(obj, div, controlname) {
    with (document.getElementById(div)) {
        if (style.display == 'none') {
            style.display = 'block';
            obj.getElementsByTagName("IMG")[0].src = "/Style Library/Images/ico_minus.jpg";
            obj.getElementsByTagName("IMG")[0].alt = "Hide details for " + controlname;
        } else {
            style.display = 'none';
            obj.getElementsByTagName("IMG")[0].src = "/Style Library/Images/ico_plus.jpg";
            obj.getElementsByTagName("IMG")[0].alt = "Show details for " + controlname;
        }
    }
}
//<----- Tree Expand/Colapse client script END ---->


//<------ Hover Popup------>

var mouseoutCount = 0;
var mouseTrackEvent = null;

var isIE = document.all ? true : false;
if (!isIE) document.captureEvents(Event.MOUSEMOVE);

var activePopupID;
var activePopupX = 0;
var activePopupY = 0;

var hoverLength = 150;
var hoverHeight = 240;

function checkMousePositionWithPopup(e) {
    
    var _x;
    var _y;
    if (!isIE) {
        _x = e.pageX;
        _y = e.pageY;
    }
    if (isIE) {
        _x = event.clientX + document.body.scrollLeft;
        _y = event.clientY + document.body.scrollTop;
    }

    // Check the div loaction

    if ((activePopupX + 2  > _x) ||
        ((activePopupX + 11 + hoverLength) < _x) ||
        (activePopupY - 1> _y) ||
        ((activePopupY + hoverHeight + 10) < _y)) {
        document.getElementById(activePopupID).style.display = 'none';
        document.getElementById(activePopupID).style.visibility = 'hidden';
        document.onmousemove = null;
    }
    
    return true;
}

var CustomEvent = function() {
    //name of the event
    this.eventName = arguments[0];
    var mEventName = this.eventName;

    //function to call on event fire
    var eventAction = null;
    var eventEndAction = null;

    //subscribe a function to the event
    this.subscribe = function(fn) {
        if (eventEndAction != null) {
            this.end(null, { message: '' });
            eventEndAction = null;
        }
        eventAction = fn;
    };

    //A function to unsubscribe the event
    this.unSubscribe = function(fn2) {
        eventEndAction = fn2;
    };

    //fire the event
    this.fire = function(sender, eventArgs) {

        if (eventAction != null) {
            eventAction(sender, eventArgs);
        }
    };

    //end the event
    this.end = function(sender, eventArgs) {

        if (eventEndAction != null) {
            eventEndAction(sender, eventArgs);
        }
    };
};

function ShowPopup(hoveritem, selectedDate) {

	// Show the selected hover popup
    hp = document.getElementById("divDay" + selectedDate);
    activePopupID = "divDay" + selectedDate;
   
    if (hp.style.visibility == "Visible" && hp.style.display == 'block') {
        return;
    }
    
    // Hide all the hover popups'	
    var arrayOfDivs = document.getElementsByTagName('div');
    var howMany = arrayOfDivs.length - 1;


    for (var i = 0; i < howMany; i++) {
        var thisDiv = arrayOfDivs[i];
        var styleClassName = thisDiv.className;
        if (styleClassName == 'hoverPopup') {
            
            thisDiv.style.display = 'none';
        }
    }

    activePopupX = getAbsX(hoveritem);
    hp.style.left = (activePopupX) + "px";

    activePopupY = getAbsY(hoveritem) - 10;
    hp.style.top = (activePopupY) + "px";
    
  
    // Set popup to visible
    hp.style.visibility = "Visible";
    hp.style.display = 'block';

    mouseoutCount = 0;

    mouseTrackEvent = new CustomEvent("MouseTrackingEvent");
    
    mouseTrackEvent.subscribe(function(sender, eventArgs) {
    document.onmousemove = checkMousePositionWithPopup;
    });

    mouseTrackEvent.unSubscribe(function(sender, eventArgs) {
        document.onmousemove = null;
    });

    mouseTrackEvent.fire(null, { message: '' });
}


function HidePopup(e, id) {
    if (e.fromElement != null) {
        if (e.fromElement.nodeName == 'DIV') {// Check whether the event from the popup div
            if (!e) var e = window.event;
            e.cancelBubble = true;
            if (e.stopPropagation) e.stopPropagation();

            var targ;
            if (e.target) targ = e.target;
            else if (e.srcElement) targ = e.srcElement;

            //            if (mouseoutCount == 0)
            //                mouseoutCount++;
            //            else {
            if (e.type == "mouseout" && targ.tagName == "DIV" && targ.id == document.getElementById(id).id) {
                document.getElementById(id).style.display = 'none';
                document.getElementById(id).style.visibility = 'hidden';
            }
            //            }
        }
    }
}


function getAbsX(obj) {

    var leftOffset = 0;

    var retrunValue = 0;
    var leftOffsetX = 0;
    var offsetParentWidth = 0;
    var paddingRight = 15;

    if (obj.offsetParent) {
        leftOffsetX = obj.offsetLeft;
        offsetParentWidth = obj.offsetParent.offsetWidth;
        offsetWidth = obj.offsetWidth;

    }

    if (obj.offsetParent) {
        while (obj.offsetParent) {
            leftOffset += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else if (obj.x) //for NN4
    {
        leftOffset = obj.x;
    }


    if (leftOffsetX + hoverLength > offsetParentWidth + paddingRight) {
        leftOffset = leftOffset - hoverLength + 20;
    }
    else {
        leftOffset = leftOffset - 5;
    }

    return leftOffset;

}
            
function getAbsY(obj)
{
  var topOffset = 0;
  if (obj.offsetParent)
  {
        while (obj.offsetParent)
        {
              topOffset += obj.offsetTop;
              obj = obj.offsetParent;
        }
  }
  else if (obj.y) // for NN4
  {
        topOffset = obj.y;
  }

  topOffset = topOffset - document.documentElement.scrollTop;
  return topOffset;
}

//<----- Hover Popup ---->




