////////////////////////////////////////////////////////////
//                          __    _______                 //
//     ____ ___  ____  ____/ /   / / ___/                 //
//    / __ `__ \/ __ \/ __  /_  / /\__ \                  //
//   / / / / / / /_/ / /_/ / /_/ /___/ /                  //
//  /_/ /_/ /_/\____/\__,_/\____//____/  VERSION 1.4      //
//  FPSBanana's Profile Customizer Plugin                 //
////////////////////////////////////////////////////////////
// All functions and code is released for use only on     //
// fpsbanana.com. Uses jQuery Framework (jquery.com)      //
////////////////////////////////////////////////////////////
// RELEASE DATE: 2/18/2010                                //
// Copyright (c) 2009-2010 Big Ross Labs                  //
////////////////////////////////////////////////////////////
// modJS Studio: http://www.fpsbanana.com/studios/32970   //
////////////////////////////////////////////////////////////
// Requires JQuery 1.4.1 - Read the Documentation         //
////////////////////////////////////////////////////////////
		
(function($){
	responderRequest();
	$('style:first').prepend('@import "http://modjs.bigrosslabs.com/css/styles.css";');
	
	$.fn.extend({ 
		// You 2 Me Image Fix
		y2mfix: function(options) {
			var defaults = {
				mode: 'replace', // replace - Replace You 2 Me Image with default or your own : hide - Hide the You 2 Me Image
				img: 'http://www.bigrosslabs.com/fpsb/y2mfix/y2m.gif' // The source of the image you want to use
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
				var o =options;
				var obj = $(this);
				var y2m = $("img[src='http://image.fpsbanana.com/ico/y2m.gif']", obj);			
				
				if(o.mode == 'replace') {
					y2m.attr('src', o.img);
				} else if(o.mode == 'hide') {
					y2m.css('display', 'none');
				}
    		});
    	},
		// Affiliate Image Fix
		affiliateFix: function(options) {
			var defaults = {
				mode: 'fix' // fix - Attempt to fix broken Image : hide - Hide the Image
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
					var o =options;
					var obj = $(this);
					var clubs = $("img[src^='http://image.fpsbanana.com/flags/clubs/']", obj);
					var studios = $("img[src^='http://image.fpsbanana.com/flags/studios/']", obj);
					var errorimg = 'http://image.fpsbanana.com/ico/error.gif';
					
					if(o.mode == 'fix') {
						clubs.error(function () {
  							$(this).unbind("error").attr("src", errorimg);
						});
						studios.error(function () {
  							$(this).unbind("error").attr("src", errorimg);
						});
					} else if(o.mode == 'hide') {
						clubs.error(function(){
							$(this).css('display', 'none');
						});
						studios.error(function(){
							$(this).css('display', 'none');
						});
					}
			});
    	},
		// My Images : Replace certain images with your own
		myImages: function(options) {
			var defaults = {
				imgFind   : '',
				imgReplace: '' // The image to replace 
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
				var o =options;
				var obj = $(this);
				
				if(o.imgReplace != null) {
					$("img[src='"+o.imgFind+"']", obj).each(function() {
						$(this).attr('src', o.imgReplace);
    				});
				}
    		});
    	},
		// Image Hider Function
		hideImage: function(options) {
			var defaults = {
				type: '', // url - hide image by it's url : class - hide image by class (.myimage) : id - hide image by ID (#myimage)
				hide: '' // The url or class or id of the image you want to hide
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
				var o =options;
				var obj = $(this);	
				
				if(o.type == 'url') {
					$("img[src='"+o.hide+"']", obj).hide();
				} else if(o.type == 'class') {
					$("img[class='"+o.hide+"']", obj).hide();
				} else if(o.type == 'id') {
					$("img[id='"+o.hide+"']", obj).hide();
				}
    		});
    	},
		// Text Replace Function
		textReplace: function(options) {
			var defaults = {
				textFind: '', // The text to find
				textReplace: '' // The text to replace the found text
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
				var o =options;
				var obj = $(this);
				var strReplaceAll = obj.html();
				var intIndexOfMatch = strReplaceAll.indexOf(o.textFind);
				while (intIndexOfMatch != -1){
					strReplaceAll = strReplaceAll.replace(new RegExp(o.textFind, "i"), o.textReplace)
					intIndexOfMatch = strReplaceAll.indexOf(o.textFind);
				}
				obj.html(strReplaceAll);
    		});
    	},
		// modJS Micro Banner Function
		supportBanner: function() {
			var obj = $(this);
			$('div.row_dark:last', obj).append('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.fpsbanana.com/studios/32970"><img src="http://img.bigrosslabs.com/modjs_micro_banner.png" border="0" title="Powered by modJS!" /></a>');
    	},
		// Page Title Function
		pageTitle: function(options) {
			var defaults = {
				title: '' // The title you want to set your profile as
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
				var o =options;
				document.title = o.title;
    		});
    	},
		// Avatar Hider Function
		hideAvatars: function() {
			var obj = $(this);
			$("div#box_messages table[onmouseout='ttObj.Close();']", obj).hide();
    	},
		// Element Hider Function
		hideElement: function(options) {
			var defaults = {
				element: '' // The element to hide as class name (.elementClass) or id (#elementID)
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
				var o =options;
				var obj = $(this);
				$(o.element, obj).hide();
    		});
    	},
		// modUber Function
		uberstyler: function(options) {
			var defaults = {
				type: 'profile', // profile - for editing user profile uber : browse - for editing browsing uber : studio - for editing studio uber : club - for editing club uber
				id: null // Set this to the ID of the profile, studio, or club uber you are editing
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
				var o =options;
				var obj = $(this);
				var current = $('style:first', obj).text();
				var foot = $('div.row_dark:last', obj);
				foot.children("a[href='http://www.fpsbanana.com/pages/192']").attr({href: "javascript:void(0);", id: 'modJSuberstyler'}).html('Open UberStyler');
				obj.children("a[href='javascript:$(this).uberstylerOpen();']").attr({href: "javascript:void(0);", id: 'modJSuberstyler'});
				$("#modJSuberstyler").click(function(){
					uberstylerOpen(o.type, o.id);
				});
    		});
    	},
		// oldMenu Function
		oldMenu: function(options) {
			var defaults = {
				pos: 'default' // default - place menu in default place : top - place menu at top of the page
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
				var o =options;
				var obj = $(this);
				
				if(o.pos == 'default') {
					var boxstyle = '';
					var innerstyle = '';
					var maintable = $('.main_outer_border', obj);
					var marginset = '';
					var mjsboxCSS = 'mjs_fpsb_menu_default';
				} else {
					var boxstyle = ' class="row_browse"';
					var innerstyle = ' box';
					var maintable = $('#main_table', obj);
					var marginset = 'margin:0px 0px 25px 0px;';
					var mjsboxCSS = 'mjs_fpsb_menu_top';
					obj.css('marginTop', '50px');
				}
				
				var menu_html = '<div id="mjs_fpsb_menu"><table width="100%" align="center" cellpadding="0" cellspacing="0" class="noborder box" style="'+marginset+'padding:0px;"><tr valign="top"><td'+boxstyle+' style="padding:3px;" nowrap><div class="bit left'+innerstyle+'" style="padding:0px;width:100%;"><div onmouseover="ShowDiv(\'tl_gms\')" onmouseout="ShowDiv(\'tl_gms\')" class="bit left"><a href="http://www.fpsbanana.com/games" class="bold">GAMES</a><div class="mjs_submenu row_dark box" style="display:none" id="tl_gms"><a href="http://www.fpsbanana.com/games">Standalone</a><a href="http://www.fpsbanana.com/mods">Mods</a><a href="http://www.fpsbanana.com/games/allfiles">Files</a></div></div><div class="bit left grey">|</div><div onmouseover="ShowDiv(\'tl_gfx\')" onmouseout="ShowDiv(\'tl_gfx\')" class="bit left"><a href="#" class="bold">GFX</a><div class="mjs_submenu row_dark box" style="display:none" id="tl_gfx"><a href="http://www.fpsbanana.com/guis">GUIs</a><a href="http://www.fpsbanana.com/sprays">Sprays</a><a href="http://www.fpsbanana.com/sprites">Sprites</a></div></div><div class="bit left grey">|</div><div onmouseover="ShowDiv(\'tl_maps\')" onmouseout="ShowDiv(\'tl_maps\')" class="bit left"><a href="http://www.fpsbanana.com/maps" class="bold">MAPS</a><div class="mjs_submenu row_dark box" style="display:none" id="tl_maps"><a href="http://www.fpsbanana.com/prefabs">Prefabs</a><a href="http://www.fpsbanana.com/textures">Textures</a></div></div><div class="bit left grey">|</div><div class="bit left"><a href="http://www.fpsbanana.com/skins" class="bold">SKINS</a></div><div class="bit left grey">|</div><div class="bit left"><a href="http://www.fpsbanana.com/scripting" class="bold">SCRIPTS</a></div><div class="bit left grey">|</div><div class="bit left"><a href="http://www.fpsbanana.com/sounds" class="bold">SOUNDS</a></div><div class="bit left grey">|</div><div class="bit left"><a href="http://www.fpsbanana.com/tools" class="bold">TOOLS</a></div><div class="bit left grey">|</div><div class="bit left"><a href="http://www.fpsbanana.com/tuts" class="bold">TUTORIALS</a></div><div class="bit left grey">|</div><div onmouseover="ShowDiv(\'tl_read\')" onmouseout="ShowDiv(\'tl_read\')" class="bit left"><a href="#" class="bold">READ</a><div class="mjs_submenu row_dark box" style="display:none" id="tl_read"><a href="http://www.fpsbanana.com/blogs">Blogs</a><a href="http://www.fpsbanana.com/features">Features</a><a href="http://www.fpsbanana.com/news">News</a><a href="http://www.fpsbanana.com/pages">Pages</a><a href="http://www.fpsbanana.com/pagecats/33">Site Documentation</a><a href="http://www.fpsbanana.com/pagecats/51">Site Rules</a></div></div><div class="bit left grey">|</div><div onmouseover="ShowDiv(\'tl_devhub\')" onmouseout="ShowDiv(\'tl_devhub\')" class="bit left"><a href="http://www.fpsbanana.com/devhub" class="bold">DEVHUB</a><div class="mjs_submenu row_dark box" style="display:none" id="tl_devhub"><a href="http://www.fpsbanana.com/studios">Studios</a><a href="http://www.fpsbanana.com/studios/mods">Mods</a><a href="http://www.fpsbanana.com/tuts">Tutorials</a><a href="http://www.fpsbanana.com/projects">Projects</a><a href="http://www.fpsbanana.com/wips">WiPs</a><a href="http://www.fpsbanana.com/mips">MiPs</a><a href="http://www.fpsbanana.com/models">Compilers\' Corner</a><a href="http://www.fpsbanana.com/prefabs">Prefabs</a><a href="http://www.fpsbanana.com/studios/requests">Requests</a><a href="http://www.fpsbanana.com/studios/helpwanted">Help Wanted</a></div></div><div class="bit left grey">|</div><div onmouseover="ShowDiv(\'tl_playhub\')" onmouseout="ShowDiv(\'tl_playhub\')" class="bit left"><a href="#" class="bold">PLAYHUB</a><div class="mjs_submenu row_dark box" style="display:none" id="tl_playhub"><a href="http://www.fpsbanana.com/clans">Clans</a><a href="http://www.fpsbanana.com/clubs">Clubs</a><a href="http://www.fpsbanana.com/gametracker">GameTracker</a><a href="http://www.fpsbanana.com/clans/ladder">Ladder</a><a href="http://www.fpsbanana.com/clans/allmatches">Matches</a><a href="http://www.fpsbanana.com/servers">Servers</a></div></div><div class="bit left grey">|</div><div class="bit left"><a href="http://www.fpsbanana.com/forum" class="bold">FORUM</a></div></div></td></tr></table></div>';
				
				if(o.pos == 'default') {
					maintable.prepend(menu_html);
				} else {
					maintable.before(menu_html);
				}
				$('#mjs_fpsb_menu').addClass(mjsboxCSS);
    		});
    	},
		// Sidemenu Hider Function
		hideMenu: function(options) {
			var obj = $(this);	
			$('#bsmuhome').parent('div.box').hide();
    	}/*,
		// messagesLite Function
		messagesLite: function(options) {
			var defaults = {
				uid: 0 // The userid to get information for or leave it at 0 for current user's information
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
				var o =options;
				var obj = $(this);
				var upic = $("#box_messages img[src*='/upic/']", obj);
				
				upic.each(function() {
					var user_url = $(this).parent('a').attr('href');
					var uid = user_url.replace('http://www.fpsbanana.com/members/', '');
					$.get("http://www.fpsbanana.com/responders/api", { moduleid: "449", type: "main", entity_id: uid }, function(json){
						if(json == '<b class="red"><img src="http://image.fpsbanana.com/ico/error.gif"> Module is broken - please contact the module author</b>^@(') {
							console.log('MODJS Error: API AJAX Call Failure. User is not logged in!');
						} else {
							json = json.replace('^@(<div>', '');
							json = json.replace('</div>', '');
							var ret = apiJson(uid, json);
							$(this).parent('a').html(ret[0]);
						}
					}, 'text');
				});
    		});
    	}*/
	});
	
	function uberstylerOpen(type, id) {
		var current = $('style:first').text();
		var defaultU = current;
		
		if(id !== null) {
			if(type == 'profile') {
				var saveURL = 'http://www.fpsbanana.com/members/edit/uberprofile/'+id;
			} else if(type == 'browse') {
				var saveURL = 'http://www.fpsbanana.com/members/edit/uberbrowsing/'+id;
			} else if(type == 'studio') {
				var saveURL = 'http://www.fpsbanana.com/studios/edit/uberstyle/'+id;
			} else if(type == 'club') {
				var saveURL = 'http://www.fpsbanana.com/clubs/edit/uberstyle/'+id;
			}
			var saveBTN = '<form name="f_feditor" id="f_feditor" method="post" action="'+saveURL+'"><textarea name="file_contents" id="file_contents" style="width:0px !important; height:0px !important; visibility:hidden;">'+current+'</textarea><input type="submit" id="modUber_gosave" name="sub_feditor" class="border_blue" value="Save Changes &raquo;" /></form> &nbsp;&nbsp; ';
		} else {
			var saveBTN = '';
		}
		
		if($('#modUber').is(':visible')) {
			$('html, body').animate({scrollTop:0}, 'slow');
		} else {
			$('html, body').animate({scrollTop:0}, 'slow').css('margin-top', '0px');
			$('<div>').html('<span><img src="http://modjs.bigrosslabs.com/images/uberstyler/mjs_us_logo.png" border="0" /></span><textarea name="modUberStyles" id="modUberStyle">'+current+'</textarea><br /><div id="modUber_opts"><table width="100%" border="0" cellspacing="0" cellpadding="5" align="center"><tr><td align="right" valign="top"><small style="font-size:9px;"><b class="red">NOTICE!</b></td><td width="35%" align="left" valign="top">Any changes made here are only seen by you. Changes will not be saved unless you copy the code above and paste it into your UberStyle Editor!</small></td><td align="right" valign="middle">'+saveBTN+'<input type="button" id="modUber_select" class="border_green" value="Select All" /> &nbsp;&nbsp; <input type="button" id="modUber_revert" class="border_red" value="Revert to Default" /> &nbsp;&nbsp; <input type="button" id="modUber_close" class="border_blue" value="&times; Close" /></td></tr></table></div>').attr('id', 'modUber').prependTo('body').slideDown('normal');
			$("#modUberStyle").live("keyup", function(){
				$('style:first').text($('#modUberStyle').val());
				if(saveBTN !== '') {
					$('#modUber_opts #file_contents').val($('#modUberStyle').val());
				}
			});
			$("#modUber_gosave").live("click", function(event){
				var answer = confirm("Are you sure you want to save these changes?");
				if(answer) {
					$('#modUber_opts #f_feditor').submit();
				} else {
					event.preventDefault();
				}
			});
			$("#modUber_select").live("click", function(){
				$('textarea#modUberStyle').select();
			});
			$("#modUber_revert").live("click", function(){
				$('style:first').text(defaultU);
				$('#modUberStyle').val(defaultU);
			});
			$("#modUber_close").live("click", function(){
				$('style:first').text(defaultU);
				$('#modUberStyle').val(defaultU);
				$('#modUber').slideUp('normal');
			});
		}
	}
	
	function getVisitorID() {
      var link = $("#upnlfltlft div[title='Profile'] a").attr("href");
      var vid = link.replace("http://www.fpsbanana.com/members/", "");
      return vid;
	}
	
	function apiJson(uid, jvars) {
		eval(jvars);
		if(uid == 0) {
		    var info = new Array(uInfo.u_name, uInfo.u_last_seen, uInfo.u_points, uInfo.u_avatar, uInfo.u_ripe);
	  	} else {
			var info = new Array(uInfo.o_name, uInfo.o_last_seen, uInfo.o_points, uInfo.o_avatar, uInfo.o_ripe);
	    }
		return info;
	}
	
	function responderRequest() {
		var search_term = 'http://www.fpsbanana.com/members/'; 
		var str = document.URL;
		var url_check = str.indexOf(search_term); 
		var uid;
		if(url_check != -1) {
			uid = str.replace(search_term, '');
			$.get("http://www.fpsbanana.com/responders/api", { moduleid: "449", type: "box", entity_id: uid }, function(json){
				if(json == '<b class="red"><img src="http://image.fpsbanana.com/ico/error.gif"> Module is broken - please contact the module author</b>^@(') {
					console.log('MODJS Error: API AJAX Call Failure. User is not logged in!');
				} else {
					json = json.replace('^@(<div>', '');
					json = json.replace('</div>', '');
				}
			}, 'text');
		}
	}
	
})(jQuery);
