String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function showFeeds(){
	if($('#bus_traffic').html() == ''){
		$('#bus_traffic').html('<div style="text-align: center;">' + szNoMessages + '</div>')
	}else{
		$('#bus_traffic h3').click(function() {
			$(this).find('.bus_post_more').toggleClass('bus_hide', !$(this).find('.bus_post_more').hasClass('bus_hide'));
			$(this).next().slideToggle(300);
			if($(this).find('.bus_post_more').html()=="Visa"){
				$(this).find('.bus_post_more').html("D&ouml;lj")
			}else{
				$(this).find('.bus_post_more').html("Visa")
			}
			return false;
		}).next().hide();
	}
	$('#bus_traffic').show();
}
function requestTrafficInfo(site) {  
    // If no url was passed, exit.  
    if ( !site ) {  
        alert('No site was passed.');  
        return false;  
    }
    if(($.browser.msie && parseInt($.browser.version, 10) == 7)){
       $.support.cors = true
    }
    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&diagnostics=true';  
    var szHTML = '';  
    $.ajax({
    type: 'GET',
    url: yql,
    success: function(data) {
       var szDates = '';
       var szTmpDate = '';
       var isOK1 = false;
       var isOK = false;
       var szTmpDesc = '';
       var szTmp = '';
       var szTmpLine = '';
       var szLines = '';
       var szAttachments = '';
       var arrLines = new Array();
       $(data).find('Info').each(function(){
          isOK1 = false;
          isOK = false;
          szLines = '';
          szTmpLine = '';
          arrLines.length = 0;
          $(this).find('TrafTypes').each(function(){
             $(this).find('string').each(function(){
                szTmp = $(this).text();
                szTmp = szTmp.trim();
                if(szTmp == 'Stadstrafik'){isOK1 = true}
             });
          });
          $(this).find('Municipalities').each(function(){
             $(this).find('string').each(function(){
                szTmp = $(this).text();
                szTmp = szTmp.trim();
                if(isOK1 && szTmp == 'Karlstad'){isOK = true}
             });
          });
          if(isOK){
             $(this).find('Aliases string').each(function(){
                szTmpLine = $(this).text();
                szTmpLine = szTmpLine.trim();
                arrLines.push(szTmpLine);
             });
             if(arrLines.length > 0){
                if(arrLines.length == 1){
                   szLines = 'Linje '
                }else{
                   szLines = 'Linjer '
                }
                szLines += arrLines.join(', ')
             }
             szHTML += '<div class="bus_post"><h3 class="bus_post_header">';
             szHTML += '<span class="bus_title">' + $(this).find('Header').text() + '</span>';
             if(szLines != ''){szHTML += '<span class="bus_lines">&nbsp;-&nbsp;' + szLines + '</span>'}
             szTmpDate = $(this).find('FromDate').text();
             szDates = szTmpDate.substring(0, szTmpDate.indexOf('T'));
             szTmpDate = $(this).find('ToDate').text();
             szDates += ' - ' + szTmpDate.substring(0, szTmpDate.indexOf('T'));
             szHTML += '<span class="bus_post_more">Visa</span></h3>';
             szHTML += '<div class="bus_post_info">';
             szHTML += '<span>(' + szDates + ')</span>';
             var szTmpDesc = '';
             $(this).find('Description').each(function(){
                if($(this).parent().get(0).tagName != 'Attachment'){szTmpDesc += $(this).text()}
             });
             szHTML += '<p>' + szTmpDesc + '</p>';
             szAttachments = '';
             $(this).find('Attachments').each(function(){
                $(this).find('Attachment').each(function(){
                   szTmpLine = $(this).find('Url').text();
                   szTmpLine = szTmpLine.trim();
                   if(szAttachments != ''){szAttachments += '<br />'}
                   szAttachments += '<a href="' + szTmpLine + '"';
                   szTmpLine = $(this).find('Description').text();
                   szTmpLine = szTmpLine.trim();
                   szAttachments += ' title="' + szTmpLine + '" target="_blank">' + szTmpLine + '</a>';
                });
             });
             szHTML += '<div>' + szAttachments + '</div>';
             szHTML += '</div>';
             szHTML += '</div>';
          }
       });
       if(szHTML != ''){$('#bus_traffic').html(szHTML)}else{$('#bus_traffic').html('<div style="text-align: center;">' + szNoMessages + '</div>')}
       showFeeds();
	   },
    error: function ajaxError(request, type, errorThrown){
       $('#bus_traffic').html('<p><strong>Din webbläsares säkerhetsinställningar tillåter inte att hämta trafikinfo automatiskt.</strong><br /><a href="http://193.45.213.116/trisvtr/trafficinfo.aspx?OwnerId=2" target="_blank">Du kan istället hitta senaste trafikinfo här</a></p>')
    }
});
}  

