// grid function
function onRowDataBound(e) {
    // user interaction
    $(e.row).unbind('hover').unbind('click');
    $(e.row).hover(function () {
        $(this).css('background-color', '#FFFFEE');
    }, function () {
        $(this).css('background-color', '');
    }).click(function () {
        window.open('/Objekte/' + e.dataItem.Id, '', 'scrollbars=yes,menubar=yes,height=600,width=800,resizable=yes,toolbar=yes,location=yes,status=yes');
    });

    $(e.row.cells[3]).format({ format: "#,###", locale: "ch" });

    // replace 0 mit '-'
    if (e.dataItem.Chambers == 0) $(e.row.cells[2]).text('-');
    if (e.dataItem.Squaremeters == 0) $(e.row.cells[3]).text('-');

    if (e.dataItem.Rent == 0 && e.dataItem.Price == 0)
        $(e.row.cells[6]).text('-');
    else if (e.dataItem.Rent != 0) {
        $(e.row.cells[6]).format({ format: "#,###.00", locale: "ch" });
        $(e.row.cells[6]).text('Fr. ' + $(e.row.cells[6]).text());
    }
    else {
        $(e.row.cells[6]).format({ format: "#,###", locale: "ch" });
        $(e.row.cells[6]).text('Fr. ' + $(e.row.cells[6]).text());
    }

    // payment type / frequency
    if (e.dataItem.AnnualRent && e.dataItem.Rent != 0)
        $(e.row.cells[6]).append('m<sup>2</sup> / Jahr');
    else if (e.dataItem.Rent != 0)
        $(e.row.cells[6]).text($(e.row.cells[6]).text() + ' im Monat');
}

function thisPageClass() {
    // Private Variables


    // Public Variables
    this.colors = {
        'holding': '#5f6062',
        'privatbank': '#aaab7c',
        'liegenschaften': '#467492',
        'treuhand': '#89687b',
        'recht': '#578375'
    }

    this.grid = '';

    // Private Functions

    // Public Functions

    this.getSection = function () {
        return $('#section').text().replace(/^\s+|\s+$/g, "");
    }

    this.showSubmenu = function (section, menuItem) {
        var left = menuItem.position().left;

        if (menuItem.hasClass('menu-item-first'))
            left += 50;

        $('#submenu-content-' + section).css('left', left);
        $('.submenu-list').slideUp(200);
        $('#seven-menu').fadeOut(100);

        if ($('#submenu-content-' + section).is(':hidden')) {
            $('#submenu-content-' + section).slideDown(200);
        }
    }

    this.menuHover = function (targetClass) {
        $(targetClass).hover(function () {
            $(this).stop().animate(
				{ backgroundPosition: '-10px -12px' },
				500
			);
        }, function () {
            $(this).stop().animate(
				{ backgroundPosition: '-10px 83px' },
				850
			);
        });
    }

    this.moveIndicator = function (position, color) {
        $('#current-item-indicator').css('background-color', color);

        $('#current-item-indicator').animate({
            left: position + 'px'
        },
			500
		);
    }

    this.getColor = function (element) {
        var cl = $(element).attr('class').split(' ');
        for (var i = 0; i < cl.length; i++) {
            if (cl[i].search('#') != -1) {
                return cl[i];
            }
        }
    }
}

var thisPage = new thisPageClass();

