﻿///<reference path="D:\Projects\MLM_ECM_LUND\src\Lund.Web\Templates\Public\Javascripts\jquery-1.3.2-vsdoc.js"/>
/* global $, lund */

$(document).ready(function() {
    var overlaytranslate = $("a[rel=#translate-form]").overlay({
        top: "20%",
        expose: {
            color: "#333", 
            loadSpeed: 50,
            opacity: 0.75
        } 
    });
    $("#translate-form .close").remove();
    $("#translate-form .left-side").before("<div class=\"Block\" ><h2 style=\"width:401px;height:20px\" >Other Languages</h2><a style=\"position:absolute;top:15px;right:15px;color:black;\" href=\"/\" class=\"close\"><span>X</span></a></div>");
    $('#translate-form .close').click(function() {
        // this.close();
        $("a[rel=#translate-form]").overlay().close();
        //alert('Handler for .click() called.');
    });
    //closeDiv.html("<div class=\"Block\" ><h2 style=\"width:120px\" >Other language <a style=\"float:right\" class=\"close\"><span>X</span></a></h2></div>");

    //$("a[rel=#translate-form]").overlay();
    $.fn.PopApplyWin();
    $.fn.BreakingNews();
    $.fn.CurrentNow();
    $.fn.InitEventCalendar();
    $.fn.InitMisc();
    $.fn.InitTrusteesSearch();
    $.fn.Styling();
    $.fn.Contact();
    $.fn.Calender();
    $.fn.BannersGrow();
    //$('body').addClass('JS');
    $TradeMarkSlideshow.init();
    $.fn.SlideButtons();
    //$.fn.MiniMe(); 
    //$.fn.VisitedLinksCounter();
    //$.fn.ImageVault();
    $.fn.ImageVault();
    $.fn.FunctionList();
    $.fn.MapLinks();
    $.fn.CopyPaging();
    $.fn.NewsArchive();
    $.fn.Dropdown();

});

$.fn.parseQueryString = function(queryString) {
    var params = {};
    var qs = queryString.replace(/\+/g, ' ');
    var ps = qs.split('?');

    if (ps.length > 1) {
        var args = ps[1].split('&');

        // split out each name=value pair
        for (var i = 0; i < args.length; i++) {
            var pair = args[i].split('=');
            var name = decodeURIComponent(pair[0]);

            var value = (pair.length == 2)
		    ? decodeURIComponent(pair[1])
		    : name;

            params[name] = value;
        }
    }

    return params;
}

$.fn.MapLinks = function() {

    var links = $("a.map");

    if (links.size() > 0) {

        $.getScript(
            "/templates/public/javascripts/map/map-functionality.js",

            function() {

                function getTitleAndAddress(str) {
                    var toReturn = {};
                    if (str.indexOf(';') !== -1) {
                        var splitted = str.split(';');
                        if (splitted.length > 0) {
                            toReturn.title = $.trim(splitted[0]);
                        }
                        if (splitted.length > 1 && splitted[1] !== undefined && splitted[1].length > 0) {
                            toReturn.poi = escape($.trim(splitted[1]));
                        }
                    }
                    return toReturn;
                }

                links.click(function() {
                    try {
                        var params = $.fn.parseQueryString($(this).attr("href"));

                        function valOrMax(val, max) {
                            if (val < max) {
                                return val;
                            }
                            else { return max; }
                        }
                        if (params.fullwindow === "true") {
                            params.height = $(window).height() - 40;
                            params.width = $(window).width() - 20;
                        }
                        else {
                            params.height = params.height || valOrMax($(window).height() - 100, 600);
                            params.width = params.width || valOrMax($(window).width() - 20, 700);
                        }

                        var fromTitle = getTitleAndAddress($(this).attr("title"));

                        if (fromTitle.title) {
                            params.title = params.title || fromTitle.title;
                        }
                        if (fromTitle.poi) {
                            params.poi = params.poi || fromTitle.poi;
                        }

                        lund.map(params).dialog("option", "position", "center").parent("div").css("visibility", "visible");
                    }
                    catch (ex) {
                        alert(ex);
                    }

                    return false;
                });
            }
        );
    }
};

