/// <reference path="jquery-1.4.2.min-vsdoc.js" />

//Function to fetch "DwTemplateTags" tables and display it in another window
$.fn.captureDwTemplateTags = function (options) {
    var settings = $.extend({
        capture: 'table[border=1]:contains("Tag")'
    }, options);

    var $capture = $(this).find(settings.capture);

    if ($capture.length != 0) {
        //local variables
        var containerId = 'dwTemplateTagsContainer';
        var windowWidth = $(window).width();
        var containerWidth = Math.round(windowWidth * 0.4); //40% of window width
        var containerPosition = Math.round((windowWidth / 2) - (containerWidth / 2));
        var currentTableContainerId;

        //creating container
        $('<div />').css({
            'position': 'absolute',
            'top': '10px',
            'left': containerPosition + 'px',
            'font-family': 'Arial, Verdana, Tahoma, Sans-Serif',
            'width': containerWidth + 'px',
            'background': '#fff',
            'z-index': '9999',
            'border-top': '5px solid #222',
            'border-bottom': '5px solid #222'
        }).attr('id', containerId).appendTo($(this));
        //setting container to a variable
        var $container = $('#' + containerId);

        $openbutton = $('<a />').css({
            'position': 'absolute',
            'top': '0',
            'left': '0',
            'display': 'block',
            'font-family': 'Arial, Verdana, Tahoma, Sans-Serif',
            'padding': '5px',
            'color': '#fff',
            'background': '#d30000',
            'border': '1px solid #dcdcdc',
            'text-decoration': 'none',
            'font-size': '12px',
            'font-weight': 'bold',
            'z-index': '9999'
        }).attr('href', '').html('dW').click(function () {
            DwCreateCookie('DwTtShowContainer', 'true', 30);
            $container.show();
            $(this).hide();
            return false;
        }).prependTo($(this)).hide();

        //grabbing all tables and displayes them inside the container
        $capture.each(function (e) {
            currentTableContainerId = 'DwLoopCounterId' + e.toString();

            //title and table container
            $('<div />').css({
                'padding': '20px'
            }).attr('id', currentTableContainerId).appendTo($container);

            //title
            $('<div />').addClass('DwLoopCounter').css({
                'font-size': '14px',
                'color': '#fff',
                'padding': '7px',
                'margin': '0 0 7px',
                'background': '#222'
            }).appendTo($('#' + currentTableContainerId)).html('DwTemplateTags loop #' + (e + 1).toString() + ' ').append($('<a />').attr('href', '').html('(Upp)').css({
                'color': '#fff',
                'text-decoration': 'none',
                'font-size': '12px'
            }).click(function () {
                $('html, body').animate({ scrollTop: ($container.offset().top) - 10 }, 500);
                return false;
            }));

            //table
            $(this).css({
                'width': '100%',
                'font-size': '9px',
                'color': '#000'
            }).removeAttr('border').appendTo($('#' + currentTableContainerId)).find('tr').css({
                'border-bottom': '1px solid #333'
            }).find('td').css('vertical-align', 'middle').find('a').css('color', '#000');
        });

        //adding direkt links
        $('<ul />').css({
            'margin': '0',
            'padding': '0',
            'list-style-type': 'none'
        }).attr('id', 'DwLoopCounterList').prependTo($container);
        $('div.DwLoopCounter').each(function () {
            $('<li />').css('display', 'inline').appendTo('#DwLoopCounterList').append($('<a />').attr('href', '').attr('rel', $(this).parent('div').attr('id')).html($(this).html().substring(0, $(this).html().indexOf(' <'))).css({
                'display': 'block',
                'padding': '3px 0 3px 10px',
                'margin': '0 0 3px',
                'width': '200px',
                'color': '#fff',
                'text-decoration': 'none',
                'font-size': '11px',
                'background': '#222'
            }).click(function () {
                $('html, body').animate({ scrollTop: $('#' + this.rel).offset().top }, 500);
                return false;
            }));
        });

        //adding minimize button
        $('<a />').attr('id', 'CloseButton').attr('href', '').html('Minimize').css({
            'display': 'block',
            'float': 'right',
            'color': '#fff',
            'background': '#d30000',
            'text-decoration': 'none',
            'padding': '4px 12px',
            'margin': '0 10px 0 0',
            'border-bottom': '1px solid #dcdcdc'
        }).prependTo($container).click(function () {
            DwCreateCookie('DwTtShowContainer', 'false', 30);
            $container.hide();
            $openbutton.show();
            return false;
        }).after($('<div />').html('&nbsp;').css({
            'font-size': '0.1px',
            'height': '0',
            'width': 'auto',
            'clear': 'both'
        }));

        //checking if disply cookie is set
        if (DwReadCookie('DwTtShowContainer') != null) {
            if (DwReadCookie('DwTtShowContainer') == 'false') {
                $container.hide();
                $openbutton.show();
            }
        }
    }

    //HELPERS
    //cookie functions
    function DwCreateCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }
    function DwReadCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
}
