$(document).ready(function(){

    // Attach VideoJS to player
    if ($('.video-js-box').length) VideoJS.setupAllWhenReady();

    // Show/Hide Arrows - about/news
    $('.title').click(function(){

        var parent = $(this).parent();
        if (parent.find('.hidden-content').is(':hidden')) {

            parent.removeClass('collapsed');
            parent.addClass('expanded');
            parent.find('.hidden-content').css('display', 'block');

        } else {

            parent.removeClass('expanded');
            parent.addClass('collapsed');
            parent.find('.hidden-content').css('display', 'none');

        }

    });




    // Flip Gold/Region partners - partners/resellers
    $('#gold-link').click(function(){
        $('#region-results').hide();
        $('#gold-results').show();

        var typeText = $(this).text();
        var regionText = $('#region-link').text();

        $(this).html('<strong>'+typeText+'</strong>');
        $('#region-link').html('<a href="">'+regionText+'</a>');
        return false;
    });

    $('#region-link').click(function(){
        $('#gold-results').hide();
        $('#region-results').show();

        var regionText = $(this).text();
        var typeText = $('#gold-link').text();

        $(this).html('<strong>'+regionText+'</strong>');
        $('#gold-link').html('<a href="">'+typeText+'</a>');
        return false;
    });




    // Flip Releases/Highlights in homepage widget
    $('#news-widget > a').click(function(){
        var content = '.'+$(this).attr('title').toLowerCase();
        $('.active').removeClass('active');
        $(this).addClass('active');
        $('#news-content > div:visible').hide();
        $('#news-content > '+content).show();
        return false;
    });




    // Track download button clicks
    $('.download').click(function(){
        var product = $(this).attr('title').split(' - ');
        pageTracker._trackEvent('Product Downloads', product[0], product[1]);
        mktoMunchkinFunction('visitWebpage',{Url:$(this).attr('href')});
        setTimeout(function(){},100);
    });




    // Track license file button clicks
    $('.license').click(function(){
        var product = $(this).attr('title');
        pageTracker._trackEvent('License File', product);
        mktoMunchkinFunction('visitWebpage',{Url:$(this).attr('href')});
        setTimeout(function(){},100);
    });




    // Add known lead to EE Eval, Download page
    $('.add-campaign').click(function(){

        var time = new Date().getTime();
        var originalText = $(this).text();
        var link = $(this);
        link.text('Please Wait...');

        $.ajax({
           url          : '/ajax/add-to-eval-campaign.php?'+time,
           error        : function(jqXHR, textStatus, errorThrown) {
               window.location = link.attr('href');
               console.log(jqXHR);
               console.log(textStatus);
               console.log(errorThrown);
           },
           success      : function(data, textStatus, jqXHR){
               link.text(originalText);
               window.location = link.attr('href');
           }
        });

        return false;

    });




    // Jump to specific url when jump menu changes
    $('.jump').change(function(){
        var form = $(this).parents('form');
        window.location = form.attr('action')+$(this).val()+'/';
    });




    $('#customers_success-stories select').change(function(){
        $('#share-block').css('display','none');
        $.post('/customers/customer_list_ajax.php', $('#filters').serialize(), function(data){
            $('#customer_list').html(data);
        });
    });

    $('#share').click(function(){
        $.post('/customers/customer_share_ajax.php', $('#filters').serialize(), function(data){
            $('#share-block').css('display','block');
            $('#share-field').attr('value', data);
        });
        return false;
    });



    if ($.fn.popupWindow) {
        $('.popupwindow').popupWindow({
            windowURL:document.location+'?display=printer_friendly',
            resizeable:1,
            width:650,
            height:650,
            scrollbars:1
        });
    }


    $('.print').click(function(){
        window.print();
    });




    // Disable multi-session event forms
    $('#event-session-form form input, #event-session-form form select, #event-session-form form textarea').attr('disabled','disabled');
    $('#event-session').change(function(){
        if ($(this).val() != '') {
            $('#event-session-form form input, #event-session-form form select, #event-session-form form textarea').removeAttr('disabled');
            $('#webinar_session').val($(this).val());
        } else {
            $('#event-session-form form input, #event-session-form form select, #event-session-form form textarea').attr('disabled','disabled');
        }
    });




    // Help me out Tips - Expand / Collapse
    $('#helpmeout_tips h3').click(function(){
        if ($(this).next().hasClass('collapse')) {
            $('.collapse').prev().find('span').text('+');
            $('.collapse').slideUp('slow').removeClass('collapse');
            return false;
        }
        $('.collapse').prev().find('span').text('+');
        $('.collapse').slideUp('slow').removeClass('collapse');
        $(this).find('span').text('-');
        $(this).next().slideDown('slow').addClass('collapse');
    });



    // Alternate contact us form
    $('#contact_form_select select').change(function(){
        $('#contact_form').load('/forms/controllers/inline_contact.php?form='+this.value);
    });



    // Homepage FOTM clickable blocks
    $('#fotm .block').click(function(){
//        window.location = $(this).find('a').attr('href');
    });
    
    
    
    // Homepage latest news ticker
    if ($('#home_home').length) {
        setInterval(function(){
            $('.releases span.first').fadeOut(function(){
                var nextRelease = $(this).next();
                if (nextRelease.index() == -1) nextRelease = $('.releases p span').eq(0);
                $(this).removeClass('first');
                nextRelease.fadeIn().addClass('first');
                
            });
        },4000);
    }



    // Convert phone to text field
    $('.edit').live('click',function(){
        var phoneNumber = $('#phone').text();
        $('#phone').html('<input type="text" value="'+phoneNumber+'" />');
        $(this).text('(Update)').addClass('update').removeClass('edit');
        return false;
    });
    

    // Post updated phone to Marketo & Revert text field to text phone
    $('.update').live('click',function(){
        $(this).addClass('waiting');
        $('[name="phone"]').val($('#phone').find('input').val());

        // Submit form via AJAX
        $.ajax({
            type    : 'POST',
            data    : $('#request-a-quote').serialize(),
            url     : baseURL+'/forms/validation/web_leads.validation.php',
            success : function(data){
                // Revert text field & display success message
                var phoneNumber = $('#phone').find('input').val();
                $('#phone').html(phoneNumber);
                $('.waiting').text('(Edit)').addClass('edit').removeClass('update').removeClass('waiting');
                $('#success').show();
            }
        });
        return false;
    });




});
