﻿/*
Global FTrySendXForm, FTrySendXMainForm, OnFailed, OnSucceeded, RegExp, fChkSrchTxt, fEntTextbox, fEnteredText, fTrySearch, getObjectById, parseFloat, postReply, replaceCharacter, sBlockVisitor, sCancel, sCommentRating, sCulture, sDelete, sPost, sRemoveSanction, sReply, sReport, toggle, togglecontent
-----------------------------------------------------------------------------
sAJAX_Scripts.js - v. 1.0
(c) Kristofer Gisslén 2009
All Rights Reserved.
*************************

GLOBAL FUNCTIONS FOR CONTROLLING ALL AJAX-DRIVEN FUNCTIONS AT Gisslen.net

*************************

TOGGLE FUNCTIONS: Controls the toggling of layers.
-----------------------------------------------------------------------------*/
// IT WOULD BE DARN NICE WITH SOME LANGUAGE CONTROL FOR THE THREAD-LINKS...
//var resource_en_expandthread = "Expand this thread";
//var resource_en_hidethread = "Hide this thread";
//var resource_en_nothreads = "There are no threads to expand/hide."
//var resource_sv_expandthread = "Veckla ut den här tråden";
//var resource_sv_closethread = "Dölj den här tråden";
//var resource_sv_nothreads = "Det finns inga svarstrådar att visa/dölja."
//var resource_en_required = "Required field"
//var resource_sv_required = "Obligatorisk uppgift"
//var resourcestring;

//if (this.clientInformation.browserLanguage = 'en-us') { resourcestring = resource_en_expandthread; } else if (this.clientInformation.browserLanguage = 'sv-se') { resourcestring = resource_sv_expandthread; }

function toggle(idx, status) {
    //replace layer-id by layer-name (name + id) to optimize the function for all kind of toggling.
    try {
        if (status == 'hide') {
            document.getElementById('list' + idx).style.display = 'none';
            //Get a set from an array of messages to display as the link
            document.getElementById('togglelistlink' + idx).innerHTML = "<a href='javascript:toggle(\"" + idx + "\", \"view\");'>&darr; Veckla ut den här tråden</a>.";
        } else if (status == 'view') {
            document.getElementById('list' + idx).style.display = 'block';
            document.getElementById('togglelistlink' + idx).innerHTML = "<a href='javascript:toggle(\"" + idx + "\", \"hide\");'>&uarr; Dölj den här tråden</a>.";
        }
    } catch (err) {
        alert('Det finns inga svarstrådar att visa/dölja!');
    }
}

//controls the toggling of replication forms inside a thread in the forum.
function postReply(idx, status) {
    if (status == 'hide') {
        //$set("content" + idx).value = '';
        //$set("author" + idx).value = '';
        document.getElementById('postreply' + idx).style.display = 'none';
        document.getElementById('postreplybutton' + idx).style.display = 'inline';
    } else if (status == 'view') {
        document.getElementById('postreply' + idx).style.display = 'block';
        document.getElementById('postreplybutton' + idx).style.display = 'none';
    }
}

//controls the toggling of replication forms inside a thread in the forum.
function togglecontent(idx, status) {
    if (status == 'hide') {
        //$set("content" + idx).value = '';
        //$set("author" + idx).value = '';
        document.getElementById(idx).style.display = 'none';
    } else if (status == 'view') {
        document.getElementById(idx).style.display = 'block';
    }
}
/*-----------------------------------------------------------------------------
END OF TOGGLE FUNCTIONS.
-
SEARCH FUNCTIONS: Controls the search function. The search function that is 
triggered whenever the search-button is hit.
-----------------------------------------------------------------------------*/
var fEnteredText = false;

function replaceCharacter(oString) {
    oString = oString.replace(/&nbsp;/g, "-");
    oString = oString.replace(/%20/g, "-");
    oString = oString.replace(new RegExp(" ", "g"), "-");

    return oString;
}

function getObjectById(elid) {
    if (document.getElementById) {
        return document.getElementById(elid);
    } else if (document.all) {
        return document.all[elid];
    } else if (document.layers) {
        return document.layers[elid];
    }
}

function fTrySearch(e, strFormName) {
    var tbQuery = getObjectById(strFormName);

    //if (!fChkSrchTxt(tbQuery.value) || !fEnteredText || tbQuery.value == '') {
    if (tbQuery.value == '') {
        window.location = window.location.protocol + '//' + window.location.hostname + '/Search/Tags/noquery/0/Default.aspx';
    } else {
        window.location = window.location.protocol + '//' + window.location.hostname + '/Search/Tags/' + replaceCharacter(tbQuery.value) + '/0/Default.aspx';
    }

    if (e && e.stopPropagation) {
        e.stopPropagation();
    }

    // To prevent the code from entering a loop while hitting the fEntTextbox-function we'll set the return to false.		
    return false;
}

