﻿/// <reference path="jquery-1.6.1.js" />
/// <reference path="jquery.watermark.js" />
/// <reference path="autocolumn.min.js" />
/// <reference path="library.js" />

$(function () {
    //Nyhedsbrev
    $("#newsletter-subscribe-btn").click(function (event) {
        event.preventDefault();
        $("#nyhedsbrev .message").remove();
        newsletterSubscribe();
    });
    $("input#newsletter-email").watermark({
        watermarkCssClass: "watermark",
        watermarkText: "Skriv e-mail-adresse"
    });
    //Forside version 2
    if ($("#forsideContainer").hasClass("forside2")) {
        $("ul.newslist").columnize({ columns: 2, lastNeverTallest: true });
    }

    //Layout...
    var sidebarheight = 20;
    var contentheight = $("#forsideOuter").height();
    $("#sidebar").children().each(function () {
        sidebarheight += $(this).height() + 25;
    });
    if (sidebarheight >= contentheight) {
        $("#forsideOuter").height(sidebarheight);
    }
    if ($(".top-image").hasClass("photochanger")) {
        PhotoChanger.init();
    }

    
});

function newsletterSubscribe() {
    var email_regex = /([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/;
    var email = $("input#newsletter-email").attr("value");
    if (email_regex.test(email)) {
        $.post("/base/Newsletter/AddEmail.aspx", { email: email }, function(data) {
            var status = $("value", data).text();
            var msg = $("<div class=\"message\">Tak. Din tilmelding er registreret.</div>").hide();
            $("#newsletter-form").before(msg);
            msg.show("normal");
            // alert(status);
        }, "xml");
    }
    else {
        var msg = $("<div class=\"message error\">Der er en fejl i din e-mail-adresse. Prøv igen.</div>").hide();
        $("#newsletter-form").before(msg);
        msg.show("normal");
    }
    //alert("click");
}

var PhotoChanger = function () {
    var index = 2; //Index = 2 to allow for first item that is used to display
    var numPhotos = 0;
    var interval;
    var timerInterval = 30000;
    return {
        init: function () {
            $(".changer .left").click(function () {
                PhotoChanger.changePhoto(-1);
            });
            $(".changer .right").click(function () {
                PhotoChanger.changePhoto(1);
            });
            $(".top-image").hover(function () {
                clearInterval(interval)
            }, function () {
                interval = setInterval(PhotoChanger.timer, timerInterval);
            });
            numPhotos = $(".photochanger ul li").length - 1;
            $(".photochanger").addClass("js");
            interval = setInterval(PhotoChanger.timer, timerInterval);
        },
        changePhoto: function (direction) {
            //Check for limits and loop around
            if (direction > 0) {
                if (((index + direction) - 2) >= numPhotos) {
                    //At end, return to beginning
                    index = 3;
                    direction = -1;

                }
            }
            else {
                if (((index + direction) - 2) < 0) {
                    //At beginning - go to end
                    index = numPhotos;
                    direction = 1;

                }
            }

            //Remove any leftover images
            $(".photochanger img.oldImg").remove();


            var photoLi = $(".photochanger ul li:nth-child(" + (index + direction) + ")");
            var showLi = $(".photochanger ul li:first-child");
            var newImgUrl = photoLi.data("imageurl");
            var newImg = $("<img></img>").attr("src", newImgUrl);
            var oldImg = showLi.find("img");

            //Changing images fast will hang the browser due to an exessive amount of images. Add a class to old images and remove them later
            oldImg.addClass("oldImg");

            newImg.insertAfter(oldImg).css({ "display": "none" });
            newImg.bind("load", null, function () {
                $(this).fadeIn("normal", function () {
                    oldImg.remove();
                });
            });

            //showLi.find("img").attr("src", photoLi.find("img").attr("src"));
            showLi.find(".info .title").text(photoLi.find(".info .title").text());
            showLi.find(".info .desc").html(photoLi.find(".info .desc").html());
            showLi.find(".info a.read-more").attr("href", photoLi.find(".info a.read-more").attr("href"));
            index += direction;


        },
        timer: function () {
            //alert("interval");
            //Change to next photo, if at end, return to start
            //            if (((index + 1) - 2) >= numPhotos) {
            //                //At end, reset to beginning
            //                index = 3;
            //                PhotoChanger.changePhoto(-1);
            //            }
            //            else {
            //                PhotoChanger.changePhoto(1);
            //            }
            PhotoChanger.changePhoto(1);

        }
    }



} ();
