﻿$(document).ready(function () {
    var showText = $("#showText").val();
    var hideText = $("#hideText").val();

    $("#nav li").hover(function () {
        $(this).find("div.linkheaderContainer.two-column").slideDown(50, function () {


            var linkHeaderHeight = 0;
            $.each($(".linkheader"), function () {
                if ($(this).height() > linkHeaderHeight) {
                    linkHeaderHeight = $(this).height();
                }
            });
            $(".linkheader").height(linkHeaderHeight);

            $(this).parent().find(".arrow").attr("class", "arrow up");
        });
        $(this).find("div.linkheaderContainer.one-column").slideDown(50, function () {
            $(this).parent().find(".arrow").attr("class", "arrow up");
        });

    }, function () {
        $(this).find("div.linkheaderContainer").slideUp(50, function () {
            $(this).parent().find(".arrow").attr("class", "arrow down");
        });
    });

    $("img#btnSearchHeader").click(function () {
        $("#searchFormHeader").submit();
    });
    $("a#search-button").click(function () {
        $("#searchFormFooter").submit();
    });

    var t;
    $("li.local").mouseover(function () {
        t = setTimeout(show, 300);
    });
    $("li.local").mouseout(function () {
        clearTimeout(t);
    });
    $("div#worldmap").mouseleave(function () {
        clearTimeout(t);
        $("div#worldmap").fadeOut();
    });
    $("#selectCountry").fancyDropdown();

    function show() { $("div#worldmap").fadeIn(); }


    //$(".linkheader").last().css('border-right', 'none');
    $(".linkheader").first().css('border-left', 'none');
    $(".linkheader").first().css('padding-left', '15px');

    $.each($(".post-body"), function () {
        var ct = $(this).find(".content-text");
        var img = $(this).find(".img-news");

        ct.css("min-height", img.height());
    });


    $("div.slickbox").hide();

    $("a.filterButton").click(function () {
        $("div.slickbox").toggle("slow");

        if ($(this).text() == showText)
            $(this).text(hideText);
        else
            $(this).text(showText);

        if ($(this).hasClass('on')) { $(this).removeClass('on').addClass('off'); }
        else if ($(this).hasClass('off')) { $(this).removeClass('off').addClass('on'); }


        return false;
    });


    $("input:reset").click(function () {
        $("input:checkbox").attr('checked', false);
    });

    $(".all").click(function () {
        $(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked);
    });



    $("#dialog").dialog({
        autoOpen: false,
        width: 630,
        modal: true,
        closeText: 'Close',
        resizable: false

    });
    var closeSpan = $("div[role='dialog'] span.ui-icon-closethick");
    //prepend a span with closeText to the closing-image
    closeSpan.parent().before('<span style=" float: right; font-family: arial;font-size: 13px; font-weight: normal;margin-right: 10px; margin-top: 3px;">' + closeSpan.text() + '</span>');

    $(".hasDownloadForm").click(function (event) {
        event.preventDefault();

        openIframe($(this).attr('href'));
    });

    $(".signup").click(function (event) {
        event.preventDefault();
        
        openIframe($(this).attr('href'));
    });

    var openIframe = function (src) {
        var $dialog = $('<div></div>');
        var iframe = $('<iframe/>').attr('src', src);

        // IE fix for iframe border bug, http://stackoverflow.com/questions/1625835/ie-8-iframe-border-problem
        if ($.browser.msie) {
            iframe.attr('frameborder', '0');
        }
        iframe.load(function () {
            // Resize iframe to the full height of its content, when it is done loading
            var newHeight = $(this.contentWindow.document).height();

            // Firefox as of version 7.0.1 has a bug causing it to calculate the height wrong
            if ($.browser.mozilla) {
                $(this).height(newHeight + 1);
            } else {
                $(this).height(newHeight);
            }

            // Reposition iframe container to the center of the browser window
            // since the iframe changed dimensions, it is no longer at center
            $dialog.dialog('widget').position({ my: 'center', at: 'center', of: window });

            $('.closeButton a', $(this).contents()).click(function (event) {
                event.preventDefault();

                $dialog.dialog('close');
            });
        });

        $dialog.append(iframe);
        $dialog.dialog({ autoOpen: true, width: 630, modal: true, zIndex: 1000000, dialogClass: 'download-Popup-Dialog', resizable: false });
    };

    var maxRows = $(".article-holder.search-results").attr("max-rows");
    if (maxRows == null || maxRows == '' || jQuery.isNaN(maxRows))
        maxRows = '4';
    $(".article-holder.search-results .section .text-section").ThreeDots({ max_rows: maxRows, text_span_class: 'ellipsis-text', e_span_class: 'threedots-ellipsis' });
    $(".article-holder.search-results .section .text-section").attr("threedots", "");
});

