/*
 * Common JS
 * @author Engineering
 * @fileoverview
 * 
 * Javascript for common project components 
 *
 */

var COMMON_globalConstants = {
  current: 'current',
  currentFirstChild: 'currentFirstChild',
  currentLastChild: 'currentLastChild', 
  currentNext: 'currentNext',
  currentPrev: 'currentPrev',
  failure: 'failure',  
  firstChild: 'firstChild',
  hide: 'hide',
  IE6Hover: 'IE6Hover',
  lastChild: 'lastChild',
  mouseoverImageSuffixRE: '_on\\.',
  mouseoutImageSuffixRE: '_off\\.',
  mouseoverImageSuffix: '_on\.',
  mouseoutImageSuffix: '_off\.',
  newWindowClass: 'openNewWindow',
  show: 'show',
  tabClass_close: 'close',
  tabClass_expand: 'expand',
  tabClass_link: 'link',
  tabClass_tab: 'tab'     
};
  
/*
 * accordion component code
 * @class COMMON_accordion
 */
var COMMON_accordion = function () {
  var classes = {
    clicked: 'clicked'
  };
  
  return {
    init: function (divObject) {
      COMMON_accordion.addClasses(divObject);
      COMMON_accordion.bindAccordionLinks(divObject);  
    },
    /*
     * adds classes to first and last li child of tabLinks ul in each accordion for styling purposes
     * 
     * What it does:
     *    adds class defined in COMMON_globalConstants variables to each tabLinks list first child and last child.
     *    Will only be applied to the div object passed in as an argument if it's not null
     * 
     */           
    addClasses: function (divObject) {
    // extra tab class hooks for styling, etc.
      if(divObject){
        //Only add classes to the divObject
        divObject.find('ul.accordionItems li:first').addClass(COMMON_globalConstants.firstChild);
        divObject.find('ul.accordionItems li:last').addClass(COMMON_globalConstants.lastChild); 
      } else {
        //Add classes to all accordionComponents in the current page
        $('div.accordionComponent').each (function () {
          var thisAccordionComponent = $(this);
      
          thisAccordionComponent.find('ul.accordionItems li:first').addClass(COMMON_globalConstants.firstChild);
          thisAccordionComponent.find('ul.accordionItems li:last').addClass(COMMON_globalConstants.lastChild);  
        });
      }
    },
    /*
     * sets up behaviors to run when the tab links are clicked
     * 
     * What it does:
     *    gathers all accordionComponent divs (or the div object passed in as an argument) on the page. For each, gather all the a tags and bind with an onclick handler. 
     *    Whenever a link is clicked, remove all 'current', 'currentNext' and/or 'currentPrev' classes from all the accordion links. Retrieve and add 'current'
     *    to the list item parent of the tab link which was clicked and also add 'currentNext'/'currentPrev' classes to the next/previous siblings (if they exist.)
     * 
     */     
    bindAccordionLinks: function (divObject) {
      if(divObject){
        //Bind only the current divObject
        var accordionLinks = divObject.find('ul.accordionItems > li a.tabLink');
        accordionLinks.bind('click', function (e) {
          var accordionLink = $(this);              
          var e = e || window.event;
          e.preventDefault(); 
          
          if(!divObject.hasClass('multipleExpand')){
            // remove these classes from everything but the clicked link
            // if multipleExpand class doesn't exist in this div
            accordionLinks.each(function () {
              var li = $(this).not(accordionLink).parents('li');
              li.removeClass(COMMON_globalConstants.current);
              li.removeClass(COMMON_globalConstants.currentPrev);
              li.removeClass(COMMON_globalConstants.currentNext);
            });
          }
                   
          // add 'current' to the list item for the link just clicked
          accordionLink.parents('li').toggleClass(COMMON_globalConstants.current);                    
          // add 'currentPrev' to the sibling of the list item for the link just clicked
          accordionLink.parents('li').prev().toggleClass(COMMON_globalConstants.currentPrev);         
          // add 'currentNext' to the sibling of the list item for the link just clicked
          accordionLink.parents('li').next().toggleClass(COMMON_globalConstants.currentNext);           
  
        });
        
      } else {
        // for each accordionComponent on the page
        $('div.accordionComponent').each (function () {
          var thisAccordionComponent = $(this);
          var accordionLinks = thisAccordionComponent.children('ul.accordionItems').children('li').children('div.toggleLink').children('a.tabLink');
          // bind accordion link clicks to showing correct tab content
          accordionLinks.bind('click', function (e) {
            var accordionLink = $(this);              
            var e = e || window.event;
            e.preventDefault(); 

            if(!thisAccordionComponent.hasClass('multipleExpand')){
              // remove these classes from everything but the clicked link 
              // if multipleExpand class doesn't exist in this div
              accordionLinks.not(accordionLink).each(function () {
                var li = $(this).parent('div').parent('li');
        li.removeClass(COMMON_globalConstants.current);
                li.removeClass(COMMON_globalConstants.currentPrev);
                li.removeClass(COMMON_globalConstants.currentNext);
              });
            }
            
            // add 'current' to the list item for the link just clicked
            accordionLink.parent('div').parent('li').toggleClass(COMMON_globalConstants.current);                    
            // add 'currentPrev' to the sibling of the list item for the link just clicked
            accordionLink.parent('div').parent('li').prev().toggleClass(COMMON_globalConstants.currentPrev);         
            // add 'currentNext' to the sibling of the list item for the link just clicked
            accordionLink.parent('div').parent('li').next().toggleClass(COMMON_globalConstants.currentNext);  

          });
        });    
      }
    }
  }
}();

 
/*
 * breadcrumb code
 * @class COMMON_breadcrumb
 */
