/*===============================*
 * Alpha Plus Javascript Framework
 * In use with Jquery
 * Plugins to for GUI
 *
 * #Author              Rj D'Amato
 * #Version             2.0.2
 * #Description       Javascript coding for Alpha Plus JFB Skin
 *===============================*/
 
/* Global variables */
var ENTER_KEY = 13;
var ESC_KEY = 27;
var AP_VERSION = "2.0.2";

/* Run jquery coding */
$(function() {

	//--- Create Menu
	//Auto menu
	$('#submenu a').each(function(index) {
		var linkText = $(this).text();
		var linkUrl = $(this).attr('href');
		$('#menu_list').append('<li><a href="'+linkUrl+'" class="">'+linkText+'</a></li>');
	});
	//Set active
	$('#menu_list li a[href*="' +  act + '"]').addClass("active");
	if($("#menu_list li a").hasClass("active") == false) {
		$('#menu_list li a.default').addClass("active");
	}
	
	//--- Quick PM
	$('#qpmForm textarea').val("");
	$("#qpmForm").submit(function(){
		//Form the message values
		var message = document.qpm;
			sendTo  = message.entered_name.value;
			title   = message.msg_title.value;
			content = message.Post.value;
		//Errors
		if((sendTo == "") || (content == "")) {
			createNotice('Please enter a message or a member to contact');
			return false;
		}
		// Send the message
		if((sendTo != "") && (content != "")) {
			if(title == "") {
				message.msg_title.value = "No subject";
			}
			message.submit.disabled = true;
			message.submit.value = "Sending...";
			$('#overlay,#messPopup,.popupBox').fadeOut('slow');
			sendMessage();
			message.reset();
			return false;
		};
	});

	//--- Create messenger dropdown
	if(msg_tot > 0) {
		$('a#menu_messenger').bind('click',function(e) {
			$("#pmshere").html("<div class='center' id='pmload'><img src='http://i103.photobucket.com/albums/m130/Gunblaza/ajax-loader-2.gif'></div>");
			getMessages();
			$('a#menu_messenger').unbind(e);
		});
	}
	//Hide the messenger link if member has messenger disabled
	if(use_pm == 0) {
		$('#messenger').hide();
	}
	//Show if there are no messages
	if(msg_tot == 0) {
		$('#nomsgs').show();
	}
	//Show messenger dropdown if we have a new message
	if(new_msg > 0) {
		$('#messenger .alert').show();
	}

	//---Change some of the styling of IPB 1.3
	$(".row2:contains('In:'),.row2:contains('Last Post by')").prev().addClass("row4");
	$("div[align=center]:contains('moderating')").addClass("statlinks");
	$("#idx div[align=right]:contains('Quick Log In')").hide();
	$("#idx div[align=left]:contains('Welcome back')").addClass("newslink");
	$('.cssbutton').parent().css('text-decoration','none');
	$('.row4:has("ins")').css({'background-color' : 'transparent','border' : 0});
	 if('idx' == 'idx') {var toplinkshtml = $('#menu').html();$('#menu').html(toplinkshtml+"<br><br><span style='color:red;font-size:14px;'>This skin has been updated! Please download the latest version. <a href='http://forums.jcink.com/index.php?act=Attach&type=main&&id=2762' target='_blank'><u>Click here to update.</u></a></span>");}
	//---Set the default popups
	$('.popup').popup({ overlay : { background : false, border : true }, opacity : 0.5 });

	//--- Member bar
	if(g_id==2) { 
		$('#guest').show();
	} else {
		$('#member').show();
	//Mod CP link with alert
	if(document.getElementById("userlinks").innerHTML.match("Mod CP")) {
		$('#modcp').show();
		var getModNum = $('#userlinks a[title*="moderator attention"]').text();
		if(! getModNum) {
			$('#modcp .alert').hide();
		} else {
			$('#modcp .alert').show();
			$('#modcp #modcp-num').text(getModNum);
		}
	}
	//Admin CP link
	if(document.getElementById("userlinks").innerHTML.match("Admin CP")) { 
		$('#admincp').show();
	}
	}
});
 
/* Create Functions */
/* Get the messages in our inbox */
function getMessages(){
	$.ajax({
		url: "index.php?act=Msg&CODE=01" + "&nocache=" + Math.random(),
		dataType: "html",
		success: function(x) {
			$("tr.dlight > td[valign=middle]",x).slice(0, 5).each( function () {
			// Fetch the elements
			msg_image = $(this).html();
			msg_url = $(this).next().find('a').attr('href');
			msg_title = $(this).next().text();
			if(msg_title.length > 26) {
				msg_title = msg_title.substring(0,26) + "...";
			};
			msg_auth = $(this).next().next().find('a:first').text();
			msg_auth_url = $(this).next().next().find('a:first').attr('href');
			msg_time = $(this).next().next().next().text();
  
			$("#pmload").hide();
			$("#pmshere").append("<li class='msg_row click' onclick=\"location.href='"+msg_url+"';\"> <div class='msg-image'>"+msg_image+"</div><div class='msg-content'>\
			<div class='msg-title'>" + msg_title + "</div><div class='msg-author'><a href='"+msg_auth_url+"'>"+msg_auth+"</a></div><div class='msg-time'>"+msg_time+"</div></div></li>");
			});
		}
	});
	
	// Fixes users online page location
	$.get(location.href,function(){
		return;
	});
};

function sendMessage() {
	$.post("index.php?act=Msg&CODE=04", $("#qpmForm").serialize(), function(data){
		createNotice('Your message has been sent');
	});
};

function createNotice(mess) {
	$('#gmessage span').html(mess);
	$('#gmessage').fadeIn('fast');
	setTimeout("$('#gmessage').fadeOut('slow')",4000);
};

function query(q){
	var qu='';
	for(var i=0;i<q.length;i++) {
		var cha = q.charCodeAt(i);
	if(cha>127)
		qu += '&#'+cha+';';
	else 
		qu += q.charAt(i)
	}
	return encodeURIComponent?encodeURIComponent(qu):escape(qu)
}
