/*
Type 1.1.1
IPB style My Assistant for IPB free hosted boards
Made by gunblaza
Scribes Coding Community 
*/

//Drag assistant
var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

//Show/close assistant
function assist(id){
if (document.getElementById){
ID = document.getElementById(id);
if (ID.style.display == "none"){
ID.style.display = "";
} else {
ID.style.display = "none";
}} 
//Get width of PC
var winInWt=window.innerWidth? window.innerWidth : Math.max(document.body.clientWidth, (document.documentElement? document.documentElement.clientWidth : 0))
var free_width = window.opera? winInWt/ 2 - document.getElementById(id).offsetWidth : winInWt/ 2 - (document.all? document.all[id] : document.getElementById(id)).offsetWidth/2;
with ((document.all? document.all[id] : document.getElementById(id)).style) {
zIndex=100;
position='absolute';
left=free_width + 'px';
}}

//Tab system
function myatab(num) {
document.getElementById('matab1').className = "mya-closedtab";
document.getElementById('matab2').className = "mya-closedtab";
document.getElementById('matab3').className = "mya-closedtab";
document.getElementById('matab4').className = "mya-closedtab";
document.getElementById('matab'+num).className = "mya-opentab";

var obj; //Div placement
obj=document.getElementById("macon1");
obj.style.display="none";
obj=document.getElementById("macon2");
obj.style.display="none";
obj=document.getElementById("macon3");
obj.style.display="none";
obj=document.getElementById("macon4");
obj.style.display="none";

if(num==1){
obj=document.getElementById("macon1");
}
if(num==2){
obj=document.getElementById("macon2");
}
if(num==3){
obj=document.getElementById("macon3");
}
if(num==4){
obj=document.getElementById("macon4");
}
obj.style.display="block";
}

//Time
var Days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

var mnames = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");


var today = new Date();
var Year = takeYear(today);
//var Month = leadingZero(today.getMonth()+1);
Month=today.getMonth()+1; // Get the month

// Make month number correspond to month name
if (Month==1) monthName=("January");
else if (Month==2) monthName=("February");
else if (Month==3) monthName=("March");
else if (Month==4) monthName=("April");
else if (Month==5) monthName=("May");
else if (Month==6) monthName=("June");
else if (Month==7) monthName=("July");
else if (Month==8) monthName=("August");
else if (Month==9) monthName=("September");
else if (Month==10) monthName=("October");
else if (Month==11) monthName=("November");
else monthName=("December");

var DayName = Days[today.getDay()];
var Day = leadingZero(today.getDate());

// Add suffix to date (1st, 2nd, 4th, etc.)
if (Day==1) suffix=("st");
else if (Day==2) suffix=("nd");
else if (Day==3) suffix=("rd");
else if (Day==21) suffix=("st");
else if (Day==22) suffix=("nd");
else if (Day==23) suffix=("rd");
else if (Day==31) suffix=("st");
else suffix=("th");

var Hours = today.getHours();
var ampm = "AM";
if (Hours == 0) Hours = 12;
if (Hours > 11)
	ampm = "PM";
if (Hours > 12)
	Hours -= 12;
Hours = leadingZero(Hours);

var Minutes = leadingZero(today.getMinutes());
var Seconds = leadingZero(today.getSeconds());

