var AppBehavior = Class.create();
AppBehavior.Load = function() {
	OS.RegisterBehaviour(AppBehavior.Rules);
}
AppBehavior.Rules = {
    '#Media_Splash': function(element) {
        //swfobject.embedSWF("flash/BAX_3000_Microsite.swf", 'Media_Splash', "980", "576", "9.0.0", "flash/expressInstall.swf", null, { wmode: 'transparent' }, null);
    },
    '#Media_Webinar': function(element) {
        swfobject.embedSWF("flash/webinar/shell.swf", 'Media_Webinar', "980", "659", "9.0.0", "flash/expressInstall.swf", null, { wmode: 'transparent' }, null);
    },
    '#Media_Webinar2': function(element) {
        swfobject.embedSWF("flash/webinar2/shell.swf", 'Media_Webinar2', "980", "659", "9.0.0", "flash/expressInstall.swf", null, { wmode: 'transparent' }, null);
    },
    '#Cnt_Content': function(element) {
        //Event.observe(window, 'resize', _OnResize);
    },
    '.command': function(element) {
        Application.SetRollover(element);
    },
    '#Cnt_Content a': function(element) {
        Application.SetRollover(element);
    },
    '.togglable': function(element) {
        //Application.SetTogglableBehavior(element, null, _OnResize);
    },
    '#Nav_Main .navItem': function(element) {
        Application.SetRollover(element);
    },
    '#Nav_Contact .navItem': function(element) {
        Application.SetRollover(element);
    },
    '#Nav_Footer .navItem': function(element) {
        Application.SetRollover(element);
    },
    'body': function(element) {
        Event.observe($(document), 'click', function(e) {
            $$('.autocomplete').each(function(autocomplete) {
                if (Position.within(autocomplete, e.pointerX(), e.pointerY())) return;
                Element.hide(autocomplete);
            });
        });
    }
};

AppBehavior.NavigateTo = function(section) {
}
AppBehavior.OnChangeHistory = function(hash) {
    if (hash.length == 0 && Application.CurrentHash) {
        Application.CurrentHash = null;
    }
    if (Application.CurrentHash != hash) {
        var arrItem = hash.split('|');
        //if (arrItem.length < 2) return;
        Application.CurrentHash = hash;
        AppBehavior.NavigateTo(arrItem[0]);
    }
    if (Prototype.Browser.IE) setTimeout(function() { Application.SetTitle(Application.Title); }, 0);
}

AppBehavior.ShowAutocompleter = function(autocompleter) {
    autocompleter.changed = true;
    autocompleter.hasFocus = true;
    if (autocompleter.observer) clearTimeout(autocompleter.observer);
    autocompleter.observer =
		setTimeout(autocompleter.onObserverEvent.bind(autocompleter), autocompleter.options.frequency * 1000);
}

AppBehavior.LastActivePanel = null;
AppBehavior.LastActiveNavItem = null;
AppBehavior.OnMainPanelSwitch = function(active, navItem) {
};

