﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />

var alertIntervalId = 0;
var pingBackInterval = 0;
var inboxUpdateIntervalId = 0;

var killWelcomeVideo = false;
var showWelcomeVideo = true;


function SetBreadcrumbTips()
{
    $('a.breadcrumbTips').cluetip({
        showTitle: false,
        positionBy: 'bottomTop',
        onShow: function(ct, c) {  },
        cluetipClass: 'rounded'
    });
}

function RemoveBackground()
{
    $('#mainWrapper').css('background-image', 'none');
    $('#mainWrapper').css('background-color', '#000');
}

function SetBackground()
{
    //$('#mainWrapper').addClass('mainWrapperBackground');
}

function FlashOnlineNow()
{
    $('.onlineNowText').each(function() {
        
        if ($(this).css("visibility") == "visible") {
            $(this).css("visibility", "hidden");
        }
        else {
            $(this).css("visibility", "visible");
        }
        
    });
}

function Unload()
{
    ReportLocation(true);
}

function CheckAuthentication() {
    
    if (IsAuthenticated() == false) {
       $('#divLoginDialog').jqm({
            overlayClass: 'modalBoxOverlay'
        }).jqmShow();
        
        return false;
        
    }
    else
    {
        return true;
    }
    
}

function IsAuthenticated()
{
    var isAuthenticated = '<%=(ViewContext.HttpContext.User.Identity.IsAuthenticated) ? "true" : "false" %>';
    
    if (isAuthenticated == 'false')
        return false;
    else
        return true;
}

function PingBack()
{
    if (IsAuthenticated()) {
        ReportLocation(false);
    }
}

function ReportLocation(unload)
{
    var location;
    
    if (unload == true) {
        location = "unload";
    }
    else {
        location = window.location.pathname;
    }
    
    var data = new Object();
        
    data["location"] = location;

    //$.post('<%=Url.Action("SetUserLocation", "MyKu") %>', data);
    $.post(_setUserLocation, data);
}

function SetAlertInterval()
{
    alertIntervalId = setInterval("GetAlerts()", 10000);
}

function ClearAlertInterval()
{
    clearInterval(alertIntervalId);
}

function SetPingBackInterval()
{
    pingBackInterval = setInterval("PingBack()", 10000);
}

function SetInboxUpdateInterval()
{
    inboxUpdateIntervalId = setInterval("UpdateInboxCount()", 10000);
}

function GetAlerts()
{
    ClearAlertInterval();
    
    var data = new Object();
        
    //$.post('<%=Url.Action("GetAlerts", "MyKu") %>', data,
    $.post(_getAlerts, data,
        function(result) {
            var resultData = eval('(' + result + ')');
            if (resultData.isSuccessful == true) {
                var lineArray = resultData.data.split('|');
                
                if (lineArray.length > 0) {
                    jqalert('You have a new message from ' + lineArray[0] + '.');
                }
                
            }
            
        });

    SetAlertInterval();
}

function ShowWhatIsPage(id)
{
    $('#whatIsPage').hide();
    $('#whatIsPage').html($('#' + id).html());
    $('#whatIsPage').fadeIn('slow');   
}

function ShowGetStartedPage(id)
{
    $('#getStartedPage').hide();
    $('#getStartedPage').html($('#' + id).html());
    $('#getStartedPage').fadeIn('slow');   
}

function CheckBrowserVersion()
{
    
    //if (IsAuthenticated() == false) {
        if ($.browser.name == 'ie') {
            var version = $.browser.versionX;
            
            if (version < 7) {
                
                showWelcomeVideo = false;
                
                $('#divBrowsers').jqm({
                    overlayClass: 'modalBoxOverlay',
                    overlay: 100,
                    modal: true
                    }).jqmShow();
            }
        }
    //}
}

function HideBrowserCheck()
{
    $('#divBrowsers').jqmHide();
    showWelcomeVideo = true;
    ShowWelcomeVideo();
}

function ShowWelcomeVideo() {
    $('#divWelcomeVideoDialog').jqm({
        overlayClass: 'modalBoxOverlay',
        modal: true,
        onShow: openWelcomeVideoCallback,
        onHide: closeWelcomeVideoCallback
    }).jqmShow();
}

function WelcomeVideoEnd()
{
    $('#divWelcomeVideoDialog').jqmHide();
    $('#divSignupDialog').jqmShow();
}

