/*
*   WWW Solutions - Sebastien LEVEILLE NIZEROLLE 
*   Copyright (c) 2010 WWW Solutions
*
*   Optimized using YUI compressor.
*/

var applicationBaseUrl = "http://www.swell.be/";
var masterService = applicationBaseUrl + "Services/HttpService.svc/";
var currentUser = new Object();
var authenticated = 0;
var speed = 0;
var addthis_pub = "swellbe";
var page = 0;
var itemType = 0;
var itemId = "";
var okImg = "GlobalImg/okS.ico";
var nokImg = "GlobalImg/nokS.ico";

$(document).ready(function () {

    //Init Application Settings
    page = $("#ctl00_ctl00_PageType")[0].value;
    itemType = $("#ctl00_ctl00_PageItemType")[0].value;
    itemId = $("#ctl00_ctl00_PageItemID")[0].value;

    PageEvents();
    AnimatePage();

    //Init Users
    $.ajaxDotNet(masterService + "User_Init", {
        verb: "POST",
        success: function (obj) {
            if (obj.d.uName != undefined) {
                authenticated = 1;
                currentUser = obj.d;
            }
//            InitLoginControl();
        }
    });

});

function AnimatePage() {   
    //help text for inputs
    $(".helpText").focus(
        function() { 
            if($(this).val() == $(this)[0].title)
            {
                $(this).removeClass("helpTextActive");
                $(this).val("");
            }
        });
    
    $(".helpText").blur(
        function()  {
            if($(this).val() =="")
            {
                $(this).addClass("helpTextActive");
                $(this).val($(this)[0].title);
            }
        });
    
    //to turn active as default
    $(".helpText").blur();

    //test if all function are ok
    //alert("AnimatedPage OK");    
}

//function InitLoginControl() {
//    if (authenticated > 0) {
//        $(".authenticated").attr("style", "display:inline;");
//        $(".authenticate").attr("style", "display:none;");
//        $("#UserHl").attr("href", applicationBaseUrl + "rider/" + currentUser.uName + ".html");
//        $("#UserHl").text(currentUser.uName);
//    }
//    else {
//        $(".authenticated").attr("style", "display:none;");
//        $(".authenticate").attr("style", "display:inline;");
//    }
//}

function PageEvents() {
    //open register form
    $('a.register').click(function(e) {
        e.preventDefault();
        $.modal.close();
        $("#RegPn").modal();
        return false;
    });

    //open login form
    $('a.login').click(function(e) {
        e.preventDefault();
        $("#LoginPn").modal();
        return false;
    });

    //open message form
    $('a.message').click(function(e) {
        e.preventDefault();
        $("#MessagePn").modal();
        return false;
    });

    //open comment form
    $('a.comment').click(function(e) {
        e.preventDefault();
        $("#CommentPn").modal();
        return false;
    });
    
    $('a[href=comment]').click(function(e) {
        e.preventDefault();
        $("#CommentPn").modal();
        return false;
    });

    //logout user
    $('a.logout').click(function(e) {
        e.preventDefault();
        $.ajaxDotNet(masterService + "User_Logout", {
            verb: "POST",
            success: function(obj) {
                currentUser = new Object();
                authenticated = 0;
                //InitLoginControl();
            }
        });
    });

    $('a.forgot').click(function(e) {
        e.preventDefault();
        $.modal.close();
        $("#ForgotPn").modal();
    });
    
    $('a.feedback').click(function(e) {
        e.preventDefault();
        $.modal.close();
        $("#FeedbackPn").modal();
    });
}

function checkUserAvailability(control, image, checkbox) {    
    if ($(control)[0].value != null) {
        var value = $(control)[0].value;
        $.ajaxDotNet(masterService + "User_Exists", {
            verb: "POST",
            data: { uname: value },
            success: function(obj) {
                if(obj.d)
                {
                    $(image).attr("src", applicationBaseUrl + nokImg);
                    $(checkbox).attr('checked', false);
                }
                else{
                    $(image).attr("src", applicationBaseUrl + okImg);
                    $(checkbox).attr('checked', true);
                }
            } 
        });
        
    }
    else
    {
        $(image).attr("src", applicationBaseUrl + nokImg);
    }
}

