﻿function updateSongster() {
    /* Stop refresh interval */
    clearInterval(refreshSongster);
    
    /* Start scroller if not running */
    if (scrollTicker == false) {
        scrollRight();
    }
    var xmlhttp=false;
    
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	try {
    		xmlhttp = new XMLHttpRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    if (!xmlhttp && window.createRequest) {
    	try {
    		xmlhttp = window.createRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    
    /* Make reguest */
	var rDate = new Date();
	xmlhttp.open("GET", "/cs/inc/songster.xml?"+rDate.getTime(),true);
	/* Parse data if respence recieved */
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
            // Get Songster data
            var sTitle = null;
            var sAuthor = null;
            var sSduration = null;
            var sMduration = null;
            var sCover = null;
            var sAddsduration = null;
            var sAddmduration = null;
            
            var songsterData = xmlhttp.responseText;
            
            /* Parse response data into variables */
            sTitle = parseSongster(songsterData,"title");
            sAuthor = parseSongster(songsterData,"artist");
            sSduration = parseSongster(songsterData,"sduration");
            sMduration = parseSongster(songsterData,"mduration");
            sCover = parseSongster(songsterData,"cover");
            sAddsduration = parseSongster(songsterData,"addsduration");
            sAddmduration = parseSongster(songsterData,"addmduration");

            /* If no Title set - set to default */
            if (sTitle == null || sAuthor == null) {
                $("#author").text("Rádio Bonton");
                $("#track").text(" Pop-rock nezastavíš");
                $("#cover").hide();
                $("#timer").hide();
            } else {
                /* Show title and artist */
                $("#author").text(sAuthor);
                $("#track").text(" " + sTitle);
                
                /* Set and show cover if passed */
                if (sCover != null && sCover.indexOf("NULL") < 0) {
                    $("#cover").html("<img src=\"http://songster.rmc.fg.cz/img/u/" + sCover + ".jpg\" width=\"31\" height=\"31\" alt=\"\" />");
				    $("#cover").fadeIn('normal');
                } else {
                    $("#cover").hide();
                }
                
                /* Set time evaluation */
                if (sAddsduration != null && sAddmduration != null) {
                    if (sAddsduration < 10) {
                        $("#sduration").text("0" + sAddsduration);
                    } else {
                        $("#sduration").text(sAddsduration);
                    }
                    $("#mduration").text(sAddmduration);
                    $("#timer").show();
                    lastsDuration = sAddsduration;
                    lastmDuration = sAddmduration;
                }
            }
            
            /* Update scroll on title change */
            resetWidth = true;
            
            /* Set timers */
            refreshSongster = setInterval("countDown()", 1000);
            setTimeout("updateSongster()",15000);


		}
	}
	
    /* Clear data */
    xmlhttp.send(null);
}

/* Parse songster response */
function parseSongster(sSongster,sTag) {
    var regexp = eval("/<"+sTag+">(.+?)<\\/"+sTag+">/");
    if (sSongster.search(regexp) > -1) {
        return sSongster.match(regexp)[1];
    }
    return null;
}


/* Timer */        
function countDown() {
    lastsDuration++;
    
    if (lastsDuration > 59) {
        lastsDuration = 0;
        lastmDuration = Math.floor(lastmDuration) + 1;
    }
    if (lastsDuration < 10) {
        $('#sduration').text("0"+lastsDuration);
    } else {
        $('#sduration').text(lastsDuration);
    }
    $('#mduration').text(lastmDuration);
}