﻿$(document).ready(function() {
    MPATS.SiteMaster.setDefaults();

    // show a simple loading indicator
    try {
        var loader = $('<div id="loader"><img src="\/assets\/images\/loading.gif" alt="loading..." /><\/div>')
			.css({ position: "absolute", top: "1em", left: "25em" })
			.prependTo("body")
			.hide();
        $().ajaxStart(function() {
            loader.show();
        }).ajaxStop(function() {
            loader.hide();
        }).ajaxError(function(a, b, e) {
            if (b && b.status && b.status != 0) { alert("There was a problem processing the AJAX request.\r\n\r\n" + b.status + ': ' + b.statusText + '\r\n' );
            throw e; }
        });
    } catch (ex) { }
    try {
    if ($('ul#listticker').length) {
      $('#box-news').hide();
      $.ajax({ url: "/embed/news/rss.json", success: function (result) {
      var list = $('ul#listticker');
      $.each(result.item, function () {
        list.append('<li><a href="' + this.link + '" class="news-title">' + this.title + '</a><span class="news-date">' + this.pubDate + '</span><span class="news-text">' + this.description.replace(/<img[^>]+\>/gi, '').replace(/<img[^>]+\>/gi, '') + '</span></li> ');
      })
      $('ul#listticker img').remove();
      $('#box-news').show();
      }, type: "GET", cache: true, dataType: "json" });
      
            var first = 0;
            var speed = 700;
            var pause = 3500;

            function removeFirst() {
            var firstitem = $('ul#listticker li:first');
            if (!firstitem.length) return;
                first = firstitem.html();
                $('ul#listticker li:first')
			.animate({ opacity: 0 }, speed)
			.slideUp('slow', function() { $(this).remove(); });
                addLast(first);
            }

            function addLast(first) {
                last = '<li style="display:none">' + first + '</li>';
                $('ul#listticker').append(last)
                $('ul#listticker li:last')
			.animate({ opacity: 1 }, speed)
			.fadeIn('slow')
            }

            interval = setInterval(removeFirst, pause);
        }
        } catch(ex) { }
});

var MPATS = {
    SiteMaster: {
        setDefaults: function() {
            if ($.jgrid) {
                if ($.jgrid.defaults) {
                    $.jgrid.defaults = $.extend($.jgrid.defaults, {
                        datatype: 'json',
                        height: 'auto',
                        imgpath: '/Scripts/jqGrid/themes/basic/images',
                        jsonReader: {
                            repeatitems: false
                        },
                        loadui: "block",
                        mtype: 'GET',
                        multiboxonly: true,
                        rowNum: 10,
                        rowList: [10, 20, 50, 100],
                        viewrecords: true
                    });
                }
            }
        }
    }
};
