
$(function ($) {//on page ready
    //setup AJAX
    jQuery.ajaxSetup({
        dataType: "jsonp",
        jsonp: "action"
    });

    //check for web fonts
    isFontFaceSupported.ready(function (supported) {
        if (!supported) { $('body').addClass('nofontface'); }
        else { $('body').addClass('fontface'); }
    });

    //remove border
    $('#nav > li:last > a, #sec_nav li:last a, .side_subscription .item:last, .chat_plans:last').css('border', 'none');
    //remove right border and margin
    $('.two_col:nth-child(2n), .two_col_wide:nth-child(2n), .two_col_noborder:nth-child(2n), .three_col:nth-child(3n), .three_col_noborder:nth-child(3n), .service_channel:last, .chat_category:nth-child(3n)').css({ 'borderRight': 'none', 'marginRight': '0px' });

    //global nav
    $('#nav > li:last ul').css({ 'left': 'auto', 'right': '0' });

    //make link lists into columns
    $('.columnize').makeacolumnlists();

    //search box
    $('.search_box').focus(function () {
        if ($(this).val() == 'Search...') { $(this).val(''); }
        $(this).parent().find('.search_dd').slideDown('fast');
    });
    $('.search_box').keypress(function (event) {
        if (event.keyCode == 13) {
            ShowAllSearchResults($(this).parent());
            return false;
        }
    });
    $('.search_box').keyup(function (event) {
        $(this).parent().find('.search_load').show();
        RefreshSearchResults($(this).parent());
    });
    $('.search_btn, .view_all').click(function () {
        ShowAllSearchResults($(this).closest('div.search'));
    });
    $('body').click(function () {
        if ($('.search_box').val() == '') { $('.search_box').val('Search...'); }
        $('.search_dd').slideUp('fast');
        $('.search_load').hide();
    });
    $('.search_box, .search_dd').click(function (event) {
        event.stopPropagation();
    });

    //home
    $('.home_category').each(function (index) {
        var category_link = $(this).find('li:last a').attr('href');
        $(this).find('a:first').attr('href', category_link);
    });
    $('.home_category').hover(function () {
        $(this).css('background-color', '#f5f5f5');
    }, function () {
        $(this).css('background-color', '#fff');
    });

    //promos
    $('.promo, .multilink_promo, #billboard').each(function (index) {
        var promo_height = $(this).find('.caption').innerHeight();
        var bottom = promo_height - 20;
        $(this).find('.caption').css('bottom', '-' + bottom + 'px');
        $(this).hover(function () {
            $(this).find('.caption').stop().animate({ 'bottom': '0px' }, { queue: false, duration: 450, easing: 'easeOutQuint' });
        }, function () {
            $(this).find('.caption').stop().animate({ 'bottom': '-' + bottom + 'px' }, { queue: false, duration: 350, easing: 'easeInQuint' });
        });
    });

    //tabs
    $(".tab_content").hide();
    if (window.location.hash == '#member') {
        $('.tabs li.member').addClass("active").show();
        $('div.member').show();
    }
    else if ($('input:hidden[id*=tabToSelect]').val() != null && $('input:hidden[id*=tabToSelect]').val() != '0') {
        var val = $('input:hidden[id*=tabToSelect]').val();
        $('.tabs li:nth-child(' + val + ')').addClass("active").show();
        $('.tab_content:nth-child(' + val + ')').show();
    }
    else {
        $('.tabs li:first').addClass("active").show();
        $('.tab_content:first').show();
    }
    $('.tabs li').click(function () {
        $('.tabs li').removeClass('active');
        $(this).addClass('active');
        $('.tab_content').hide();
        var activeTab = $(this).find('a').attr('href');
        $(activeTab).fadeIn();
        return false;
    });

    //modal window
    $('a.popup').nyroModal({ forceType: 'iframe', width: 685, height: 375 });
    $('a.video').nyroModal();

    $.nyroModalSettings({
        processHandler: function (settings) {
            var from = settings.from;
            if (from && from.href && from.href.indexOf('http://www.youtube.com/watch?v=') == 0) {
                $.nyroModalSettings({
                    type: 'swf',
                    width: 640,
                    height: 390,
                    url: from.href.replace(new RegExp("watch\\?v=", "i"), 'v/')
                });
            }
        }
    });

    //link tracking
    $('.track_outgoing_link').click(function () {
        trackEvent.event('event.link', { lastLink: '' + $(this).attr('title') + '' });
    });
    $('.track_portal_link').click(function () {
        trackEvent.event('event.link', { lastLink: 'portal: ' + $(this).attr('title') + '' });
    });

});     //end page ready function

//search box functions
function RefreshSearchResults(searchDiv) {
    var searchTextbox = searchDiv.find('input.search_box');
    //    var searchHidden = searchDiv.find('.search_dd input[type=hidden]')[0];
    //    $(searchHidden).val($(searchTextbox).val());
    //    __doPostBack($(searchHidden).attr('id'), '');
    jQuery.get("/searchservice.svc/search", { query: searchTextbox.val() }, function (data) {
        var searchResults = searchDiv.find('.searchResults');
        var resultTemplate = searchDiv.find('.resultTemplate');

        jQuery('.search_load').hide();
        searchResults.empty();
        if (data && data.d)
            resultTemplate.tmpl(data.d).appendTo(searchResults);
    });
}

function ShowAllSearchResults(searchDiv) {
//    var searchButton = searchDiv.find('.search_btn')[0];
    //    __doPostBack($(searchButton).attr('id').replace(/_/g, '$'), '');
    var searchTextbox = searchDiv.find('input.search_box');
    window.location = '/search/default.aspx?query=' + searchTextbox.val();
}
function EndAsyncRequest() {
    $('.search_load').hide();
}
  
//link tracking
function trackContactForm(reason){
	trackEvent.event('event.link', {contactReason:''+ reason +''});
};
