function SlideShowHint(aHintText) {
	if (aHintText == '' ) {
		HideHint();
	}
	else {
		ShowHint('Click here and go directly to subsidiary')
	}
}


function ShowHint(aHintText) {
	var hint = document.getElementById('HintID');

	if(hint == null)
	{
	   	hint = document.createElement('div');
	   	hint.setAttribute('id', 'HintID');
	   	hint .className = 'hintclass';
	  	document.body.appendChild(hint);
	} 
	
	hint.style.display = 'block';
    hint.innerHTML = aHintText;
    
    //window.status = aUrl;
}

function HideHint() {
	var hint = document.getElementById('HintID');
	if(hint != null)
	{
	   	hint.style.display = 'none';
	}
	
	//window.status = '';
}

function SubsidiariesClick(menuUrl) {
	var url = _menuUrl;
	if (menuUrl != '') {
		url = menuUrl;
	}
	
	if (url != '') {
		var e = window.event;
        var obj = e.target || e.srcElement;
        if (obj != null) {        
			if (obj.nodeName.toUpperCase() != 'IMG' || obj.id == 'popUpImage') {
				window.location = url;			
			}
		}
	}
}

function MouseOverMenuImage() {
	if (_menuUrl != '') {
		document.body.style.cursor = 'pointer';
		
		//ShowHint(_menuUrl);
		ShowHint('Click here and go directly to subsidiary');
	}
}

function MouseOutMenuImage() {
	document.body.style.cursor = 'default';
	HideHint();
}





function toggle(div_id) {
    var el = document.getElementById(div_id);
    if (el != null) {
        if (el.style.display == 'none') {
            el.style.display = 'block';
        }
        else {
            el.style.display = 'none';
        }
    }
}


function window_pos_blanket() {
    var div = document.getElementById('blanket');
    if (div != null) {
        var obj = document.getElementById('ww_zone_for_changing_background_image');
        if (obj != null) {
            div.style.top = getTopPosition(obj) + 'px';
            div.style.left = getLeftPosition(obj) + 'px';
          
        }
    }
}

function window_pos_bridge(rowID) {
    var div = document.getElementById('subsidiariesbridge');
    if (div != null) {
        var left = document.getElementById('ww_zone_for_changing_background_image');
        if (left != null) {
            div.style.left = getLeftPosition(left) - 4 + 'px';
        }

        var top = document.getElementById(rowID);
        if (top != null) {
            div.style.top = getTopPosition(top) + 'px';
        }
    }
}

function window_pos_info(popUpDivVar, rowID) {
    var div = document.getElementById(popUpDivVar);
    if (div != null) {
        var rowleft = 0;
        var maxtopvalue = 0;
        var mintopvalue = 0;
        var maxtop = document.getElementById('ww_zone_for_changing_background_image');
        if (maxtop != null) {
            maxtopvalue = getTopPosition(maxtop);
            mintopvalue = maxtopvalue + $('#ww_zone_for_changing_background_image').height();
            rowleft = getLeftPosition(maxtop);

        }

        $('#' + popUpDivVar).height('auto');
        var rowheight = $('#' + rowID).height();
        if ($('#' + popUpDivVar).height() < rowheight) {
            $('#' + popUpDivVar).height(rowheight);
        }

        var top = 0;
        var rowtop = 0;
        var rowbottom = 0;
        var row = document.getElementById(rowID);
        if (row != null) {
            rowtop = getTopPosition(row);
            rowbottom = rowtop + rowheight;
        }

        if (rowtop + $('#' + popUpDivVar).height() > mintopvalue - (mintopvalue * 0.1)) {
            if (rowbottom - $('#' + popUpDivVar).height() > maxtopvalue) {
                rowtop = rowbottom - $('#' + popUpDivVar).height() - 20;
            }
            else {
                rowtop = maxtopvalue;
            }
        }



        rowtop = rowtop + 3;
        rowleft = rowleft + 8;


        div.style.top = rowtop + 'px';
        div.style.left = rowleft + 'px';

        var corner = document.getElementById('subsidiariescorner');
        if (corner != null) {
            corner.style.top = rowtop - 3 + 'px';
            corner.style.left = rowleft + 'px';

        }

    }
}


function window_pos(popUpDivVar, rowID) {
    window_pos_blanket();
    window_pos_bridge(rowID);
    window_pos_info(popUpDivVar, rowID);
}

var _hoverID = "";
var _menuUrl = '';
function ChangeRowFocusColor(rowID, textID, imageUrl, menuUrl) {
    if (_hoverID != rowID) {
        if (_currentRowID != "") {
            var tID = _currentTextID;
            var rowID = _currentRowID;
            openPopup('', '', _currentImageID, _currentRowID, _currentTextID);
            ResetRowColor(rowID, tID, '');
        }
    }


    _hoverID = rowID;
    _menuUrl = menuUrl;
    var divtext = document.getElementById(textID);
    if (divtext != null) {
        var row = document.getElementById(rowID);
        if (row != null) {
            divtext.style.color = 'white';

            if (_currentRowID == "") {
                if (imageUrl != "") {
                    ChangeImage(imageUrl)
                }
            }
        }
    }
    
    
    if (menuUrl != '') {
    	document.body.style.cursor = 'pointer';
    	//ShowHint(menuUrl);
    	ShowHint('Click here and go directly to subsidiary');
    }
    
	var e = window.event;
	//var obj = e.relatedTarget || e.toElement || e.target || e.srcElement;
	var obj = e.target || e.srcElement;
	if (obj != null) {
		if (obj.nodeName.toUpperCase() == 'IMG') {		
			document.body.style.cursor = 'pointer';					
			
			div = document.getElementById('popupDiv');
        	if (div != null) {
            	if (div.style.display != 'none') {
            		ShowHint('Click on minus to close dropdown menu', '');            	
            	}
            	else {
            		ShowHint('Click on plus to get a dropdown menu', '');
            	}
            }				
		}
	}

}

