﻿function nextPhoto() {
    if (sliderOffset >= ($("#Content #BannerPhotos .BannerPhoto").size() - 1) * 580) {
        window.sliderOffset = 0;
    } else {
        window.sliderOffset += 580;
    }
    $("#BannerPhotos").animate({ "margin-left": -1 * window.sliderOffset }, 500, "easeInOutQuart");

    $(".BannerControlActive").removeClass("BannerControlActive");
    $("#BannerControls li").eq(window.sliderOffset / 580).addClass("BannerControlActive");
}

function loadSection(section) {
    $(".SectionList li a[href=" + section + "]").addClass("SectionActive");
    $(section).addClass("SectionActive");
    $("#ContentSliderLeft").addClass("SlidedOut").css({ "margin-left": -601 });
    $(".SectionBackButton").css("visibility", "visible");
}


function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
} 

function colorArrows() {
    if (parseInt($("#NewsWrapper .NewsItem:first").css("margin-top").replace("px", "")) < 0) {
        $("#NewsScrollerUp").css("color", "#0079BC");
    } else {
        $("#NewsScrollerUp").css("color", "#D9D9D9");
    }

    if (parseInt($("#NewsWrapper .NewsItem:first").css("margin-top").replace("px", "")) <= -(window.newsHeight - 280)) {
        $("#NewsScrollerDown").css("color", "#D9D9D9");
    } else {
        $("#NewsScrollerDown").css("color", "#0079BC");
    }
}

jQuery.expr[':'].containsCaseInsensitive = function (a, i, m) {
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
};

