// - ///////////////////////////////////////////////////////////////////////
// - BE BASE MODULE
// - ///////////////////////////////////////////////////////////////////////
//fields
beBaseModule.prototype._transport;
beBaseModule.prototype._channelId = 'site.shared';
beBaseModule.prototype._homeSplashExecuter = null;
beBaseModule.prototype._homeSuccessExecuter = null;
beBaseModule.prototype._homeProvidersExecuter = null;
beBaseModule.prototype._homeSuccessCount = 0;
beBaseModule.prototype._homeSuccessCurrent = 0;
beBaseModule.prototype._homeSplashCount = 0;
beBaseModule.prototype._homeSplashCurrent = 0;

function beBaseModule() {}

beBaseModule.prototype.initialize = function(transport){
	this._transport = transport;
	
	if($('home') != null){this.homeConfigure();}	
	if($('contact') != null){this.contactConfigure();}
	if($('about') != null){this.aboutConfigure();}
	if($('team') != null){this.teamConfigure();}
	if($('success') != null){this.successConfigure();}
};

//HOME METHODS
beBaseModule.prototype.homeConfigure = function(){    
    this._homeSplashCount = $$('.home-splash').length;
    this._homeSplashCurrent = 1;
    this._homeSuccessCount = $$('.home-success').length;
    this._homeSuccessCurrent = 1;
    
    var me = this;
    this._homeSplashExecuter = new PeriodicalExecuter(this.homeToggleSplash.bind(this), 10);
    this._homeSuccessExecuter = new PeriodicalExecuter(this.homeToggleSuccess.bind(this), 8);
    
    $$('.home-splash-nav').each(function(div){
        div.select('a').each(function(a){
            a.observe('click', me.homeSplashNavTo.bind(me));
        });
    });
    
    this.homeProvidersLoad(null);
    this._homeProvidersExecuter = new PeriodicalExecuter(this.homeProvidersLoad.bind(this), 12);   
};

beBaseModule.prototype.homeProvidersLoad = function(event){
    this._transport.transmitRequest(this._channelId, 'getrandomproviders', {ignoreJson:true}, this.homeProvidersReceived.bind(this));    
};

beBaseModule.prototype.homeProvidersReceived = function(objResponse){
    var provider;
    var s = '';   
    for(var x=0; x<objResponse.providers.length; x++){
        provider = objResponse.providers[x];
        
        s += '<a href="/about/providers/' + escape(provider.ContactDisplayName) + '-' + provider.Id + '/">';
        s += '<img src="/ProfileImageService.ashx?' + provider.Id + '" border="0"/>';
        s += '</a>';
        s += '<strong><a href="/about/providers/' + escape(provider.ContactDisplayName) + '-' + provider.Id + '/">' + provider.ContactDisplayName + '</a></strong>';
        s += (provider.BioSummary.length > 0) ? '<p>' + provider.BioSummary.truncate(80) + '</p>' : '<p>For more information visit this providers profile</p>';
        s += '<hr/>';
    }
    
    $('home-providers').update(s);    
};

beBaseModule.prototype.homeToggleSplash = function(event){
    this.homeSplashHide(this._homeSplashCurrent);    
    if(this._homeSplashCurrent + 1 > this._homeSplashCount){this._homeSplashCurrent = 1;}else{this._homeSplashCurrent += 1;}    
    this.homeSplashShow(this._homeSplashCurrent);
};

beBaseModule.prototype.homeSplashNavTo = function(event){
    this._homeSplashExecuter.stop();
    this.homeSplashHide(this._homeSplashCurrent);
    this.homeSplashShow(event.element().readAttribute('rel').replace('home-splash-',''));
};

beBaseModule.prototype.homeSplashHide = function(elementId){
    $('home-splash-' + elementId).hide();
};

beBaseModule.prototype.homeSplashShow = function(elementId){
    $('home-splash-' + elementId).show();
    this._homeSplashCurrent = elementId;
};

beBaseModule.prototype.homeToggleSuccess = function(event){
    new Effect.Fade($('home-success-' + this._homeSuccessCurrent), { duration: 2.0 });    
    if(this._homeSuccessCurrent + 1 > this._homeSuccessCount){this._homeSuccessCurrent = 1;}else{this._homeSuccessCurrent += 1;}    
    this.homeSuccessShow.delay(2, this._homeSuccessCurrent);
};

beBaseModule.prototype.homeSuccessShow = function(elementId){
    new Effect.Appear($('home-success-' + elementId), { duration: 2.0 });
};

//ABOUT METHODS
beBaseModule.prototype.aboutConfigure = function(){   
    
};

//TEAM METHODS
beBaseModule.prototype.teamConfigure = function(){
    var me = this;
    $$('.bio-extend').each(function(button){button.observe('click', me.teamBioExtend.bind(me));});
};

beBaseModule.prototype.teamBioExtend = function(event){
    event.stop();
    var button = event.element();
    button.next().show();
    button.hide();
};

//SUCCESS METHODS
beBaseModule.prototype.successConfigure = function(){
    var me = this;
    $$('.quote-extend').each(function(button){button.observe('click', me.successQuoteExtend.bind(me));});
};

beBaseModule.prototype.successQuoteExtend = function(event){
    event.stop();
    var button = event.element();
    button.up().next().show();
    button.hide();
};

//CONTACT METHODS
beBaseModule.prototype.contactConfigure = function(){   
    $('contactSubmit').observe('click', this.contactFormSubmit.bind(this));
};

beBaseModule.prototype.contactFormSubmit = function(event){
    event.stop();
    var errorList = '';
    
    if($('contactName').value == ''){errorList += '<li>Enter your name</li>';}
    if($('contactEmail').value == ''){errorList += '<li>Enter your email address</li>';}
    if($('contactPhone').value == ''){errorList += '<li>Enter a phone number to call</li>';}
    if($('contactBestTimeToCall').value == ''){errorList += '<li>Enter the best time to call</li>';}
    if($('contactComments').value == ''){errorList += '<li>Enter your comments and questions</li>';}
    
    if(errorList != ''){
        $('contactError').update('<ul>' + errorList + '</ul>');
        $('contactError').show();
    }else{
        $('contactError').hide();
        
        var request = {
            name : $('contactName').value,
            email : $('contactEmail').value,
            phone : $('contactPhone').value,
            time : $('contactBestTimeToCall').value,
            comments : $('contactComments').value
        };
        
        this._transport.transmitRequest(this._channelId, 'setcontact', request, this.contactFormSubmitCompleted.bind(this));
    }
};

beBaseModule.prototype.contactFormSubmitCompleted = function(res){
    if(res.result == 'ok'){
        $('contactForm').hide();
        $('contactSuccess').show();
        be_globalSiteFramework.trackPageview('/contact-us-completed');
    }else{
        $('contactError').update(res.message);
        $('contactError').show();        
    }
};