/* ++++++++++++++++++++++++++++++++
++	yourzine.js              ++
++	extracted from           ++
++	jquery.pallette-0.1.9.js ++
++                           ++
++  Dependencies:            ++
++  jQuery 1.2.6 or higher   ++
+++++++++++++++++++++++++++++++ */

var whatToDo;
function frameLoadDone() {
    whatToDo();
}

(function($) {
    $.fn.extend({
        /* ================================
        ==  Extending jQuery methods  ==
        ================================ */

        palette: "0.1.9",
        nutrinorm: "0.1",

        contentObj: window.contentObj || {},

        forRef: function() { // v0.1.5
            // Finds corresponding label(s), input, select or textarea, based on for= attribute of first matched element. Works both ways.
            return this.is("label[for]") ? $("#" + this[0].htmlFor) : $("label[for='" + this[0].id + "']");
        },

        overLabel: function() { // v0.1.7
            // Makes 'overLabel' behavior possible. Works both on labels and form fields.
            // Concept based on http://www.alistapart.com/articles/makingcompactformsmoreaccessible/
            // CSS: .jsOverLabel (on label)
            //      .jsOverLabelBlur (on parent of form field: blur)
            // Deps: forRef() method (v0.1+)
            return this
                .each(function() {
                    var jThis = $(this);
                    var jRef = jThis.forRef();
                    if (jThis.is("label[for]")) {
                        var jFormControl = jRef;
                        var jLabels = jThis;
                    } else {
                        var jFormControl = jThis;
                        var jLabels = jRef;
                    }

                    if (!(jFormControl.length > 0 && jLabels.length > 0)) return true; // continue
                    jLabels.each(function() {
                        $(this).addClass("jsOverLabel");
                        if (jFormControl.val() === "") jFormControl.parent().addClass("jsOverLabelBlur");
                        jFormControl
                            .focus(function() { $(this).parent().removeClass("jsOverLabelBlur"); })
                            .blur(function() {
                                var jFormControl = $(this);
                                if (jFormControl.val() === "") jFormControl.parent().addClass("jsOverLabelBlur");
                            })
                            .click(function() { 						// needed for Webkit to pass focus to input
                                $(this).forRef().each(function() {	// .focus() does not seem to work on every matched element, therefore .each() 
                                    this.focus();
                                });
                            });
                    });
                });
        },

        clickable: function(cpTtl) { // v0.1.8
            // Makes elements clickable, linking to location specified by href= attribute of first descending link (where href is not "#" and does not start with "javascript:").
            // Args.:   cpTtl:	[Boolean | optional]	Specifies whether or not to copy the title= attribute from the link to clickable element (default: true)
            // CSS:     .jsClickable (on this)
            //          .jsClickableHover (on this:hover)
            //          .jsGuide (on guiding link)
            return this
            .each(function() {
                var jClickElem = $(this);
                var jGuideLink = $("a[href]:not([href='#']):not([href^='javascript:'])", jClickElem).eq(0);
                var href = jGuideLink.attr("href");
                if (!href) return true; // continue

                $(this).data("href", href);
                if ((cpTtl || cpTtl == null) && !this.title && jGuideLink[0].title) this.title = jGuideLink[0].title;
                jClickElem
                    .click(function() { window.location.href = $(this).data("href"); })
                    .hover(
                        function() { $(this).addClass("jsClickableHover"); },
                        function() { $(this).removeClass("jsClickableHover"); }
                    )
                    .addClass("jsClickable");
                jGuideLink.addClass("jsGuide");
            });
        },

        IEinlineIcons: function() {
            // Appends a span with a non-breaking space to the matched elements (in IE) as a placeholder for an icon
            // CSS:		.jsIEfix (on matched elements)
            //				.jsInlineIcon (on appended span)
            return !$.browser.msie ? this : this.append('<span class="jsInlineIcon">&nbsp;</span>').addClass("jsIEfix");
        }
    });

    $.extend({
        /* ========================
        ==  Custom functions  ==
        ======================== */

        iconOnLastElement: function() {
            // Set icon on last element
            // CSS:		.jsIcon (on last element)
            //				.jsIconOnLastElement (on parent)
            var lastChilds = $("#additionalNav .overview li.jsClickable > :last-child, #sidebar .overview li.jsClickable > :last-child");
            lastChilds
                .append('<span class="jsInlineIcon">&nbsp;</span>')
                .addClass("jsIcon")
                .parent().addClass("jsIconOnLastElement");
        },

        addJSbuttons: function() {
            addPrintButton();
            addFontSize();

            function addPrintButton() {
                if (!window.print) return;
                if ($("iframe").length <= 0) {
                    $("#siteHeader").append('<p id="printPage"><a href="#">Print</a></p>');
                    $("#printPage a").click(function(e) {
                        window.print();
                        if (contentObj.printClick) ns_onclick(this, '', contentObj.printClick, 'clickout');
                        e.preventDefault();
                    });
                }
            };

            function addFontSize() {
                var serviceMenu = $("#serviceMenu ul.nav");
                if (!serviceMenu) return;

                var fSize = $.cookie('fontSize');
                if (!fSize) {
                    $.cookie('fontSize', 'default', { expires: 122, path: '/' });
                    fSize = $.cookie('fontSize');
                    if (!fSize) return;
                };

                function addStyleSheetsToIframe() {
                    var frames = $("iframe");
                    var head = frames.contents().find("head")[0];
                    $("link[title$='_fontsize']").each(function() {
                        var node = this.cloneNode(true);
                        if ($.browser.msie)
                            $(head).append(node.outerHTML);
                        else
                            $(node).appendTo($(head));
                    });
                }

                function swapSize(size) {
                    $("link[title$='_fontsize']").each(function() {
                        this.disabled = ($(this).attr("title") == size + "_fontsize") ? false : true;
                    });

                    var frames = $("iframe");
                    var head = frames.contents().find("head")[0];
                    $(frames).contents().find("head link[title$='_fontsize']").each(function() {
                        this.disabled = ($(this).attr("title") == size + "_fontsize") ? false : true;
                    });

                    $.cookie('fontSize', size, { expires: 122, path: '/' });
                };

                if ($("iframe").length > 0) {
                    fSize = $.cookie('fontSize');
                    var frames = $("iframe");
                    whatToDo = function() { addStyleSheetsToIframe(); swapSize(fSize); };
                } else
                    swapSize(fSize);

                serviceMenu.prepend('<li id="fontSize">\n'
						+ '<span class="offScreen">Lettergrootte:</span>\n'
						+ '<ul>\n'
							+ '<li class="first"><small><a href="#" title="kleine letters">A</a></small></li>\n'
							+ '<li><a href="#" title="standaard lettergrootte">A</a></li>\n'
							+ '<li><big><a href="#" title="grote letters">A</a></big></li>\n'
						+ '</ul>\n'
					+ '</li>');

                $("#fontSize").click(function(evt) {
                    evt.preventDefault();
                    var jTarget = $(evt.target);
                    var parentElm = jTarget.parent().get(0).tagName.toLowerCase();
                    var sizeObj = {
                        "small": "small",
                        "li": "default",
                        "big": "big"
                    };

                    swapSize(sizeObj[parentElm]);
                });
            };
        },

        collapseComments: function() {
            $("#blogComments h3")
                .wrapInner('<a href="#" title="Toon reacties"></a>')
                .parent().eq(0)
                    .addClass("jsHideComments");
            $("#blogComments h3 a")
                .toggle(
                    function(e) {
                        $(this)
                            .attr("title", 'Verberg reacties')
                                .parents().eq(1)
                            .removeClass("jsHideComments");
                        e.preventDefault();
                    },
                    function(e) {
                        $(this)
                            .attr("title", 'Toon reacties')
                            .parents().eq(1)
                                .addClass("jsHideComments");
                        e.preventDefault();
                    }
                );
        }
    });

    $(document).ready(function() {
        var browser = $.browser;

        // overLabel
        $("#siteSearch label").overLabel();
        if (!browser.msie || parseInt(browser.version) >= 7) $("#knowledgeBaseSearch .keywords label, #login label").overLabel();

        // add links that depend on JavaScript
        $.addJSbuttons();

        // make whole items clickable and set icons on correct element (+ fix IE bug)
        $("#additionalNav .overview li, #sidebar .overview li, #searchResults > li").clickable();
        $.iconOnLastElement();
        $("#contentOverview .content a, #mainContent .relatedContent li a, #mainContent .linkList li a").IEinlineIcons();

        // make blog comments collapsable
        $.collapseComments();
    });
})(jQuery);
