var isInternetExplorer = false;
if(navigator.appName == "Microsoft Internet Explorer"){
    isInternetExplorer = true;
}

function LoadTopMenu(){    
    var navigation = document.getElementById("mainNavigation");
    navChildren = navigation.childNodes;    
    for(var i=0; i<navChildren.length; i++) {
        if(navChildren[i].nodeName.toLowerCase() == "li") {
            addEvent(navChildren[i], "mouseover", function() {
                addClass(this, "over");
                if(this.getElementsByTagName("ul").length > 0)
                    this.getElementsByTagName("ul")[0].style.display = "block";
            });
            
            addEvent(navChildren[i], "mouseout", function() {                        
                removeClass(this, "over");
                if(this.getElementsByTagName("ul").length > 0)
                    this.getElementsByTagName("ul")[0].style.display = "none";
            });
            
        }
    }  
}



    /*
    if($(elementID).getElementsByTagName("ul").length > 0){       
        if ( isInternetExplorer){
            SetHeight($(elementID).id,(originalHeights[$(elementID).id] - 5));                        
        }
        else{       
            clearTimeout(CurrentSettimeOut1[$(elementID).id]);                               
            CurrentSettimeOut1[$(elementID).id] = setTimeout("SetHeight('" + $(elementID).id + "','" + (originalHeights[$(elementID).id] - 5 ) +"')",5);                        
        }                  
        
        
    }*/       

function removeChildren(element) {
  if(element != null){
    if(element.childNodes){
        if(element.childNodes.length != 0) {
            while(element.firstChild)
                element.removeChild(element.firstChild)
        }
      }
   }
}
function CloseModalPopup(){
    if ( typeof(document.getElementById("ctl00_panVideoDetails")) != "undefined"){        
        removeChildren(document.getElementById("ctl00_panVideoDetails"));
    }
}

var CurrentOpenDeviceId = '';
var CurrentOpenDeviceHeight = '';  
function OpenSubMenuDevice( elementID ){
    if (elementID != CurrentOpenDeviceId){
        CurrentOpenDeviceHeight = $(elementID).offsetHeight; 
        addClass($(elementID), "over");    
        $(elementID).style.height = ( 15 + $(elementID).getElementsByTagName("ul")[0].offsetHeight ) + "px";
        if ( CurrentOpenDeviceId != ''){
            removeClass($(CurrentOpenDeviceId), "over");
            $(CurrentOpenDeviceId).style.height = ( CurrentOpenDeviceHeight - 4) + "px";        
        }
        CurrentOpenDeviceId = elementID;
    }
    else{
        removeClass($(CurrentOpenDeviceId), "over");
        $(CurrentOpenDeviceId).style.height = ( CurrentOpenDeviceHeight - 4) + "px";
        CurrentOpenDeviceId = ''; 
    }
}

var CurrentOpenDingId = '';
var CurrentOpenDingHeight = '';  
function OpenSubMenuDing( elementID ){
    if ( elementID != CurrentOpenDingId){
        CurrentOpenDingHeight = $(elementID).offsetHeight; 
        addClass($(elementID), "over");    
        $(elementID).style.height = ( 15 + $(elementID).getElementsByTagName("ul")[0].offsetHeight ) + "px";
        if ( CurrentOpenDingId != ''){
            removeClass($(CurrentOpenDingId), "over");
            $(CurrentOpenDingId).style.height = ( CurrentOpenDingHeight - 4) + "px";        
        }
        CurrentOpenDingId = elementID;
    }
    
    
}


   
var SlideShowOn = true;
var CurrentScheduleID;