$(document).ready(function () {
    font.setup();

    $.cookie("animationEnabled", 1);

    window.newsHeight = 0;
    $("#NewsWrapper .NewsItem").each(function () {
        window.newsHeight += $(this).height();
    });

    $(".NewsItem").click(function (event) {
        window.location.href = $(this).find("a.ReadMoreLink").attr("href");
    });

    $(".NewsItem .EditButton").click(function (event) {
        event.stopPropagation();
    });

    if ($("#NewsWrapper").size() > 0) {
        colorArrows();
    }

    if (!font.isInstalled("Sakkal Majalla")) {
        $(".ServiceHeader").addClass("ServiceHeaderTahoma");
        $("body").addClass("Tahoma");
    }

    if (window.location.hash != "") {
        if ($(window.location.hash).size() > 0) {
            loadSection(window.location.hash);
        }
    }
    /* OBSOLETE
    $("#Menu li a:contains(Media)").each(function () {
    var href = $(this).attr("href");
    var append = '<ul class="SubMenu">';
    var menu = {
    "PUBLIKACE": "#publikace",
    "TISKOVÉ ZPRÁVY": "#tiskove-zpravy",
    "NAPSALI O NÁS": "#napsali-o-nas",
    "NSG BLOGY": "#NSG-blogy",
    "PŘÍKLADOVÉ STUDIE": "#prikladove-studie",
    "MORISON Int. NEWS": "#morison-international-news"
    };

    $.each(menu, function (key, value) {
    append += '<li><a href="' + href + value + '" >' + key + '</a></li>';
    });

    append += '</ul>';
    $(this).after(append);
    });
    */

    $("#Menu li a:containsCaseInsensitive(Media)").after($("#HiddenMediaMenu").html());

    $(".SubMenu li a").not(".NotInternal").click(function (event) {
        if ($(this).parent().parent().parent().is(".MenuActive")) {
            var href = "#" + $(this).attr("href").split("#")[1];
            $(".SectionList li a[href=" + href + "]").click();
            event.preventDefault();
            return false;
        }
    });

    $("#NewsletterInput").addClass("PreText").val("zde zadejte váš e-mail").focus(function () {
        if ($(this).hasClass("PreText")) {
            $(this).removeClass("PreText");
            $(this).val("");
        }
    }).blur(function () {
        if (!$(this).hasClass("PreText") && isEmpty($(this).val())) {
            $(this).addClass("PreText");
            $(this).val("zde zadejte váš e-mail");
        }
    });

    $("#NewsletterButton").click(function (event) {
        if (isValidEmailAddress($("#NewsletterInput").val())) {
            $.ajax({
                type: 'POST',
                url: "../../WebServices/RegistrationService.asmx/RegisterToNewsletter",
                data: '{ "email": "' + $("#NewsletterInput").val() + '" }',
                contentType: "application/json",
                success: function (data) {
                    $("#NewsletterInput").val("děkujeme!").addClass("PreText");
                },
                dataType: "json"
            });
            $("#NewsletterInput").val("odesílám...").attr("disabled", "disabled");
        }
        event.preventDefault();
        return false;
    });

    $("#SearchArrow, #SearchButton").hover(function () {
        $("#SearchInputWrapper").animate({ "width": 162 }, 500, "easeInOutQuart");
        $("#SearchArrow").animate({ "color": "#AAAAAF" }, 500);
    });

    $(".SectionList li a").not(".NotInternal").click(function (event) {
        if ($($(this).attr("href")).size() > 0 && $(this).attr("href") != "#") {
            if ($("#ContentSliderLeft").hasClass("SlidedOut")) {
                var $this = $(this);
                $(".SectionList li a.SectionActive").removeClass("SectionActive");
                $(this).addClass("SectionActive");
                $("#ContentSliderRight .SectionActive").fadeOut(500, function () {
                    $(this).removeClass("SectionActive");
                    $($this.attr("href")).fadeIn(500, function () {
                        $(this).addClass("SectionActive").show();
                    });
                });
            } else {
                $(this).addClass("SectionActive");
                $($(this).attr("href")).addClass("SectionActive").show();
                $("#ContentSliderLeft").addClass("SlidedOut").animate({ "margin-left": -601, "margin-right": 0 }, 500, "easeInOutQuart");
                $(".SectionBackButton").css("visibility", "visible");
            }
        }
        event.preventDefault();
        return false;
    });

    $(".SectionBackButton").click(function (event) {
        $("#ContentSliderLeft").removeClass("SlidedOut").animate({ "margin-left": 0, "margin-right": 20 }, 500, "easeInOutQuart", function () {
            $("#ContentSliderRight .SectionActive").hide();
        });
        $(".SectionList li a.SectionActive").removeClass("SectionActive");
        $(".SectionBackButton").css("visibility", "hidden");
        event.preventDefault();
        return false;
    });

    $(".SubMenu li a").css("opacity", 0.85);

    window.sliderOffset = 0;
    window.sliderInterval = setInterval(nextPhoto, 5000);

    $("#BannerControls").html("");
    $("#BannerPhotos .BannerPhoto").each(function () {
        $("#BannerControls").append('<li><a href="#"><span></span></a></li>');
    });
    $("#BannerControls li:first").addClass("BannerControlActive");
    $("#BannerControls li a").click(function () {
        clearInterval(window.sliderInterval);
        window.sliderOffset = 580 * $(this).parent().index();
        $("#BannerPhotos").animate({ "margin-left": -1 * window.sliderOffset }, 500, "easeInOutQuart");
        window.sliderInterval = setInterval(nextPhoto, 5000);
        $(".BannerControlActive").removeClass("BannerControlActive");
        $(this).parent().addClass("BannerControlActive");
    });

    $("#NewsWrapper .NewsItem:first").css("margin-top", 0);

    $("#NewsScrollerUp").click(function (event) {
        if (!window.lockNewsScroll) {
            window.lockNewsScroll = true;
            if (parseInt($("#NewsWrapper .NewsItem:first").css("margin-top").replace("px", "")) < 0) {
                $("#NewsWrapper .NewsItem:first").animate({ "margin-top": "+=150" }, 500, "easeInOutQuart", function () {
                    colorArrows();
                    window.lockNewsScroll = false;
                });
            } else {
                window.lockNewsScroll = false;
            }
        }
        event.preventDefault();
        return false;
    });
    $("#NewsScrollerDown").click(function (event) {
        if (!window.lockNewsScroll) {
            window.lockNewsScroll = true;
            if (parseInt($("#NewsWrapper .NewsItem:first").css("margin-top").replace("px", "")) >= -(window.newsHeight - 280)) {
                $("#NewsWrapper .NewsItem:first").animate({ "margin-top": "-=150" }, 500, "easeInOutQuart", function () {
                    colorArrows();
                    window.lockNewsScroll = false;
                });
            } else {
                window.lockNewsScroll = false;
            }
        }
        event.preventDefault();
        return false;
    });

    $("#NewsletterInput, #SearchInput").focus(function () {
        $(this).val("");
    });

    $(".ServiceDetailContent ul li").each(function () {
        $(this).html("<span>" + $(this).html() + "</span>");
    });

    if ($("#ContactMap").size() > 0) {
        var myLatlng = new google.maps.LatLng(50.08809, 14.424834);
        var myCenter = new google.maps.LatLng(50.089657, 14.424834);
        var myOptions = {
            zoom: 15,
            center: myCenter,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        var map = new google.maps.Map(document.getElementById("ContactMap"), myOptions);

        var contentString = '<div><img src="/Styles/StaticImages/logo_small.png" /><br /><br />Jakubská 2/647, 110 00 Praha 1</div>';

        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });

        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: 'loudmark'
        });
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });
        infowindow.open(map, marker);
    }

    $(".EditButton").fancybox({
        'width': 1000,
        'height': '85%',
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe'
    });
});
