﻿/************************************
*
* Core Component
* vblog.js
* Blogging Library
* Provides AJAX based Blogging features
*

VLog 2.x - Multi-user ASP powered weblogging/content management system
Copyright (C) 2006-2008  Robert Vesse
rvesse@vdesign-studios.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

* Code is self executing to force the Namespace to be created and the Object made available
************************************/

(function() {

    if (!window.VLog) window['VLog'] = {};

    //Check that vajax.js and vutil.js are included
    if (!window.VLog.Utilities) {
        alert('Error: You must include vutil.js in order for this page to function correctly!');
    }
    if (!window.VLog.AJAXEngine) {
        alert('Error: You must include vajax.js in order for this page to function correctly!');
    }

    if (!window.VLog.Blogging) {
        window.VLog.Blogging = {};
    } else {
        //Already loaded so quit
        return false;
    }

    window.VLog.Blogging.ShowCommentForm = function(blogId, parentId) {
        //Build the Callback Extra Info
        var comment = {};
        comment.blogID = blogId;
        comment.parentElementID = parentId;

        //Try to get an AJAX Engine
        var ajax = new window.VLog.AJAXEngine(handleShowCommentForm, comment);
        if (!ajax.AJAXSupported) {
            location.href = 'comments.asp?blogID=' + blogId;
        } else {
            //Make the Request
            ajax.Get('exec/core_comments.asp?blogID=' + blogId + '&ajax=True&ajaxMode=1');
        }
    }

    function handleShowCommentForm(response, comment) {
        //Get the Parent Element
        var el = window.VLog.Utilities.GetElement(comment.parentElementID);
        if (el) {
            window.VLog.Utilities.InsertPopup('commentBlock' + comment.blogID, 0, 5, 800, 400, 'main', window.VLog.Utilities.Overflow.Visible, response.responseText, window.VLog.Utilities.AddPadding3px, true, comment.parentElementID);
        }
    }

    window.VLog.Blogging.MakeComment = function(f) {
        //Package everything up and POST it
        var destURL = f.action;
        var postData = '';

        for (var i = 0; i < f.elements.length; i++) {
            var el = f.elements[i];
            if (el.name != '') {
                if (postData != '') {
                    postData += '&' + el.name + '=' + encodeURI(el.value);
                } else {
                    postData = el.name + '=' + encodeURI(el.value);
                }
            }
        }

        //Try to get and use an AJAX Engine
        var ajax = new window.VLog.AJAXEngine(handleMakeComment, f);
        if (!ajax.AJAXSupported) {
            f.submit();
        } else {
            //Make Request
            ajax.FormPost(destURL, postData);
        }

    }

    function handleMakeComment(response, f) {
        //Get Element
        var el = f.parentNode;
        var parent = el.parentNode;
        parent.removeChild(el);
    }

    window.VLog.Blogging.ViewComments = function(blogId, parentId) {
        //Build the Callback Extra Info
        var comment = {};
        comment.blogID = blogId;
        comment.parentElementID = parentId;

        //Try to get an AJAX Engine
        var ajax = new window.VLog.AJAXEngine(handleViewComments, comment);
        if (!ajax.AJAXSupported) {
            location.href = 'comments.asp?blogID=' + blogId;
        } else {
            //Make the Request
            ajax.Get('exec/core_comments.asp?blogID=' + blogId + '&ajax=True&ajaxMode=2');
        }
    }

    function handleViewComments(response, comment) {
        //Get the Parent Element
        var el = window.VLog.Utilities.GetElement(comment.parentElementID);
        if (el) {
            window.VLog.Utilities.InsertPopup('commentViewBlock' + comment.blogID, 0, 5, 800, 400, 'main', window.VLog.Utilities.Overflow.Scroll, response.responseText, window.VLog.Utilities.AddPadding3px, true, comment.parentElementID);
        }
    }

    window.VLog.Blogging.ShowEmailForm = function(blogId, bloggerId, parentId) {
        //Build the Callback Extra Info
        var entry = {};
        entry.blogID = blogId;
        entry.bloggerID = bloggerId;
        entry.parentElementID = parentId;

        //Try to get an AJAX Engine
        var ajax = new window.VLog.AJAXEngine(handleShowEmailForm, entry);
        if (!ajax.AJAXSupported) {
            location.href = 'mail.asp?bloggerID=' + bloggerId;
        } else {
            //Make the Request
            ajax.Get('exec/core_mail.asp?bloggerID=' + bloggerId + '&ajax=True');
        }
    }

    function handleShowEmailForm(response, entry) {
        //Get the Parent Element
        var el = window.VLog.Utilities.GetElement(entry.parentElementID);
        if (el) {
            window.VLog.Utilities.InsertPopup('emailBlock' + entry.blogID, 0, 5, 800, 400, 'main', window.VLog.Utilities.Overflow.Visible, response.responseText, window.VLog.Utilities.AddPadding3px, true, entry.parentElementID);
        }
    }

    window.VLog.Blogging.MakeEmail = function(f) {
        //Package everything up and POST it
        var destURL = f.action;
        var postData = '';

        for (var i = 0; i < f.elements.length; i++) {
            var el = f.elements[i];
            if (el.name != '') {
                if (postData != '') {
                    postData += '&' + el.name + '=' + encodeURI(el.value);
                } else {
                    postData = el.name + '=' + encodeURI(el.value);
                }
            }
        }

        //Try to get and use an AJAX Engine
        var ajax = new window.VLog.AJAXEngine(handleMakeEmail, f);
        if (!ajax.AJAXSupported) {
            f.submit();
        } else {
            //Make Request
            ajax.FormPost(destURL, postData);
        }
    }

    function handleMakeEmail(response, f) {
        //Get Element
        var el = f.parentNode;
        var parent = el.parentNode;
        parent.removeChild(el);
    }

    window.VLog.Blogging.ShowSubscribeForm = function(blogId, subLevel, subItemId, parentId) {
        //Build the Callback Extra Info
        var entry = {};
        entry.blogID = blogId;
        entry.subLevel = subLevel;
        entry.subItemID = subItemId;
        entry.parentElementID = parentId;

        //Try to get an AJAX Engine
        var ajax = new window.VLog.AJAXEngine(handleShowSubscribeForm, entry);
        if (!ajax.AJAXSupported) {
            location.href = 'subscribe.asp?subLevel=' + subLevel + '&subItem=' + subItemId;
        } else {
            //Make the Request
            ajax.Get('exec/core_subscribe.asp?subLevel=' + subLevel + '&subItem=' + subItemId + '&ajax=True');
        }
    }

    function handleShowSubscribeForm(response, entry) {
        //Get the Parent Element
        var el = window.VLog.Utilities.GetElement(entry.parentElementID);
        if (el) {
            window.VLog.Utilities.InsertPopup('subscribeBlock' + entry.blogID, 0, 5, 800, 160, 'main', window.VLog.Utilities.Overflow.Visible, response.responseText, window.VLog.Utilities.AddPadding3px, true, entry.parentElementID);
        }
    }

    window.VLog.Blogging.MakeSubscribe = function(f) {
        //Package everything up and request it
        var destURL = f.action;
        var queryData = '';

        for (var i = 0; i < f.elements.length; i++) {
            var el = f.elements[i];
            if (el.name != '') {
                if (queryData != '') {
                    queryData += '&' + el.name + '=' + encodeURI(el.value);
                } else {
                    queryData = el.name + '=' + encodeURI(el.value);
                }
            }
        }

        //Try to get and use an AJAX Engine
        var ajax = new window.VLog.AJAXEngine(handleMakeSubscribe, f);
        if (!ajax.AJAXSupported) {
            f.submit();
        } else {
            //Make Request
            ajax.Get(destURL + '?' + queryData);
        }
    }

    function handleMakeSubscribe(response, f) {
        //Get Element
        var el = f.parentNode;
        f.innerHTML = response.responseText;
    }


})();