var engine = "engine.php";
var upd_freq = 15000;

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function reqUpd(type, params, divid, profid) {
  var tobj = new Date();
  params = params + "&tobj=" + tobj.getTime();  

  var tr_resp = "";

  if (type == "preview") {
    fmethod = 'post';
  } else {
    fmethod = 'get';
  }

  new Ajax.Request(engine, {
               method:     fmethod,
               parameters: params,
               onSuccess:  function(transport) {
                 switch (type) {
                   case "overheard":
                     Element.replace($("overheard"), "<div id=\"overheard\" class=\"overheard\">" + transport.responseText + "</div>");
                   break;
                   case "self_points":
                     Element.replace($("mainpoint"), "<div id=\"mainpoint\" class=\"points\">" + transport.responseText + "</div>");
                   break;
                   case "prof_pts":
                     Element.replace($("prof_pts"), "<span id=\"prof_pts\">" + transport.responseText + "</span>");
                   break;
                   case "thumbup":
                     reqUpd("self_points", "canhas=points"                                     , "mainpoint", "");
                     reqUpd("prof_pts"   , "canhas=points&profile_id=" + $("prof_id").innerHTML, "prof_pts" , "");
                   break;
                   case "thumbdown":
                     reqUpd("self_points", "canhas=points"             , "mainpoint", "");
                     reqUpd("prof_pts"   , "canhas=points&profile_id=" + $("prof_id").innerHTML, "prof_pts" , "");
                   break;
                   case "prof_pics":
                     Element.replace($("prof_pics"), "<div id=\"prof_pics\" class=\"prof_pics\">" + transport.responseText + "</div>");
                   break;
                   case "preview":
                     Element.replace($("preview"), "<div id=\"preview\" class=\"box\">You said, <br />" + transport.responseText +  "</div>");
                   break;
                   case "curtime":
                     tr_resp = transport.responseText;
                     return String(tr_resp);
                   break;
                   case "markasread":
                     Element.replace($(divid), "<div id=\"" + divid + "\">" + transport.responseText +  "</div>");
                   break;
                   default:
                   break;
                 }
               }
                           });
}

// tsShow() - Convert UNIX timestamps to "x time units ago" string
function tsShow() {
  if ($("postcount") == null) {
    return true;
  }

  // HTML variable "posts" for post count
  var curtime = Number(servertimeOBJ.getTime() / 1000);
  var count   = Number($("postcount").innerHTML);

  // Start iterating through each var and post div
  for(var i = 1; i <= count; i++) {
    var div    = $("timestamp_"     + i);
    var divvar = $("timestamp_var_" + i);

    var seconds = Number(divvar.innerHTML);
    var minutes = 0, hours = 0, days = 0, output  = "", suffix = "";

    // Math to get amount of each time unit
    seconds = curtime - seconds;
    if (seconds < 0) {
      seconds = 1;
    }
    minutes = seconds / 60;
    hours   = minutes / 60;
    days    = hours   / 24;

    // outer conditional  - decide which time unit to use
    // inner conditionals - decide singular or plural days
    if (days >= 1) {
      if (Math.round(days) >= 2)    { suffix = " days "   ; } else { suffix = " day "   ; }
      output = Math.round(days)    + suffix;
    } else if (hours >= 1) {
      if (Math.round(hours) >= 2)   { suffix = " hours "  ; } else { suffix = " hour "  ; }
      output = Math.round(hours)   + suffix;
    } else if (minutes >= 1) {
      if (Math.round(minutes) >= 2) { suffix = " minutes "; } else { suffix = " minute "; }
      output = Math.round(minutes) + suffix;
    } else {
      if (Math.round(seconds) >= 2) { suffix = " seconds "; } else { suffix = " second "; }
      output = Math.round(seconds) + suffix;
    }

    div.innerHTML = "(" + output + "ago)";
  }
}

