// Main JavaScript Init
$(document).ready(function(){
    
    initMenu();
    initNewsTicker();
    initToggle();
    initTabs();
    initApplyForm();
    
    if ($("#spanish_contact_cards_form").length>0) {
        espContactCards();
    }
    if ($("#bug_reporting_form").length>0) {
        bugReportingForm();
    }
    
    
});

function initMenu() {
    //init dropdown nav
    $('#mainNav').dropDownMenu({timer: 500, parentMO: 'parent-hover', childMO: 'child-hover'});
};

function initToggle() {
    //init "read more" content toggle
    $('h6.toggle').click(function() {
        $(this).next().toggle("normal");
        $(this).toggleClass('upArrow');
        return false;
    });
    
    //init FAQ toggle
    $('a.toggle').click(function() {
        $(this).next().toggle("normal");
        $(this).toggleClass('open');
        return false;
    });    
};

function initTabs() {
    if ($("ul#tabs").length>0) {  
        //faq tabs
        $("ul#tabs").tabs("div.panes > div").history(); 
    };
}

function getpage(page) {
    
    $.post('http://www.sorensonvrs.com/content/' + page,{},function(data) {
        $('#subContentWrap').html(data);
        initToggle();
        initTabs();
        $('#video_chooser').hide(); //hide video chooser when using ajax'd content
    },'html');
    
    $.post('http://www.sorensonvrs.com/template/secondary_nav?page=' + page,{},function(data) {
        $('#leftBar').html(data);
    },'html');
    
    $.post('http://www.sorensonvrs.com/template/header?page=' + page,{},function(data) {
        $('#header').html(data);
        initMenu();
    },'html');
    
};


function getheader(page) {
    
    $.post('http://www.sorensonvrs.com/template/header?page=' + page,{},function(data) {
        $('#header').html(data);
        initMenu();
    },'html');
    
};

function initNewsTicker() {
    // init scrollable  
    
    if($.browser.msie && $.browser.version < 7) {
        //no alpha effects for ie6
        
        $("div.latestNews").scrollable({
            vertical: true,  
            size: 1,
            loop: true, 
            speed: 600
        }).autoscroll({
            autoplay: true,
            steps: 1,
            interval: 4000 
        });
        
    } else {
        
        $("div.latestNews").scrollable({
            vertical: true,  
            size: 1,
            loop: true, 
            speed: 600,
            onBeforeSeek: function() { this.getItems().fadeTo(150, 0.2); }, 
            onSeek: function() { this.getItems().fadeTo(150, 1); } 
        }).autoscroll({
            autoplay: true,
            steps: 1,
            interval: 4000 
        });
        
    }
};  


function submitSearch() {
    document.forms["search_form"].submit();
}

function espContactCards() {
    
    if ($.browser.msie) {
        $('input:radio').click(function(){
            this.blur();
            this.focus();
        });
    }    
    
    $('input:radio[name=template]').change(function(){
        var state = true;
        if ($(this).val() == 'directvp_esp_cards_template') {
            state = false;
        }
        
        $("#email").css("display", (state) ? 'block' : 'none');
        $("label[for='email']").css("display", (state) ? 'block' : 'none');
        
        $("#phone2").css("display", (state) ? 'block' : 'none');        
        $("label[for='phone2']").css("display", (state) ? 'block' : 'none');
        
        $("label[for='phone1']").html((state) ? "Número 1" : "Número");
    });            
}

function bugReportingForm() {
    
    if ($.browser.msie) {
        $('input:radio').click(function(){
            this.blur();
            this.focus();
        });
    }    
    
    $('input:radio[name=reportType]').change(function(){
        var state = true;
        if ($(this).val() == 'Suggestion') {
            state = false;
        }        
        $("#bug_reporting").css("display", (state) ? 'block' : 'none');
    });    

}


function clearField(tF) {
    if(tF.defaultValue == tF.value) {
        tF.value = ""; tF.style.color = "black";
    }
}


function initApplyForm() {
    //selectShow(select input, trigger value, element to control)
    selectShow('#applyingForAnother', 'Yes', '#whoApplyingFor');
    selectShow('#internetProvider', 'Other', '#otherProvider');
    selectShow('#vrsAtWork', 'Yes', '#workInfo');    
}

function selectShow(select, trigger, element) {
    $(select).change(function() {
        $(element).css("display", ($(this).val() == trigger) ? 'block' : 'none');
    });
    //fire change event in case trigger is default value
    $(select).trigger("change"); 
}

