var lastHash;

$(document).ready(function() {
    //Bind search button
    $('.search input[type="submit"]').click(function() {
        doSearch();
    });

    $('.search input[type="text"]').keydown(function(event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            doSearch();
            return false;
        }
    });

    //Bind top navigation buttons
    $('#topNavigation .topNavigation a').each(function() {
        bindLink($(this), '#topContent', '#mainContent, #bottomContent', topToggle, topToggle);
    });

    $('#content .mainNavigation a, #content #textPageNavigation a').each(function() {
        bindLink($(this), '#mainContent', '#topContent, #bottomContent', mainToggle, mainLoad);
    });

    $('#bottom .navigation a').each(function() {
        bindLink($(this), '#bottomContent', '#topContent, #mainContent', mainToggle, mainLoad);
    });

    $(window).bind('hashchange', function(event) {
        $('#loadmask').mask('<img src="/css/images/ajax-loader.gif" alt="loading" />');

        $('#topNavigation .topNavigation li').removeClass('active');

        var hashValue = window.location.hash.substr(1);
        if (hashValue.length > 0) {
            if (hashValue.indexOf('topp') != -1) {
                toggleContent(hashValue, '#topContent', '#topContent, #mainContent, #bottomContent, #objectSearch', topToggle, topToggle);
                $('a[href="' + window.location.hash + '"]').parent('li').addClass('active');
            }
            else if (hashValue.indexOf("main") != -1 && countCharacters(hashValue, '/') > 3) {
                toggleContent(hashValue, '#mainContent', '#topContent, #bottomContent, #objectSearch', mainToggle, mainLoad);
            }
            else if (hashValue.indexOf("main") != -1) {
                toggleContent(hashValue, '#mainContent', '#topContent, #mainContent, #bottomContent, #objectSearch', mainToggle, mainLoad);
            }
            else if (hashValue.indexOf("bottom") != -1) {
                toggleContent(hashValue, '#bottomContent', '#topContent, #mainContent, #bottomContent, #objectSearch', mainToggle, mainLoad);
            }
            else if (hashValue.indexOf("objekt") != -1) {
                searchObject(hashValue);
            }
        }
        else {
            $('#topContent, #mainContent, #bottomContent, #objectSearch').slideUp('fast', function() { $('#loadmask').unmask(); });
            $('#content .mainNavigation li, #bottom .bottomNavigation li').addClass('active');
        }

        if (hashValue.indexOf('uthyrning') > -1) {
            $('#searchBar').addClass('highlight');
        }
        else {
            $('#searchBar').removeClass('highlight');
        }

        Cufon.refresh();

        lastHash = window.location.hash;
    });

    //Bind object search
    $('#btnObjectSearch').click(function(event) {
        event.preventDefault();
        doObjectSearch();
    });

    $('#objectSearchBar input[type="text"]').keydown(function(event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            doObjectSearch();
            return false;
        }
    });

    $("#tblObjectList").tablesorter({
        headers: {
            0: { sorter: false },
            5: { sorter: false }
        }
    });

    //$.updnWatermark.attachAll();

    $('#searchBar .tbMin').updnWatermark();
    $('#searchBar .tbMax').updnWatermark();
    $('#searchBar .tbAddress').updnWatermark();

    $('.expandlable').hide();
});

function doSearch() {
    query = $('.search input[type="text"]').val();

    if (query.length > 0) {
        window.location.hash = '#/' +$('body').attr('id')  +'/topp/search.aspx?q=' + query;
    }
    else window.location.hash = '#';

    $('.search input[type="text"]').val('');
}

function doObjectSearch() {
    address = $('#objectSearchBar .tbAddress').val();
    type = $('#objectSearchBar .ddlObjectType').val();
    min = $('#objectSearchBar .tbMin').val();
    max = $('#objectSearchBar .tbMax').val();
    area = $('#objectSearchBar .ddlArea').val();
    window.location.hash = '#/sv/objekt.aspx?q=' + address + '&type=' + type + '&min=' + min + '&max=' + max + '&area=' + area;
}

function searchObject(href) {
    $('#topContent, #mainContent, #bottomContent').slideUp('fast');
    $('#content .mainNavigation li, #bottom .bottomNavigation li').addClass('active');

    $('#objectSearch').load(encodeURI(href) + ' #loadContent'
        , function() {
            cssClass = $(this).find('#loadContent').attr('class');
            $("#tblObjectList").tablesorter({
                headers: {
                    0: { sorter: false },
                    5: { sorter: false }
                }
            });
            $('#objectSearch').slideDown('fast', function() {
                ReinitializeAddThis();
                $('#loadmask').unmask();
            });
        });
}

function ReinitializeAddThis() {
    if (window.addthis) {
        window.addthis.ost = 0;
        window.addthis.ready();
    }
}

function bindLink(link, loadTo, contentToHide, toogleFunction, loadFunction) {
    href = '#' + link.attr('href');
    link.attr('href', href);

    link.click(function() {
        if ($(this).attr('href') == lastHash)
            $(loadTo).slideToggle('fast', toogleFunction);
    });
}

function toggleContent(href, loadTo, contentToHide, toogleFunction, loadFunction) {
    $(contentToHide).slideUp('fast', function() {
        $(loadTo).load(encodeURI(href) + ' #loadContent', function() {
            cssClass = $(this).find('#loadContent').attr('class');
            $(loadTo).slideDown('fast', loadFunction(cssClass));

            $('#content #textPageNavigation a, #content .references a, #searchResults a').each(function() {
                bindLink($(this), '#mainContent', '#topContent, #bottomContent', mainToggle, mainLoad);
            });

            $('.expandlable').hide();
        });
    });
    
}

function mainToggle(clicked) {
    $('#content .mainNavigation li, #bottom .bottomNavigation li').addClass('active');
    $('#loadmask').unmask();
}

function topToggle(clicked) {
    $('#content .mainNavigation li, #bottom .bottomNavigation li').addClass('active');
    $('#loadmask').unmask();
}

function mainLoad(cssClass) {
    var clicked = $('#content .mainNavigation li.' + cssClass + ', #bottom .bottomNavigation li.' + cssClass);
    if (cssClass.length > 0) {
        $('#content .mainNavigation li, #bottom .bottomNavigation li').removeClass('active');
        $(clicked).addClass('active');
    }

    $('.referenceSlideshow div').cycle({
        fx: 'fade',
        timeout: 4000,
        speed: 2000,
        height: 235
    });

    $('#loadmask').unmask();
}

function togglePanel(selector) {
    $(selector).slideToggle('fast');
}

function countCharacters(stringToCount, character){
    counter = 0;
    for (var i = 0; i < stringToCount.length; i++) {
        if (character == stringToCount[i])
            counter++;
    }
    return counter;
}

function postwith(to, p) {
    var myForm = document.createElement("form");
    myForm.method = "post";
    myForm.action = to;
    for (var k in p) {
        var myInput = document.createElement("input");
        myInput.setAttribute("name", k);
        myInput.setAttribute("value", p[k]);
        myForm.appendChild(myInput);
    }
    document.body.appendChild(myForm);
    myForm.submit();
    document.body.removeChild(myForm);
}

function checkEnter(e) {
    var kC = window.event ? event.keyCode :
        e && e.keyCode ? e.keyCode :
        e && e.which ? e.which : null;
    if (kC) return kC == 13;
    else return false;
}
