// Main JavaScript Init
$(document).ready(function(){
    
    initMenu();
    initToggle();
    initTabs();
    
    if ($("#test_bandwidth").length>0) {
        testBandwidth();
    }    
    if ($("div.latestNews").length>0) {
        initNewsTicker();
    }            
    if ($("#application_form").length>0) {        
        initApplyForm();
    }    
    if ($("#spanish_contact_cards_form").length>0) {
        espContactCards();
    }
    if ($("#bug_reporting_form").length>0) {
        bugReportingForm();
    }
    //facebox lightbox
    $('a[rel*=facebox]').facebox({
        loadingImage : 'http://www.sorensonvrs.com/assets/images/loading.gif',
        closeImage   : 'http://www.sorensonvrs.com/assets/images/closelabel.png'
    });
    
});

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) {
    
    // page controller only returns content when requested from ajax        
    $.post('http://www.sorensonvrs.com/' + page,{},function(data) {
        $('#subContentWrap').html(data);
        initToggle();
        initTabs();
        $('#video_chooser').hide(); //hide video chooser when using ajax'd content
    },'html');
    
    // last segment of URI is selected page    
    $.post('http://www.sorensonvrs.com/template/secondary_nav/' + page,{},function(data) {
        $('#leftBar').html(data);
    },'html');
    
    // last segment of URI is selected page
    $.post('http://www.sorensonvrs.com/template/header/' + page,{},function(data) {
        $('#header').html(data);
        initMenu();
    },'html');
    
};


function getheader(page) {
    
    // last segment of URI is selected page
    $.post('http://www.sorensonvrs.com/template/header/' + 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";
    }
}


/* submitQASPicklist ensures that at least one picklist option button has been selected before submitting the form */
function submitQASPicklist() {
    var items = document.getElementsByName("picklist_input");
    for (var i=0; i < items.length; i++) {
        if (items[i].checked == true) {
            $('#application_form').submit();
        }
    }
}    

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

function selectShow(select, trigger, element) {
    $(select).change(function() {
        //$(element).css("display", ($(this).val() == trigger) ? 'block' : 'none');
        // use the option label so we don't need to know value here
        $(element).css("display", ($("option:selected", this).text() == trigger) ? 'block' : 'none');        
    });
    //fire change event in case trigger is default value
    $(select).trigger("change"); 
}


