﻿(function ($) {
    $.fn.print_setup = function () {
        return this.each(function () {
            SetupCategoryPrint();
            SetupItemPrint();
        });
    };
})(jQuery);

var print_objects = undefined;
var body_color;
var body_color_print = "#FFF";
var print_area = "print_area2";
var print_area_header = print_area + "_header";
var print_area_body = print_area + "_body";
var print_area_footer = print_area + "_footer";
var button_options = print_area + "_buttons";


function AddPrintItem(variables) {
    if (!CheckForExistingHandler(variables)) {
        $(variables.clickTarget).click(function () {
            OrderPrintItems();
            PrintSelected(variables.clickTarget);
            $(document).scrollTop(0);
        });
    }
    if (print_objects === undefined) {
        print_objects = new Array();
    }
    print_objects.push(variables);
}

function AddPrintGroup(variables) {
    if (!CheckForExistingHandler(variables)) {
        $(variables.clickTarget).click(function () {
            OrderPrintItems();
            PrintSelected(variables.clickTarget);
            $(document).scrollTop(0);
        });
    }
    if (print_objects === undefined) {
        print_objects = new Array();
    }
    var temp_variables = variables;
    var ele3 = $(variables.content);
    function print_object(content) {
        for (var property in variables) {
            this[property] = variables[property];
        }
        this.content = content;
    }

    $(variables.content).each(function () {
        var t_object = new print_object($(this));
        print_objects.push(t_object);
    });
}

function CheckForExistingHandler(variables) {
    if (print_objects !== undefined) {
        for (var i = 0; i < print_objects.length; i++) {
            if (variables.clickTarget === print_objects[i].clickTarget) {
                return true;
            }
        }
    }
    else {
        return false;
    }
}

function OrderPrintItems() {
    for (var i = 0; i < print_objects.length-1; i++) {
        if (print_objects[i].position > print_objects[i + 1].position) {
            var temp = print_objects[i];
            print_objects[i] = print_objects[i + 1];
            print_objects[i + 1] = temp;
            i = -1;
        }
    }
}

function PrintSelected(clickTarget) {
    if (print_objects !== undefined) {
        SetupToPrint();
        for (var i = 0; i < print_objects.length; i++) {
            if (print_objects[i].clickTarget === clickTarget) {
                PrintElement(print_objects[i]);
            }
        }
    }
}

function PrintElement(variables) {
    var ele2 = $(variables.content).clone();
    if (variables.removeChufon) {
        RemoveCufonText(ele2);
    }
    if (variables.removeCss) {
        StripCssClass(ele2);
    }
    if (variables.removeInlineStyle) {
        StripInlineStyles(ele2);
    }
    if (variables.replaceCssClass !== undefined) {
        ele2.addClass(variables.replaceCssClass.newCssClass);
        ele2.removeClass(variables.replaceCssClass.oldCssClass);
    }
    if (variables.setupOptions !== undefined) {
        FurtherSetupOptions(variables.setupOptions);
    }
    if (variables.headerItem !== undefined) {
        $("#" + print_area_header, "#" + print_area).append(ele2);
    }
    else if (variables.footerItem !== undefined) {
        $("#" + print_area_footer, "#" + print_area).append(ele2);
    }
    else {
        $("#" + print_area_body, "#" + print_area).append(ele2);
    }
}

function FurtherSetupOptions(variables) {
    var ele = $("#" + print_area_body, "#" + print_area);
    for (var css in variables) {
        ele.css(css.toString(), variables[css]);
    }
}

function SetupToPrint() {
    body_color = $("body").css("background-color");
    $("body").css({backgroundColor:body_color_print});
    $("body").children().css({display:"none"});
    $("body").append("<div id=\"" + print_area + "\"></div>");
    var ele = $("#" + print_area, "body");
    ele.append("<div id=\"" + button_options + "\"><a>Return to page</a></div>");
    ele.append("<div id=\"" + print_area_header + "\"></div>");
    ele.append("<div id=\"" + print_area_body + "\"></div>");
    ele.append("<div id=\"" + print_area_footer + "\"></div>");
    var children = $("#" + button_options, ele).children();
    $(children[0]).click(function () {
        EndPrintingMode();
    });
}