function checkEmailAvailability(control, image, checkbox) {    
    if ($(control)[0].value != null) {
        var email =$(control)[0].value;
        $.ajaxDotNet(masterService + "User_EmailExists", {
            verb: "POST",
            data: { email: email },
            success: function(obj) {
                if(obj.d)
                {
                    $(image).attr("src", applicationBaseUrl + nokImg);
                     $(checkbox).attr('checked', false);
                }
                else{
                    $(image).attr("src", applicationBaseUrl + okImg);
                     $(checkbox).attr('checked', true);
                }
            } 
        });
    }
    else
    {
        $(image).attr("src", applicationBaseUrl + nokImg);
    }
}

function sendMessage()
{
    //send message with email object
    if(Utilities.ValidateForm("message")){
        var omessage = new Object();
        omessage.to = $("#contactId")[0].value;
        omessage.subject = $("#ctl00_ctl00_MessageSubjectTx")[0].value;
        omessage.text = $("#ctl00_ctl00_MessageTextTx")[0].value;
        omessage.author = $("#ctl00_ctl00_MessageNameTx")[0].value;
        omessage.authorMail = $("#ctl00_ctl00_MessageEmailTx")[0].value;    
            
        var data = new Object();
        data.omessage = omessage;
        
        $.ajaxDotNet(masterService + "Message", {
            verb: "POST",
            data: data,
            success: function(obj) {
                if (obj.d)
                { 
                    ShowDialog("Uw bericht werd verzonden"); }
                else 
                {
                    ShowDialog("Uw bericht kon niet verzonden worden."); }
            }
        });
        
        $.modal.close();
    }
}

function sendFeedback()
{
    var omessage = new Object();
    omessage.author = $("#ctl00_ctl00_FbNameTx")[0].value;
    omessage.text = $("#ctl00_ctl00_FbMessageTx")[0].value;
    
    var data = new Object();
    data.omessage = omessage;
    
    $.ajaxDotNet(masterService + "Feedback", {
        verb: "POST",
        data: data,
        success: function(obj){
            if(obj.d)
            {
                ShowDialog("Bedankt voor de feedback!");
            }
            else 
            {
                ShowDialog("Foutje, uw feedback werd niet verzonden.  Kan je nog eens proberen aub? <br><br>thx, de swell-redactie");
            }
        }
    });               
}

function addComment()
{
    if(Utilities.ValidateForm("comment")){
        var ocomment = new Object();
        ocomment.author = $('#ctl00_ctl00_CommentNameTx')[0].value;
        ocomment.authorMail  = $('#ctl00_ctl00_CommentEmailTx')[0].value;
        ocomment.text = $('#ctl00_ctl00_CommentTextTx')[0].value;                   
        ocomment.source = itemType;
        if (itemType == 0 || itemType ==4) { //comment video OR mag
            ocomment.sourceId = itemId;
        }        
        else {
            ocomment.sourceIdGuid = itemId;            
        }

        var data = new Object();
        data.ocomment = ocomment;
        $.ajaxDotNet(masterService + "Comment_Insert", {
            verb: "POST",
            data: data,
            success: function(obj) {
                if (obj.d == "ok") {
                    $('#newCommentAuthor').text(currentUser.uName);
                    $('#newCommentDate').text("net gereageerd!");
                    $('#newCommentText').text(ocomment.text);
                }
                else {
                    alert(obj.d);
                }
                $.modal.close();
            }
        });
     }
                                                 
}

function deleteComment(id)
{
        if(id!=null)
        {
            $.ajaxDotNet(masterService + "Comment_Delete", {
            verb: "POST",
            data: { commentId: id },
            success: function(obj) {
                if(obj.d)
                {
                   ShowDialog("reactie verwijderd");
                }
                else
                {
                    ShowDialog("reactie kon niet verwijderd worden");
                }
            } 
        });
        }
        else
        {
            ShowDialog("Reactie kon niet verwijderd worden.");
        }
}

function passRecovery()
{
        if (Utilities.ValidateForm("forgot")) {
            var email =$('#ctl00_ctl00_ForgotEmailTx')[0].value;
            $.ajaxDotNet(masterService + "User_PassRecovery",{
                verb:"POST", 
                data: {email: email},
                success: function(obj) {
                    if(obj.d)
                    {
                        ShowDialog("Je hebt via mail een nieuw paswoord ontvangen.");
                    }
                    else 
                    {
                        ShowDialog("Dit e-mail adres is nog niet bij ons gekend.");
                    }
                }
            });
        }
}

function ShowDialog(message)
{
    $("#DialogText").html(message);
    $("#DialogPn").modal();
}