﻿/// <reference path="jquery-vsdoc.js" />
/// <reference path="jquery.qtip-1.0.0-rc3.js" />

function ConfirmLooseSelection()
{
    if (!this.href || this.href.indexOf('javascript:') != -1)
        return;

    if (this.target && this.target == '_blank')
        return;

    elements = document.getElementsByName('delete')

    if (!elements || !elements.length || elements.length == 0)
        return;

    isChecked = false;

    for (i = 0; i < elements.length; i++)
        if (elements[i] && elements[i].checked)
    {
        isChecked = true;
        break;
    }

    if (!isChecked)
        return;

    return confirm("Wenn sie die Seite wechseln, geht die Selektion verloren!");
}

function SetConfirmLooseSelection(node)
{
    if (!node || !node.childNodes || !node.childNodes.length)
        return;

    for (var i = 0; i < node.childNodes.length; i++)
        SetConfirmLooseSelection(node.childNodes[i]);

    if (!node.href)
        return;

    if (!node.onclick && !($(node).is(".iconsubmit")))
        node.onclick = ConfirmLooseSelection;
}


function ApplyRolloverAndClickToIconLinks()
{
    $(".iconlink").each(function()
    {
        var current = $(this);
        var child = current.children("img,input");

        if (child.size() == 0) { return; }

        child.data("normalsrc", child.attr("src"));

        current.mouseover(function()
        {
            child.attr("src", child.data("normalsrc").replace(".gif", "-over.gif"));
        });

        current.mouseout(function()
        {
            var src = child.attr("src");
            child.attr("src", child.data("normalsrc"));
        });

        if (child.is("input"))
        {
            var hook = function()
            {
                // otherwise this raises a stackoverflow
                child.click();

                current.one("click", hook);
            }

            current.one("click", hook);
        }
    });
}

function AssignTooltip(context)
{
    context.qtip({
        content: function() { return this.title; },
        position: { corner: { target: 'bottomLeft' }, adjust: { x: 10, y: -1} },
        style: { color: 'white', background: '#eb1b14', border: { color: '#eb1b14', width: 3, radius: 3 }, tip: 'topLeft' },
        show: 'mouseover',
        hide: 'mouseout'
    });
}

function DestroyTooltip(context)
{
    context.qtip("destroy");
}

$(document).ready(function()
{
    ApplyRolloverAndClickToIconLinks();

    $("input.date").datepick({ dateFormat: 'dd.mm.y', duration: 0, closeAtTop: false, mandatory: true });

    SetConfirmLooseSelection(document.getElementById('page_margins'));

    AssignTooltip($(".tooltip"));

    $(".customfeed_action").each(function() { new CustomfeedButton(this).init() });
});

CustomfeedButton = function(div)
{
    var self = this;
    var addName = "Add";
    var removeName = "Remove";
    var div = $(div);
    var input;
    var id;

    this.init = function()
    {
        var text = self.isInFeed() ? removeName : addName;

        id = div.attr("id");

        input = $("<a href='javascript:' class='customRssButton'><span>&nbsp;</span></a>");
        input.click(self.click);
        AssignTooltip(input);
        self.setTooltip();

        div.append(input);
    }

    this.isInFeed = function()
    {
        return div.is(".customfeed_active");
    }

    this.setTooltip = function()
    {
        DestroyTooltip(input); 
        
        if (self.isInFeed())
        {
            input.attr("title", "Meldung aus Individuellem RSS-Feed entfernen.");
        }
        else
        {
            input.attr("title", "Meldung zum Individuellem RSS-Feed hinzufügen.");
        }

        AssignTooltip(input);
    }

    this.setName = function(name)
    {
        input.attr("value", name);
    }

    this.disable = function()
    {
        input.toggleClass("customRssDisabled", true);
        self.setTooltip();
    }

    this.enable = function()
    {
        input.toggleClass("customRssDisabled", false);
        self.setTooltip();
    }

    this.onAdd = function(data)
    {
        if (data == "ok") { self.setName(removeName); }

        div.toggleClass("customfeed_active", true);
        self.enable();
    }

    this.onRemove = function(data)
    {
        if (data == "ok") { self.setName(addName); }

        div.toggleClass("customfeed_active", false);
        self.enable();
    }

    this.click = function()
    {
        self.disable();

        if (self.isInFeed())
        {
            $.get("/CustomFeed/Remove?id=" + id, self.onRemove);
        }
        else
        {
            $.get("/CustomFeed/Add?id=" + id, self.onAdd);
        }
    }
}

