﻿var active_page = 0;
var ButtonKeys = { "EnterKey": 13 };

$.fn.exists = function()
{
    return $(this).is('*');
}

// Initialisierungspunkt wenn DOM geladen worden ist
$(document).ready(function()
{
    $("div #footer_text a").each(function()
    {
        $(this).click(function()
        {
            contentloader($(this).attr('id'));
            return false;
        });
    });

    $("div .npArtikel form").each(function()
    {
        $(this).keypress(function(e)
        {
            if (e.which == ButtonKeys.EnterKey)
            {
                var defaultButtonId = $(this).attr("defaultbutton");
                $("#" + defaultButtonId).click();
                return false;
            }
        });
    });

    dialogInit("#home_sites", 800, 600);
    dialogInit("#productinfo", 800, 600);
    dialogInit("#email", 430, 500);

    //$("#btnSearch").button();

    $("#btnSend").button();
    $("#btnSend").click(function()
    {
        LoadEmailContent();
        return false;
    });
    $("#btnSend span").css("padding", "0em 1em");

    addDialogLink();
});

// beim Klick auf den Go Button, lädt Produktliste und Seitenanzeige
// Suchfunktion, holt sich alle Daten aus dem DOM und führt eine 
// AJAX-Anfrage zum Server aus für die Produktliste
function searching()
{
    $("#SearchResult").animate({ opacity: 0.25 }, "slow");
    $("#Loading").fadeIn("slow");
    
    //var action = $("#searchform").attr('action');
    var action = "Products/Search"
    var sw = "sw=" + $("#sw").val();

    //Produktliste laden
    $.ajax(
    {
        type: "POST",
        url: action,
        data: sw,
        success: function(result)
        {
            $("#SearchResult").html(result);
            $("#Loading").fadeOut("slow");
            $("#SearchResult").animate({ opacity: 1 }, 1000);
            addDialogLink();
        },
        error: function(a, b, c)
        {
            $("#SearchResult").html("<b style=\"color: #000;\">Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später noch einmal.</b>");
            $("#Loading").fadeOut("slow");
            $("#SearchResult").animate({ opacity: 1 }, 1000);
        }

    });
}

function addDialogLink()
{
    $("div .ProductBox").each(function()
    {
        var box = $(this);
        $(this).find(".ProductName").click(function()
        {
            productDialogOpen(box); return false;
        });
        $(this).find(".ProductImageBox").click(function()
        {
            productDialogOpen(box); return false;
        });
    });
}

function contentloader(site)
{
    $.ajax(
    {
        type: "GET",
        url: "http://" + window.location.host + "/Home/" + site + "JS",
        data: "",
        success: function(result)
        {
            $("#home_sites").html(result);
            var title = $("#home_sites").find("#home_sites_title").html();
            var content = $("#home_sites").find("#home_sites_content").html();

            $("#home_sites").html(content);
            $("#home_sites").dialog("option", "title", title);
            $("#home_sites").dialog("open");
        }
    });
}

function LoadEmailContent()
{
    $.ajax(
    {
        type: "GET",
        url: "Notepad/EmailFormJS",
        data: "",
        success: function(result)
        {
            $("#email").html(result);
            var title = $("#email").find("#email_site_title").html();
            var content = $("#email").find("#email_site_content").html();

            $("#email").html(content);
            $("#email button").css("display" , "none");
            $("#email").dialog("option", "title", title);
            $("#email").dialog("open");
        }
    });
}

function SendEmailContent()
{
    $("#email").animate({ opacity: 0.01 }, 250);
    // get the form
    var f = $("#emailform");
    // get the action attribute
    var action = f.attr("action") + "JS";
    // get the serialized data
    var s = f.serialize();

    $.post(action, s, function(result)
    {
        $("#email").html(result);
        var content = $("#email").find("#email_site_content").html();
        $("#email").html(content);
        $("#email button").css("display", "none");
        if ($("#isvalid").exists())
        {
            $("#email").dialog(
            {
                buttons:
                {
                    "OK": function()
                    {
                        $(this).dialog("close");
                        location.reload();
                    }
                }
            });
        }

        $("#email").animate({ opacity: 1 }, 250);
    });

    return false;
}