function takeYear(theDate)
{
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

function leadingZero(nr)
{
	if (nr < 10) nr = "0" + nr;
	return nr;
}


myatime = monthName + " "+Day + suffix +" " + Year + "  -  " + Hours + ":" + Minutes + " " + ampm + " ";

/* //Offline stuff
offmess ='Code is being updated to work better with positioning. It maybe out of place but that is what is being fixed at this time. Also we are doing under the hood fixes. Code will be back online in a few hours!<br><br>Gunblaza,<br>Scribe Coding.';
offarea = "<div class='mya-contentbox-back'><div class='mya-contentbox'>"+offmess+"</div></div>";
// */

//Main style of My Assistant
myatemp = "<div id='myabox' style='display:none;width:400px;text-align:left;top:100px;'>";
//Header
myatemp += "<div class='tableborder'>"
myatemp += "<div class='maintitle' title='Click and hold to drag this window' id='mya-drag' style='cursor:move;'>";
myatemp += "<div style='float:right;padding-right:3px;'><a href='javascript:assist(\"myabox\")' title='Close Window'>[X]</a></div>";
myatemp += "My Assistant"
myatemp += "</div>";
myatemp += "<div class='mya-back'>";


//Code Offline message
//myatemp += offarea;

// /*

//Tabs
myatemp += "<div class='mya-tabwrap' style='margin-left:1px'>";
myatemp += "<div  id='matab1' class='mya-opentab' OnClick='myatab(1);'>Information</div>";
myatemp += "<div id='matab2' class='mya-closedtab' OnClick='myatab(2);'>New Post</div>";
myatemp += "<div id='matab3' class='mya-closedtab' OnClick='myatab(3);'>Search</div>";
myatemp += "<div id='matab4' class='mya-closedtab' OnClick='myatab(4);'>Recent PMs</div>";
myatemp += "</div>";

//New style infopage 

mya_user = document.getElementById("userlinks").getElementsByTagName("A")[0].innerHTML;
myagtd = document.getElementsByTagName("td");
myagul = document.getElementById("userlinks");
myaga = myagul.getElementsByTagName('a')[0];

if(myaga.href.match(/showuser=(\d+)/i)){
var myauserid = RegExp.$1;
}


myatemp += "<div id='macon1'><div class='mya-contentbox-back'><div class='mya-contentbox'>";
myatemp += "<span style='float:right'><b>Welcome, "+mya_user+"</b> ";
myatemp += "<span style='font-size:8px;'>(<a href='index.php?act=Login&CODE=03' title='Log Out' style='text-decoration:none;'>X</a>)</span> </span><a href='index.php?act=UserCP&CODE=00' style='font-size:9px;'>Edit Profile</a> | <a href='index.php?showuser="+myauserid+"' style='font-size:9px;'>View Profile</a><br><br>";
myatemp += "This is your forum Assistant, this little widget will help you keep up to date on things going on in the forum.<br>";
myatemp += "<table style='width:100%' cellspacing='0'><tr>";
myatemp += "<td style='width:50%;padding:6px;' class='mya-info' valign='top'><strong>Forum Information</strong><br>";
myatemp += "&nbsp;· <a href='/index.php?act=Stats&amp;CODE=leaders'>The moderating team</a>"
myatemp += "</td>";
myatemp += "<td style='width:50%;padding:6px;' valign='top'><strong>Posting Information</strong><br />";
myatemp += "&nbsp;· <a href=\"index.php?act=Stats\">Today's top 10 posters</a>";
myatemp += "<br />&nbsp;· <a href=\"index.php?act=Members&amp;max_results=10&amp;sort_key=posts&amp;sort_order=desc\">Overall top 10 posters</a>";
myatemp += "<br />&nbsp;· <a href=\"index.php?act=Search&CODE=getactive\">Today's active topics</a></div>";
myatemp += "</td></tr></table>";
myatemp += "<span style='float:right;font-size:9px;'>"+myatime+"</span><a href='/m/index.php/' style='text-decoration:none;font-size:9px;'>LoFi Version</a><br>";
myatemp += "</div></div></div>";

//Active topics
myatemp += "<div id='macon2' style='display:none;'><div class='mya-contentbox-back'><div class='mya-contentbox' style='padding:0px;'>";
myatemp += '<span id="postshere"></span>';
myatemp += "</div></div></div>";

//Search
myatemp += "<div id='macon3' style='display:none;'><div class='mya-contentbox-back'><div class='mya-contentbox'>";
myatemp += "<strong>Search post for...</strong> <form action='index.php?act=Search&amp;CODE=01&amp;forums=all&amp;cat_forum=forum&amp;joinname=1&amp;search_in=posts&amp;result_type=topics' method='post' name='theForm'>";
myatemp += "<input type='text' size='17' name='keywords' class='forminput' />&nbsp;";
myatemp += "<input type='submit' value='Go' class='forminput'>";
myatemp += "</form>";
myatemp += "</div></div></div>";

//Recent PMs
myatemp += "<div id='macon4' style='display:none;'><div class='mya-contentbox-back'><div class='mya-contentbox' style='padding:0px;'>";
myatemp += "<div class='mya-contentbox-back' style='margin:1px;'><div class='mya-contentbox' style='padding:0px;'>";
myatemp += '<span id="pmshere"></span>';
myatemp += '</div></div><div class="pformstrip" style="text-align:center;"><strong><a href="index.php?act=Msg&CODE=01">Go to Inbox</a> <small>>></small></div>';
myatemp += "</div></div></div>";
// */

//Footer
myatemp += "</div></div></div></div>";

//My Assistant link
myalink = "<a href='javascript:assist(\"myabox\");'>My Assistant</a>";
var gettd = document.getElementsByTagName("td");
for(i=0; i<gettd.length; i++){
if(gettd[i].innerHTML.match(/my assistant/i)){
gettd[i].innerHTML=gettd[i].innerHTML.replace("My Assistant",myalink);
break;
}}

//Place My Assistant  on board
document.write(myatemp);




//based off Latest topics by james of Ifusion.

var reqTopics;
var openField = "";
function getActiveTopics(url){
if (window.XMLHttpRequest || window.ActiveXObject)  {
 reqTopics= (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
 reqTopics.onreadystatechange=test_xml_complete;
 reqTopics.open("GET",url,true);
 reqTopics.send(null);
}
}
var reqpms;
var opField = "";
function getPMS(url){
if (window.XMLHttpRequest || window.ActiveXObject)  {
 reqpms= (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
 reqpms.onreadystatechange=test2_xml_complete;
 reqpms.open("GET",url,true);
 reqpms.send(null);
}
}

function test_xml_complete(){
if(reqTopics.readyState == 4 && reqTopics.status == 200)
 showLinky(reqTopics.responseText);
}
function test2_xml_complete(){
if(reqpms.readyState == 4 && reqpms.status == 200)
 showPMlinky(reqpms.responseText);
}

function showLinky(txt){
RegExp.multiline="true";
var regEx = new RegExp("100%.*>.*<a href='(.*)showtopic=(\\d+)&amp;hl='>" + "(.*)<\/A>.*\n.*\n.*\n.*\n.*align='center'>(.*)<.*\n.*\n.*\n.*\n.*<b><b>(.*)<\/b>","i");
if(txt.match(regEx) ){
  var apps = '<table width="100%">';
  var i = 0;
  var matches = regEx.exec(txt);
  while (matches != null && i < 10){
     i++;
     apps += '<td class="mya-row"><span style="float:right;"><i>By ' + RegExp.$5 + '</i></span><a href="' + RegExp.$1 + 'showtopic=' + RegExp.$2 + '" style="text-decoration:none;">' + RegExp.$3 + '</a></td></tr>';
     txt = txt.substring(matches.index + 50, txt.length);
   var matches = regEx.exec(txt);
  }
  apps+='</table>';
 document.getElementById('postshere').innerHTML = apps;
} else {
 document.getElementById('postshere').innerHTML = "<div style='margin:8px;text-align:center;'><i>No new posts found</i></div>";
}
}
function showPMlinky(txt2){
RegExp.multiline="true";
var regEx2 = new RegExp("<a href='(.*)act=Msg&amp;CODE=03&amp;VID=in&amp;MSID=(\\d+)'>" + "(.*)<\/A>","i");
if(txt2.match(regEx2) ){
  var tempm = '<table width="100%">';
  var i2 = 0;
  var matches2 = regEx2.exec(txt2);
  while (matches2 != null && i2 < 10){
     i2++;
     tempm += '<td class="mya-row"><a href="' + RegExp.$1 + 'act=Msg&amp;CODE=03&amp;VID=in&amp;MSID=' + RegExp.$2 + '" style="text-decoration:none;">' + RegExp.$3 + '</a></td></tr>';
     txt2 = txt2.substring(matches2.index + 50, txt2.length);
   var matches2 = regEx2.exec(txt2);
  }
  tempm+='</table>';
 document.getElementById('pmshere').innerHTML = tempm;
} else {
 document.getElementById('pmshere').innerHTML = "<div style='margin:8px;text-align:center;'><i>(No messages in your inbox)</i></div>";
}
}

getPMS('/index.php?act=Msg&CODE=01');
getActiveTopics('/index.php?act=Search&CODE=getactive');

var theHandle = document.getElementById("mya-drag");
var theRoot = document.getElementById("myabox");
Drag.init(theHandle, theRoot);