var COMMON_breadcrumb = function () { 
  return {
    init: function () {
      COMMON_breadcrumb.addClasses();
    },
    /*
     * adds classes to first and last li child of breadcrumb ul in each breadcrumb for styling purposes
     * 
     * What it does:
     *    adds class defined in COMMON_globalConstants variable to each breadcrumb list first child and last child
     * 
     */           
    addClasses: function () {
      // extra tab class hooks for styling, etc.
      $('div.breadcrumb').each (function () {
        var thisTabsComponent = $(this);
    
        thisTabsComponent.find('ul li:first').addClass(COMMON_globalConstants.firstChild);
        thisTabsComponent.find('ul li:last').addClass(COMMON_globalConstants.lastChild);    
      });
    }
  };
}(); 
 
 
/*
 * content pager code
 * @class COMMON_contentPager
 */ 
var COMMON_contentPager = function () {
  return {
    init: function () {   
      COMMON_contentPager.bindMouseEvents();    
      COMMON_contentPager.addClasses();     
    },
    /*
     * adds classes to first and last li child of content pager control progress indicator in each content pager for styling purposes
     * 
     * What it does:
     *    adds class defined in COMMON_globalConstants variable to each progress indicator list first child and last child
     * 
     */               
    addClasses: function () {
      $('div.contentPager').each(function () {
        var thisContentPager = $(this);   
        thisContentPager.find('ul.contentPagerControl li ul li:first').addClass(COMMON_globalConstants.firstChild);   
        thisContentPager.find('ul.contentPagerControl li ul li:last').addClass(COMMON_globalConstants.lastChild);   
        thisContentPager.find('li.progressIndicator ul li:first').addClass(COMMON_globalConstants.current);
      });
    },
    /*
     * sets up behaviors to run when the content pager links are clicked
     * 
     * What it does:
     *    gathers all contentPager divs on the page. For each, gather all the previous page links and bind with an onclick handler. Whenever a previous link is clicked,
     *    find the content page with the 'current' link and add 'current' class to the previous content page. If that is successful, remove the 'current' class from the 
     *    one that was previously current. if the progress indicator links are being shown, update the controls. Do something similar for the next page link. For the 
     *    progress indicator links, when a link is clicked, remove 'current' class from all content pages and then, based on the index of the control link clicked, 
     *    select the corresponding content div and add the 'current' class.
     * 
     */         
    bindMouseEvents: function () {
      $('div.contentPager').each(function () {
        var thisContentPager = $(this);
        var progressIndicatorLinks = thisContentPager.find('li.progressIndicator a');       
        
        thisContentPager.find('li.prevPage a').bind('click', function (e) {   
          var e = e || window.event;
          e.preventDefault();
          
          /* todo: get correct tracking stuff 
          COMMON_tracking.triggerTracking('Navigation: Previous > Click ', 'UOPX: Home > Highlights');
          */
          var currentPage = thisContentPager.find('div.pagerContentContainer div.current');         
          var result = currentPage.prev().addClass(COMMON_globalConstants.current); 
          if (result.length) {
            currentPage.removeClass(COMMON_globalConstants.current);
            if (thisContentPager.find('li.progressIndicator').length) {           
              COMMON_contentPager.updateControls(thisContentPager, progressIndicatorLinks, 'prev');
            }
          }
		  
		  // force redraw to reposition controls
		  thisContentPager.find('div.pagerControlsContainer').attr('style','border-width: 0');
		  thisContentPager.find('div.pagerControlsContainer').removeAttr('style');
        });

        thisContentPager.find('li.nextPage a').bind('click', function (e) {
          var e = e || window.event;
          e.preventDefault();
        
          /* todo: get correct tracking stuff 
          COMMON_tracking.triggerTracking('Navigation: Previous > Click ', 'UOPX: Home > Highlights');
          */
          var currentPage = thisContentPager.find('div.pagerContentContainer div.current');                 
          var result = currentPage.next().addClass(COMMON_globalConstants.current);         
          if (result.length) {
            currentPage.removeClass(COMMON_globalConstants.current);
            if (thisContentPager.find('li.progressIndicator').length) {
              COMMON_contentPager.updateControls(thisContentPager, progressIndicatorLinks, 'next');
            }
          }

		  // force redraw to reposition controls
		  thisContentPager.find('div.pagerControlsContainer').attr('style','border-width: 0');
		  thisContentPager.find('div.pagerControlsContainer').removeAttr('style');
        });
        
        progressIndicatorLinks.bind('click', function (e) {
          var e = e || window.event;
          e.preventDefault();
  
          /* todo: get correct tracking stuff 
          COMMON_tracking.triggerTracking('Navigation: Previous > Click ', 'UOPX: Home > Highlights');
          */
          // get the index of the link that was clicked
          var linkIndex = progressIndicatorLinks.index(this); 
          // remove 'current' from all content pages
          thisContentPager.find('div.pagerContentContainer div.pagerContent').removeClass(COMMON_globalConstants.current);
          // remove 'current' from all control list items
          progressIndicatorLinks.each(function () {
            $(this).parent('li').removeClass(COMMON_globalConstants.current);
          });
          // add 'current' to the list item parent of the clicked link
          $(this).parent('li').addClass(COMMON_globalConstants.current);
          // add 'current to the content page that corresponds to the index of the tab link clicked 
          $(thisContentPager.find('div.pagerContentContainer div.pagerContent').get(linkIndex)).addClass(COMMON_globalConstants.current);
		  
		  // force redraw to reposition controls
		  thisContentPager.find('div.pagerControlsContainer').attr('style','border-width: 0');
		  thisContentPager.find('div.pagerControlsContainer').removeAttr('style');
		  
        });
                
      });
    },
    /*
     * update progress indicator links when stepping via next/prev page links
     * 
     * What it does:
     *    remove the 'current' class from all control list items. find the index of the page marked as current; using this index, get the corresponding
     *    progress control link and add the 'current' class to its list item.
     * 
     */             
    updateControls: function (thisContentPager, progressIndicatorLinks, direction) {
      progressIndicatorLinks.each(function () {
        $(this).parent('li').removeClass(COMMON_globalConstants.current);
      });       
    
      var currentPageIndex = thisContentPager.find('div.pagerContentContainer div.pagerContent').index(thisContentPager.find('div.pagerContentContainer div.current')); 
      $(progressIndicatorLinks.get(currentPageIndex)).parent('li').addClass(COMMON_globalConstants.current);    
    }   
  }
}(); 