function StopStartSlideShow(object){
    var CurrentlyOn = true;
    if ( object.src.indexOf("/images/play.gif") > 0 ){
        CurrentlyOn = false;
    }
    else{
        CurrentlyOn = true;
    }   
    if ( CurrentlyOn ){
        object.src = "/images/play.gif";
        if (typeof(CurrentScheduleID) != "undefined")  
        {
            clearTimeout (CurrentScheduleID);
        } 
    }
    else{  
        var currentIndex = 0;
        var innerNodes = document.getElementsBySelector('#SlideShowContent .ContentContainer');
        for ( var loop = 0 ; loop < innerNodes.length; loop++){
            if ( innerNodes[loop].style.display == "block"){
                currentIndex = loop;
            }
        }   
        if ( currentIndex == (innerNodes.length - 1)){
            ShowSlideShowContent(0);
        }
        else{
            ShowSlideShowContent(currentIndex + 1);
        }
        object.src = "/images/pause.gif";
    }
}
function ShowSlide(activeIndex){     
    if (typeof(CurrentScheduleID) != "undefined")  
    {
        clearTimeout (CurrentScheduleID);
    } 
    ShowSlideShowContent(activeIndex)    
}
function ShowSlideShowContent(currentIndex){    

    var innerNodes = document.getElementsBySelector('#SlideShowContent .ContentContainer');
    for ( var loop = 0 ; loop < innerNodes.length; loop++){
        innerNodes[loop].style.display = "none";
        removeClass(document.getElementsBySelector('#Numbers .Number')[loop], "Active");
    }        
    
    //document.getElementsBySelector('#SlideShowContent .ContentContainer')[currentIndex].style.display = "block";
    addClass(document.getElementsBySelector('#Numbers .Number')[currentIndex], "Active");
    
    if ( currentIndex == innerNodes.length -1 ){
        nextIndex = 0;
    }
    else{
        nextIndex = currentIndex + 1;
    }
    
    CurrentScheduleID = setTimeout('ShowSlideShowContent(' + nextIndex + ')',5000);            
}



function addClass(element, className) {
	if (!hasClass(element, className)) {
		if (element.className) element.className += " " + className;
		else element.className = className;
	}
}

function removeClass(element, className) {
	var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
	element.className = element.className.replace(regexp, "$2");
}

function hasClass(element, className) {
	var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
	return regexp.test(element.className);
}
function addEvent( obj, type, fn ) { 
	if (obj.addEventListener) { 
		obj.addEventListener( type, fn, false );
	}
	else if (obj.attachEvent) { 
		obj["e"+type+fn] = fn; 
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); } ;
		obj.attachEvent( "on"+type, obj[type+fn] ); 
	} 
} 
function $(strElementId) {
	return document.getElementById(strElementId);
}
/* document.getElementsBySelector(selector)
   - returns an array of element objects from the current document
     matching the CSS selector. Selectors can contain element names, 
     class names and ids and can be nested. For example:
     
       elements = document.getElementsBySelect('div#main p a.external')
     
     Will return an array of all 'a' elements with 'external' in their 
     class attribute that are contained inside 'p' elements that are 
     contained inside the 'div' element which has id="main"

   New in version 0.4: Support for CSS2 and CSS3 attribute selectors:
   See http://www.w3.org/TR/css3-selectors/#attribute-selectors

   Version 0.4 - Simon Willison, March 25th 2003
   -- Works in Phoenix 0.5, Mozilla 1.3, Opera 7, Internet Explorer 6, Internet Explorer 5 on Windows
   -- Opera 7 fails 
*/

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  //return e.all ? e.all : e.getElementsByTagName('*');
   if ( document.all ){   
    if ( e != null ){
        return e.all;
    }
    else{
        return null;
    }
  }
  else{
    return e.getElementsByTagName('*');    
  } 
}

