﻿var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}

function jsddm_close()
{ if (ddmenuitem) ddmenuitem.css('visibility', 'hidden'); }

function jsddm_timer()
{ closetimer = window.setTimeout(jsddm_close, timeout); }

function jsddm_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

$(document).ready(function() {
    $('#jsddm > li').bind('mouseover', jsddm_open);
    $('#jsddm > li').bind('mouseout', jsddm_timer);
});

document.onclick = jsddm_close;


// Social Networking
//$ektron(function() {
//    $ektron(".sharerTwitter").click(function() {
//        var title = document.title;
//        var targetUrl = 'http://twitter.com/home/?status='
//                            + encodeURIComponent(title) + "+++"
//                            + encodeURIComponent(window.location);
//        window.open(targetUrl);
//    });
//    $ektron(".sharerFacebook").click(function() {
//        var title = document.title;
//        var targetUrl = 'http://www.facebook.com/sharer.php'
//                                + '?u=' + encodeURIComponent(window.location)
//                                + "&t=" + encodeURIComponent(title);
//        window.open(targetUrl);
//    });
//    $ektron(".sharerMyspace").click(function() {
//        var title = document.title;
//        var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto'
//                            + '&t=' + encodeURIComponent(title)
//                            + '&c=' + encodeURIComponent('<p><strong>' + title + '</strong></p>')
//                            + '&u=' + encodeURIComponent(window.location) + '&l=3';
//        window.open(targetUrl);
//    });
//});

jQuery(function() {
//    jQuery(".sharerEmail").click(function() {
//        var id = getQueryVariable("id");
//        var url = escape(window.location);
//        window.location = '/EmailStory.aspx?id=' + id + "&url=" + url;
//    });
//    jQuery(".sharerPrint").click(function() {
//        var id = getQueryVariable("id");
//        var url = escape(window.location);
//        window.location = '/Printer.aspx?id=' + id + "&url=" + url;
//    });

    // search
    jQuery(".gcse").css("background", "url(/images/gcse_bg.jpg)");
    jQuery(".gcse").focus(
        function() {
            jQuery(this).css("background", "#fff");
    });
    jQuery(".gcse").blur(
        function() {
            jQuery(this).css("background", "url(/images/gcse_bg.jpg)");
    });

});

// QueryString
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var parms = query.split("?");
    if (parms[0] != null) {
        var vars = parms[0].split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
    } else {
        return "";
    }
}

// function to trim a twitter status to 140 chars (based on title and url)
function trimTwitterStatus(title, url) {
    var result, titleLengthAllowed, fullTitle;
    fullTitle = title + ' [' + url + ']';
    if (fullTitle.length > 139) {
        // status is too long with full title and url
        // shorten title to fit
        // start with max 140 char - 3 for {space}[] - full url length
        titleLengthAllowed = 137 - (url.href.length);
        result = title.substring(0, titleLengthAllowed) + ' [' + url + ']';
    } else {
        result = title + ' [' + url + ']';
    }
    return result
}


// email a video link
function EmailVideo(title, url) {
    // retrieve user data 
    var EmailTo = $("#EmailVideoTo").val();
    var EmailFrom = $("#EmailVideoFrom").val();
    var encURL = encodeURI('Title=' + title + '&URL=' + url + '&emailTo=' + EmailTo + '&emailFrom=' + EmailFrom);

    // hide form, show results pane
    $("#emailFormTable").hide('slow');
    $("#EmailVideoResults").html("&nbsp;");
    $("#emailFormResults").show('slow');

    // send request to server as POST
    // send encoded URL parameters and set contentType as such
    // expect response in XML
    $.ajax({
        type: "POST",
        url: "http://action.afa.net/ajax/videoService.asmx/emailVideoLink",
        data: encURL,
        dataType: 'xml',
        contentType: "application/x-www-form-urlencoded",
        success: function(msg) {
            successEmailVideo(msg);
        },
        error: function(msg) {
            failedEmailVideo(msg);
        }

    });
}

// handle response on successful AJAX call
function successEmailVideo(msg) {
    var returnMsg;
    // extract responses
    $(msg).find('string').each(function() {
        returnMsg = $(this).text();
    });

    $("#EmailVideoResults").html(returnMsg);

}

// handle response on failed AJAX call
function failedEmailVideo(msg) {
    $("#EmailVideoResults").html("<span style='color:#ff0000;'>" + msg.responseText + "</span>");
}
        