/*
 * form code
 * @class COMMON_form
 */ 
 
var COMMON_form = function () {
  var tmpInputText = false;
    
  return {
    init: function () {
      COMMON_form.bindMouseEvents();
    },
     /*
     * sets up behaviors for text inputs
     * 
     * What it does:
     *      gathers all inputs with type "text" on the page. When an input is given focus, remove the global 'fail' class if it's assigned, cache the current field value
     *    if it exists and set value to ''. On blur, if the field doesn't already have a value and there's one cached in tmpInputText, replace the value with tmpInputText and
     *    clear out the current value of tmpInputText for future caching.
     * 
     */         
    bindMouseEvents: function () {
      $('input[type="text"]').each(function () {
         $(this).bind('focus', function () {
          $(this).removeClass(COMMON_globalConstants.failure);
          if ($(this).val()) {
            tmpInputText = $(this).val();
          }
          $(this).val('');        
          });

        $(this).bind('blur', function () {
          if (!$(this).val() && tmpInputText) {
            $(this).val(tmpInputText);
            tmpInputText = false;
          }
        });
      });

    }
  };
}(); 
  
 
 
/*
 * image mouseover code
 * @class COMMON_imageMouseover
 */ 
 
var COMMON_imageMouseover = function () {
  var constants = {
    mouseoverClass: 'mouseover'
  };

  return {
    init: function () {
      COMMON_imageMouseover.bindMouseEvents();
    },
    /*
     * adds mouseover/mouseout events to images for rollover states
     * 
     * What it does:
     *    gathers all images on the page. for mouseovers: if the image has '_off.' in the image path, replace '_off.' with '_on.' and add a class to the image to indicate a mouseover
     *    was triggered (to differentiate when mousing over/out of images that don't have rollover states.) for mouseouts: if the image has '_on.' in the image path AND has the 
     *    mouseover trigger class, replace '_on.' with '_off.' and remove the class.
     * 
     */     
    bindMouseEvents: function () {
      $('img, input[type=image]').each(function () {
        $(this).bind('mouseover', function () {
          var src = $(this).attr('src');
          var regexp = new RegExp(COMMON_globalConstants.mouseoutImageSuffixRE, 'g');

          if (src.search(regexp) > -1) {
            $(this).attr('src', src.replace(regexp, COMMON_globalConstants.mouseoverImageSuffix));
            // add class as indicator mouseover activated
            $(this).addClass(constants.mouseoverClass);
          }
        });

        $(this).bind('mouseout', function () {            
          var src = $(this).attr('src');
          var regexp = new RegExp(COMMON_globalConstants.mouseoverImageSuffixRE, 'g');

          if (src.search(regexp) > -1 && $(this).hasClass(constants.mouseoverClass)) {                
            $(this).attr('src', src.replace(regexp, COMMON_globalConstants.mouseoutImageSuffix));
            $(this).removeClass(constants.mouseoverClass);
          }
        });
      });     
    }
  };
}();

