// initialization
$(function() {

    var ShareURL = window.location;
    var ShareTitle = document.title;

    $(".share_email").click(function() {
        $("#emailFormResults").hide();
        $("#emailFormTable").show('slow');
    });
    $("#EmailVideoSend").click(function() {
        EmailVideo(ShareTitle, ShareURL);
    });

    // setup ajax login
    $("#ctl00_cphHeaderSubbar_ucLoginLogout1_ucLoginDialog1_ctl00__okButton").unbind("click");
    $("#ctl00_cphHeaderSubbar_ucLoginLogout1_ucLoginDialog1_ctl00__okButton").click(function() {
        //alert("Hello");
        var myU = $("#ctl00_cphHeaderSubbar_ucLoginLogout1_ucLoginDialog1_ctl00_EmailTextBox").val();
        var myP = $("#ctl00_cphHeaderSubbar_ucLoginLogout1_ucLoginDialog1_ctl00_PasswordTextBox").val();

        $.post("/ajax/ajax_Login.aspx", { u: myU, p: myP }, function() {
            // refresh current page to update login button
            window.location = window.location;
        });
    });

});


// Social Networking

// 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
}

function shareFacebook() {
	var targetUrl = 'http://www.facebook.com/sharer.php'
						+ '?u=' + encodeURIComponent(ShareURL)
						+ "&t=" + encodeURIComponent(ShareTitle);
	window.open(targetUrl);
}

function shareTwitter() {
	var twitterStatus = trimTwitterStatus(ShareTitle, ShareURL);
	var targetUrl = 'http://twitter.com/home/?status='
				+ encodeURIComponent(twitterStatus);
	window.open(targetUrl);
}

function shareMyspace() {
	var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto'
				+ '&t=' + encodeURIComponent(ShareTitle)
				+ '&c=' + encodeURIComponent('<p><strong>' + ShareTitle + '</strong></p>')
				+ '&u=' + encodeURIComponent(ShareURL) + '&l=3';
	window.open(targetUrl);
}

function shareWindowsLive() {
	var targetUrl = 'https://skydrive.live.com/sharefavorite.aspx%2f.SharedFavorites??&mkt=en-us'
				+ '&url=' + encodeURIComponent(ShareURL) + '&l=3'
				+ '&title=' + encodeURIComponent(ShareTitle)
				+ '&top=1';
	window.open(targetUrl);
}

function shareLiveJournal() {
	var targetUrl = 'http://www.livejournal.com/update.bml'
				+ '?subject=' + encodeURIComponent(ShareTitle)
				+ '&event=' + encodeURIComponent(ShareURL);
	window.open(targetUrl);
}

// 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 "";
    }
}


// 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>");
}


// mega menu functions
function addMega(){   
    $(this).addClass("hovering");   
}

function removeMega(){   
    $(this).removeClass("hovering");
}

var megaConfig = {       
    interval: 200,   
    sensitivity: 10, 
    over: addMega,
    timeout: 500,   
    out: removeMega   
};

$(function() {
    $("li.mega").hoverIntent(megaConfig);
});
