﻿/// <reference path="jquery-1.4.1.min.js" />

jQuery.fn.watermark = function (text) {
    this.clearWatermark = function () {
        var $target = $(this);
        if ($target.hasClass("watermark")) {
            $target
                .removeClass("watermark")
                .val("");
        }
    };
    var target = this;
    return target
        .addClass("watermark")
        .val(text)
        .focus(function () { target.clearWatermark(); })
        .closest("form")
            .submit(function () { target.clearWatermark(); })
        .end();
};

jQuery.fn.fancyPanel = function () {
    this.index = 0;
    this.count = 0;
    this.delay = 2500;
    this.moveToNext = function () {
        var teaser = this;

        teaser.index += 1;
        if (teaser.index === teaser.count) teaser.index = 0;
        var leftIndex = teaser.index * 315;

        var $activeImage = $($(".fancy-panel-images .images img", teaser)[teaser.index]);

        $(".fancy-panel-content h2", teaser)
            .fadeOut(150, function () { $(this).text($activeImage.attr("alt")); })
            .fadeIn(150);

        $(".fancy-panel-images .images", teaser)
            .animate({
                marginLeft: '-' + leftIndex + 'px'
            },
            300,
            function () {
                window.setTimeout(
                    function () { teaser.moveToNext(); },
                    teaser.delay);
            });

        teaser.setCredit($activeImage);
    };
    this.setCredit = function ($activeImage) {
        var teaser = this;
        var $credit = $(".fancy-panel-images .credit", teaser);
        $credit.empty();
        var activeImageCredit = $activeImage.attr("data-credit");
        if (activeImageCredit != undefined)
            $credit.append(
                $("<a rel='external'>&copy;</a>")
                    .attr("href", activeImageCredit)
            );
    };

    var teaser = this;
    var $images = $(".fancy-panel-images .images img", teaser);
    teaser.setCredit($($images[0]));
    teaser.count = $images.length;
    if (teaser.count > 1)
        window.setTimeout(function () { teaser.moveToNext(); }, teaser.delay);

    return this;
};

jQuery.fn.urlSlug = function (source) {
    this.makeUrlKey = function (inputText) {
        var outputText = new String();

        inputText = inputText.toLowerCase();

        for (i = 0; i < inputText.length; i++) {
            var charCode = inputText.charCodeAt(i);
            if
    		(
    			(charCode >= 97 && charCode <= 122) ||
    			(charCode >= 48 && charCode <= 57)
    		) { outputText = outputText.concat(String.fromCharCode(charCode)); }
            else if
    		(
    			(charCode == 38) ||
    			(charCode == 43)
    		) { outputText = outputText.concat("and"); }
        }

        if (outputText.length > 25)
            outputText = outputText.substring(0, 25);

        return outputText;
    };
    var target = this;
    $(source)
        .change(function () {
            target.val(target.makeUrlKey($(this).val()));
        })
        .keyup(function () {
            target.val(target.makeUrlKey($(this).val()));
        });
    return this;
};