/*
 * links code
 * @class COMMON_links
 */ 
 
var COMMON_links = function () {
  return {
    init: function () {
      COMMON_links.bindMouseEvents();
    },
    /*
     * intercepts links to open in new window
     * 
     * What it does:
     *    gathers all links with class 'openNewWindow'. prevent default event behavior and redirect href to new window.
	 * 
     */     
    bindMouseEvents: function () {
		$('a.' + COMMON_globalConstants.newWindowClass).each(function () {
			$(this).bind('click keypress', function (e) {
				var e = e || window.event;               
				e.preventDefault(); 
				
				window.open($(this).attr('href'));
			});			
		});     
    }
  };
}();


/*
 * scroller code
 * @class COMMON_scroller
 */ 
var COMMON_scroller = function () {
  var classes = {
    numItemsVisiblePrefix: 'numItemsVisible'
  }

  return {
    init: function () {
      COMMON_scroller.addWidths();
      COMMON_scroller.bindMouseEvents();
    },
    /*
     * adds classes to various scroller elements
     * 
     * What it does:
     *    for each scroller on the page, adds 'show' class to the first N number of elements to be shown as defined by numItemsVisibleN class on ul element. Also adds firstChild/lastChild
     *    classes to first and last element in scroller and firstVisible/lastVisible classes to current first and last visible elements in scroller. Finally, add current class to first visible
     *    element in scroller.
     * 
     */       
    addWidths: function () {
		$('div.scrollerComponent').each (function () {
			var thisScroller = $(this); 
			var scrollerUl = thisScroller.find('ul[className*="' + classes.numItemsVisiblePrefix + '"]');
			var numItemsVisible = scrollerUl.attr('className').substr(classes.numItemsVisiblePrefix.length);

			
			var itemWidth = scrollerUl.children('li.scrollerContentContainer').width() / numItemsVisible;
			//itemWidth = 120;
			thisScroller.find('div.scrollerContent').children('div.section').width(itemWidth);

		});
    },
    /*
     * adds mouseclick events to scroller
     * 
     * What it does:
     *      for each scroller on the page, define the previous/next item links, if available, and bind a click event. With each previous link, determine first visible and try to add
     *    show class to previous item. If previous item exists and action is successful, remove show class from last visible item. Based on new collection of items with show class (ie, visible)
     *    reassign first/last visible classes. Finally, determine current and try to add current class to previous item, caching that element in the process. If previous item 
     *    exists and action is successful, remove current class from next element to cached element. Do similar (but reversed) actions for next link binding.
     */       
    bindMouseEvents: function () {
      $('div.scrollerComponent a.prevItemLink, div.scrollerComponent a.nextItemLink').each (function () {
        $(this).bind('click', function (e) {
			var e = e || window.event;               
			e.preventDefault(); 
        });
      });
    }
  };
}();

 
/*
 * table code
 * @class COMMON_table
 */
