var fs = fs || {};

fs = {
    init: function() {


        $('input:checkbox').checkBox();
        fs.showAndHideSearch.init();
        fs.selectAll.init();
        $('input').nInputDefault();
        fs.print.init();
        fs.toolTip.init();
        fs.lightbox.init();
        fs.selectReplace.init();
        fs.quickSearch.init();
    },
    toolTip: {
        init: function() {

            $('#footer .section ul.share li a img').each(function() {

                $(this).unbind('mouseover');
                $(this).bind('mouseover', function(e) {
                    e.preventDefault();
                    if ($.support.opacity) {
                        $(this).parent().parent().children('div').animate({ opacity: "show", top: "0" }, 400);
                    }
                    else {
                        $(this).parent().parent().children('div').show();
                    }

                });
                $(this).unbind('mouseout');
                $(this).bind('mouseout', function(e) {
                    e.preventDefault();
                    if ($.support.opacity) {
                        $(this).parent().parent().children('div').animate({ opacity: "hide", top: "0" }, 400);
                    }
                    else {
                        $(this).parent().parent().children('div').hide();
                    }
                });
            });
        }
    },
    lightbox: {
        init: function() {
            $("a[rel^='prettyPhoto']").prettyPhoto({ showTitle: true, /* true/false */
                allowresize: true
            });
        }
    },
    showAndHideSearch: {
        init: function() {
            $('a.showAdvSearch').bind('click', function(e) {
                e.preventDefault();
                $('#search-content').slideToggle(200);
                $(this).toggleClass('active');
                $('#tagCloud').hide();

                if (!$('a.showAdvSearch').hasClass('active')) {
                    $('#tagCloud').fadeIn();
                }
            });
        }
    },
    selectAll: {
        init: function() {
            $('.checkAll').bind('click', function(e) {
                e.preventDefault();
                $("#searchContainer dl input").attr('checked', true);
                $('#searchContainer .ui-checkbox').addClass('ui-checkbox-state-checked');
            });
            $('.uncheckAll').bind('click', function(e) {
                e.preventDefault();
                $("#searchContainer dl input").attr('checked', false);

                $('#searchContainer .ui-checkbox').removeClass('ui-checkbox-state-checked');
            });

        }
    },
    print: {
        init: function() {
            $(".print").bind('click', function(e) {
                e.preventDefault();
                window.print();
            });
        }
    },
    selectReplace: {
        init: function() {
            $('select.advSelect').selectmenu({
                style: 'dropdown',
                width: 197,
                transferClasses: true

            });
        }
    },
    quickSearch: {
        init: function() {
            $(".qs_input").keyup(function() { fs.quickSearch.search(); })

            if ($('#searchLinks li').length > 40) {
                $('#searchLinks').hide();

                $('#showAllEntrepreneurs').show();
                $('#showAllEntrepreneurs').click(fs.quickSearch.showAllEntrepreneurs);
                
            }
        },
        showAllEntrepreneurs: function(e) {
            fs.quickSearch.search(true);
            e.preventDefault();
        },
        search: function(showAll) {
            $('#searchLinks').hide();

            var qs_input = $(".activeSearch .qs_input");
            var hits;
            var searchText = qs_input[0].value;
            if (!showAll) {
                if (searchText.length == 0) {
                    if ($("#searchLinks li a").length > 40) {
                        $("#activeSearchResults").hide();
                        return;
                    }
                }
                var regexp = new RegExp(qs_input[0].value, "gim");

                var hits =
                        $("#searchLinks li a").filter(function() {
                            return $(this).text().match(regexp);
                        });

            } else {
                hits = $("#searchLinks li a");
            }


            var result = '';

            for (var i = 0; i < hits.length; i++) {
                var currentLink = $(hits[i]);
                if (i % 2 == 0) {
                    result += "<li><a href='" + currentLink.attr("href") + "'>" + $(hits[i]).html().replace(regexp, function(match) { return "<strong>" + match + "</strong>"; }) + "</a></li>";
                } else {
                    result += "<li class='eq'><a href='" + currentLink.attr("href") + "'>" + $(hits[i]).html().replace(regexp, function(match) { return "<strong>" + match + "</strong>"; }) + "</a></li>";
                }
            }

            $("#activeSearchResults").html(result);
            $('#showAllEntrepreneurs').hide();
            $("#activeSearchResults").show();
            
        }
    }
}

function openTimeline() {
    $.prettyPhoto.open('/flash/foretagsamheten.swf?width=875&height=550');
}

$(document).ready(function() {
    fs.init();
});