//GLOBAL JAVASCRIPT FUNCTIONS FOR SITE

$(document).ready(function() {
	homeTwitter();
	$(function(){
		sitehome = new SiteHome ;
	});
});

function SiteHome() {
	this.jumbotron_state = "";
	this.jumbotron_state_previous = "";
	this.jumbotron_max = 185;
	this.jumbotron_min=92;
	this.jumbotron_increment_start = 23; //starting interval
	this.jumbotron_increment_easeadjustor = .75;//arc of easing
	this.jumbotron_increment_current = this.jumbotron_increment_start;
	
	
	this.init();
}

SiteHome.prototype = {
	/////////////////////////////////////////////////////
	//INITIALIZERS
	/////////////////////////////////////////////////////
	init:function(){
		//PSN LOG IN
		$("#your_stats_login_psnid input").focus(function() {
			var val = $("#your_stats_login_psnid input").val();
			if (val=="PSN LOG IN") { 
				$("#your_stats_login_psnid input").val('');
			} 
		});
		$("#your_stats_login_psnid input").blur(function() {
			var val = $("#your_stats_login_psnid input").val();
			if (val=="") { 
				$("#your_stats_login_psnid input").val('PSN LOG IN');
			}
		});
		
		//PSN PASS
		$("#your_stats_login_psnpass #psn_plain").focus(function() {
			$("#your_stats_login_psnpass #psn_pass").removeClass("hide");
			$("#your_stats_login_psnpass #psn_plain").addClass("hide");
			$("#your_stats_login_psnpass #psn_pass").val('');
			$("#your_stats_login_psnpass #psn_pass").focus();
		});
		$("#your_stats_login_psnpass #psn_pass").blur(function() {
			var val = $("#your_stats_login_psnpass #psn_pass").val();
			if (val=="") { 
				$("#your_stats_login_psnpass #psn_pass").addClass("hide");
				$("#your_stats_login_psnpass #psn_plain").removeClass("hide");
				$("#your_stats_login_psnpass #psn_plain").val('PSN PASSWORD');
			}
		});
		
		//SEARCH
		$("#your_stats_login_search input").focus(function() {
			var val = $("#your_stats_login_search input").val();
			if (val=="SEARCH USER NAMES") { 
				$("#your_stats_login_search input").val('');
			} 
		});
		$("#your_stats_login_search input").blur(function() {
			var val = $("#your_stats_login_search input").val();
			if (val=="") { 
				$("#your_stats_login_search input").val('SEARCH USER NAMES');
			}
		});
		
		
		//JUMBOTRON HOVERS
		/*
		$(".jumbotron_right_item").mouseover(function() {
			
			if ($('.jumbotron_right_item_img',this).height()<185) { 
				var id = $(this).attr("id");				
				$('.jumbotron_right_item_img',this).stop().animate({height:185}, 250, function() {  });
				
				//make the first one short
				
				if (id!="jt0") { 
					if ($('.jumbotron_right_item_img:first').height()==185) { 
						$('.jumbotron_right_item_img:first').stop().animate({height:92}, 250, function() {  });
					}
				}
				
			}
			
		});
		
		$(".jumbotron_right_item").mouseleave(function() {
			$('.jumbotron_right_item_img',this).stop().animate({height:92}, 250, function() {  });
		});
		$("#jumbotron_right").mouseleave(function() {
			$('.jumbotron_right_item_img:first').stop().animate({height:185}, 250, function() {  });
		});
		*/
		
		$(".jumbotron_right_item").mouseover(function() {
			var id = $(this).attr("id");
			sitehome.jumbotron_state = id;
		});
		$(".jumbotron_right_item").mouseleave(function() {
			
		});
		$("#jumbotron_right").mouseleave(function() {
			sitehome.jumbotron_state = "jt0";
		});
		
		setTimeout(updateJumboTron,20);
		function updateJumboTron() {
			var jt0h = $('#jt0 .jumbotron_right_item_img').height();
			var jt1h = $('#jt1 .jumbotron_right_item_img').height();
			var jt2h = $('#jt2 .jumbotron_right_item_img').height();
			var doit=false;
			
			
			
			if (sitehome.jumbotron_state=="jt0") { 
				if (jt0h<sitehome.jumbotron_max) { 
					jt0h += sitehome.jumbotron_increment_current;
					jt0h = Math.round(jt0h);
					if (jt0h>sitehome.jumbotron_max) jt0h = sitehome.jumbotron_max;
					doit=true;
				}
				if (jt1h>sitehome.jumbotron_min) { 
					jt1h -= sitehome.jumbotron_increment_current;
					jt1h = Math.round(jt1h);
					if (jt1h<sitehome.jumbotron_min) jt1h = sitehome.jumbotron_min;
					doit=true;
				}
				if (jt2h>sitehome.jumbotron_min) { 
					jt2h -= sitehome.jumbotron_increment_current;
					jt2h = Math.round(jt2h);
					if (jt2h<sitehome.jumbotron_min) jt2h = sitehome.jumbotron_min;
					doit=true;
				}
				
				jt2h = 	sitehome.jumbotron_max + sitehome.jumbotron_min + sitehome.jumbotron_min - jt0h - jt1h;
				
							
			} else if (sitehome.jumbotron_state=="jt1") { 
				if (jt1h<sitehome.jumbotron_max) { 
					jt1h += sitehome.jumbotron_increment_current;
					jt1h = Math.round(jt1h);
					if (jt1h>sitehome.jumbotron_max) jt1h = sitehome.jumbotron_max;
					doit=true;
				}
				if (jt0h>sitehome.jumbotron_min) { 
					jt0h -= sitehome.jumbotron_increment_current;
					jt0h = Math.round(jt0h);
					if (jt0h<sitehome.jumbotron_min) jt0h = sitehome.jumbotron_min;
					doit=true;
				}
				if (jt2h>sitehome.jumbotron_min) { 
					jt2h -= sitehome.jumbotron_increment_current;
					jt2h = Math.round(jt2h);
					if (jt2h<sitehome.jumbotron_min) jt2h = sitehome.jumbotron_min;
					doit=true;
				}	
				
				jt2h = 	sitehome.jumbotron_max + sitehome.jumbotron_min + sitehome.jumbotron_min - jt0h - jt1h;
							
			} else if (sitehome.jumbotron_state=="jt2") { 
				if (jt2h<sitehome.jumbotron_max) { 
					jt2h += sitehome.jumbotron_increment_current;
					jt2h = Math.round(jt2h);
					if (jt2h>sitehome.jumbotron_max) jt2h = sitehome.jumbotron_max;
					doit=true;
				}
				if (jt0h>sitehome.jumbotron_min) { 
					jt0h -= sitehome.jumbotron_increment_current;
					jt0h = Math.round(jt0h);
					if (jt0h<sitehome.jumbotron_min) jt0h = sitehome.jumbotron_min;
					doit=true;
				}
				if (jt1h>sitehome.jumbotron_min) { 
					jt1h -= sitehome.jumbotron_increment_current;
					jt1h = Math.round(jt1h);
					if (jt1h<sitehome.jumbotron_min) jt1h = sitehome.jumbotron_min;
					doit=true;
				}	
				
				jt0h = 	sitehome.jumbotron_max + sitehome.jumbotron_min + sitehome.jumbotron_min - jt2h - jt1h;		
			}
			
			
			
			if (doit) { 
				//adjust speed
				if (sitehome.jumbotron_state_previous==sitehome.jumbotron_state) { 
					sitehome.jumbotron_increment_current *= sitehome.jumbotron_increment_easeadjustor;
					if (sitehome.jumbotron_increment_current <1) sitehome.jumbotron_increment_current=1;
				} else { 
					sitehome.jumbotron_increment_current = sitehome.jumbotron_increment_start;
				}
				sitehome.jumbotron_state_previous=sitehome.jumbotron_state;
				
				$('#jt0 .jumbotron_right_item_img').css("height",jt0h);
				$('#jt1 .jumbotron_right_item_img').css("height",jt1h);
				$('#jt2 .jumbotron_right_item_img').css("height",jt2h);
				//IE7 FIX - these three below force the thing to be at bottom 0 - seems IE7 needs the parent height recalculated manually
				$('#jt0').css("height",jt0h);
				$('#jt1').css("height",jt1h);
				$('#jt2').css("height",jt2h);
				
				
				//$('#log').append(jt0h+":"+jt1h+":"+jt2h+"<br>");
			}
			
			//call function
			setTimeout(updateJumboTron,20);	
		}
			
		
	},
	
	
	
	//these three used for global kills chart
	calculateKills:function(k1,k2,k3) {
		var themax = 0;
		if (k1>themax) themax=k1;
		if (k2>themax) themax=k2;	
		if (k3>themax) themax=k3;
		sitehome.drawKills(k1,themax,"#gs_1");
		sitehome.drawKills(k2,themax,"#gs_2");
		sitehome.drawKills(k3,themax,"#gs_3");	
		$("#global_stats_inner").removeClass("hide");
	},
	 drawKills:function(val,maxval,id) {
		var pct=val/maxval;
		var width = pct*243;
		$(id + " .global_stats_item_kills").css("width",width);
		$(id + " .global_stats_item_kills_kills").html(sitehome.addCommas(val));
		$(id + " .global_stats_item_kills_bullet").css("left",width-10);
		$(id + " .global_stats_item_kills_kills").css("left",width+20);
	},
	addCommas:function(nStr){
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	},
	
	//your stats chart
	drawYourChart:function(numw,numl,txtw,txtl,id) {
		var w = numw/(numw+numl);
		w=parseFloat(w*100);
		var num;
		if (w>=100-6.25) {
				num=100;
		} else if (w>=87.5-6.25) { 
				num=88;
		} else if (w>=75-6.25) { 
				num=75;
		} else if  (w>=62.5-6.25) { 
				num=62;
		} else if  (w>=50-6.25) {
				num=50;
		} else if  (w>=37.5-6.25) { 
				num=37
		} else if  (w>=25-6.25) {
				num=25;
		} else if (w>=12.5-6.25) {
				num=12;
		} else {
				num=0;								
		}
// 		$(id).addClass("pie"+num);
// 		var h = "<div class='pie_text'><span class='pie_orange'>" + txtw + "</span> / <span class='pie_blue'>" + txtl + "</span></div>";
// 		h += "<div class='pie_text_left'><span class='pie_orange'>" + numw + "</span></div>";
// 		h += "<div class='pie_text_right'><span class='pie_blue'>" + numl + "</span></div>";		
// 		$(id).html(h);
		
		$(id).addClass("pie"+num);
        var h = "<div class='pie_text'><table>";
        h += "<tr><td><span class='pie_orange'>" + txtw + "</span>&nbsp;/&nbsp;</td><td><span class='pie_blue'>" + txtl + "</span></td></tr>";
        h += "<tr><td><div class='pie_text_left'><span class='pie_orange'>" + numw + "</span></div></td>";
        h += "<td><div class='pie_text_right'><span class='pie_blue'>" + numl + "</span></div></td></tr>";           
        h += "</table></div>";
        $(id).html(h);
	}
	
	
}// p