var COMMON_table = function () {
  var classes = {
    rowStyle: 'altRow'
  };
  
  return {
    init: function () {
      COMMON_table.addClasses();
      COMMON_table.stripeRows();
    },
    addClasses: function () {
      $('table').each(function () {
        var thisTable = $(this);
        
        thisTable.find('tr:first').addClass(COMMON_globalConstants.firstChild);
        thisTable.find('tr:last').addClass(COMMON_globalConstants.lastChild);       
      });
      
      $('tr').each(function () {
        var thisRow = $(this);
      

        thisRow.find('td:first').addClass(COMMON_globalConstants.firstChild);
        thisRow.find('td:last').addClass(COMMON_globalConstants.lastChild); 
        
        thisRow.find('th:first').addClass(COMMON_globalConstants.firstChild);
        thisRow.find('th:last').addClass(COMMON_globalConstants.lastChild);     
      });
    },
    /*
     * adds a class to every other row of all tables for styling purposes
     * 
     * What it does:
     *    adds class defined in rowStyle variable to all even rows in page tables
     * 
     */       
    stripeRows: function () {
      $('table').find('tr:even').addClass(classes.rowStyle);
    }
  };
}();


/*
 * tabs_display component code
 * @class COMMON_tabs_display
 */
var COMMON_tabs_display = function () {

  return {
    init: function () {
      COMMON_tabs_display.addClasses();
      COMMON_tabs_display.bindTabLinks();   
    },
    /*
     * adds classes to first and last li child of tabLinks ul in each tabsComponent for styling purposes
     * 
     * What it does:
     *    adds class defined in COMMON_globalConstants variables to each tabLinks list first child and last child 
     * 
     */           
    addClasses: function () {
      // extra tab class hooks for styling, etc.
      $('div.tabsComponent').each (function () {
        var thisTabsComponent = $(this);
    
        thisTabsComponent.children('ul.tabLinks').children('li:first').addClass(COMMON_globalConstants.firstChild);
        thisTabsComponent.children('ul.tabLinks').children('li:last').addClass(COMMON_globalConstants.lastChild); 

        // if the first content tab is already being displayed (ie, has class 'current'), add 'currentFirstChild' for stupid ie6, which doesn't support chained classes
        if (thisTabsComponent.children('div.tabContentContainer').children('div.tabContent:first').hasClass(COMMON_globalConstants.current)) {
          thisTabsComponent.children('ul.tabLinks').children('li:first').addClass(COMMON_globalConstants.current);
          thisTabsComponent.children('ul.tabLinks').children('li:first').addClass(COMMON_globalConstants.currentFirstChild);
          thisTabsComponent.children('ul.tabLinks').children('li:first').next().addClass(COMMON_globalConstants.currentNext);       
		  
		  thisTabsComponent.children('ul.tabLinks').children('li:first').children('a.tabLink').addClass(COMMON_globalConstants.tabClass_close);
		  thisTabsComponent.children('ul.tabLinks').children('li:first').children('a.tabLink').removeClass(COMMON_globalConstants.tabClass_expand);
		  
        }
      });
    },
    /*
     * sets up behaviors to run when the tab links are clicked
     * 
     * What it does:
     *    gathers all tabsComponent divs on the page. For each, gather all the a tags and bind with an onclick handler. Whenever a link is clicked,
     *    remove all 'current', 'currentNext' and/or 'currentPrev' classes from all the tabLinks list items and content tabs. Retrieve and add 'current'
     *    to the list item parent of the tab link which was clicked and also add 'currentNext'/'currentPrev' classes to the next/previous siblings (if they exist.)
     *    Based on the index of the tab link clicked, select the corresponding content div and add the 'current' class to that as well.
     * 
     */     
    bindTabLinks: function () {
      // for each tabsComponent on the page
      $('div.tabsComponent').each (function () {
        var thisTabsComponent = $(this);
        var tabLinks = thisTabsComponent.children('ul.tabLinks').find('li a.tabLink');
        // bind tab link clicks to showing correct tab content
        tabLinks.bind('click', function (e) {
          var tabLink = $(this);              
          var e = e || window.event;
                
          // don't do any of this class switching if we're clicking on a link tab, since we're just going to a new page anyway
          if (!tabLink.hasClass(COMMON_globalConstants.tabClass_link)) {
            e.preventDefault(); 
            
            // get the index of the tab that was clicked
            var tabIndex = tabLinks.index(this);  
          
            // remove 'current' from all tab list items
            thisTabsComponent.children('ul.tabLinks').children('li').removeClass(COMMON_globalConstants.current);
            thisTabsComponent.children('ul.tabLinks').children('li:first').removeClass(COMMON_globalConstants.currentFirstChild);
            thisTabsComponent.children('ul.tabLinks').children('li:last').removeClass(COMMON_globalConstants.currentLastChild);
            // remove 'currentPrevious' and 'currentNext' from all tab list items
            thisTabsComponent.children('ul.tabLinks').children('li').removeClass(COMMON_globalConstants.currentPrev);
            thisTabsComponent.children('ul.tabLinks').children('li').removeClass(COMMON_globalConstants.currentNext);         
      
      // remove 'current' from all content tabs
      thisTabsComponent.children('div.tabContentContainer').children('div.tabContent').removeClass(COMMON_globalConstants.current);
   
      if (!tabLink.hasClass(COMMON_globalConstants.tabClass_close)) {
        // add 'current' to the list item for the link just clicked
        tabLink.parents('li').addClass(COMMON_globalConstants.current);
        // add 'currentFirstChild' for stupid ie6, which doesn't support chained classes
        if (tabLink.parents('li').hasClass(COMMON_globalConstants.firstChild)) {
          tabLink.parents('li').addClass(COMMON_globalConstants.currentFirstChild);
        }
        // add 'currentLastChild' for stupid ie6, which doesn't support chained classes
        if (tabLink.parents('li').hasClass(COMMON_globalConstants.lastChild)) {
          tabLink.parents('li').addClass(COMMON_globalConstants.currentLastChild);
        }
        
        // add 'currentPrev' to the sibling of the list item for the link just clicked
        tabLink.parents('li').prev().addClass(COMMON_globalConstants.currentPrev);          
        // add 'currentNext' to the sibling of the list item for the link just clicked
        tabLink.parents('li').next().addClass(COMMON_globalConstants.currentNext);

        // add 'current to the content tab that corresponds to the index of the tab link clicked 
        $(thisTabsComponent.children('div.tabContentContainer').children('div.tabContent').get(tabIndex)).addClass(COMMON_globalConstants.current);
            }
    
            tabLinks.not(tabLink).each(function () {
              if ($(this).hasClass(COMMON_globalConstants.tabClass_close)) {
                $(this).removeClass(COMMON_globalConstants.tabClass_close);
                $(this).addClass(COMMON_globalConstants.tabClass_expand);
              }
            });
           
            if (tabLink.hasClass(COMMON_globalConstants.tabClass_expand)) {
             tabLink.removeClass(COMMON_globalConstants.tabClass_expand);
              tabLink.addClass(COMMON_globalConstants.tabClass_close);
            }         
            else if (tabLink.hasClass(COMMON_globalConstants.tabClass_close)) {
              tabLink.removeClass(COMMON_globalConstants.tabClass_close);
              tabLink.addClass(COMMON_globalConstants.tabClass_expand);
            }         
          } 
        });
      });     
    }
  }
}();


