/// <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);

    //capturing DW tables
    var dw_table_temp_retainer = $(this).find(settings.capture);
    //removing unwanted data and setting css
    dw_table_temp_retainer.find('tr td:nth-child(1)').remove();
    dw_table_temp_retainer.find('tr td:nth-child(2)').remove();
    dw_table_temp_retainer.css({
        'width': '100%',
        'font-size': '9px',
        'color': '#000',
        'border-collapse': 'collapse'
    }).removeAttr('border').find('tr td').css({
        'vertical-align': 'middle',
        'padding': '5px'
    }).find('a').css('color', '#000');
    dw_table_temp_retainer.find('tr td:nth-child(2)').css({ 'width': '100px', 'text-align': 'center' });
    //creating array
    var dw_table_array = $.makeArray(dw_table_temp_retainer);

    //deleting temporaty data
    dw_table_temp_retainer.remove();
    dw_table_temp_retainer = undefined;

    if (dw_table_array.length != 0) {
        //local variables
        var windowWidth = $(window).width();
        var containerWidth = Math.round(windowWidth * 0.4); //40% of window width
        var containerPosition = Math.round((windowWidth / 2) - (containerWidth / 2));
        var current_table_container, dw_table_array_length;

        //creating container
        var $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'
        }).appendTo($(this));
        //setting container to a variable

        $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
        for (var i in dw_table_array) {
            //currentTableContainerId = 'DwLoopCounterId' + i;

            //title and table container
            current_table_container = $('<div />').css({ 'padding': '20px' }).attr('id', 'DwLoopCounterId' + i).appendTo($container);

            //tag count
            dw_table_array_length = $(dw_table_array[i]).find('tr').length;

            //title
            $('<div />').addClass('DwLoopCounter').css({
                'font-size': '14px',
                'color': '#fff',
                'padding': '7px',
                'background': '#222'
            }).appendTo(current_table_container).html('Loop #' + (parseInt(i) + 1) + ', Row count: ' + dw_table_array_length + ' ').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
            $(dw_table_array[i]).appendTo(current_table_container);

            current_table_container = undefined;
        }

        //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;
    }
}
