﻿$(document).ready(function() {
    $("#newsNav a").click(function(e) {
        e.preventDefault();
        var el = $(this);
        var _el = $("marquee a[item='" + el.attr("item") + "']");
        loadNews(_el);
    });
    $("marquee a").click(function(e) {
        marqueeStop($("marquee")[0], true);
        var el = $(this);

        e.preventDefault();
        $("#newsPopup").modal({
            closeHTML: ("<a class='modalCloseImg' title='Close'></a>"),
            overlayId: 'forgetpass-overlay',
            onOpen: function(dialog) {
                dialog.overlay.show();
                dialog.container.show();
                dialog.data.show();
                loadNews(el);
                /*
                dialog.overlay.fadeIn('fast', function() {
                dialog.container.show();
                dialog.data.slideDown('normal');
                loadNews(el);
                });*/
            },
            onClose: function(dialog) {
                marqueeStart($("marquee")[0], true);
                $.modal.close();
            }
        });
    });
});

function loadNews(el) {
    var id = el.attr("item");
    $("#newsPopup div div:first").load("/Home/ShowNewsItem?id=" + id);
    /*
    var nxt = hasNext(el);
    var prv = hasPrev(el);
    if (nxt != null) {
    $("a.nxt")
    .attr("href", nxt.attr("href"))
    .attr("item", nxt.attr("item"))
    .parent().css("visibility", "visible");
    }
    else {
    $("a.nxt").parent().css("visibility", "hidden");
    }
    if (prv != null) {
    $("a.prv")
    .attr("href", prv.attr("href"))
    .attr("item", prv.attr("item"))
    .parent().css("visibility", "visible");
    }
    else {
    $("a.prv").parent().css("visibility", "hidden");
    }*/
}

function hasNext(el) {
    var $next = el.parent().parent().next().next();
    if ($next.length > 0 && $next.is("div")) {
        return $("a", $next);
    }
    else {
        return null;
    }
}

function hasPrev(el) {
    var $prev = el.parent().parent().prev().prev();
    if ($prev.length > 0 && $prev.is("div")) {
        return $("a", $prev);
    }
    else {
        return null;
    }
}

var _stopedOnClick = false;

function marqueeStart(mrq, isByDemand) {
    if (isByDemand) {
        _stopedOnClick = false;
        mrq.start();
    }
    else {
        if (!_stopedOnClick) {
            mrq.start();
        }
    }
}

function marqueeStop(mrq, isByDemand) {
    _stopedOnClick = isByDemand;
    mrq.stop();
}