// The fEntTextbox-function is triggered whenever the Enter-key is hit.
function fEntTextbox(e, strFormName) {
    if (e && e.keyCode == 13) {
        /*switch (strFormName) {
        case oSrcField: {*/
        return fTrySearch(e, strFormName);
        /*break; 
        }
        default: {  }
        }*/
    }

    return true;
}

// Validate the content in the search-form.
function fChkSrchTxt(wz) {
    var ws = /^[\s]*$/;
    return wz && !ws.test(wz);
}

/*-----------------------------------------------------------------------------
END OF SEARCH FUNCTIONS.
-
FORM VALIDATION: When submitting a form, these actions are called to verify
that expected information has been entered into the form fields. If information
has been entered a validation-method is called to check wether the information
is correct or not.
-----------------------------------------------------------------------------*/
function FTrySendXForm(idx, qid) {
    var tb1 = document.getElementById('author' + idx);
    var tb2 = document.getElementById('content' + idx);

    if ((tb1.value === null) || (tb1.value === "")) {
        document.getElementById('authorerrorlabel' + idx).className = 'formlabel red';
        tb1.focus();
        return false;
    } else {
        document.getElementById('authorerrorlabel' + idx).className = 'formlabel';
    }

    if ((tb2.value === null) || (tb2.value === "")) {
        document.getElementById('contenterrorlabel' + idx).className = 'formlabel red';
        tb2.focus();
        return false;
    } else {
        document.getElementById('contenterrorlabel' + idx).className = 'formlabel';
    }

    return true;
}

function FTrySendXMainForm(idx, qid) {
    var tb1 = document.getElementById('author' + idx);
    var tb2 = document.getElementById('content' + idx);

    if ((tb1.value === null) || (tb1.value === "")) {
        document.getElementById('authorerrorlabel' + idx).className = 'formlabel red';
        tb1.focus();
        return false;
    } else {
        document.getElementById('authorerrorlabel' + idx).className = 'formlabel';
    }

    if ((tb2.value === null) || (tb2.value === "")) {
        document.getElementById('contenterrorlabel' + idx).className = 'formlabel red';
        tb2.focus();
        return false;
    } else {
        document.getElementById('contenterrorlabel' + idx).className = 'formlabel';
    }

    return true;
}
/*-----------------------------------------------------------------------------
END OF FORM VALIDATION.
-
PAGE METHODS: These functions are AJAX-driven and invoked by the ASP.NET's 
ability to call for page methods.
-----------------------------------------------------------------------------*/
function OnSucceeded() {
    window.location.reload();
}

// Stub to make the page method call happy.
function OnFailed(error, userContext, methodName) {
    if (error != '') {
        alert(methodName + "\n" + error.get_message());
    }
}

function sCulture(lang) {
    PageMethods.SetCulture(lang, OnSucceeded, OnFailed);
}

function sReport(idx) {
    PageMethods.ReportThread(idx, OnSucceeded, OnFailed);
}

function sCancel(idx, qid) {
    PageMethods.Cancel(idx, qid, OnSucceeded, OnFailed);
}

function sDelete(idx, qid) {
    PageMethods.Delete(idx, qid, OnSucceeded, OnFailed);
}

function sBlockVisitor(idx, qid) {
    PageMethods.BlockVisitor(idx, qid, OnSucceeded, OnFailed);
}

function sRemoveSanction(idx) {
    PageMethods.RemoveSanction(idx, OnSucceeded, OnFailed);
}

function sCommentRating(idx, rating) {
    PageMethods.SetCommentRating(idx, rating, OnSucceeded, OnFailed);
}

function sPost(idx, qid) {
    var author = $get("author" + idx).value;
    var spambot = $get("spambot" + idx).value;
    var content = $get("content" + idx).value;

    //alert(author + '/' + spambot + '/' + content);
    PageMethods.PostComment(qid, author, spambot, content, OnSucceeded, OnFailed); // This is not called and I don't know why. An alert viewing idx, qid, content, author will run as expected...
}

