
var content = new Array();
var links = new Array();
var taperChangeInt = 2000;
var taperRoolInt = 60;
var idx = 0;
var currChar = 0;
var rollpause = false;
var pause = false;
var clientid = "";
var checkingNewsPeriod = 1000;

function PrintValues(){
    setTimeout('PrintVal()', checkingNewsPeriod);
}

function PrintVal(){
	$.ajax({
	  type: "POST",
	  url: "/HomePage/Views/CallBackView.ashx",	  
	  data : "Command=news",
	  dataType: "text",
	  success: function(result) {
		ChangeContent(result);
	  }
	});
}

function ChangeContent(newsArray){
    if(newsArray != ""){
		newsArray = $('<div>' + newsArray + '</div>').text();
        var val1 = newsArray.split('||')[0];
        var val2 = newsArray.split('||')[1];
        content = val1.split('|');
        links = val2.split('|');
        if(content.length > 0){
            TT = document.getElementById('TickTaper'); 
            chars = content[idx].split('');
            TT.href = links[idx];
            RI = window.setInterval(PrintTape, taperRoolInt);
        }
    }else{
        PrintValues();
    }
}

function PrintTape(){
    if(currChar < chars.length && !rollpause){
        val = TT.innerHTML;
        TT.innerHTML = val + chars[currChar];
        currChar++;
    }else{
        currChar = 0;
        rollpause = true;
        clearInterval(RI);
        setTimeout(NextTaper, taperChangeInt);
    }
}

function PauseTaper(){
    if(content.length > 0){
		if(pause){
			pause = false;
			NextTaper();
		}else{
			pause = true;
			document.getElementById('TaperPauseBtn').src = '/CC/Images/tt_pause_ovr.gif';
			clearInterval(RI);
			TT.innerHTML = content[idx];   
			TT.href = links[idx];  
		}
    }
}

function NextTaper(){
    if(content.length > 0){
    document.getElementById('TaperPauseBtn').src = '/CC/Images/tt_pause.gif';
    clearInterval(RI);
    idx++;
    if(idx < 0 || idx >= content.length)
        idx = 0;
		TT.href = links[idx];
		TT.innerHTML = "";
		chars = content[idx].split('');
		rollpause = false;
		currChar = 0;
		RI = window.setInterval(PrintTape, taperRoolInt);
		pause = false;
    }
}

function PrevTaper(){
    if(content.length > 0){
		document.getElementById('TaperPauseBtn').src = '/CC/Images/tt_pause.gif';
		clearInterval(RI);
		idx--;
		if(idx < 0 || idx >= content.length){
			idx = 0;
		}
		TT.href = links[idx];
		TT.innerHTML = "";
		chars = content[idx].split('');
		rollpause = false;
		currChar = 0;
		RI = window.setInterval(PrintTape, taperRoolInt);
		pause = false;
    }
}
