﻿/**
* **************************************************************
*                                                              *
* JavaScript IATAN Framework                               *
*                                                              *
****************************************************************
*/

function Trim(string) {
    return string.replace(/^\s+|\s+$/g, '');
}

window.IATAN = window.IATAN || {};
var test;

/**
* IATAN.org: main object and utilities methods
*/
IATAN = {}
IATAN.org = {

    // Active Debug Mode
    _DEBUG_: false,

    /**
    * Init function, the first function called once the page is loaded
    */
    Init: function() {
        //$.debug = true;
        $.debug(this._DEBUG_);
        //$.log("Begin IATAN.org.Init");
        //$.log($.browser.version);
        IATAN.org.MainNavigation.Init();
        IATAN.org.FAQ.Init();
        IATAN.org.Search.Init();
        IATAN.org.Forms.Init();

        //$.log("End IATAN.org.Init");

    }
}

/**
* IATAN.orgMainNavigation: handle the main navigation behaviour
*/
IATAN.org.MainNavigation = {

    Init: function() {
        this.AttachHandlers();
        this.GetHeighAndSet();
        this.RemoveDefaultBehavior();
    },


    AttachHandlers: function() {

        $("#mn-nav > div").mouseenter(function() {
            $("ul", this).css({ left: $(this).position().left + "px" });
        });

        $("#mn-nav > div.col2").mouseenter(function() {
            $(".bg-menu", this).css({ left: $(this).position().left + "px" });
            $("ul.first", this).css({ "left": "10px", "padding-left": "0px" });
            $("ul.sec", this).css({ "left": "160px", "padding-left": "0px" });


        });

        $("#mn-nav h2 a").focus(function() {
            $("#mn-nav ul").each(function() { $(this).css({ left: "-9999em" }) });
            $(this).parent().next().css({ left: $(this).parent().parent().position().left + "px" });
        });

        $("#mn-nav > div").mouseleave(function() {
            $("ul", this).css({ left: "-9999em" });
            $(".bg-menu", this).css({ left: "-9999em" });

        });
    },

    /*Keep the same height for the 2 col dropdown */
    GetHeighAndSet: function() {

        var UlFH = $("#mn-nav > div.col2 ul.first").height();
        var UlSH = $("#mn-nav > div.col2 ul.sec").height();
        var FinalHeight = null;
        var padding = 15;
        //$.log(UlFH);

        if (UlFH > UlSH) {
            $("ul.first").css("height", UlFH);
            $("ul.sec").css("height", UlFH);
            FinalHeight = UlFH;

        } else {
            $("ul.first").css("height", UlSH);
            $("ul.sec").css("height", UlSH);
            FinalHeight = UlSH;
        }

        $(".bg-menu").css({ height: +FinalHeight + padding + "px" });

    },

    /* remove the postback on the links titles in the main navigation */
    RemoveDefaultBehavior: function() {


        $("#mn-nav h2 a").click(function(e) {
            if ($(this).parent().hasClass("last") == false) {

                e.preventDefault();

            }
        });

    }


}
/**
* IATAN.Search: active the enter behavior on a keypress for IE
*/
IATAN.org.Search = {

    Init: function() {
        this.PressEnter();
    },

    PressEnter: function() {

        $("#srch input").keypress(function(e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                $('#srch [type=submit]').click();
                return false;
            } else {
                return true;
            }
        });
    }
}


/**
*  Handle all the FAQ behavior along the website
*/
IATAN.org.FAQ = {

    Init: function() {

        this.HandleOldHTMLMarkup();
        this.HandleHTMLMarkup();

    },

    /**
    * Some page of the site inherit from the old HTML markup
    * We ensure the retro-compatibility inside this method
    */
    HandleOldHTMLMarkup: function() {

        // Cancel the display: none CSS property of the old markup and replace it by the offscreen class
        $("ul#OpencloseList div").each(function() {
            $(this).css('display', 'block').addClass('offs');
        });

        // Handle the click on a question and expand the associate answer
        $("ul#OpencloseList li > a").click(function(e) {
            $("div", $(el).parent()).toggleClass('offs');
            e.preventDefault();
        });
    },

    /**
    * Some page of the site inherit from the old HTML markup
    * We ensure the retro-compatibility inside this method
    */
    HandleHTMLMarkup: function() {
        // Handle the click on a question and expand the associate answer
        $("p.question > a").click(function(e) {
            IATAN.org.FAQ.ToggleAnswer(this);
            e.preventDefault();
        });
    },

    /**
    * Display an answer if it is offscreen
    * Place the answer offscreen if it is already displayed
    */
    ToggleAnswer: function(el) {
        $(el).parent().next().toggleClass('offs');
    }

}

/**
* Utilities for the forms
*/

IATAN.org.Forms = {
    Init: function() {
        $("p.msg-2 span:hidden").parent().addClass("offs");
        $("p.msg-2 span:visible").parent().removeClass("offs");
    }
},

