﻿/// <reference path="jquery-1.4.min.js" />
/// <reference path="plugins.js" />
(function($) {
    $(function() {

        // ---[ Main menu ]---
        (function() {
            // parameters
            var animationSpeed = 250;
            var mainMenu = $(".main .menu ul.navigation");
            var heightDataName = "startHeight";
            if (mainMenu.length > 0 && mainMenu.find(">li").length > 0) {
                // record values for each child ul element
                mainMenu.find(">li>ul").each(function() {
                    $(this).data(heightDataName, $(this).height());
                }).height(0);

                // open active element
                if (mainMenu.find(">li.active").length > 0) {
                    // find first-level active element
                    var activeUl = mainMenu.find(">li.active>ul");
                    activeUl.height(activeUl.data(heightDataName));
                } else {
                    // second-level active element
                    var activeUl = mainMenu.find("li.active").parent();
                    activeUl.height(activeUl.data(heightDataName)).parent().addClass("active");
                }

                // hover functionality
                mainMenu.find(">li").mouseenter(function() {
                    var t = $(this);
                    var current = t.find(">ul");
                    mainMenu.find(">li>ul").stop().not(current).animate({ height: 0 }, animationSpeed);
                    current.animate({ height: current.data(heightDataName) }, animationSpeed);
                });
            }

        })();


        // ---[ Gallery lightbox ]---
        (function() {
            // build the gallery lightbox popup
            var buildPopup = function() {
                var lightboxItems = $(".gallery.lightbox li");
                var popup = $('<div class="lightbox-popup"/>').appendTo("body");
                popup.append('<div class="top-cap"/><div class="content"/><div class="bottom-cap"/>');
                var content = popup.find(".content");
                var list = $('<ul/>').appendTo(content);
                lightboxItems.each(function(i) {
                    var imgSource = $(this).find("a:first").attr("href");
                    var caption = $(this).find(".caption").html();
                    list.append('<li><img src="' + imgSource + '" alt=""/><p class="caption">' + caption + '</p></li>');
                    $(this).find("a").click(function() {
                        displayImage(i);
                        return false;
                    });
                });
                var listControl = $('<div class="controls"><button class="back">&lt;&lt;</button> <span class="pager">1/' + list.find("li").length + '</span> <button class="forward">&gt;&gt;</button></div>').appendTo(content);
                list.find("li").hide();
                popup.hide().click(function(event) {
                    var target = $(event.target);
                    if (target.is(".close")) {
                        popup.hide();
                    } else if (target.is(".back")) {
                        if (currentImage > 0) {
                            displayImage(currentImage - 1);
                        }
                    } else if (target.is(".forward")) {
                        if (currentImage < list.find("li").length - 1) {
                            displayImage(currentImage + 1);
                        }
                    }
                });

                // displays the selected image in the popup
                var currentImage = 0;
                var displayImage = function(i) {
                    popup.css({
                        position: "absolute",
                        top: "300px",
                        left: ($(window).width() - popup.outerWidth()) / 2 + "px"
                    }).show().find('li').hide().eq(i).show();
                    popup.find(".pager").text(i + 1 + "/" + popup.find("li").length);
                    currentImage = i;
                };
            };

            if ($(".gallery.lightbox li").length > 0) {
                buildPopup();
            }
        })();


        // ---[ Profile lightbox ]---
        (function() {
            var lightboxLinks = $("a.profile-link");
            lightboxLinks.click(function(event) {
                var overlay = $('<div class="lightbox-popup"><div class="top-cap"></div><div class="content"></div><div class="bottom-cap"></div></div>');
                $(this).parent().find(".profile").clone().appendTo(overlay.find("content"));
                overlay.appendTo('body').css({
                    left: ($(window).width() - 760) / 2 + "px",
                    top: "230px"
                }).click(function() {
                    $(this).remove();
                    return false;
                });
                event.preventDefault();
            });
        })();
        /**/

        // ---[ Wizard steps ]---
        (function() {
            var wizardSteps = $("ul.wizard-steps");
            // check if wizard steps are present on page
            if (wizardSteps.length) {
                wizardSteps.each(function() {
                    var steps = $(this).find("li");
                    var arrow;
                    var pastActive = false;
                    for (var i = 0; i < steps.length - 1; i++) {
                        if (steps.eq(i).hasClass("active")) {
                            pastActive = true;
                        }
                        if (!pastActive) {
                            steps.eq(i).addClass("done");
                            arrow = $('<li class="arrow visited"></li>');
                        } else {
                            arrow = $('<li class="arrow"></li>');
                        }
                        arrow.insertAfter(steps.eq(i));
                    }
                    $(this).find("li.arrow:last").addClass("blank");
                    $(this).find("li:last").addClass("final");
                    var width = 0;
                    $(this).find("li").each(function() {
                        width += $(this).outerWidth(true);
                    });
                    wizardSteps.css("width", width + 2 + "px");
                });
            }
        })();

        // ---[ Rounded corners plugin ]---
        $.fn.extend({
            roundIt: function(params) {
                params = $.extend(true, $.fn.roundIt.defaults, params);
                return $(this).each(function() {
                    if (params.topCapClass !== false)
                        var topCap = $('<div class="' + params.topCapClass + '">&nbsp;</div>').prependTo(this);
                    if (params.bottomCapClass !== false)
                        var bottomCap = $('<div class="' + params.bottomCapClass + '">&nbsp;</div>').appendTo(this);
                });
            }
        });

        $.fn.roundIt.defaults = {
            topCapClass: "top-cap",
            bottomCapClass: "bottom-cap"
        };

        // execute rounded corners
        $(".main,.dark-shade,.light-shade,.section,.green-shade,.push-box").roundIt();
        $(".main .menu").roundIt({ bottomCapClass: false });


        // ---[ Element centering plugin ]---
        $.fn.extend({
            centerOnElement: function(params) {
                params = $.extend(true, $.fn.centerOnElement.defaults, params);
                return $(this).each(function() {
                    var el = $(this);
                    if (params.container) {
                        var t = $(params.container);
                        el.remove().appendTo("body").css({
                            position: "absolute",
                            visibility: "visible",
                            display: "block",
                            width: t.outerWidth() + "px",
                            height: t.outerHeight() + "px",
                            left: t.offset().left + "px",
                            top: t.offset().top + "px",
                            zIndex: 10000
                        });
                    }
                });
            }
        });

        $.fn.centerOnElement.defaults = { container: undefined };

    });
})(jQuery);






String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}

String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}


if (!window.NtkTeaserWebRole)
    window.NtkTeaserWebRole = {};

NtkTeaserWebRole.preloader = function() {
}

NtkTeaserWebRole.preloader.prototype =
{
    handleLoad: function(plugIn, userContext, rootElement) {
        this.plugIn = plugIn;

        // Sample button event hookup: Find the button and then attach event handlers
        // this.button = rootElement.children.getItem(0);	

        // this.button.addEventListener("MouseDown", Silverlight.createDelegate(this, this.handleMouseDown));
    }

    // Sample event handler
    //handleMouseDown: function(sender, eventArgs) 
    //{
    //}
}

function onSourceDownloadProgressChanged(sender, eventArgs) {
    if (sender.findname("loaderProgress") != null) {
        var loaded = Math.round(eventArgs.progress * 200);
        sender.findname("loaderProgress")["Width"] = loaded;
    }
}