var openWhatIsCallback = function(hash) { hash.w.fadeIn('slow'); ShowWhatIsPage("whatis_page1"); };
var openTestimonialsCallback = function(hash) { hash.w.fadeIn('slow'); };
var openWelcomeVideoCallback = function(hash) { hash.w.fadeIn('slow'); };
var openGetStartedCallback = function(hash) { hash.w.fadeIn('slow'); ShowGetStartedPage("getstarted_page1"); };

var closeWelcomeVideoCallback = function(hash) { 
    $('#welcomeVideo').hide();
    killWelcomeVideo = true; 
    hash.w.fadeOut('slow'); 
    hash.o.remove(); 
    
    var swf = document.getElementById('welcomeVideo');
    
    swf.parentNode.removeChild(swf);
    
}; 

function UpdateInboxCount()
{
    var data = new Object();
    //$.getJSON('<%=Url.Action("CountUnreadMessages", "InboxCount") %>', data, function(result) {
    //$.getJSON('/InboxCount.mvc/CountUnreadMessages', data, function(result) {
    $.getJSON(_countUnreadMessages, data, function (result) {
        
        $('#inboxCount').text(result.data);
        
    });
    
}

$().ready(function() {

    UpdateInboxCount();
    SetInboxUpdateInterval();

    SetBreadcrumbTips();

    SetPingBackInterval();

    jQuery('a.showmessage').click(function() {
        jqalert('Testing of notification system');
        return false;
    })

    $('#divWhatIsDialog').jqm({
        trigger: 'a.whatisDialogTrigger',
        overlayClass: 'modalBoxOverlay',
        onShow: openWhatIsCallback
    }).jqDrag('.titleContainer');

    $('#divTestimonialsDialog').jqm({
        trigger: 'a.testimonialsDialogTrigger',
        overlayClass: 'modalBoxOverlay',
        onShow: openTestimonialsCallback
    }).jqDrag('.titleContainer');

    $('#divGetStartedDialog').jqm({
        trigger: 'a.getStartedDialogTrigger',
        overlayClass: 'modalBoxOverlay',
        onShow: openGetStartedCallback
    }).jqDrag('.titleContainer');

    $('#divPasswordDialog').jqm({
        trigger: 'a.passwordDialogTrigger',
        overlayClass: 'modalBoxOverlay'
    }).jqDrag('.titleContainer');

    $("#btnPassword").click(function() {
        var data = new Object();

        data["email"] = $("#txtForgotEmail").val();

        //$.post('/User.mvc/SendPassword', data,
        $.post(_sendPassword, data,
            function(result) {
                var resultData = eval('(' + result + ')');
                if (resultData.isSuccessful == true)
                //location.reload();
                    location = '/Home.mvc/Index'
                else {
                    $("#passwordMessage").text(resultData.errorMessage);
                    $("#passwordMessage").show();
                }
            });
    });

    $("#btnSignup").click(function() {
        var data = new Object();

        var termsChecked = $('#checkTerms').is(':checked');

        var dateOfBirth = new Date($('#txtDateOfBirth').val());
        var currentDate = new Date();

        var dateDiff = currentDate.getFullYear() - dateOfBirth.getFullYear();

        if (dateDiff < 13) {
            alert("We are sorry but you do not meet the qualification requirements to register on this website.");
            return;
        }

        if (termsChecked == true) {

            data["userName"] = $("#txtSignupUserName").val();
            data["password"] = $("#txtSignupPassword").val();
            data["email"] = $("#txtSignupEmail").val();
            data["age"] = 0;
            data["dateOfBirth"] = $("#txtDateOfBirth").val();
            data["sex"] = $("#cboSignupSex").val();
            data["postalCode"] = $("#txtSignupPostalCode").val();
            data["educationLevel"] = $("#txtSignupEducationLevel").val();
            data["charityCode"] = $("#txtCharityCode").val();

            //$.getJSON("/User.mvc/Signup", data, function(result) {
            $.getJSON(_signup, data, function (result) {
                if (result.isSuccessful == true) {
                    location = _joinComplete;
                }
                else {
                    alert(result.errorMessage);
                    $("#signupMessage").text(result.errorMessage);
                    $("#signupMessage").show();
                }
            });
        }
        else {
            alert('Please read the terms of service and privacy document before joining.');
        }

    });


    $('#chkHideVideo').click(function() {
        var isChecked = $('#chkHideVideo').is(':checked');

        if (isChecked == true) {
            $.cookie('hideWelcomeVideo', 'true', { expires: 365 });
        }
        else {
            $.cookie('hideWelcomeVideo', 'false', { expires: 365 });
        }
    });

});
