/*
Type 1.2.1
IPB style My Assistant for IPB free hosted boards
Made by gunblaza
Scribes Coding Community 
*/

$(document).ready(function() {	
	//select all the a tag with name equal to modal
	$('a[rel=mya]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		//Get the A tag
		var ids = $(this).attr('href');

		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(ids).css('top',  winH/2-$(ids).height()/2);
		$(ids).css('left', winW/2-$(ids).width()/2);
	
		//transition effect
		$(ids).fadeIn('fast'); 
	
	});
	
	//if close button is clicked
	$('#myabox .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
               $('#myabox').fadeOut('fast'); 
	});		
});



//Tab system
function myatab(num) {
document.getElementById('matab1').className = "closedtab";
document.getElementById('matab2').className = "closedtab";
document.getElementById('matab3').className = "closedtab";
document.getElementById('matab4').className = "closedtab";
document.getElementById('matab'+num).className = "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";
}
$(document).ready(function() {	
if(new_msg > '0') {
//Get the window height and width
var winH2 = $(window).height();
var winW2 = $(window).width();
$('#myabox').css('top',  winH2/2-$('#myabox').height()/2);
$('#myabox').css('left', winW2/2-$('#myabox').width()/2);
$('#myabox').fadeIn('fast'); 

document.getElementById('matab4').setAttribute("class", 'opentab');
document.getElementById('matab1').setAttribute("class", 'closedtab');
document.getElementById('matab2').setAttribute("class", 'closedtab');
document.getElementById('matab3').setAttribute("class", 'closedtab');
document.getElementById('macon4').style.display = 'block'
document.getElementById('macon1').style.display = 'none'
document.getElementById('macon2').style.display = 'none'
document.getElementById('macon3').style.display = 'none'
}
	});

//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'>";
//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='#' class='close' 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='opentab' OnClick='myatab(1);'>Information</div>";
myatemp += "<div id='matab2' class='closedtab' OnClick='myatab(2)'>New Post</div>";
myatemp += "<div id='matab3' class='closedtab' OnClick='myatab(3);'>Search</div>";
if(new_msg > '0'){
myatemp += "<div id='matab4' class='closedtab' OnClick='myatab(4);'>"+new_msg+" New Messages</div>";
}else{
myatemp += "<div id='matab4' class='closedtab' OnClick='myatab(4);'>Recent PMs</div>";
}
myatemp += "</div>";

//New style infopage 

myatemp += "<div id='macon1'><div class='mya-contentbox-back'><div class='mya-contentbox'>";
myatemp += "<span style='float:right'><b>Welcome, "+username+"</b> ";
myatemp += "<span style='font-size:8px;'><a href='index.php?act=Login&CODE=03&key="+auth_key+"' title='Log Out' style='text-decoration:none;'><img width='13px' height='13px' src='http://i103.photobucket.com/albums/m130/Gunblaza/fams/hourglass_delete.png'></a></span> </span><a href='index.php?act=UserCP&CODE=00' style='font-size:9px;'>Edit Profile</a> | <a href='index.php?showuser="+id+"' 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="perrorshere"</span>';
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;'>";
if(msg_tot > '0'){
myatemp += '<span id="pmshere"></span>';
}else{
myatemp += "<div style='margin:8px;text-align:center;'><i>(No messages in your inbox)</i></div>";
}
myatemp += '<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>";


//Place My Assistant  on board
document.write(myatemp);


/*
*Get Recent PM's and Post by gunblaza (now thats smaller and better)
*http://gunprojects.b1.jcink.com
*/
$.get("index.php?act=Msg&CODE=01",function(d) {
$("tr.dlight",d).slice(0, 5).each(function () {
img = $(this).find("td[valign=middle]").html();
link = $(this).find("td[valign=middle]").next("td").html();
time = $(this).find("td[valign=middle]").next().next().next().html();

if(img == null){
$("#pmshere").append("<div style='display:none;'></div>");
}else{
$("#pmshere").append('<div class="mya-row"><div class="right"><span class="desc"><i>('+time+')</i></span></div>'+img+' '+link+'</div>');
}
});
});


perror = '<div style="text-align:center;"><i>(No new post to show)</i></div>';
$.get("index.php?act=Search&CODE=getactive",function(d) {
$(".tableborder:contains('Active') table tr:gt(0)",d).slice(0, 10).each(function () {
post = $(this).find("table td").next().html();
author = $(this).find("td.row4:first").next().next().next().next().html();
replies = $(this).find("td.row4:first").next().next().next().next().next().html();
views = $(this).find("td.row4:first").next().next().next().next().next().next().html();
pimg = $(this).find("td.row4:first").html();
show = '<div class="right"><span class="desc">(<b>Replies</b>: '+replies+' | <b>Views</b>: '+views+')</span></div>'+pimg+' '+post+' <span class="desc">(Author: '+author+')</span>';
if(pimg == null){
$("#perrorshere").append('<div class="mya-row">'+perror+'</div>');
}else{
$("#postshere").append('<div class="mya-row">'+show+'</div>');
$("#perrorshere").hide();
}
});});


var theHandle = document.getElementById("mya-drag");
var theRoot = document.getElementById("myabox");
Drag.init(theHandle, theRoot);