// Scripts für Dialog "ProductInfo"
function dialogInit(container, w, h)
{
    $(container).dialog(
    {
        bgiframe: true,
        autoOpen: false,
        draggable: false,
        resizable: false,
        height: h,
        width: w,
        modal: true,
        closeOnEscape: true,
        show: "blind", //'blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale', 'size', 'pulsate'
        hide: "blind",
//        buttons:
//        {
//            text: func
//        },
        close: function()
        {
            //allFields.val('').removeClass('ui-state-error');
        }
    });

    if (container == "#email")
    {
        $(container).dialog(
        {
            buttons:
            {
                "Abbrechen": function() { $(this).dialog("close"); },
                "Senden": function() { SendEmailContent(); }
            }
        });
    }
    else
    {
        $(container).dialog(
        {
            buttons:
            {
                "OK": function() { $(this).dialog("close"); }
            }
        });
    }
}

function productDialogOpen(box)
{
    // 1. Loading anzeigen
    // 2. AJAX absenden
    // 3. Dialog öffnen
    // 4. Result in Dialog einfügen
    // 5. Loading verstecken

    var id = $(box).find("input:hidden").val();

    $.ajax(
    {
        type: "POST",
        url: "http://" + window.location.host + "/Products/ProductInfoJS",
        data: "ArtikelID=" + id,
        success: function(result)
        {
            $("#productinfo").html(result);
            var title = $("#productinfo").find("#prod_info_title").html();
            var content = $("#productinfo").find("#prod_info_content").html();

            $("#productinfo").html(content);
            $("#productinfo").dialog("option", "title", title);
            $("#productinfo").dialog("open");
        },
        error: function(a, b, c)
        {
            $("#productinfo").html("Es ist ein Fehler aufgetreten");
            $("#productinfo").dialog("option", "title", "Fehler");
            $("#productinfo").dialog("open");
        }
    });

    return false;
}

function AdvertiseDialogOpen(id)
{
    $.ajax(
    {
        type: "POST",
        url: "http://" + window.location.host + "/Products/ProductInfoJS",
        data: "ArtikelID=" + id,
        success: function(result)
        {
            $("#productinfo").html(result);
            var title = $("#productinfo").find("#prod_info_title").html();
            var content = $("#productinfo").find("#prod_info_content").html();

            $("#productinfo").html(content);
            $("#productinfo").dialog("option", "title", title);
            $("#productinfo").dialog("open");
        },
        error: function(a, b, c)
        {
            $("#productinfo").html("Es ist ein Fehler aufgetreten");
            $("#productinfo").dialog("option", "title", "Fehler");
            $("#productinfo").dialog("open");
        }
    });
}

function addToCartListing(id)
{
    var form = $("#pc" + id).parent();
    var count = $("#pc" + id).val();
    addToCart(form, id, count);
}

function addToCartInfo(id)
{
    var form = $("#pci" + id).parent();
    var count = $("#pci" + id).val();
    addToCart(form, id, count);
}

function addToCart(f, id, count) 
{
    if (isNaN(count))
    {
        alert(count + " ist keine Zahl!");
    }
    else
    {
        // get the action attribute
        var action = f.attr("action") + "JS";
        // get the serialized data
        var s = f.serialize();

        //Produktliste laden
        $.ajax(
        {
            type: "POST",
            url: action,
            data: s,
            success: function(result)
            {
                $("#notepad_info_content").animate({ backgroundColor: "white" }, 200);
                $("#notepad_info_content").html(result);
                $("#notepad_info_content").animate({ backgroundColor: "#666666" }, 200);
            },
            error: function(a, b, c)
            {
                $("#notepad_info_content").html("<b style=\"color: #F00;\">Es ist ein Fehler aufgetreten.</b>");
            }

        });
    }
}