function giveThumb(type, params, t_id, t_type) {
  if (params != "") {
    var newtup;
    var newtdn;

    var tup_div = "thumbup_" + t_id;
    var tdn_div = "thumbdown_" + t_id;
    var tup_div_img = "thumbup_img_" + t_id;
    var tdn_div_img = "thumbdown_img_" + t_id;

    var s_up_1_src = "<img id=\"" + tup_div_img + "\" class=\"thumbup\" src=\"./glyphs/thumb_up.gif\">";
    var d_up_1_src = "<img id=\"" + tup_div_img + "\" class=\"thumbup\" src=\"./glyphs/graythumb_up.gif\">";
    var s_dn_1_src = "<img id=\"" + tdn_div_img + "\" class=\"thumbdown\" src=\"./glyphs/thumb_down.gif\">";
    var d_dn_1_src = "<img id=\"" + tdn_div_img + "\" class=\"thumbdown\" src=\"./glyphs/graythumb_down.gif\">";

    var s_up_2_src = "<img id=\"" + tup_div_img + "\" class=\"thumbup\" src=\"./glyphs/thumb_up_l.gif\">";
    var d_up_2_src = "<img id=\"" + tup_div_img + "\" class=\"thumbup\" src=\"./glyphs/graythumb_up_l.gif\">";
    var s_dn_2_src = "<img id=\"" + tdn_div_img + "\" class=\"thumbdown\" src=\"./glyphs/thumb_down_l.gif\">";
    var d_dn_2_src = "<img id=\"" + tdn_div_img + "\" class=\"thumbdown\" src=\"./glyphs/graythumb_down_l.gif\">";

    var s_up_src, d_up_src, s_dn_src, d_dn_src;

    if (t_type == 1) {
      s_up_src = s_up_1_src;
      d_up_src = d_up_1_src;
      s_dn_src = s_dn_1_src;
      d_dn_src = d_dn_1_src;
    } else if (t_type = 2) {
      s_up_src = s_up_2_src;
      d_up_src = d_up_2_src;
      s_dn_src = s_dn_2_src;
      d_dn_src = d_dn_2_src;
    } else {
      return;
    }

    if (type == "thumbup") {
      // Blank out thumbs up first
      newtup = "<a id=\"" + tup_div + "\" class=\"thumbup\" onClick=\"giveThumb('', '', '', '" + t_type + "');\">" + s_up_src + "</a>";

      // Make thumb down work again
      newtdn = "<a id=\"" + tdn_div + "\" class=\"thumbdown\" onClick=\"giveThumb('thumbdown', " + "'" + params.replace(/up/, "down") + "'" + 
", " + "'" + t_id  + "', '" + t_type + "'" + ");\">" + d_dn_src + "</a>";
    } else if (type == "thumbdown") {
      // Blank out thumbs down first
      newtdn = "<a id=\"" + tdn_div + "\" class=\"thumbdown\" onClick=\"giveThumb('', '', '', '" + t_type + "');\">" + s_dn_src + "</a>";

      // Make thumb up work again
      newtup = "<a id=\"" + tup_div + "\" class=\"thumbup\" onClick=\"giveThumb('thumbup', " + "'" + params.replace(/down/, "up") + "'" +
", " + "'" + t_id  + "', '" + t_type + "'" + ");\">" + d_up_src + "</a>";
    }

    // Do the thumb and change the HTML
    reqUpd(type, params, t_id, t_id);
    Element.replace($(tup_div), newtup);
    Element.replace($(tdn_div), newtdn);
  }

  return false;
}

function replyTo(post_id, handle) {
  if (BrowserDetect.browser == "Explorer") {
    replyIE(post_id, handle);
  } else {
    replyNonIE(post_id, handle)
  }
}

function replyNonIE(post_id, handle) {
  var txt = "\n\n<a href=\"#" + post_id + "\">\@" + handle + "</a>:";
  var txtarea = document.getElementById('say_it_box');
  var oldtxt = txtarea.value;
  var newtxt = trimStr(oldtxt + txt) + "  ";

  txtarea.value = newtxt;
}

function replyIE(post_id, handle) {
  var txt = "\n\n<a href=\"#" + post_id + "\">\@" + handle + "</a>:  ";
    try {
      document.say_it_form.say_it_box.value = trimStr(document.say_it_form.say_it_box.value + txt) + "  ";
    } catch(err) {
    }
    try {
      document.Form.say_it_form.say_it_box.value = trimStr(document.Form.say_it_form.say_it_box.value + txt) + "  ";
    } catch(err) {
    }
}

function page_load() {
  tsShow();
  reqUpd("overheard"  , "canhas=overheard"   , "overheard", "");
  reqUpd("self_points", "canhas=points"      , "mainpoint", "");
  setTimeout(page_load, upd_freq);
}

function photoRotate(profile_id, big_picture) {
  var params = "canhas=photo_rotate&profile_id=" + profile_id + "&big_picture=" + big_picture;
  reqUpd("prof_pics", params, "prof_pics", "");
}

function markAsRead(params, div_id) {
  reqUpd("markasread", params, div_id, "");
}

function trimStr(str){
  str = str.replace(/^\s*|\s*$/g,'');
  return str;
}

function previewPost() {
  reqUpd("preview", "canhas=preview_message&stuff=" + escape(document.say_it_form.message.value), "preview", "");
}
