$(document).ready(function()
{
    SetPopup();

    SetTreeImages();

    //SetNavigationLinkEvents();

    //watermark for search-textboxes
    SetWatermarks();

    //open login modal popup on click for any items that have the class "triggerLoginPopup"
//    SetLoginPopupTriggers();

});

function SetLoginPopupTriggers()
{
    $(".triggerLoginPopup").click(function() { showLoginPopup(); return false; });
}

function ChangeBackground(editor, args)
{
    var style = editor.get_contentArea().style;
    style.backgroundImage = "none";
    style.backgroundColor = "#ffffff";
}

function SetWatermarks()
{
    $(".watermarkName").Watermark("Search by name...", "#5D5D53");
    $(".watermarkCity").Watermark("Search by city...", "#5D5D53");
    $(".watermarkKeyword").Watermark("Search by keyword...", "#5D5D53");
    $(".enterName").Watermark("Enter name", "#5D5D53");
    $(".enterEmail").Watermark("Enter e-mail address", "#5D5D53");
}

function fbs_click() { u = location.href; t = document.title; window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436'); return false; }

//swap out images on hover
function SetNavigationLinkEvents()
{
    $(".navigationLink img").hover(
      function()
      {
          if ($(this).attr("src").indexOf("_on") == -1)
              $(this).attr("src", $(this).attr("src").replace("_off", "_hover"));
      },
      function()
      {
          if ($(this).attr("src").indexOf("_on") == -1)
              $(this).attr("src", $(this).attr("src").replace("_hover", "_off"));
      }
    );
}

function SetPopup()
{
    //redirect all external links to disclaimer-page
    $('a').filter(
                    function()
                    {
                        return this.target == "_pop";

                    }).each(

                        function(i)
                        {
                            $(this).attr("href", "javascript:openWindow('" + $(this).attr("href") + "', 550, 655);");
                            $(this).attr("target", "");
                        }
                );
}

function toggleMore()
{

    $(".more").parent().children(".heading2:first").html($(".more").parent().children(".heading2:first").html().replace("...", $(".more").parent().children(".heading2:last").html()));
    $(".more").parent().children("a:first").hide();
    $(".more").parent().children("a:last").show();

}

function toggleLess()
{

    $(".more").parent().children(".heading2:first").html($(".more").parent().children(".heading2:first").html().replace($(".more").parent().children(".heading2:last").html(), "") + "...");
    $(".more").parent().children("a:first").show();
    $(".more").parent().children("a:last").hide();

}

//sets the onclick for the images in the topic-tree
function SetTreeImages()
{
    $(".topicTree img").click(function()
    {
        if (this.src.indexOf("Minus") != -1)
        {
            $(this).parent().children("ul").slideUp("fast");
            this.src = this.src.replace("Minus", "Plus");
            $(this).parent().children("a").removeClass("current");
        }
        else
        {
            $(".level2").slideUp("fast");
            $(".topicTree img").attr("src", this.src.replace("Minus", "Plus"));
            $(".topicTree a").removeClass("current");
            $(this).parent().children("ul").slideDown("fast");
            $(this).parent().children("a").addClass("current");
            this.src = this.src.replace("Plus", "Minus");
        }
    })
}

//clears a fileupload control
function reset_html(id)
{
    $('#' + id).html($('#' + id).html());
}

//use this to set a maxlength for multiline textboxes
function maxLength(text, long)
{

    var maxlength = new Number(long); // Change number to your max length.

    if (text.value.length > maxlength)
    {

        text.value = text.value.substring(0, maxlength);

        alert(" This field allows a maximum of " + long + " characters");

    }

}

//little function to cause postback when ok button in modalpopup is clicked
function clickOK(sender, e)
{
    __doPostBack(sender, e);
}

//background color for radeditor
function RadEditorLoad(editor)
{
    var style = editor.GetContentArea().style;
    //style.backgroundColor = document.bgColor;
    style.backgroundColor = "#FFFFFF";
}

//open a new window
function openWindow(url, w, h)
{
    window.open(url, "Resource", "toolbar=no, location=yes, directories=no, status=yes, menubar=yes, scrollbars=yes, resizable=yes, width=" + w + ", height=" + h + ", top=220, left=200");
}

//prevent enter from triggering
function noenter()
{
    return !(window.event && window.event.keyCode == 13);
}

//make enter submit form
function entsub(myform)
{
    if (window.event && window.event.keyCode == 13)
        myform.submit();
    else
        return true;
}

//the purpose of this function is to allow the enter key to 
//point to the correct button to click.
function doClick(buttonName, e)
{
    var key;

    if (window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox


    if (key == 13)
    {
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null)
        { //If we find the button click it

            btn.click();

            if (window.event)
                event.keyCode = 0; //IE

            event.returnValue = false;
            event.cancel = true;
        }
    }
}

//select all checkboxes in a given gridview
function selectGridViewAll(gridView)
{
    var list = document.getElementById(gridView);

    var options = list.getElementsByTagName('input');

    for (i = 0; i < options.length; i++)
        options[i].checked = true;
}

function selectGridViewNone(gridView)
{
    var list = document.getElementById(gridView);

    var options = list.getElementsByTagName('input');

    for (i = 0; i < options.length; i++)
        if (!options[i].disabled)
        options[i].checked = false;
}

function toggleVisibility(elementId)
{
    if ($get(elementId).style.visibility == 'visible')
        $get(elementId).style.visibility = 'hidden';
    else
        $get(elementId).style.visibility = 'visible';
}



/*
* Copyright (c) 2007 Josh Bush (digitalbush.com)
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:

* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE. 
*/

/*
* Version: Beta 1
* Release: 2007-06-01
*/
(function($)
{
    var map = new Array();
    $.Watermark = {
        ShowAll: function()
        {
            for (var i = 0; i < map.length; i++)
            {
                if (map[i].obj.val() == "")
                {
                    map[i].obj.val(map[i].text);
                    map[i].obj.css("color", map[i].WatermarkColor);
                } else
                {
                    map[i].obj.css("color", map[i].DefaultColor);
                }
            }
        },
        HideAll: function()
        {
            for (var i = 0; i < map.length; i++)
            {
                if (map[i].obj.val() == map[i].text)
                    map[i].obj.val("");
            }
        }
    }

    $.fn.Watermark = function(text, color)
    {
        if (!color)
            color = "#aaa";
        return this.each(
			function()
			{
			    var input = $(this);
			    var defaultColor = input.css("color");
			    map[map.length] = { text: text, obj: input, DefaultColor: defaultColor, WatermarkColor: color };
			    function clearMessage()
			    {
			        if (input.val() == text)
			            input.val("");
			        input.css("color", defaultColor);
			    }

			    function insertMessage()
			    {
			        if (input.val().length == 0 || input.val() == text)
			        {
			            input.val(text);
			            input.css("color", color);
			        } else
			            input.css("color", defaultColor);
			    }

			    input.focus(clearMessage);
			    input.blur(insertMessage);
			    input.change(insertMessage);

			    insertMessage();
			}
		);
    };
})(jQuery);