/**
*  Handle the edit mode inline of HTML tables
*/
IATAN.org.EditableTable = {
    Init: function() {

    },

    /**
    * 
    */
    ToggleEditMode: function(table, element, columns) {
        if ($('input', element).length > 0) {
            var val = $('input', element).val();
            $(element).html(val);
        } else {
            $(element).html('<input type="text" value="' + $(element).html() + '" />');
            $('input', element).change(function(e) { IATAN.org.EditableTable.SaveXmlData(table, columns); });
        }
    },

    /**
    * Add a table
    */
    AddTable: function(id, columns) {

        $.log("Begin IATAN.org.EditableTable.AddTable");
        $.log(id + " " + columns);
        var element = $('#' + id);

        var templateTable = "";
        var labels = columns.split(";");
        for (i = 0; i < labels.length; i++) {
            templateTable += '<th>' + labels[i] + '</th>';
        }
        templateTable = '<table class="editable"><thead><tr>' + templateTable + '<th><a href="">Add</a></th></tr></thead><tbody></table>';
        $(element).after(templateTable);
        var newTable = $('table.editable:last');

        IATAN.org.EditableTable.LoadXmlData(newTable, columns);


        // Add AddRow EventHandler to the new created table
        $('thead th a', newTable).click(function(event) {
            var values = "";
            for (var i = 1; i < labels.length; i++) {
                values += ";";
            }
            IATAN.org.EditableTable.AddRow($(this).parent().parent().parent().parent(), columns, values);
            event.preventDefault();
        });
        $.log("End IATAN.org.EditableTable.AddTable");
    },

    /**
    * Delete the specified table
    */
    DeleteTable: function() {
    },


    /**
    * Add a row to the specified table
    * @table the table where the row will be added
    * @nColumns the number of columns which will be added with the row
    */
    AddRow: function(table, columns, values) {
        var templateRow = "";
        for (i = 0; i < columns.split(';').length; i++) {
            templateRow += '<td>' + values.split(';')[i] + '</td>';
        }
        templateRow = '<tr>' + templateRow + '<th><a href="">Delete</a></th></tr>';
        // Add the row
        var element = $('tbody tr:last', table);
        if (element.length <= 0) {
            $('tbody', table).html(templateRow);
        } else {
            $(element).after(templateRow);
        }

        // Get the added row
        element = $('tbody tr:last', table);

        // Attach DeleteRow EventHandler to the new created row
        $('th a', element).click(function(event) {
            IATAN.org.EditableTable.DeleteRow($(this).parent().parent(), table, columns);
            event.preventDefault();
        });


        $('td', element).each(
		function(index) {
		    IATAN.org.EditableTable.ToggleEditMode(table, $(this), columns);
		}
	);
    },

    /**
    * Delete the specified row
    * @row the element which represents the row to delete
    */
    DeleteRow: function(row, table, columns) {
        $(row).remove();
        IATAN.org.EditableTable.SaveXmlData(table, columns);
    },

    /**
    * Save XML data from all tables into the hidden field
    */
    SaveXmlData: function(table, columns) {
        var labels = columns.split(';');

        var xml = "<Data>";
        $('input', table).each(
		    function(index) {
		        xml += "<" + labels[index % labels.length] + " content='" + $(this).attr('value') + "' />";
		    }
	    );
        xml += "</Data>";

        $(table).next().attr('value', xml);
    },

    /**
    * Load XML data and build corresponding tables from the hidden field
    */
    LoadXmlData: function(table, columns) {
        var empty = true;
        var labels = columns.split(';');

        var data = $($(table).next().attr('value'));
        // create a dummy parent node, so the find method will work in IE
        var parent = $("<data></data>");
        parent.append(data);
        parent.find(labels[0]).each(
		    function(index) {
		        empty = false;

		        var values = $(this).attr('content');
		        // BUG IE, we have to do a next next instead of only one next
		        if ($.browser.msie) {
		            values += ";" + $($(this).next().next()).attr('content');
		        }
		        else {
		            values += ";" + $($(this).next()).attr('content');
		        }
		        /*for (var i = 1; i < labels.length; i++) {
		        values += ";" + $(next).attr('content');
		        next = $(next).next().next();
		        }*/

		        IATAN.org.EditableTable.AddRow(table, columns, values);
		    }
	    );

        if (empty) {
            var values = "";
            for (var i = 1; i < labels.length; i++) {
                values += ";";
            }
            IATAN.org.EditableTable.AddRow(table, columns, values);
        }
    }
}

$(document).ready(function() {
    IATAN.org.Init();
});



/*
**  jquery.debug.js -- jQuery plugin for debugging
**  Copyright (c) 2007 Ralf S. Engelschall <rse@engelschall.com> 
**  Licensed under GPL <http://www.gnu.org/licenses/gpl.txt>
**
**  $LastChangedDate$
**  $LastChangedRevision$
*/