// on load
$(document).ready(function () {
    $.ajaxSetup({
        // Disable caching of AJAX responses */
        cache: false
    });

    thisPage.menuHover('.menu-item');

    var currentColor = thisPage.colors[thisPage.getSection()];

    $('#colored-line').css('background-image', 'url(../../Content/Images/Footers/' + thisPage.getSection() + '.png)');
    $('#footer').css('background-color', currentColor);
    $('#content-left-text a').each(function () {
        if (!$(this).hasClass('t-link'))
            $(this).css('color', currentColor);
    });
    $('.modal-normal strong').css('color', currentColor);
    //$('#content-left-text strong').css('color', currentColor);
    $('h2').css('color', currentColor);
    $('.colored-paragraph').css('color', currentColor);
    if ($('.t-header').length = 0)
        $('#content-left-text').find('a').css('color', currentColor);

    if ((thisPage.getSection() == "holding") || (thisPage.getSection() == "liegenschaften")) {
        $("#estate-search-container").show();
    }
    else {
        $("#estate-search-container").hide();
    }

    $('#button-' + thisPage.getSection()).each(function () {
        if ($(this).hasClass('menu-item-first'))
            thisPage.moveIndicator($(this).position().left + 50, thisPage.getColor(this));
        else
            thisPage.moveIndicator($(this).position().left, thisPage.getColor(this));
    });

    $('.menu-item').click(function () {
        // remember the margin :-)
        if ($(this).hasClass('menu-item-first'))
            thisPage.moveIndicator($(this).position().left + 50, thisPage.getColor(this));
        else
            thisPage.moveIndicator($(this).position().left, thisPage.getColor(this));

        thisPage.showSubmenu($(this).attr('id').split('-')[1], $(this));
    });

    // hide submenu on mouse out
    $('#submenu').mouseleave(function () {
        thisPage.hideSubmenu();
        $('#button-' + thisPage.getSection()).each(function () {
            if ($(this).hasClass('menu-item-first'))
                thisPage.moveIndicator($(this).position().left + 50, currentColor);
            else
                thisPage.moveIndicator($(this).position().left, currentColor);
        });
    });

    // modal
    $('a.inline-modal').fancybox({
        'overlayColor': currentColor,
        'overlayOpacity': 0.8,
        'hideOnContentClick': false,
        'width': '500px',
        'height': '300px'
    });

    $('a.estate-image').fancybox({
        'overlayColor': currentColor,
        'overlayOpacity': 0,
        'titlePosition': 'over'
    });

    // load page search results
    $('#page-search').keyup(function () {
        var search = $(this).val();
        if (search.length > 3 && !$(this).hasClass('searching')) {
            $(this).addClass('searching');
            $('#page-search-results').load('/Search/Content/' + search, null, function () {
                $('#page-search').removeClass('searching');
            });
        };
    });

    $('#document-search').keyup(function () {
        var search = $(this).val();
        if (search.length > 3 && !$(this).hasClass('searching')) {
            $('.document-search-item').remove();
            $('#document-search-loader').show();
            $(this).addClass('searching');
            $('#document-search-results').load('/Search/Documents/' + search, null, function () {
                $('#document-search').removeClass('searching');
                $('#document-search-loader').hide();
            });
        };
    });

    $('#estate-search').keyup(function () {
        var search = $(this).val();
        if (search.length > 3 && !$(this).hasClass('searching')) {
            $('.estate-search-item').remove();
            $('#estate-search-loader').show();
            $(this).addClass('searching');
            $('#estate-search-results').load('/Search/Estate/' + search, null, function () {
                $('#estate-search').removeClass('searching');
                $('#estate-search-loader').hide();
            });
        };
    });

    $('#seven-menu').mouseleave(function () {
        $(this).fadeOut(20);
    });

    $('.submenu-list').children().each(function () {
        var background = 'url(../../Content/Images/dropdown-menuitem-over-background-nc.png)';

        if ($(this).children().length > 1) {
            var background = 'url(../../Content/Images/dropdown-menuitem-over-background.png)';
            $(this).children('a').after('<div class="submenu-list-arrow"></div>');
            $(this).children('a').mouseenter(function () {
                $('#seven-menu').hide();
                $('#seven-menu').css('left', $(this).parent().parent().position().left + 150);
                $('#seven-menu').css('top', $(this).parent().position().top + 165);
                $('#seven-menu').children('ul').html($(this).siblings('ul').html());
                $('#seven-menu').fadeIn(500);
            });
            $(this).children('div').click(function () {
                $('#seven-menu').hide();
                $('#seven-menu').css('left', $(this).parent().parent().position().left + 150);
                $('#seven-menu').css('top', $(this).parent().position().top + 165);
                $('#seven-menu').children('ul').html($(this).siblings('ul').html());
                $('#seven-menu').fadeIn(500);
            });
        }
        $(this).mouseenter(function () {
            $(this).css('background-image', background);
        });

        $(this).mouseleave(function () {
            $(this).css('background-image', '');
        });
    });

    if ($('#filteredRentGrid').length > 0) {
        $('#filteredRentGrid').gridFilter({
            applyButtonClass: 'gridfilter'
        });
    }

    if ($('#filteredBuyGrid').length > 0) {
        $('#filteredBuyGrid').gridFilter({
            applyButtonClass: 'gridfilter'
        });
    }

    if ($('.microaccordion').length > 0) {
        $('.microaccordion').microAccordion({
            openSingle: false
        });
    }

    if ($('.microfilter').length > 0) {
        $('.microfilter').microFilter({
            inputClasses: 'microfilter-textbox',
            element: '<span class="microfilter-element" />',
            elementHoverClass: 'microfilter-element-hover'
        });
    }

    $('#rentTabLink').click(function () {
        $('.gridfilter').attr('checked', false);
        $('.gridfilter').each(function () {
            if ($(this).attr('type') == 'text')
                $(this).val('');
        });
        $('.microfilter-element ').remove();
        var grid = $('#filteredRentGrid').data('tGrid');
        grid.filter('');
        $('#filteredRentGrid').show();
        $('#filteredBuyGrid').hide();
    });

    $('#buyTabLink').click(function () {
        $('.gridfilter').attr('checked', false);
        $('.gridfilter').each(function () {
            if ($(this).attr('type') == 'text')
                $(this).val('');
        });
        $('.microfilter-element ').remove();
        var grid = $('#filteredBuyGrid').data('tGrid');
        grid.filter('');
        $('#filteredBuyGrid').show();
        $('#filteredRentGrid').hide();
    });


    // Validation
    if ($('.validate-mwst').length > 0) {
        $('.validate-mwst').validate({
            rules: {
                EMail: {
                    required: true,
                    email: true
                },
                Anrede: {
                    required: true
                },
                Vorname: {
                    required: true
                },
                Nachname: {
                    required: true
                },
                PLZ: {
                    required: true
                },
                Ort: {
                    required: true
                },
                Strasse: {
                    required: true
                }
            },
            messages: {
                EMail: {
                    required: "Bitte ausf&uuml;llen.",
                    email: "Adresse nicht valid."
                },
                Anrede: {
                    required: "Bitte ausf&uuml;llen."
                },
                Vorname: {
                    required: "Bitte ausf&uuml;llen."
                },
                Nachname: {
                    required: "Bitte ausf&uuml;llen."
                },
                PLZ: {
                    required: "Bitte ausf&uuml;llen."
                },
                Strasse: {
                    required: "Bitte ausf&uuml;llen."
                },
                Ort: {
                    required: "Bitte ausf&uuml;llen."
                }
            }
        });
    }
});