var COMMON_tracking = function () {
  return  {
    init: function () {
    
    },
    trackingRedirect: function (url, trackingId, trackingGroup) {
      cmCreatePageElementTag(trackingId, trackingGroup);
      
      var passedUrl = url;
      //Check if the url starts with "http://" or not
      if(passedUrl.match('^http://') == 'http://'){
        passedUrl = passedUrl.substring(passedUrl.indexOf('/', 7));
      }
      cmCreateManualLinkClickTag(passedUrl, 'Features');
      
      //Redirect to the url
      window.location = url;      
    },
    triggerTracking: function (trackingId, trackingGroup) {
      cmCreatePageElementTag(trackingId, trackingGroup);    
    } 
  }
}();


/* FlowPlayerComponent */
/* Flowplayer configuration is found at http://flowplayer.org */
var COMMON_flowPlayer = function () {
	
	//phoenix.com key authorises the commercial license and strips the watermark from the player.
	var flowPlayerSiteKey 		= "#@d24313f0b73359e8961";
	
	//initial object that will holf all flowPlayer params
	var flowPlayerObject 		= null;
	var autoNextVideo = false;
	return {
		
		//init is only used by the single non-carousel player, IF one is found on the page. We use jQuery to look for a class name that is 
		//unique to the small player. 
		//This method is called on every page to look for the single player, in /etc/designs/ac/js/scripts.js/files/default.js	
		init : function () {
			if ($('.singleFlowPlayer').length) {
				//if any instance of the small player exists, initialize it.
				COMMON_flowPlayer.flowPlayerInit ("single", null, null);
			}
		},
		
		//flowPlayerInit is used to initialize EVERY flowPlayer instance.
		flowPlayerInit : function (flowPlayerType, videoArray, carouselID, autoNext) {
			// Here we determine if it is a carouselPlayer or not:
			flowPlayerID = (flowPlayerType == "single") ? "a.singleFlowPlayer" : flowPlayerType;
			// The method 'flowPlayer()' is from the included flowPlayer.js file, here:
			// /etc/designs/ac/js/scripts.js/files/flowplayer-3.1.4.min.js;
			// flowplayer.js is included by:
			// /etc/designs/ac/js/scripts.js/includes.txt
			if (autoNext == "true") autoNextVideo = true;
			flowplayer(  flowPlayerID, 
						"/etc/designs/common/swf/videoplayer/flowplayer.commercial-3.1.5.swf",
						
						//testing onFail, and version behavior:
						//{	src:"/etc/designs/common/swf/videoplayer/flowplayer.commercial-3.1.5.swf",
							//no-flash code for carousel player
							//version: 9, 0]
							//onFail: function()  {
								//var largePlayers = getElementsByClassName("largevideoplayerdiv", document.body);
								//for (var k = 0; k < largePlayers.length; k++) {
								//	largePlayers[k].innerHTML = "TEST";
								//}
							//	var smallplayers = getElementsByClassName("smallvideoplayerdiv", document.body);
								//for (var k = 0; k < smallplayers.length; k++) {
								//	smallplayers[k].innerHTML = "TEST";
							//	}
							//}
						//},
						
						COMMON_flowPlayer.getFlowPlayerObject(flowPlayerType, videoArray, carouselID)

						);
		},
		
		// here we get the desired configuraton for our player, depending on the type of player : multi or single.
		// The single player passes in a param value of null for both videoArray and carouselID.
		getFlowPlayerObject : function (flowPlayerType, videoArray, carouselID) {
			
			if(flowPlayerType != "single") {
				//if(videoArray){
					// Here we take care of the carousel thumbnail menu for each multiplayer.
					COMMON_flowPlayer.flowPlayerSetUpMulti(flowPlayerType, carouselID);
				//}
				//else {
					// this should only alert if the array is null. 
					// This would indicate that something is wrong in the passing of the array to this method.
				//alert("videoArray is :" + videoArray);
				//}
			}
			
			// lets get started
			var flowPlayerSetUpObj 					= new Object();
			flowPlayerSetUpObj.key    				= flowPlayerSiteKey;
			flowPlayerSetUpObj.screen 				= { top: 0, left: 0, width: '50%', height: '100%'};
			flowPlayerSetUpObj.plugins				= new Object();
			flowPlayerSetUpObj.plugins.controls	 	= {
														url: '/etc/designs/common/swf/videoplayer/skins/flowplayer.controls-skinless-3.1.5.swf', 
														skin: 'customskin',
														backgroundColor: '#414141',						
														bottom: 0, 
														width: '50%', 
														alpha: .6,
														time: false,
														autoHide: 'always'
													   }										  
			flowPlayerSetUpObj.plugins.customskin	= { url: "/etc/designs/common/swf/videoplayer/skins/buttons_tube.swf", type: "classLibrary" };
			flowPlayerSetUpObj.clip					= { autoPlay: true,
														autoBuffering: true,
														onStart : function () {
															var url = this.getClip().url;
															var flvname = COMMON_flowPlayer.getFLVName(url);
															cmCreatePageElementTag("Play clicked", "UOPX: " + flvname);
														},
														onFinish : function () {
															var url = this.getClip().url;
															var flvname = COMMON_flowPlayer.getFLVName(url);
															cmCreatePageElementTag("Video play ended", "UOPX: " + flvname);
														},
														onPause : function () {
															var url = this.getClip().url;
															var flvname = COMMON_flowPlayer.getFLVName(url);
															cmCreatePageElementTag("Pause Clicked", "UOPX: " + flvname);
														}
													
													};
			flowPlayerSetUpObj.onLoad				= function() {
														this.setVolume(30);
													  };
			 
			flowPlayerSetUpObj.onFullscreen			= function() {
														var url = this.getClip().url;
														var flvname = COMMON_flowPlayer.getFLVName(url);
														cmCreatePageElementTag("Full screen clicked", "UOPX: " + flvname);
													  };
			flowPlayerObject = flowPlayerSetUpObj;
			
			//}
			// set up the continuous play for the multiplayer:
			if(flowPlayerType != "single") {
				flowPlayerObject.onFinish				= function(){
															var nextVideoIndex = flowPlayerObject.currentVideoIndex + 1;
															if((nextVideoIndex < videoArray.length) && autoNextVideo) {
																$f(flowPlayerType).play(videoArray[nextVideoIndex]);
															}
														  };
				flowPlayerObject.onBegin				= function () {
																//alert("onBegin");
																var currentFLV = $f(flowPlayerType).getClip().url;
																for (var p = 0; p < videoArray.length; p++) {
																	if(currentFLV == videoArray[p]){
																		flowPlayerObject.currentVideoIndex = p;
																	}
																}
															}
				
			}
			
			flowPlayerObject.play = {
				replayLabel: ''
			};
			return flowPlayerObject;
		},

		flowPlayerSetUpMulti : function (multiPlayerID, carouselID) {
			var links = document.getElementById(carouselID).getElementsByTagName("a"); 
			for (var i = 0; i < links.length; i++) {
				links[i].onclick = function() {
					// play the clip specified in href- attribute with Flowplayer 
					$f(multiPlayerID).play(this.getAttribute("href", 2));
					//kill dotted outline
					this.blur();
					// by returning false normal link behaviour is skipped 
					return false; 
				} 
			}
		},
		
		getFLVName : function (flvPath) {
			var flvNameArr = flvPath.split("/");
			var flvName = flvNameArr[flvNameArr.length - 1];
			return flvName;
		}
		
	}
}();
/*End FlowPlayer Component*/




