MSBC_HeroWidget=new function(){
    var _dcsURI = "";
    var _prefix;
    var _sliding=false;
    var _fading=false;
    var _lastSlide=0;
    var _slideDelegates=[];
    var _jumpToDelegates=[];
    var _connectDelegates=[];
    
    var _featureTimeout;
    var _currentFeature=0;
    var _featuresPlaying=false;
    var _totalFeatures = 3;
    var _widgetLoaded=false;
    
    var _elts={
        actions:[],
        connects:[],
        connect:null,
        connectFooter:null,
        callToAction:null,
        featuresDiv:null,
        features:[],
        images:[],
        panels:[],
        contents:[],
        links:[],
        texts:[],
        backgrounds:[],
        jumpTos:[],
        select:null,
        play:null,
        pause:null,
        closeConnect:null
    };
    
    var _panelData = [
	    [{ w: 240, x: 0, ct: -190 }, { w: 240, x: 245, ct: -190 }, { w: 240, x: 490, ct: -190 }],
	    [{ w: 430, x: 0, ct: 30 }, { w: 145, x: 435, ct: -240 }, { w: 145, x: 585, ct: -240 }],
	    [{ w: 145, x: 0, ct: -240 }, { w: 430, x: 150, ct: 30 }, { w: 145, x: 585, ct: -240 }],
	    [{ w: 145, x: 0, ct: -240 }, { w: 145, x: 150, ct: -240 }, { w: 430, x: 300, ct: 40 }]
    ];
    
    function $(id,lax){
        var elt=document.getElementById(id);
        if(!elt&&!lax)throw new Error("Missing UI Element. ID: "+id);
        return elt;
    }
    this.set_prefix=function(prefix){_prefix=prefix+'_';}
    this.popup=function(url, width, height) {
        window.open(url, 'popUp','menubar=0, toolbar=0, location=0, status=0, scrollbars=0, resizable=0, width='+width+'px,height='+height+'px');
    }
    
    
    main();
    function main(){
		$addHandler(window,'load',initialize);
    }
    
    function initialize(){
	    if(!_widgetLoaded)return setTimeout(initialize,50);
        ensureUIElements();
        setEvents();
	    for(var i = 0; i < _totalFeatures; i++) {
		    AjaxControlToolkit.Animation.OpacityAction.play(_elts.backgrounds[i], 0.1, 100, 0);
		    _elts.backgrounds[i].style.display = "block";
		    _elts.backgrounds[i].style.backgroundColor = "#ffffff";
		    if(!i)continue;
	 	    AjaxControlToolkit.Animation.OpacityAction.play(_elts.features[i], 0.1, 100, 0);
		    _elts.features[i].style.display = "block";
	    }
	    toggleFeature(null,true);
    }
    
    this.notifyLoad=function(){
	    _widgetLoaded=true;
    }
    
    function dispose(){
        setEvents(true);
        for(var x in _elts){
            if(_elts[x] instanceof Array){
                for(var i=0;i<_elts[x].length;i++)_elts[x][i]=null;
            }
            _elts[x]=null;
        }
        var delegates=[_slideDelegates,_connectDelegates,_jumpToDelegates];
        for(var d=0;d<delegates.length;d++){
            for(var i=0;i<delegates[d].length;i++)delegates[d][i]=null;
            delegates[d].length=0;
        }
        delegates.length=0;
        MSBC_HeroWidget=null;
    }

    function ensureUIElements(){
        try{
            _elts.widget=$(_prefix+'HeroWidget');
            _elts.widget.className+=(navigator.userAgent.indexOf("MSIE")>-1?' IE':' NotIE');
            _elts.featuresDiv=$(_prefix+'HW_Features');
            _elts.play=$('HWFN_Play');
            _elts.pause=$('HWFN_Pause');
            _elts.connect=$('HWF_Connect');
            _elts.closeConnect=$('HWFC_Action_Close');
            _elts.connectFooter=$('HWFC_Footer');
            _elts.callToAction=$('HWF_CallToActions');
            _elts.select=$('HW_Select');

            var map={
                actions:'HWFC_Action_',
                connects:'HWFC_Content_',
                features:_prefix+'HW_Feature_',
                panels:_prefix+'HW_Panel_',
                contents:_prefix+'HWP_Content_',
                links:'HWP_Link_',
                texts:'HWP_Text_',
                images:'HWP_Image_',
                backgrounds:_prefix+'HWP_Background_',
                jumpTos:'HWFN_JumpTo_'
            }
            for(var x in map){
                if(!(_elts[x] instanceof Array))continue;
                for(var i=1;i<=_totalFeatures;i++)_elts[x].push($(map[x]+i));
            }
            for(var i=0;i<=_totalFeatures;i++){
                _slideDelegates.push(Function.createCallback(slidePanels,i));
                if(i){
                    _connectDelegates.push(Function.createCallback(connect,i));
                    _jumpToDelegates.push(Function.createCallback(selectFeature,i));
                }
            }
            for(var i=0;i<_elts.images.length;i++)fixPNG(_elts.images[i].getElementsByTagName('IMG')[0]);
	        _elts.connectFooter.style.display='none';
	        for(var i=0;i<_elts.connects.length;i++)_elts.connects[i].style.display="none";
        }catch(e){
            TrackClick(e.description||e.message);
            //throw e;
            alert('There was an error initializing the page, please try to reload the page.\nIf the problem persists, please contact support.');
        }
    }

    function setEvents(disposing){
        var handler=disposing?$removeHandler:$addHandler;
        handler(window,'unload',dispose);
	    handler(_elts.featuresDiv,'mouseover',_slideDelegates[0]);
	    for(var i=0;i<_elts.panels.length;i++)handler(_elts.panels[i],'mouseover',_slideDelegates[i+1]);
	    for(var i=0;i<_elts.jumpTos.length;i++)handler(_elts.jumpTos[i],'click',_jumpToDelegates[i]);
        for(var i=0;i<_elts.actions.length;i++)handler(_elts.actions[i],'click',_connectDelegates[i]);
	    handler(_elts.select,'change',selectProduct);
	    handler(_elts.play.parentNode,'click',toggleFeature);
	    handler(_elts.closeConnect,'click',closeConnect);
    }
    function TrackClick(trackCode) {
	    //dcsMultiTrack(_dcsURI, "/Support/wt.html","WT.ti", trackCode);
    }
    
    function selectProduct() {
	    if(_elts.select.selectedIndex>0) document.location = _elts.select.value;
    }

    function slidePanels(e,panelID) {
	    if(_fading)return setTimeout(function(){slidePanels(e,panelID)},50);
	    if (!_sliding && _lastSlide != panelID) {
		    _sliding = true;
		    var effects = new Array();
		    for (var i = 0; i < 3; i++) {
			    var panel = _elts.panels[i];
			    effects.push(new AjaxControlToolkit.Animation.MoveAnimation(panel, null, null, _panelData[panelID][i].x, 0, false));
			    effects.push(new AjaxControlToolkit.Animation.ResizeAnimation(panel, null, null, _panelData[panelID][i].w, 250));
			    panel = _elts.contents[i];
			    effects.push(new AjaxControlToolkit.Animation.MoveAnimation(panel, null, null, _panelData[panelID][i].ct, 27, false));
			    _elts.links[i].style.display = (panelID == 0) ? "block" : "none";
			    _elts.texts[i].style.display = (panelID == 0 || panelID-1 != i) ? "none" : "block";
		    }
		    if (panelID != 0) {
			    panel = _elts.backgrounds[panelID-1];
			    effects.push(new AjaxControlToolkit.Animation.FadeInAnimation(panel, null, null, 0, 0.5));
		    }
		    if (_lastSlide != 0) {
			    panel = _elts.backgrounds[_lastSlide-1];
			    effects.push(new AjaxControlToolkit.Animation.FadeOutAnimation(panel, null, null, 0, 0.5));
		    }
		    effects.push(new AjaxControlToolkit.Animation.ScriptAction(panel, null, null, "MSBC_HeroWidget.slideComplete()"));
		    AjaxControlToolkit.Animation.ParallelAnimation.play(panel, .2, 12, effects);			
		    _lastSlide = panelID;
	    }
    }
    this.slideComplete = function () { _sliding = false; }

    function toggleFeature(e,firstRun) {
	    if (_featuresPlaying) {
            if(_featureTimeout)clearTimeout(_featureTimeout);	    
	        _featuresPlaying=false;
		    _elts.play.style.display = "block";
		    _elts.pause.style.display = "none";
		    TrackClick("header_controls_pause_click");
	    } else {
	        _featuresPlaying=true;
		    _elts.play.style.display = "none";
		    _elts.pause.style.display = "block";
		    TrackClick("header_controls_play_click");
		    selectFeature(null,firstRun*1);
	    }
    }
    function selectFeature(e,featureID) {
	    if(_featureTimeout)clearTimeout(_featureTimeout);
	    var nextFeature;
	    if(featureID){
	        if (e&&_featuresPlaying) toggleFeature(e,false);
	        nextFeature=featureID-1;
	    }else nextFeature=_currentFeature<_totalFeatures-1?_currentFeature + 1:0;

	    if (_currentFeature != nextFeature)waitToFade();
        else restartTimer();

	    function waitToFade(){
	        if(_sliding)return setTimeout(waitToFade,50);
	        _fading=true;
		    var effects=[];
		    for (var i=0;i<_totalFeatures;i++)_elts.features[i].style.display = "block";
		    effects.push(new AjaxControlToolkit.Animation.FadeInAnimation(_elts.features[nextFeature], null, null, 0, 1));
		    effects.push(new AjaxControlToolkit.Animation.FadeOutAnimation(_elts.features[_currentFeature], null, null, 0, 1));
		    effects.push(new AjaxControlToolkit.Animation.ScriptAction(_elts.features[_currentFeature], null, null, "MSBC_HeroWidget.fadeComplete()"));
		    AjaxControlToolkit.Animation.ParallelAnimation.play(_elts.features[_currentFeature], .5, 10, effects);
		    _elts.jumpTos[nextFeature].style.backgroundColor = "#D38527";
		    _elts.jumpTos[_currentFeature].style.backgroundColor = "#F1C929";
		    TrackClick("header_controls_" + (nextFeature - 1) + "_click");
		    _currentFeature = nextFeature;
		    restartTimer();
	    }
	    function restartTimer(){
		    if(_featuresPlaying)_featureTimeout = setTimeout(selectFeature, 8000);
        }
    }
    this.fadeComplete = function () {
	    for (var i = 0; i < _totalFeatures; i++){
		    _elts.features[i].style.display = (i == _currentFeature ? "block" : "none");
        }
        _fading=false;
    }
    function connect(e,connectID) {
	    var connectNames = ["chat", "call", "partner"];
        _elts.connectFooter.style.display='block';
        for(var i=0;i<_elts.connects.length;i++)_elts.connects[i].style.display=(i==connectID-1?"block":"none");
        var effects = new Array();
        // changes div#HWF_Connect {left:323px; top:25px;}
        effects.push(new AjaxControlToolkit.Animation.MoveAnimation(_elts.connect, null, null, 323, 25, false));
	    // changes div#HWF_Connect {width:418px; height:222px;}
        effects.push(new AjaxControlToolkit.Animation.ResizeAnimation(_elts.connect, null, null, 418, 222));
	    // changes div#HWF_CAllToActions {left:420px; top:0px};
	    effects.push(new AjaxControlToolkit.Animation.MoveAnimation(_elts.callToAction, null, null, 420, 0, false));
	    AjaxControlToolkit.Animation.ParallelAnimation.play(_elts.callToAction, .25, 10, effects);
	    TrackClick("connect_panel_" + connectNames[connectID - 1] + "_click");		
    }
    function closeConnect() {
        var effects = new Array();
        // changes div#HWF_Connect {left:641px; top:25px;}
        effects.push(new AjaxControlToolkit.Animation.MoveAnimation(_elts.connect, null, null, 641, 25, false));
        // changes div#HWF_Connect {width:100px; height:52px;}
        effects.push(new AjaxControlToolkit.Animation.ResizeAnimation(_elts.connect, null, null, 100, 52));
        // changes div#HWF_CAllToActions {left:0px; top:0px};
	    effects.push(new AjaxControlToolkit.Animation.MoveAnimation(_elts.callToAction, null, null, 0, 0, false));
	    effects.push(new AjaxControlToolkit.Animation.ScriptAction(_elts.connect, null, null, "MSBC_HeroWidget.closeComplete()"));

	    AjaxControlToolkit.Animation.ParallelAnimation.play(_elts.callToAction, .25, 10, effects);	
	    TrackClick("connect_panel_close_click");
    }
    
    this.closeComplete=function(){
        _elts.connectFooter.style.display='none';
        for(var i=0;i<_elts.connects.length;i++)_elts.connects[i].style.display="none";
    }
    function fixPNG(img){
        var arVersion = navigator.appVersion.split("MSIE");
        var version = parseFloat(arVersion[1])
        if(version >= 7 || !document.body.filters)return;
        img.style.visibility='hidden';
        if(!img.width||!img.height)return setTimeout(function(){fixPNG(img)},10);
        img.style.visibility='inherit';
        var imgName = img.src.toUpperCase();
        if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
             var imgID = (img.id) ? "id='" + img.id + "' " : ""
             var imgClass = (img.className) ? "class='" + img.className + "' " : ""
             var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
             var imgStyle = "display:inline-block;" + img.style.cssText 
             if (img.align == "left") imgStyle = "float:left;" + imgStyle
             if (img.align == "right") imgStyle = "float:right;" + imgStyle
             if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
             var strNewHTML = "<span " + imgID + imgClass + imgTitle
             + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
             + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
             + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
             img.outerHTML = strNewHTML;
        }
    }
}