$.fn.FunctionList = function() {
    var c = $("#FunctionList");
    c.find(".GoBack").click(function() {
        history.go(-1);
    });
};

$.fn.CurrentNow = function() {
    var c = $("#NowCurrent");
    function context(selector) {
        return c.find(selector);
    }

    context("ul").tabs(
        "#NowCurrent div.Pane",
        {
            initialIndex: null,
            event: 'click',
            tabs: "h3",
            effect: "slide"
        });

    // Handling keyboard input
    var links = context("h3 a");
    links.focus(function() {
        $(this).contents("span").css("text-decoration", "underline");
        $(this).closest("h3").click();

    });
    links.blur(function() {
        $(this).contents("span").css("text-decoration", "none");
    });

    context("h3").click(function() {
        context("li").removeClass("current");
        $(this).closest("li").toggleClass("current");
    });

    // Initial state
    context("li:first-child").toggleClass("current");
    context("li").not(context("li:first-child")).find(".Pane").css("display", "none");

    var height = context("div.Body").height();
    var minHeight = 202;
    if (minHeight < height) {
        context("div.Body").height(height);
    }
};

$.fn.InitEventCalendar = function() {
    var h2span = $("#EventCalendar h2 span");
    h2span.click(function() { $("#EventCalendar div.Body > div.Pane").slideToggle("normal"); });
    h2span.addClass("LinkPointer");

    $("#EventCalendar div.Body > div.Pane").hide();
};

$.fn.InitMisc = function() {
    var slider = $("#ContactFormSlider");
    slider.hide();
    $("#FunctionList ul li.Contact a").click(function() { slider.slideToggle("normal"); });
    slider.hide();
};

$.fn.InitTrusteesSearch = function() {
    var c = $("#Trustees");
    function context(selector) {
        return c.find(selector);
    }

    context("td.Name").each(function() {
        var link = $(this).find("a");
        var name = link.text();
        link.replaceWith(name);
    });

    context("tbody tr.Basics").hover(
        function() {
            $(this).addClass("Hover");
        },
        function() {
            $(this).removeClass("Hover");
        })
       .click(function() {
           $(this).toggleClass("Opened").next().find(".Pane").toggle("fast");
       });
};

$.fn.Styling = function() {
    $(".BodyText").find("table.Striped tr:nth-child(odd)").addClass("Odd");
};

$.fn.Contact = function() {
    var link1 = $('#ContactLink');
    link1.attr("href", "#");
    link1.click(function() {
        $("#FunctionList").toggleClass("Open");
    });
};

$.fn.BreakingNews = function() {
    var context = $("#BreakingNews li");
    context.hide();

    if ($.cookie("BreakingNews") === null) {
        $.cookie("BreakingNews", '-1', { path: '/' });
        context.show();
    }
    else {
        var closed = $.cookie("BreakingNews");

        closed = closed.split('l');

        context.each(function() {
            var isClosed = false;

            for (var i = 0; i < closed.length; i++) {
                if (this.id === closed[i]) {
                    isClosed = true;
                }
            }
            if (!isClosed) { $(this).show(); }
        });
    }

    //Cookie
    context.find(".Close").click(function() {
        $(this).closest("li").slideUp("fast");

        var closed = $.cookie("BreakingNews");
        closed = closed + 'l' + $(this).parent().attr('id');
        $.cookie("BreakingNews", closed, { path: '/' });
    });
};

$.fn.Calender = function() {
    var calenderWrapper = $(".CalenderSlider");
    calenderWrapper.removeClass("CalenderSlider");
    calenderWrapper.addClass("Calendar");
};

$.fn.PopApplyWin = function() {
    $("#ApplicationLink").click(function() {
        var url = $("#ApplicationLink").attr("href");
        var loc = window.location;
        $("#ApplicationLink").attr("href", loc);
        var x = window.open(url, "myWin", "height=550,width=700,resizable=1,scrollbars=1");
    });
};