/*
 * this function is called from dialog.xml files to change the storage path
 * 
 * What it does:
 *    replaces the dialog.loadContent function with one that changes the content path; then it executes
 *    the original function.
 * 
 */         
function setDialogStorePath(obj, strPath)
{
  var dlgParent=obj.findParentByType('dialog');
  dlgParent.tmpLoad = dlgParent.loadContent;
  dlgParent.loadContent = function(content) {
      dlgParent.content=strPath;
      dlgParent.tmpLoad(dlgParent.content);
  } 
}

var _XHR_MS = false;

function createXHR()
{ 
  var request = false;
  try
  {
    request = new ActiveXObject('Msxml2.XMLHTTP');
    _XHR_MS = true;
  }
  catch (err2)
  {
    try
    {
      request = new ActiveXObject('Microsoft.XMLHTTP');

      _XHR_MS = true;
    }
    catch (err3)
    {
      try
      {
        request = new XMLHttpRequest();
        _XHR_MS = false;
      }
      catch (err1)
      {
        request = false;
      }
    }
  }
  return request;
}

function isXHR_MS()
{
  return _XHR_MS;
}
/*
Found here:
http://www.tek-tips.com/viewthread.cfm?qid=1143850&page=1
function getElementsByClassName( strClassName, obj ) {
    var ar = arguments[2] || new Array();
    var re = new RegExp("\\b" + strClassName + "\\b", "g");

    if ( re.test(obj.className) ) {
        ar.push( obj );
    }
    for ( var i = 0; i < obj.childNodes.length; i++ )
        getElementsByClassName( strClassName, obj.childNodes[i], ar );
    
    return ar;
}
*/

