﻿WL.registerOnloadEvent(function () {
    $('#shareThis').hoverIntent({
       sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
       interval: 100, // number = milliseconds for onMouseOver polling interval
       over: function (e) {
		    var $a = $('#shareThis a.share'), p = $a.offset(), $cont = $('#shareThis .hoverCont');
		    if (!$cont.attr('style')) $cont.css({left:-9999}).removeClass('closed').css({left:p.left + $a.outerWidth() - 240});//set position once
		    else $cont.removeClass('closed');
	    },
       timeout: 250, // number = milliseconds delay before onMouseOut
       out: function (e) {
		   $('#shareThis .hoverCont').addClass('closed');
	    }
    });
    $('#shareThis .favorites').bind('click', setFavorite);
    $('#moreInfo').jqm({
        /*ajax: '@href',*/
        trigger: false,
        modal:true,
        overlay:75,
        target:'.modalDetail',
        onShow:function (h/* object: w, o, c, t*/) {
            h.w.removeAttr('style');if (h.t.is('.sysReqs')) {
                $.ajax({
                    url:'/Content/SysReqs.txt',
                    success: function (response) {
                        $('#moreInfo').addClass('wlSysReqsModal');
                        h.w.find('.modalDetail').html(response);
                    }
                });
            } 
        },
        onHide:function (h/* object: w, o, c, t*/) {
            $('.MediaPlayer').attr({src:''});
            h.w.removeClass('wlModal')
            .removeClass('wlVideoModal')
            .removeClass('wlSysReqsModal')
            .find('.modalDetail').html('');
            if(h.o) h.o.fadeOut('fast', function() {$(this).remove();});           
        },
        onLoad: function () {
        },
        closeClass:'closeModal'
    });

    WL.eventHandler.events['sendToMobile'] = function ($e) {
        $('#moreInfo').jqmShow($e);
    };
    WL.eventHandler.events['connectLearnMore'] = function ($e) {
        $('#moreInfo').jqmShow($e);
    };
    WL.eventHandler.events['sysReqs'] = function ($e) {
        $('#moreInfo').jqmShow($e);
    };
    /*
    WL.eventHandler.events['emailafriendLink'] = function ($e) {
        $('#moreInfo').jqmShow($e);
    };
    */
    WL.eventHandler.events['videoModal'] = function ($e) {
        //$('#moreInfo').addClass('wlVideoModal');
        $('#moreInfo').jqmShow($e);
    };
    var $articleID = $('#articleID'), aId = $articleID.val();
    WL.eventHandler.events['closeModal'] = function () {$('#moreInfo').jqmHide()};
    if ($articleID.length == 1) WL.eventHandler.events['shareThis'] = function ($e) {trackShareThis($e, aId)};
});

var trackShareThis = function ($e,aId) {
     $.ajax({
        beforeSend: function () {
        },
        url:'shareArticle?articleId='+aId,
        data: null,
        dataType: 'html',
        success: function (response) {
        },
        error: function (xhr, status, error) {
        },
        cache: false
    });
};

var setFavorite = function () { 
    if (document.all)window.external.AddFavorite(window.location,document.title);
    else window.sidebar.addPanel(document.title,window.location,'');
    return false;
};

var initFormValidation = function () {
    $('form.validate').validate({
        errorElement:'em',
        ignore:'.ignore',
        submitHandler: function () {
   	        sendEmail();
        }
    });
};

var sendEmail = function () {
    var emailData = {};
    $('form#emailForm :input').not(':submit').each(function () {
        if ($(this).is(':checkbox')) {
            emailData[$(this).attr('id')] = $(this).is(':selected')?'true':'false';
        } 
        else {
            emailData[$(this).attr('id')] = $(this).val();
        }
        
    });

    $.ajax({
        beforeSend: function () {
        
        },
        url:'/sendemail',
        data: emailData,
        dataType: 'html',
        success: function (response) {
            $('#moreInfo .modalDetail').html(response);
        },
        error: function (xhr, status, error) {
        
        },
        cache: false
    });
};