document.getElementsBySelector = function(selector) {
  // Attempt to fail gracefully in lesser browsers
  if (!document.getElementsByTagName) {
    return new Array();
  }
  // Split selector in to tokens
  var tokens = selector.split(' ');
  var currentContext = new Array(document);
  for (var i = 0; i < tokens.length; i++) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
    if (token.indexOf('#') > -1) {
      // Token is an ID selector
      var bits = token.split('#');
      var tagName = bits[0];
      var id = bits[1];
      var element = document.getElementById(id);
      if (tagName && element.nodeName.toLowerCase() != tagName) {
        // tag with that ID not found, return false
        return new Array();
      }
      // Set currentContext to contain just this element
      currentContext = new Array(element);
      continue; // Skip to next token
    }
    if (token.indexOf('.') > -1) {
      // Token contains a class selector
      var bits = token.split('.');
      var tagName = bits[0];
      var className = bits[1];
      if (!tagName) {
        tagName = '*';
      }
      // Get elements matching tag, filter them for class selector
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        if ( elements != null){
            for (var j = 0; j < elements.length; j++) {
            found[foundCount++] = elements[j];
            }
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue; // Skip to next token
    }
    // Code to deal with attribute selectors
    if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
      var tagName = RegExp.$1;
      var attrName = RegExp.$2;
      var attrOperator = RegExp.$3;
      var attrValue = RegExp.$4;
      if (!tagName) {
        tagName = '*';
      }
      // Grab all of the tagName elements within current context
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      var checkFunction; // This function will be used to filter the elements
      switch (attrOperator) {
        case '=': // Equality
          checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
          break;
        case '~': // Match one of space seperated words 
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
          break;
        case '|': // Match start with value followed by optional hyphen
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
          break;
        case '^': // Match starts with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
          break;
        case '$': // Match ends with value - fails with "Warning" in Opera 7
          checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
          break;
        case '*': // Match ends with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
          break;
        default :
          // Just test for existence of attribute
          checkFunction = function(e) { return e.getAttribute(attrName); };
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (checkFunction(found[k])) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      // alert('Attribute Selector: '+tagName+' '+attrName+' '+attrOperator+' '+attrValue);
      continue; // Skip to next token
    }
    // If we get here, token is JUST an element (not a class or ID selector)
    tagName = token;
    var found = new Array;
    var foundCount = 0;
    for (var h = 0; h < currentContext.length; h++) {
      var elements = currentContext[h].getElementsByTagName(tagName);
      for (var j = 0; j < elements.length; j++) {
        found[foundCount++] = elements[j];
      }
    }
    currentContext = found;
  }
  return currentContext;
}

/* That revolting regular expression explained 
/^(\w+)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/
  \---/  \---/\-------------/    \-------/
    |      |         |               |
    |      |         |           The value
    |      |    ~,|,^,$,* or =
    |   Attribute 
   Tag
*/


/*######################################################
Following code makes sure the update progress is 
displayed on any asynchronous call
######################################################*/


var prm;
var postBackElement;

function AssociateAjaxUiEvents()
{
    prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);
}

function Cover(bottom, top, elemToCenter, ignoreSize) {
    var location = Sys.UI.DomElement.getLocation(bottom);
    top.style.position = 'absolute';
    top.style.top = (location.y) + 'px';
    top.style.left = (location.x) + 'px';
    //alert(elemToCenter.offsetHeight);
    
    var dimensions = Sys.UI.DomElement.getBounds(elemToCenter);
    elemToCenterHeight = 28/2;//dimensions.height/2;
    elemToCenterWidth = 92/2;//dimensions.width/2;
    
    //var offSetHeight = (bottom.offsetHeight/2) - (elemToCenter.offsetHeight/2) + 'px';
    //var offSetWidth = (bottom.offsetWidth/2) - (elemToCenter.offsetWidth/2) + 'px';
    
    var offSetHeight = (bottom.offsetHeight/2) - elemToCenterHeight + 'px';
    var offSetWidth = (bottom.offsetWidth/2) - elemToCenterWidth + 'px';
    
    if (!ignoreSize) {
        top.style.height = bottom.offsetHeight + 'px';
        top.style.width = bottom.offsetWidth + 'px';
    }
    if(elemToCenter){
        elemToCenter.style.left = offSetWidth;
        elemToCenter.style.top = offSetHeight;
        //alert(offSetHeight + ' ' + offSetWidth);
    }
}

function InitializeRequest(sender, args) {    
    var ownerId = sender._postBackSettings.sourceElement.id;    
    var elementToOverlap = $get('MainContainer');
    
    Cover(elementToOverlap, $get('ajaxLoadingBackground'), $get('ajaxLoadingMessage'));
   
    if (prm.get_isInAsyncPostBack()) {
        args.set_cancel(true);
    }
    postBackElement = args.get_postBackElement();
    $get('ajaxLoadingBackground').style.display = 'block';   
}

function EndRequest(sender, args) {
    $get('ajaxLoadingBackground').style.display = 'none';
}

function pageLoad(){
    //Set parameters for ajax calls
    setTimeout('AssociateAjaxUiEvents()', 2000);
}

if(typeof(Sys) != 'undefined') Sys.Application.notifyScriptLoaded();

/*######################################################
End update progress display
######################################################*/