function EndPrintingMode() {
    $("#" + print_area, "body").remove();
    $("body").css({ backgroundColor: body_color });
    $("body").children().css({display:"block"});
}

function RemoveImageFormatting(ele, t_class, selector) {
    var a = $(t_class, ele).find(selector);
    for (var i = 0; i < a.length; i++) {
        var b = $(a[i]);
        var text = b.text();
        b.empty().text(text);
    }
}

function RemoveUnwantedElements(ele) {
    $(".category_item_add", ele).remove();
}

function SetupCategoryPrint() {
    $(".category_print").click(function () {
        var bodyColour = $("body").css("background-color");
        var cat_parent = $(this).parent().parent().parent();
        var ul_html = $("ul.ul_category_item", cat_parent).clone();
        $("#print_area").empty();
        $("#print_area").append("<a id=\"return_click\">Return to page view</a>");
        $("#print_area").append(ul_html);
        $(".ul_category_item", "#print_area").attr("class", "ul_category_item_print");
        $("#whole_page").css({ display: "none" });
        $("body").css({ backgroundColor: "#FFF" });
        $("li", "#print_area").each(function () {
            RemoveImageFormatting($(this), ".category_item_name", "a");
            RemoveUnwantedElements($(this));
        });
        $("#return_click", "#print_area").click(function () {
            $("#print_area").empty();
            $("body").css({ backgroundColor: bodyColour });
            $("#whole_page").css({ display: "block" });
        });
        $(document).scrollTop(0);
    });
}

function SetupItemPrint() {
    $(".item_print").click(function () {
        var bodyColour = $("body").css("background-color");
        var parent = $(this).parent().parent().parent().parent().parent();

        $("#whole_page").css({ display: "none" });
        $("body").css({ backgroundColor: "#FFF" });

        $("#print_area").empty();
        $("#print_area").append("<a id=\"return_click\">Return to page view</a>");
        $("#print_area").append("<div id=\"print_item_area\"></div>");
        $("#print_item_area").append($("#item_version_widget", parent).clone());

        $("#print_item_area").append($("#stock_level_container", parent).clone());
        $("#print_item_area").append($("#stock_price_panel", parent).clone());
        AppendImagesFromWidget(parent, "#print_item_area");
        $("#print_item_area").append("<h4>Further Specifications</h4>");
        $("#print_item_area").append($("#item_spec_widget").clone());
        RemoveImageFormatting($("#print_item_area"), "h3", "span");
        RemoveImageFormatting($("#print_item_area"), "h4", "span");
        StripCssClass($("#print_item_area"));
        AddCssStyles($("#print_item_area"));
        $("#return_click", "#print_area").click(function () {
            $("#print_area").empty();
            $("body").css({ backgroundColor: bodyColour });
            $("#whole_page").css({ display: "block" });
        });
        $(document).scrollTop(0);
    });
}

function AppendImagesFromWidget(ele, input) {
    var html = $("#images_widget", ele);
    $("img", html).each(function (i) {
        if (i != 0) {
            $(input).append($(this).clone());
            $(input).append("<span style=\"display:inline-block;width:5px;height:1px;\"></span>");
        }
    });
}

function StripCssClass(ele) {
    ele.attr("class", "");
    ele.children().each(function () {
        StripCssClass($(this));
    });
}

function StripInlineStyles(ele) {
    ele.removeAttr("style");
    ele.children().each(function () {
        StripInlineStyles($(this));
    });
}

function RemoveCufonText(ele) {
    $("h1", ele).each(function () {
        $(this).text($(this).text());
    });
    $("h2", ele).each(function () {
        $(this).text($(this).text());
    });
    $("h3", ele).each(function () {
        $(this).text($(this).text());
    });
    $("h4", ele).each(function () {
        $(this).text($(this).text());
    });
    $("h5", ele).each(function () {
        $(this).text($(this).text());
    });
}

function AddCssStyles(ele) {
    $("ul", ele).attr("class", "ul_s_b");
}


