//Copyright 2010. All rights reserved. Price Box (r)
var tt;
var tn;
var ts;
var protocol = "http://";
var www = "";
var vcn;
var vct;

function init(){
	var loc = document.location.href;
	if(loc.indexOf("https://") > -1){
		protocol = "https://";
	}
	if(loc.indexOf("//www.") > - 1){
		www = "www.";
	}
	
	GetNews();
	GetTweets();
	ts = setInterval("UpdateSeconds()", 1000);
	var headerDiv = document.getElementById("header");
	headerDiv.innerHTML = "<span onclick=\"pause()\">▮▮</a>";
}


function pause(){
	clearTimeout(tt);
	clearTimeout(tn);
	clearInterval(ts);
	var headerDiv = document.getElementById("header");
	headerDiv.innerHTML = "<span onclick=\"init()\">▶</a>";
}

function GetNews(){
	runAjax(protocol + www + "mcruss.com/pricebox/news/news_v2.asp?q=art&max=1&r=" + (new Date()).getTime());
	vcn = new VisualCounter(60000, 10, "news-counter", false);
	tn = setTimeout("GetNews()",60000); //every 60 seconds
}

function GetTweets(){
	runAjax(protocol + www + "mcruss.com/pricebox/twitter/TSearch_v2.asp?q=art&max=1&user=1&r=" + (new Date()).getTime());
	vct = new VisualCounter(15000, 10, "tweets-counter", false);
	tt = setTimeout("GetTweets()",15000); //every 15 seconds
}

function createXHR(){
	var objXHR = null;
	if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
  		objXHR = new XMLHttpRequest();
  	}
	else{// code for IE6, IE5
  		objXHR = new ActiveXObject("Microsoft.XMLHTTP");
  	}
	return objXHR;
}

function runAjax(url){
	var xhr = createXHR();  

      // Inner function to bind handler and create closure
      function bindCallback(){
          if(xhr.readyState == 4){
              try{
                  if(xhr.status == 200)
					  eval(xhr.responseText); //calls callback function if correct response
                 else{
                     //alert("Status error");
				 }
             }catch(e){
                 //alert(e.message);
             }
         }
     }
                
     if(xhr){
         try{
             xhr.open("GET", url, true);
             xhr.onreadystatechange = bindCallback;
             xhr.send(null);
         }catch(e){
             //Handle error
         }
     }
}


//Callback from news_v2.php
function PriceBox_News(news){
	pubAgo = timeAgo("gtime", "Google News (", " ago)", news.aryNews[0].pubDateU * 1000);
	
	var contentDiv = document.getElementById("news");
	
	while(contentDiv.hasChildNodes()){
        contentDiv.removeChild(contentDiv.firstChild);
    }
	
	var newsLink = "<a href=\"" + news.aryNews[0].link + "\">" + news.aryNews[0].title + "</a>";
	
	var newsSpan = document.createElement("span");
	var newsSpanWhen = document.createElement("span");
	contentDiv.appendChild(newsSpan);
	contentDiv.appendChild(document.createElement("br"));
	newsSpan.setAttribute("class", "quote");
	newsSpan.innerHTML = newsLink;
	contentDiv.appendChild(newsSpanWhen);
	newsSpanWhen.setAttribute("class", "quoted");
	newsSpanWhen.innerHTML = pubAgo;
	contentDiv.appendChild(document.createElement("br"));
	contentDiv.appendChild(document.createElement("br"));
	
	vcn.vcShow = true;
}

//Callback from TSearch.php
function PriceBox_TSearch(tweets){
	pubAgo = timeAgo("ttime", "Twitter (", " ago)", Date.parse(tweets.results[0].created_at));
	
	var contentDiv = document.getElementById("tweets");
	
	while(contentDiv.hasChildNodes()){
        contentDiv.removeChild(contentDiv.firstChild);
    }
	
	var strTweet = "<a href=\"http://twitter.com/" + tweets.results[0].from_user + "/status/" + tweets.results[0].id + "\">" + tweets.results[0].text + "</a>";
	
	var tweetSpan = document.createElement("span");
	var tweetSpanWhen = document.createElement("span");
	contentDiv.appendChild(tweetSpan);
	contentDiv.appendChild(document.createElement("br"));
	tweetSpan.setAttribute("class", "quote");
	tweetSpan.innerHTML = strTweet;
	contentDiv.appendChild(tweetSpanWhen);
	tweetSpanWhen.setAttribute("class", "quoted");
	tweetSpanWhen.innerHTML = pubAgo;
	
	var contentDiv = document.getElementById("tweets-counter");
	contentDiv.style.cssText = "display:block;";
}

function timeAgo(id, start, end, pubU){
	var d = new Date();
    var ue = d.getTime(); //milliseconds since Unix Epoch
	ue = parseInt(ue / 1000); //seconds since UE
	var published = pubU; //milliseconds since the Unix Epoch
	published = parseInt(published / 1000);
	dS = ue - published;
	var days = parseInt(dS / 86400);
	dS = dS - (days * 86400);
	var hours = parseInt(dS / 3600);
	dS = dS - (hours * 3600);
	var minutes = parseInt(dS / 60);
	dS = dS - (minutes * 60);
	var strDay = " days ";
	var strHour = " hours ";
	var strMinute = " minutes ";
	var strSecond = " seconds ";
	var pubAgo = "";
	var strStart = start; //start of published time ago sentence
	var strEnd = end; //end of published time ago sentence
	if(days == 1) strDay = " day ";
	if(hours == 1) strHour = " hour ";
	if(minutes == 1) strMinute = " minute ";
	if(dS == 1) strSecond = " second ";
	if(days > 2) pubAgo = strStart + days + strDay + strEnd;
	else if(days > 0) pubAgo = strStart + days + strDay + hours + strHour + strEnd;
	else if(hours > 0) pubAgo = strStart + hours + strHour + minutes + strMinute + strEnd;
	else if(minutes > 0) pubAgo = strStart + minutes + strMinute + "<span id=\"" + id + "\">" + dS + "</span>" + strSecond + strEnd;
	else pubAgo = strStart + "<span id=\"" + id + "\">" + dS + "</span>" + strSecond + strEnd;
	
	return pubAgo;
}

function UpdateSeconds(){
	var secondsSpan = document.getElementById("ttime");
	if(secondsSpan){
		var curSeconds = parseInt(secondsSpan.innerHTML);
		newSeconds = curSeconds + 1;
		secondsSpan.innerHTML = newSeconds;
	}
}

//time and interval in ms
var intervalC;
var vcShow;
function VisualCounter(time, segments, id, show){
	this.vcShow = show;
	var interval = parseInt(time / segments);
	
	var contentDiv = document.getElementById(id);
	if(this.vcShow == false){
		contentDiv.style.cssText = "display:none;"
	}
	
	while(contentDiv.hasChildNodes()){
        contentDiv.removeChild(contentDiv.firstChild);
    }
	
	for(i = 0; i < segments; i++){
		contentDiv.appendChild(document.createTextNode("￭"));
	}
	
	var iCounter = segments;
	this.intervalC = setInterval(
		function(){
			if(contentDiv.hasChildNodes() && segments > 0){
        		contentDiv.removeChild(contentDiv.firstChild);
    		}
			else{
				clearInterval(this.intervalC);
			}
			segments--;
		}, interval);
}