AppBehavior.SliderOptions = new Hash();
AppBehavior.SliderValue = new Hash();
AppBehavior.SliderSpace = function(key) {
    return 2;
}
AppBehavior.FormatSliderValueMin = function(key, value) {
    return value;
}
AppBehavior.FormatSliderValueMax = function(key, value) {
    return value;
}
AppBehavior.SliderChange = function(key, iMin, iMax) {
    if (typeof (SearchBehavior) != 'undefined') {
        if (SearchBehavior.fireSliderChanged) SearchBehavior.fireSliderChanged(key, iMin, iMax);
    }
}
AppBehavior.SliderMaxValue = function(key) {
    var arrOptions = AppBehavior.SliderOptions.get(key);
    return (arrOptions)[arrOptions.length - 1];
}
AppBehavior.BuildSlider = function(key, element) {
    var eMin = element.down('.handleMin');
    var eMax = element.down('.handleMax');

    //Safari fix
    if (element.offsetWidth > 500 || eMin.offsetWidth > 100 || eMax.offsetWidth > 100) { setTimeout(function() { AppBehavior.BuildSlider(key, element) }, 50); return; }
    var eMinValue = element.down('.valueMin').down('.value');
    var eMaxValue = element.down('.valueMax').down('.value');
    var iIncrement = 10000;
    var arrOptions = AppBehavior.SliderOptions.get(key);
    var oValue = AppBehavior.SliderValue.get(key) || {};
    var fSlide = function(value, slider, changed) {
        var iMin = arrOptions[parseInt(value[0])];
        var iMax = arrOptions[parseInt(value[1])];
        var iMaxBound = arrOptions[arrOptions.length - 1];
        if (oValue.maxIndex && oValue.maxIndex - AppBehavior.SliderSpace(key) >= 0) iMaxBound = parseInt(arrOptions[oValue.maxIndex - AppBehavior.SliderSpace(key)]) || 0;
        var iMinBound = arrOptions[0];
        if (oValue.minIndex && oValue.minIndex + AppBehavior.SliderSpace(key) < arrOptions.length) iMinBound = parseInt(arrOptions[oValue.minIndex + AppBehavior.SliderSpace(key)]) || 0;

        AppBehavior.SliderChange(key, iMin, iMax);
        var bValid = true;
        if (iMin > oValue.minValue && (value[0] - value[1] >= -1 || iMin > iMaxBound)) {
            value[0] = value[1] - AppBehavior.SliderSpace(key); if (value[0] < 0) value[0] = 0;
            iMin = arrOptions[parseInt(value[0])];
            slider.setValue(value[0], 0);
        }
        if (iMax < oValue.maxValue && (value[1] - value[0] <= 1 || iMax < iMinBound)) {
            value[1] = value[0] + AppBehavior.SliderSpace(key); if (value[1] >= arrOptions.length) value[1] = arrOptions.length - 1;
            iMax = arrOptions[parseInt(value[1])];
            slider.setValue(value[1], 1);
        }
        //console.log(value[0]);
        oValue.minValue = iMin;
        oValue.minIndex = parseInt(value[0]) || 0;
        eMinValue.innerHTML = AppBehavior.FormatSliderValueMin(key, iMin);
        oValue.maxValue = iMax;
        oValue.maxIndex = parseInt(value[1]) || 0;
        var sMax = AppBehavior.FormatSliderValueMin(key, iMax);
        if (iMax >= AppBehavior.SliderMaxValue(key)) sMax += '+';
        eMaxValue.innerHTML = sMax;

        AppBehavior.SliderValue.set(key, oValue);

        var iLeft = parseInt(eMin.getStyle('left')) || 0;
        iLeft += 10;
        var iRight = parseInt(eMax.getStyle('left')) || 0;
        var eActiveValues = element.down('.activeValues');
        eActiveValues.setStyle({
            left: iLeft + 'px',
            width: (iRight - iLeft + 10) + 'px'
        });
    }
    var oSlider = new Control.Slider([eMin, eMax], element, {
        axis: 'horizontal',
        range: $R(0, arrOptions.length - 1),
        onSlide: function(value, slider) { fSlide(value, slider, false); },
        onChange: function(value, slider) { fSlide(value, slider, true); },
        onCreated: function(slider) {
            slider.setValue(0, 0);
            slider.setValue(arrOptions.length, 1);
        }
    });
    oSlider.applyDefault = function() {
        this.setValue(0, 0);
        this.setValue(arrOptions.length, 1);
    }
    return oSlider;
}
AppBehavior.Autocompletes = new Hash();
AppBehavior.InitStaticAutocomplete = function(commandElement, inputContainer, autocompleteContainer, data, options) {
    commandElement = $(commandElement);
    inputContainer = $(inputContainer);
    autocompleteContainer = $(autocompleteContainer);
    if (!commandElement || !inputContainer || !autocompleteContainer) return;
    var sKey = Element.identify(commandElement);
    if (!options) options = { partialChars: 0, partialSearch: true, fullSearch: true }
    AppBehavior.Autocompletes.set(sKey, new Autocompleter.Local(inputContainer, autocompleteContainer, data, options));
    Event.observe(sKey, 'click', function() { AppBehavior.ShowAutocompleter(AppBehavior.Autocompletes.get(sKey)); });
}

AppBehavior.ProcessViewChange = function(view) {
    new Ajax.Request('webservices/Contact.ashx', {
        method: 'post',
        parameters: {
            Command: 'SaveView',
            View: view
        },
        onFailure: function() {
            console.warn('Could not store view.  Please try again later.');
        }
    });
}
AppBehavior.OnTrack = function(view) {
    //Force webinar tracking
    if(!view) return;
    //console.log('OnTrack: ' + view);
    if (view.toLowerCase().indexOf("chapter") >= 0) {
        AppBehavior.ProcessViewChange(view);
    }
}
Application.TrackObserver = AppBehavior;

AppBehavior.Load();

