/* site.js */

function fetchDisturbances() {
        //jQuery.getJSON('http://localhost/td.axd?limit=3', function(data) {
        jQuery.getJSON('http://www.ul.se/td.axd?limit=3', function(data) {
        if (data[2].length > 0) {
            $('#disturbances #trains H2 img').attr('src', '/Templates/Public/Images/TrafficMessages/red.png');
            $('#disturbances #trains ul').empty();
            $('#disttmpl').tmpl(data[2]).appendTo('#disturbances #trains ul');
        } else {
            $('#disturbances #trains H2 img').attr('src', '/Templates/Public/Images/TrafficMessages/green.png');
            $('#disturbances #trains ul').empty().append('<li class="noitems">Inga st&ouml;rningar inrapporterade.</li>')
        }
        if (data[1].length > 0) {
            $('#disturbances #regional H2 img').attr('src', '/Templates/Public/Images/TrafficMessages/red.png');
            $('#disturbances #regional ul').empty();
            $('#disttmpl').tmpl(data[1]).appendTo('#disturbances #regional ul');
        } else {
            $('#disturbances #regional H2 img').attr('src', '/Templates/Public/Images/TrafficMessages/green.png');
            $('#disturbances #regional ul').empty().append('<li class="noitems">Inga st&ouml;rningar inrapporterade.</li>')
        }
        if (data[0].length > 0) {
            $('#disturbances #city H2 img').attr('src', '/Templates/Public/Images/TrafficMessages/red.png');
            $('#disturbances #city ul').empty();
            $('#disttmpl').tmpl(data[0]).appendTo('#disturbances #city ul');
        } else {
            $('#disturbances #city H2 img').attr('src', '/Templates/Public/Images/TrafficMessages/green.png');
            $('#disturbances #city ul').empty().append('<li class="noitems">Inga st&ouml;rningar inrapporterade.</li>')
        }
        if (data[3].length > 0) {
            $('#trafficmessages ul').empty();
            $('#messtmpl').tmpl(data[3]).appendTo('#trafficmessages ul');
        } else {
            $('#trafficmessages ul').empty().append('<li class="noitems">Inga meddelanden inrapporterade.</li>')
        }
    });
}

function popup(element) {
	  message = $(element).html();
	  var width = $(window).width();
		var height = $(window).height();
		var dheight = $(document).height();
		var obj = $('<div class="popup"><img src="/Templates/Public/Images/interactivemap/close.gif" alt="Stäng" class="close"/><div class="padding">' + message + '</div></div>').css('left', ((width / 2) - 254) + 'px').click(function(e) { var x = e.pageX - this.offsetLeft; var y = e.pageY - this.offsetTop; if (x >= 11 && x <= 18 && y >= 10 && y <= 17) { return true; } return false; });
		obj.find('A').click(function() {window.open($(this).attr('href'));});
		var container = $('<div class="popupoverlay"></div>').css('height', dheight + 'px').click(function() { $(this).fadeOut('fast', function() { $(this).remove(); }); }).append(obj)
		$('BODY').append(container);
		$('.popup .close').click(function() { $('.popupoverlay').fadeOut('fast', function() { $(this).remove(); }); })
		$('.popup').css('top', ((height / 2)  + $(window).scrollTop() - $('.popup').height()/2) + 'px');
		//$('.helplist LI').toggle(function() {$(this).siblings().children('DIV').slideUp(100);$(this).children('DIV').slideDown(100);}, function() {$(this).children('DIV').slideUp(100);});
		$('.helplist LI').click(function() {
			if($(this).children('DIV').css('display') == 'none') {
				$(this).siblings().children('DIV').slideUp(100);
				$(this).children('DIV').slideDown(100);
			}
			else {
				$(this).children('DIV').slideUp(100);
			}
        });
}

$(function($) {
    $(window).load(function() {
        var sfEls = document.getElementById("nav").getElementsByTagName("LI");
        for (var i = 0; i < sfEls.length; i++) {
            sfEls[i].onmouseover = function() {
                this.className += " sfhover";
            }
            sfEls[i].onmouseout = function() {
                this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
            }
        }
    });
    $('.jclock').jclock();
    $('.sitemaptree UL LI:last-child').css('background', '#ffffff url(/templates/public/images/sitemap/elementLineEnd.gif) top left no-repeat');

    setInterval(fetchDisturbances, 180000); //5*60*1000

    $('#openpopup').click(function() { popup('#maphelp'); return true; });
    $(window).scroll(function() {
        $('.popup').each(function() { var height = $(window).height(); $(this).css('top', ((height / 2) + $(window).scrollTop() - $(this).height() / 2) + 'px'); });
    });


});