$.fn.BannersGrow = function() {
    var c = $("div.Banners");
    function context(selector) {
        return c.find(selector);
    }

    var row1 = context("li:nth-child(1) a, li:nth-child(2) a, li:nth-child(3) a");
    var row2 = context("li:nth-child(4) a, li:nth-child(5) a, li:nth-child(6) a");

    function setHeight(elements) {
        var highest = 0;
        if (elements) {
            elements.each(function() {
                if ($(this).height() > highest) {
                    highest = $(this).height();
                }
            });
            if (highest > 110) {
                elements.each(function() {
                    $(this).height(highest);
                });
            }
        }
    }

    setHeight(row1);
    setHeight(row2);

};

$.fn.SlideButtons = function() {
    $(".PauseSlide").click(function() {
        $("div.Slides > ul", $TradeMarkSlideshow.context).cycle('pause');
        $(".PauseSlide").hide();
        $(".ResumeSlide").show();

    });
    $(".ResumeSlide").click(function() {
        $("div.Slides > ul", $TradeMarkSlideshow.context).cycle('resume');
        $(".PauseSlide").show();
        $(".ResumeSlide").hide();
    });
};

$TradeMarkSlideshow = {
    context: false,
    tabs: false,
    timeout: 8000,
    slideSpeed: 2000,
    tabSpeed: 300,
    fx: 'fade',

    init: function() {
        this.context = $('#TradeMarkSlideshow');
        this.tabs = $('ul.Slides-nav li', this.context);
        this.tabs.remove();
        this.prepareSlideshow();
        this.context.find(".Slides > ul").width("200px").css("position", "relative");
    },

    prepareSlideshow: function() {

        $("div.Slides > ul", $TradeMarkSlideshow.context).cycle({
            fx: $TradeMarkSlideshow.fx,
            timeout: $TradeMarkSlideshow.timeout,
            speed: $TradeMarkSlideshow.slideSpeed,
            fastOnEvent: $TradeMarkSlideshow.tabSpeed,
            pager: $("ul.Slides-nav", $TradeMarkSlideshow.context),
            pagerAnchorBuilder: $TradeMarkSlideshow.prepareTabs,
            before: $TradeMarkSlideshow.activateTab,
            pauseOnPagerHover: true,
            pause: true,
            height: '133px',
            fit: true
        });
    },

    prepareTabs: function(i, slide) {

        return $TradeMarkSlideshow.tabs.eq(i);
    },

    activateTab: function(currentSlide, nextSlide) {
        var activeTab = $('a[href="#' + nextSlide.id + '"]', $TradeMarkSlideshow.context);
        if (activeTab.length) {
            $TradeMarkSlideshow.tabs.removeClass('on');

            activeTab.parent().addClass('on');
        }
    }
};

$.fn.VisitedLinksCounter = function() {
    var links = $("#visitedLinks a");

    var visitedLinks = links.filter(
        function() {
            var color = $(this).css("color");
            var sameColor = color == "rgb(18, 52, 86)";
            return sameColor;
        });

    $("#spanText").text((links.size() - visitedLinks.size()));

    var links1 = $("#visitedLinks1 a");

    var visitedLinks1 = links1.filter(
        function() {
            var color1 = $(this).css("color");
            var sameColor1 = color1 == "rgb(18, 52, 86)";
            return sameColor1;
        });

    $("#spanText1").text((links1.size() - visitedLinks1.size()));
};

