var BING_API_KEY = '76DE60A6FA05F1C2EB7AEB45A07F2239CB806B94';
var SITE = 'vitalsourcegpo.com';
var BECOME_MEMBER_EMAIL = 'amy.valley@cardinalhealth.com';
var WEBCAST_EMAIL= 'amy.valley@cardinalhealth.com';
var CONTACT_US_EMAIL = 'amy.valley@cardinalhealth.com';

$(document).ready(function() {
    //if ($.browser.mozilla && $.browser.version == "1.8.1.20") {
    //$("#footer_container").css("position", "inherit");
    //}

    $("#menu_container a").attr("hideFocus", "hidefocus");

    // New Button
    $(".button").hover(function() {
        $(this).toggleClass('hovered');
    });

    // Navigation
    $("#navigation li").hover(function() {
        $(this).toggleClass('hovered');
    });

    $("#providers").hover(function() {
        $('ul.subnav', this).slideDown('fast').show();
    }, function() {
        $('ul.subnav', this).slideUp('fast').show();
    });

    $(".subnav li").hover(function() {
        $('.triangle', this).css('visibility', 'visible');
    }, function() {
        $('.triangle', this).css('visibility', 'hidden');
    });


    // Logged Navigation
    $("#navigation-logged li").hover(function() {
        $(this).toggleClass('hovered');
    });

    // Forms
    //$('.field input[type=text]').focus(function(){
    //$(this).val('');
    //});

    // RSS Ajax
    var news_releases = $("#news_releases");
    if (news_releases.length) {
        $.ajax({
            type: 'GET',
            url: "newsticker.xml",
            dataType: "xml",
            success: function(xml) {
                var str = '';
                jQuery(xml).find('item').each(function(i) {
                    var title = jQuery(this).find('title').text();
                    var link = jQuery(this).find('link').text();
                    var date = new Date(jQuery(this).find('pubDate').text());
                    var month = date.getMonth() + 1;
                    if (month < 10) {
                        month = '0' + month;
                    }
                    var day = date.getDate() + 1;
                    if (day < 10) {
                        day = '0' + day;
                    }
                    var year = date.getFullYear();
                    str += '<li><div class="date">' + month + '.';
                    str += day + '.' + year;
                    str += '</div><div class="news_title"><a href="' + link + '">' + title;
                    str += '</a></div></li>';
                });
                news_releases.append(str);
            }
        });
    }

    //Search Button
    $("#search_button").click(function() {
        $(this).attr("hideFocus", "hidefocus");
        if ($("#terms").val() != '') {
            $(this).parent().submit();
        }
        else {
            return false;
        }
    });

    // Search Results
    var search_results_container = $("#search_results_container");
    if (search_results_container.length) {
        var ajax_loader = $("#ajax_loader");
        var search_input = $.getURLParam("terms");
        var search_txt = decodeURIComponent(search_input.split('+').join(' '));
        if (search_input == '') {
            $("#terms").val('');
            ajax_loader.hide("slow");
        } else {
            $("#terms").val(search_txt);
            var keyword = encodeURIComponent(search_input);// Bing Search API
            var bing_url = 'http://api.search.live.net/json.aspx?JsonType=callback&JsonCallback=?&Appid=' + BING_API_KEY + '&query=site:' + SITE + ' ' + keyword + '&sources=web';
            $.ajax({
                type: "GET",
                url: bing_url,
                dataType:"jsonp",
                beforeSend: function() {
                    ajax_loader.show("fast");
                },
                success: function(response) {
                    var total = response.SearchResponse.Web.Total;
                    if (total > 0) {
                        $("#pagination").pagination(total, {
                            items_per_page:10,
                            callback: handlePaginationClick
                        });
                    }
                    else {
                        ajax_loader.hide("fast");
                        $("#search_results").html("<div id='no'>No Results for '" + $("#terms").val() + "'</div>");
                    }
                }
            });
        }
    }

    //Become Member form to mail
    form_to_mail('become_member_form', 'become_member_submit', 'Become A Member', BECOME_MEMBER_EMAIL);
    form_to_mail('webcast_form', 'webcast_submit', 'Register for Webcasts', WEBCAST_EMAIL);
    form_to_mail('contact_us_form', 'contact_us_submit', 'Contact Us', CONTACT_US_EMAIL);

    // Functions
    function form_to_mail(form_id, form_btn_id, subject, email){
        $('#' + form_btn_id).click(function(){
            var msg_body = '';
            $('#' + form_id + ' input,select').each(function(i,e){
                var el = $(e);
                msg_body += el.attr('label') + el.val();
                msg_body += ' %0A%0A ';
            });
            $(this).attr('href','mailto:' + email + '?subject=' + subject + '&body=' + msg_body);
        });
    }
//    function form_to_mail(form_id, form_btn_id, subject, email){
//        $('#' + form_btn_id).click(function(){
//            var msg_body = '';
//            $('#' + form_id + ' input,select').each(function(i,e){
//                var el = $(e);
//                msg_body += el.attr('label') + el.val();
//                msg_body += ' %0A%0A ';
//            });
//            $(this).attr('href','mailto:' + email + '?subject=' + subject + '&body=' + msg_body);
//        });
//    }

    function handlePaginationClick(page_index, jq) {
        var search_input = $.getURLParam("terms");
        var keyword = encodeURIComponent(search_input);// Bing Search API
        var offset = 0;
        if (page_index > 0) {
            offset = (10 * (page_index)) + 1;
        }
        var bing_url = 'http://api.search.live.net/json.aspx?JsonType=callback&JsonCallback=?&Appid=' + BING_API_KEY + '&query=site:' + SITE + ' ' + keyword + '&sources=web&web.offset=' + offset;
        $.ajax({
            type: "GET",
            url: bing_url,
            dataType:"jsonp",
            beforeSend: function() {
                ajax_loader.show("fast");
            },
            complete: function() {
                ajax_loader.hide("fast");
            },
            success: function(response) {
                $("#search_results").html('');
                //if (response.SearchResponse.Web.Results.length) {
                if (response.SearchResponse.Web.Total > 0) {
                    var results = '';
                    $.each(response.SearchResponse.Web.Results, function(i, data) {
                        var title = data.Title;
                        var dis = data.Description;
                        var url = data.Url;
                        var result_class = 'search_result';
                        if (i % 2 == 0) {
                            result_class += ' even'
                        }
                        results += "<div class='" + result_class + "'>" +
                                "<a class='title' href='" + url + "'>" + title + "</a>" +
                                "<div class='description'>" + dis + "</div></div>";
                        //"<div class='url'>" + url + "</div></div>";
                    });
                    $("#search_results").append(results);
                }
                else {
                    $("#search_results").html("<div id='no'>No Results</div>");
                }
            }
        });
    }
});