(function($) {
    /* jQuery class extension methods */
    $.extend({
        /* boolean status whether debugging is enabled */
        _debug$: null,

        /* method for getting and setting debug status */
        debug: function(onoff) {
            var old_value = ($._debug$ == true ? true : false);
            $._debug$ = (onoff ? true : false);
            return old_value;
        },

        /* method for logging an object or message */
        log: function(message) {
            if ($._debug$ == true)
                window.console.debug(message);
        }
    });

    /* jQuery object extension methods */
    $.fn.extend({
        /* method for logging all jQuery items */
        log: function(message) {
            if ($._debug$ == true) {
                return this.each(function() {
                    if (typeof message !== "undefined")
                        $.log(message);
                    $.log(this);
                });
            }
        }
    });

    /* determine default enable status */
    $(document).ready(function() {
        var req = $("html").attr("debug");
        if (req == "true" || req == "false")
            $.debug(req == "true" ? true : false);
    });

    /* minimum Firebug emulation (see http://getfirebug.com/firebug/firebugx.js) */
    $(document).ready(function() {
        if (typeof window.console === "undefined") {
            /* minimum conversion of arbitrary object to text representation */
            function object2text(obj) {
                var text = null;
                if (typeof obj === "undefined")
                    text = "[undefined]";
                else if (typeof obj === "boolean")
                    text = (obj ? "true" : "false");
                else if (typeof obj === "number")
                    text = "" + obj;
                else if (typeof obj === "string")
                    text = obj;
                else if (typeof obj === "function")
                    text = obj;
                else if (typeof obj === "object") {
                    if (typeof obj.nodeType !== "undefined") {
                        if (obj.nodeType == 1) { /* W3C DOM element node */
                            text = '&lt;';
                            text += obj.nodeName.toLowerCase();
                            for (var i = 0; i < obj.attributes.length; i++)
                                text += ' ' + obj.attributes[i].nodeName.toLowerCase() +
                                        '="' + obj.attributes[i].nodeValue + '"';
                            text += '&gt;';
                        }
                        else if (obj.nodeType == 2) /* W3C DOM attribute node */
                            text = obj.nodeName + '="' + obj.nodeValue;
                        else if (obj.nodeType == 3) /* W3C DOM text node */
                            text = obj.nodeValue;
                    }
                    else if (typeof obj.toJSONString !== "undefined")
                        text = obj.toJSONString();
                    else if (typeof obj.toString !== "undefined")
                        text = obj.toString();
                }
                if (text == null)
                    text = "[unknown]";
                return text;
            };

            /* create the custom console <div> node */
            $(document).ready(function() {
                $("body")
                    .append('<div id="jQueryDebug"><ol></ol></div>');
                $("#jQueryDebug")
                    .css("display", "none")
                    .css("fontFamily", "monospace")
                    .css("backgroundColor", "#ffffff")
                    .css("color", "#000000")
                    .css("padding", "10px 0px 10px 0px")
                    .css("border", "4px solid #666699")
                    .css("margin", "4px 4px 4px 4px");
                $("#jQueryDebug ol")
                    .css("margin", "0px 0px 0px 0px")
                    .css("paddingRight", "20px");
            });

            /* attach a logging function to each of the usual Firebug methods */
            var names = [
                "log", "debug", "info", "warn", "error", "assert",
                "dir", "dirxml", "group", "groupEnd", "time", "timeEnd",
                "count", "trace", "profile", "profileEnd"
            ];
            var logfunc = function(msg) {
                $('#jQueryDebug')
                    .css("display", $._debug$ == true ? "block" : "none");
                $('#jQueryDebug ol')
                    .append('<li>' + object2text(msg) + '</li>').css;
                $("#jQueryDebug ol li")
                    .css("borderBottom", "1px solid #cccccc")
                    .css("padding", "1px 10px 1px 10px");
            };
            window.console = {};
            for (var i = 0; i < names.length; i++)
                window.console[names[i]] = logfunc;

            /* indicate that we are the one who is providing the Firebug interface */
            window.console["jQueryDebug"] = true;
        }
    });

})(jQuery);

// Please leave this comment in place
// Created by: Randy Drisgill (The Mossman)
// August 17, 2007
//
// THIS OVERRIDES THE OOTB SHAREPOINT FUNCTION WHICH CAUSES ACTIVEX INSTALL ISSUES
//
// Essentially it overrides the ootb sharepoint function which calls the activex object
// See http://support.microsoft.com/default.aspx/kb/931509 for info on the issue


function ProcessDefaultOnLoad(onLoadFunctionNames) {
    //** Uncomment this to see when this runs
    //alert('Fixing the Issue');

    ProcessPNGImages();
    UpdateAccessibilityUI();

    //** We comment out the offending ootb function
    //** and leave the rest of the functions as they were
    //ProcessImn();
    for (var i = 0; i < onLoadFunctionNames.length; i++) {
        var expr = "if(typeof(" + onLoadFunctionNames[i] + ")=='function'){" + onLoadFunctionNames[i] + "();}";
        eval(expr);
    }
    if (typeof (_spUseDefaultFocus) != "undefined")
        DefaultFocus();
} /* Override Asp.Net Validator */

function ValidatorUpdateDisplay(val) {


    var valid = val.isvalid;

    var p = $(val).closest("p");
    if (valid) {

        $(val).css("display", "none");
        $(val).css("visibility", "hidden");

    } else {

        $(val).css("display", "block");
        $(val).css("visibility", "visible");
    }


    $("p.msg-2 span:hdden").parent().addClass("offs");
    $("p.msg-2 span:visible").parent().removeClass("offs");
}