$.fn.MiniMe = function() {

    function Reset() {
        $('.Mail > ul').hide();
        $('.Mail').removeClass('opened'); $('.Mail').addClass('closed');
        $('.Activities > ul').hide();
        $('.Activities').removeClass('opened'); $('.Activities').addClass('closed');
        $('.Groups > ul').hide();
        $('.Groups').removeClass('opened'); $('.Groups').addClass('closed');
    }

    //Cookie
    $("#MiniMe .Mail > h3").click(function() {
        Reset();
        $('.Mail').removeClass('closed'); $('.Mail').addClass('opened'); $('.Mail > ul').show();
        $.cookie('MiniMe', '1', { path: '/', expires: 10 });
    });

    $("#MiniMe .Activities > h3").click(function() {
        Reset();

        $('.Activities').removeClass('closed'); $('.Activities').addClass('opened'); $('.Activities > ul').show();
        $.cookie('MiniMe', '2', { path: '/', expires: 10 });
    });

    $("#MiniMe .Groups > h3").click(function() {
        Reset();
        $('.Groups').removeClass('closed'); $('.Groups').addClass('opened'); $('.Groups > ul').show();
        $.cookie('MiniMe', '3', { path: '/', expires: 10 });
    });

    Reset();
    if ($.cookie('MiniMe') === null) {
        $.cookie('MiniMe', '1', { path: '/', expires: 360 });
    }
    var opened = $.cookie('MiniMe');

    if (opened == '1') {
        Reset();
        $('#MiniMe .Mail > ul').show();
        $('#MiniMe .Mail').removeClass('closed'); $('.Mail').addClass('opened');
    }
    else if (opened == '2') {
        Reset();
        $('#MiniMe .Activities > ul').show();
        $('#MiniMe .Activities').removeClass('closed'); $('.Activities').addClass('opened');
    }
    else if (opened == 3) {
        Reset();
        $('#MiniMe .Groups > ul').show();
        $('#MiniMe .Groups').removeClass('closed'); $('.Groups').addClass('opened');
    }
};



$.fn.ImageVault = function() {

    $('#rotator').innerfade({
        speed: 'slow',
        timeout: 6000,
        type: 'sequence',
        containerheight: '300px'
    });

    var imagegallery = $('.imagegallery');
    if (imagegallery.length) {
        var link;
        imagegallery.each(function() {
            link = $(this).find("#preview");
        });
        link.lightBox();
    }
};

$.fn.LaunchApplication = function(path) {
    var WSH = new ActiveXObject("Word.Application");
    WSH.run(path);
};

$.fn.CopyPaging = function() {
    var pagingText = $(".PagingContainer").html();
    if (pagingText != null)
        $(".PagingTop").append(pagingText);
}


$.fn.NewsArchive = function() {

    $(".NewsArchiveDate").attr("href", "#");

    $(".NewsArchiveDate").click(function() {
        if ($(this).parent().next().hasClass("DisplayNone")) {
            $(".NewsArchive").addClass("DisplayNone");
            $(".NewsArchive").slideUp("DisplayNone");
            $(".PlusMinusSign").text('+');
            $(this).parent().next().slideDown();
            $(this).parent().next().removeClass("DisplayNone");
            $(this).prev().text('-');
        }
        else {
            $(this).parent().next().slideUp();
            $(this).parent().next().addClass("DisplayNone");
            $(this).prev().text('+');
        }
    });

    $(".PlusMinusSign").click(function() {
        if ($(this).next().parent().next().hasClass("DisplayNone")) {
            $(".NewsArchive").addClass("DisplayNone");
            $(".NewsArchive").slideUp("DisplayNone");
            $(".PlusMinusSign").text('+');
            $(this).next().parent().next().slideDown();
            $(this).next().parent().next().removeClass("DisplayNone");
            $(this).text('-');
        }
        else {
            $(this).next().parent().next().slideUp();
            $(this).next().parent().next().addClass("DisplayNone");
            $(this).text('+');
        }
    });
};
$.fn.Dropdown = function() {
    $("#app-dropdown", '#ApplicationList').dropdown({
        choiceText: ".link-title",
        isLinkList: true,
        dropUp: true,
        slideSpeed: 0
    });
    $("#main-page-dropdown").dropdown({
        choiceText: ".link-title",
        isLinkList: true,
        dropUp: false,
        slideSpeed: 0
    });
};


