// JavaScript Document
function twitter_callback(twit) {

 //this is the div I'm writing the content to
// var t_div = document.getElementById("sub_twitter");

//these are some other variables, mostly
 //placeholders so that the code is a little clearer
 
 	var who,what,when,icon, bgcolor

//start the ul
 //t_div.innerHTML = "<ul>"

   //loop through the twit object
   for (i=0;i<5;i++) {

   //Look at me use the JavaScript modulus operator to do even/odd rows.
	   if(i % 2) {
		  bgcolor="#efefef"
	   } else {
		  bgcolor="#ddd"
	   }

   //Right here, I've broken out the separate bits of the string into placeholder variables
   //so you can more easily see the dot notation and array indices in place
   //once you figure out the structure it's dead easy to reference data in an Object
   	icon=twit[i].user.profile_image_url;
   	who=twit[i].user.name;
   	what=twit[i].text;
   	when=twit[i].created_at.substr(0,19);
	
	//alert(what);
	new_what = autoLink(what);
	if(document.getElementById('sub_twitter'+(i+1))){
            document.getElementById('sub_twitter'+(i+1)).innerHTML = new_what+"&nbsp; <b>"+relative_time(twit[i].created_at)+"</b>";
        }
   //and here I mash it all up into a fancy li
   //t_div.innerHTML +="<li style='background:"+bgcolor+" url("+icon+") no-repeat'><strong>"+who+"</strong>: "+what+" ("+when+" GMT) </li>"
   }
  //and close the UL
  //t_div.innerHTML += "</ul>"
}

function relative_time(time_value) {
	var values = time_value.split(" ");
	time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	var parsed_date = Date.parse(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	delta = delta + (relative_to.getTimezoneOffset() * 60);
	
	if (delta < 60) { return 'less than a minute ago'; }
	else if(delta < 120) { return 'about a minute ago'; }
	else if(delta < (60*60)) { return (parseInt(delta / 60)).toString() + ' minutes ago'; }
	else if(delta < (120*60)) { return 'about an hour ago'; }
	else if(delta < (24*60*60)) { return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago'; }
	else if(delta < (48*60*60)) { return '1 day ago'; }
	else { return (parseInt(delta / 86400)).toString() + ' days ago'; }
}

function autoLink(message) {
	var re = /(http|https|ftp|www)([^ ]+)/gi;
	var url = message.match(re);
	var disp_location="";
	
	if(url){
		for (var i=0;i<url.length;i++){
			if (url[i].indexOf("http:") >= 0){ //removing the http: and the www.klikk.com.mt
				if(url[i].indexOf("klikk")>=0){
					if (url[i].indexOf("www")>=0){
						new_url = url[i].replace(/http:\/\/www.klikk.com.mt\//gi,"");
					}else{
						new_url = url[i].replace(/http:\/\/klikk.com.mt\//gi,"");
					}
				}else{
					new_url=url[i];
				}
				
			}else{
				if(url[i].indexOf("klikk")>=0){
					if (url[i].indexOf("www")>=0){
						new_url = url[i].replace(/www.klikk.com.mt\//gi,"");
					}else{
						new_url = url[i].replace(/klikk.com.mt\//gi,"");
					}
				}else{
					new_url=url[i];
				}
			}
	
			if (new_url.indexOf("#") >= 0){ // now filtering products
				var location= new_url.split(":");
				
				for (j=0;j<location.length;j++){
					var loc=location[j].split("=");	
					if(loc[0].indexOf("scatid")>=0){
						disp_location="scatid="+loc[1];
						url_scatid = loc[1];
					}else if (loc[0].indexOf("catid")>=0){
						disp_location=disp_location+":catid="+loc[1];
						url_catid=loc[1];
					}else if (loc[0].indexOf("prodid")>=0){
						disp_location=disp_location+":prodid="+loc[1];
						url_prodid = loc[1];
					}	
				}
				disp_location='displayLocation("'+disp_location+'");return false;';
			}
			
			if (disp_location==""){
				if (new_url=="smashers.php"){
					new_message = message.replace(url[i]," <a href='"+new_url+"' onclick='smashers();return false;'>"+url[i]+"</a>");
				}else{
					new_message = message.replace(url[i]," <a href='"+new_url+"'>"+url[i]+"</a>");
				}
			}else{
				new_message = message.replace(url[i]," <a href='#' onclick='"+disp_location+"'>"+url[i]+"</a>");
			}
		}	
	}else{
		return message;
	}
	return new_message;
} 