var ImageHoverOpen = false;
function ChangeImage(imageUrl) {
    if (imageUrl != "") {
        var divImage = document.getElementById('blanket');
        if (divImage != null) {

            var imagePopup = document.getElementById('popUpImage');
            if (imagePopup != null) {
                //blanket_size('blanket');
                window_pos('popupDiv', _currentRowID);
                //toggle('blanket');
                divImage.style.display = 'block';

                imagePopup.src = imageUrl;

                ImageHoverOpen = true;
            }
        }
    }
}

function CloseImage() {
    var divImage = document.getElementById('blanket');
    if (divImage != null) {
        divImage.style.display = 'none';
    }
}


function ResetRowColor(rowID, textID, imageUrl) {
    if (_currentRowID != "") {
        var e = window.event;
        var obj = e.relatedTarget || e.toElement;
        if (obj != null) {
            if (obj.nodeName.toUpperCase() == 'FORM' ||
				obj.nodeName.toUpperCase() == 'HTML' ||
				obj.nodeName.toUpperCase() == 'BODY'
			) {
                var tID = _currentTextID;
                var rowID = _currentRowID;
                openPopup('', '', _currentImageID, _currentRowID, _currentTextID);
                ResetRowColor(rowID, tID, '');
            }
        }
    }


    var divtext = document.getElementById(textID);
    if (divtext != null) {	
        if (_currentRowID != rowID) {
            divtext.style.color = 'navy';
        }
        else {
            ChangeRowFocusColor(rowID, textID, imageUrl, _menuUrl);
        }

        if (_currentRowID == "") {
            if (imageUrl != "") {
                CloseImage();
            }
        }
    }
    document.body.style.cursor = 'default';
    HideHint();
}

function AdjustBackground(divid, rowid, textID) {
    var el = document.getElementById(divid);
    if (el != null) {

        var row = document.getElementById(rowid);
        if (row != null) {
            if (el.style.display == 'none') {
                row.className = "subsidiaries_leftmenuitem";
                ChangeRowFocusColor(rowid, textID, "", _menuUrl);         				
            }
            else {
                row.className = "tableSubMenuActive";
                ResetRowColor(rowid, textID, "");
            }

        }
    }

}


function popup(windowname, rowID, imageID, textID) {
    if (ImageHoverOpen) {
        CloseImage();
    }
    ImageHoverOpen = false;

    //blanket_size('blanket');
    window_pos(windowname, _currentRowID);
    toggle('blanket');
    toggle('subsidiariesbridge');
    toggle('subsidiariescorner');
    toggle(windowname);

    AdjustBackground(windowname, rowID, textID);
}

function getLeftPosition(obj) {

	var value = 0;
	if(obj != null)
	{
		var offset = $('#' + obj.id).offset();
		value = offset.left;
	}
	
	return value;
}


function getTopPosition(obj) {

	var value = 0;
	if(obj != null)
	{
		var offset = $('#' + obj.id).offset();
		value = offset.top;
	}
	
	return value;
}



function getLeftPopUpPosition(obj) {
	getLeftPosition(obj);
}



var _currentImageID = "";
var _currentRowID = "";
var _currentTextID = "";
function openPopup(AText, AImageUrl, AImageID, ARowID, textID) {
    var resetcolorid = "";
    if (_currentTextID != "") {
        if (_currentTextID != textID) {
            resetcolorid = _currentTextID;
        }
    }

    var str = AText.replace("_subs_", "\r\n");
    while (str != AText) {
        AText = str;
        str = AText.replace("_subs_", "\r\n");
    }

    var divPopup = document.getElementById('popupDiv');
    if (divPopup != null) {
        divPopup.innerHTML = AText;
    }

    var imagePopup = document.getElementById('popUpImage');
    if (imagePopup != null) {
        imagePopup.src = AImageUrl;
    }


    if (_currentImageID == AImageID) {
        closePopup();       //dropdown is already open for the clicked menu. close it
    }
    else {
        if (_currentImageID != "") {
            closePopup();       //dropdown is already open for another menu. close it
        }

        var image = document.getElementById(AImageID);
        if (image != null) {
            _currentRowID = ARowID;
            _currentTextID = textID;
            image.src = '/Style Library/Images/ww/Minis.png';

            _currentImageID = AImageID;
        }

        popup('popupDiv', ARowID, AImageID, _currentTextID);
    }

    if (resetcolorid != "") {
        var divtext = document.getElementById(resetcolorid);
        if (divtext != null) {
            divtext.style.color = 'navy';
        }
    }

}


function closePopup() {
    if (_currentImageID != "") {
        var image = document.getElementById(_currentImageID);
        if (image != null) {
            image.src = '/Style Library/Images/ww/Plus.png';
        }
    }

    popup('popupDiv', _currentRowID, _currentImageID, _currentTextID);
    _currentImageID = "";
    _currentRowID = "";
    _currentTextID = "";
}




$(document).ready(function () {	
	$(document).mousemove(function(e){
		var hint = document.getElementById('HintID');
		if(hint != null)
		{
			hint.style.left = e.pageX+10+'px';
    		hint.style.top = e.pageY+20+'px';
    	}

   	});


    $(window).resize(function () {
        div = document.getElementById('popupDiv');
        if (div != null) {
            if (div.style.display != 'none') {
                window_pos('popupDiv', _currentRowID);
            }
        }

    });
});