function sReply(idx, qid) {
    var author = $get("author" + idx).value;
    var spambot = $get("spambot" + idx).value;
    var content = $get("content" + idx).value;

    //alert(author + '/' + spambot + '/' + content);
    PageMethods.PostReply(idx, qid, author, spambot, content, OnSucceeded, OnFailed); // This is not called and I don't know why. An alert viewing idx, qid, content, author will run as expected...
}
/*-----------------------------------------------------------------------------
END OF PAGE METHODS.
-
JQUERY: Lots of fantastic jQuery to set dynamics in the gallery.
-----------------------------------------------------------------------------*/
$(document).ready(function () {
    $("ul.subnav").parent().append("<span></span>");

    // Folds the main menu on the start page
    $("ul.topnav li").click(function () {
        $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

        $(this).parent().hover(function () {
        }, function () {
            $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
        });
        //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function () {
        $(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function () {	//On Hover Out
        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

    // Folds the page menu on any other page
    $("ul.pagenav li span").click(function () {
        $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

        $(this).parent().hover(function () {
        }, function () {
            $(this).parent().find("ul.subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
        });
        //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function () {
        $(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function () {	//On Hover Out
        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

    $("#ctl00_ContentPlaceHolder1_SetsDropDownList").bind('change', function () {
        var sets = $(this).val();

        window.location = window.location.protocol + '//' + window.location.hostname + '/Sets/' + sets + '/0/Default.aspx';
    });

    $("div.toggle").hover(function () {
        $("div.toggle").css("cursor", "pointer");
        $("div.toggle").click(function () {
            // hides matched elements if shown, shows if hidden
            $("#image, #map").toggle();
        });
    });

    $("span#start").hover(function () {
        $("span#start").css("cursor", "pointer");
        $("span#start").click(function () {
            window.location.href = window.location.protocol + '//' + window.location.hostname;
        });
    });

    $("ul.bloglist li").hover(function () {
        $("ul.bloglist li").css("cursor", "pointer");
        $("ul.bloglist li").bind("click", function () {
            var NewLink = $(this).attr("link");

            window.location.href = window.location.protocol + '//' + window.location.hostname + NewLink;
        });
    });

    $("img#print").hover(function () {
        $("img#print").css("cursor", "pointer");
        $("img#print").click(function () {
            window.print();
        });
    });

    $("img#feed").hover(function () {
        //$("img#feed").css("cursor", "pointer");
        $("img#feed").bind("click", function () {
            $(this).attr("link").copy();
        });
    });

    $("img#feed_badge").hover(function () {
        $("img#feed_badge").css("cursor", "pointer");
        $("img#feed_badge").bind("click", function () {
            window.location.href = 'http://www.gisslen.net/Feed/Debate.aspx';
        });
    });

    $("img#facebook_badge").hover(function () {
        $("img#facebook_badge").css("cursor", "pointer");
        $("img#facebook_badge").click(function () {
            window.location.href = 'http://sv-se.facebook.com/kristofer.gisslen';
        });
    });

    $("li#blognav").hover(function () {
        $("li#blognav").css("cursor", "pointer");
        $("li#blognav").click(function () {
            window.location.href = window.location.protocol + '//' + window.location.hostname;
        });
    });

    /*$("ul.forum").hover(function () {
    $("ul.forum li.rootthread").hover(function () {
    togglecontent($("span.reportlink").attr("id"), 'view');
    }, function () {
    togglecontent($("span.reportlink").attr("id"), 'hide');
    });

    $("ul.forum li.rootthread ul li.leafthread").hover(function () {
    togglecontent($("span.reportlink").attr("id"), 'view');
    }, function () {
    togglecontent($("span.reportlink").attr("id"), 'hide');
    });
    });*/

    $("span.reportlink img").hover(function () {
        $("span.reportlink img").css("cursor", "pointer");
    });

    /*
    $("span.thread_subject span.reportlink img").hover(function () {
    $("span.reportlink img").css("cursor", "pointer");
    });
    */

    $("span.resizelink").hover(function () {
        $("span.resizelink").css("cursor", "pointer");
        $("span.resizelink").click(function () {
            var ourText = $("div.page_columns");
            var currFontSize = ourText.css("fontSize");
            var finalNum = parseFloat(currFontSize, 10);
            var stringEnding = currFontSize.slice(-2);

            if (this.id == "large") {
                finalNum *= 1.2;
            } else if (this.id == "small") {
                finalNum /= 1.2;
            }

            ourText.animate({ fontSize: finalNum + stringEnding }, 600);
        });
    });

    $(window).scroll(function () {
        if ($(window).scrollTop() > $(".smartFloatingBoxIdentifier").offset({ scroll: false }).top) {
            $(".floatingbox").css("position", "fixed");
            $(".floatingbox").css("top", "0");
        }

        if ($(window).scrollTop() <= $(".smartFloatingBoxIdentifier").offset({ scroll: false }).top) {
            $(".floatingbox").css("position", "relative");
            $(".floatingbox").css("top", $(".smartFloatingBoxIdentifier").offset);
        }
    });

    /*    
    $("li#locknav").hover(function() {
    $("li#locknav").css("cursor", "pointer");
    $("li#locknav").click(function() {
    window.location.href = window.location.protocol + '//' + window.location.hostname + '/SignIn/Default.aspx';
    });
    });
    */

    /*function() {
        // Easy Pullquotes by Mike Jolley
        // Go through each span element with a classname of "pullquote"
        $('span.pullquote').each(function () {
            // Get the text of the span
            text = $(this).text();
            // Get rid of unwanted charactors
            text = text.replace(/\((.*)\)/gi, " ");
            // Check if this is to be a right or left pull quote and output it
            if ($(this).is(".right"))
                $(this).parent().before('<blockquote class="pullquote right"><p>&quot;' + text + '&quot;</p></blockquote>');
            else
                $(this).parent().before('<blockquote class="pullquote"><p>&quot;' + text + '&quot;</p></blockquote>');
        });
        // End pull quote code
    }*/
});