var Utils = {
	getUrlVars: function() {
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

		for (var i = 0, num_hashes =  hashes.length; i < num_hashes; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}

		return vars;
	},
	
	getCurrentTimestamp: function() { 
		return Math.round(new Date().getTime() / 1000); 
	},

	addStyles: function(rule){
		var sheet = (document.styleSheets.length > 1) ? document.styleSheets[1] : document.styleSheets[0],
			ln = (sheet.cssRules||sheet.rules).length;

		if (sheet.addRule) { // IE only
			rule = rule.match(/(.*)\{(.*)\}/);
			sheet.addRule(rule[1], rule[2], ln);
		}
		else {
			sheet.insertRule(rule, ln);
		}
	},

	getDisplayTimeForDeciseconds: function(time) {
		var minutes, seconds, display_time;
		
		if (time >= 600) {
			minutes = Math.floor(time / 600);
			seconds = Math.floor((time % 600) / 10);
			display_time = (seconds >= 10) ? minutes +":"+ seconds : minutes +":0"+ seconds;
		}
		else {
			seconds = time / 10;
			display_time = seconds;
			if (time % 10 == 0) {
				display_time += ".0";
			}
		}
		
		return display_time;
	},

	getDecisecondsForDisplayTime: function(time) {
		var minutes, seconds, tenths = 0, deciseconds;
		
		minutes = parseInt(time.substring(0, 2), 10);
		seconds = parseInt(time.substring(3, 5), 10);
		if (time.length > 5) {
			tenths = parseInt(time.charAt(6), 10);
		}
		
		deciseconds = (minutes * 600) + (seconds * 10) + tenths;
		
		return deciseconds;
	},

	convertMilitaryTimeToDisplayTime: function(time) {
		var hours, minutes, display_time;
		var am_pm_text = ((time / 1200) > 1) ? "PM" : "AM";
		var std_time = time % 1200;

		hours = std_time.toString().slice(0, -2);
		minutes = std_time - (hours*100);
		if (minutes == 0) minutes += "0";
		
		display_time = hours + ":" + minutes + am_pm_text;
		
		return display_time;
	},

	getSuffixForNumber: function(num) {
		var suffix, ones_digit;
		ones_digit = num % 10;

		if (num > 3 && num < 21) {
			suffix = "th";
		}
		else if (ones_digit == 1) {
			suffix = "st";
		}
		else if (ones_digit == 2) {
			suffix = "nd";
		}
		else if (ones_digit == 3) {
			suffix = "rd";
		}
		else if (ones_digit == 2) {
			suffix = "nd";
		}
		else {
			suffix = "";
		}
		
		return suffix;
	},
	
	compareNames: function(a, b) {
		var name_a = a.firstlastname.toLowerCase();
		var name_b = b.firstlastname.toLowerCase();
		if (name_a < name_b) { return -1; }
		if (name_a > name_b) { return 1; }
		return 0;
	},
	
	day_names: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
	
	month_names: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
	
	convertCoordinates: function(x, y, tm, prd) {
		var period = prd;
		
		if( tm == 'vtm' )
			p_y = -y;
		else
			p_y = y;
		p_x = x;
	
	if ( period > 2 ){
		p_y = -p_y;
	}
	
	// Don't allow markers to go outside the court:
    // No negative Y
    // No Y greater then 2x420
    // No X less then -250 or greater then 250
    if (p_y < -250){ p_y = -250; }
    if (p_y > 250){ p_y = 250; }
    if (p_x < -5){ p_x = 0; }
    if (p_x > 840){ p_x = 840; }
	
	var l_aResult = [];
    var or_x = p_x;
    var or_y = p_y;
    var or_tm = tm;
	//if shot was made from the teams half (large numbers) //840 is the max length if the court in NBA coords
	if (or_x > 540 && or_x <= 840){
		if ( tm == 'vtm')
			tm = 'htm';
		else
			tm = 'vtm';
		p_x = 840 - p_x;
	}//then we need to flip team type back (coordinates wise)
	
	
	//mapping sectors
	var depth = 0;
	var width = 0;
	
	if (p_y == 0) depth = 0;
	else if (p_y <= -200) depth = 1;
	else if (p_y <= -150 && p_y > -200) depth = 2;
	else if (p_y <= -100 && p_y > -150) depth = 3;
	else if (p_y <= -50 && p_y > -100) depth = 4;
	else if (p_y > 0 && p_y < 50) depth = 5;
	else if (p_y < 0 && p_y > -50) depth = 5;
	else if (p_y >= 50 && p_y < 100) depth = 6;
	else if (p_y >= 100 && p_y < 150) depth = 7;
	else if (p_y >= 150 && p_y < 200) depth = 8;
	else if (p_y >= 200) depth = 9;
	
	
	if (p_x == 0) width = 0;
	else if (p_x <= 50 && p_x > 0) width = 1;
	else if (p_x <= 100 && p_x > 50) width = 2;
	else if (p_x <= 150 && p_x > 100) width = 3;
	else if (p_x <= 210 && p_x > 150) width = 4;
	else if (p_x <= 280 && p_x > 210) width = 5;
	else if (p_x <= 300 && p_x > 280) width = 6;
	
	else if (p_x <= 540 && p_x > 300) width = 8;
	
	else if (p_x < 0 && p_x >= -50) width = 7;
	
	// Offset Matrix: offset[width][depth]
	var offset = []; 
	offset[0] = [];
	offset[1] = [];
	offset[2] = [];
	offset[3] = [];
	offset[4] = [];
	offset[5] = [];
	offset[6] = [];
	offset[7] = [];
	offset[8] = [];
	offset[9] = [];
	
	//negative Y (behind basket line)
	offset[7][0] = 150;
	offset[7][1] = 100;
	offset[7][2] = 110;
	offset[7][3] = 120;
	offset[7][4] = 135;
	offset[7][5] = 275;
	offset[7][6] = 155;
	offset[7][7] = 165;
	offset[7][8] = 175;
	offset[7][9] = 185;
	
	// 0
	offset[0][0] = 160;
	offset[0][1] = 90;
	offset[0][2] = 105;
	offset[0][3] = 125;
	offset[0][4] = 140;
	offset[0][5] = 165;
	offset[0][6] = 160;
	offset[0][7] = 170;
	offset[0][8] = 185;
	offset[0][9] = 190;
	
	// 0 -> 50
	offset[1][0] = 135;
	offset[1][1] = 80;
	offset[1][2] = 95;
	offset[1][3] = 115;
	offset[1][4] = 130;
	offset[1][5] = 150;
	offset[1][6] = 150;
	offset[1][7] = 160;
	offset[1][8] = 175;
	offset[1][9] = 180;
	
	// 50 ->100
	offset[2][0] = 120;
	offset[2][1] = 66;
	offset[2][2] = 81;
	offset[2][3] = 101;
	offset[2][4] = 115;
	offset[2][5] = 135;
	offset[2][6] = 135;
	offset[2][7] = 142;
	offset[2][8] = 155;
	offset[2][9] = 155;
	
	// 100 -> 150
	offset[3][0] = 120;
	offset[3][1] = 66;
	offset[3][2] = 81;
	offset[3][3] = 101;
	offset[3][4] = 115;
	offset[3][5] = 115;
	offset[3][6] = 135;
	offset[3][7] = 142;
	offset[3][8] = 155;
	offset[3][9] = 155;
	
	// 150 -> 210
	offset[4][0] = 120;
	offset[4][1] = 66;
	offset[4][2] = 81;
	offset[4][3] = 101;
	offset[4][4] = 115;
	offset[4][5] = 115;
	offset[4][6] = 135;
	offset[4][7] = 142;
	offset[4][8] = 155;
	offset[4][9] = 155;
	
	// 210 -> 280
	offset[5][0] = 105;
	offset[5][1] = 64;
	offset[5][2] = 74;
	offset[5][3] = 94;
	offset[5][4] = 110;
	offset[5][5] = 120;
	offset[5][6] = 120;
	offset[5][7] = 115;
	offset[5][8] = 125;
	offset[5][9] = 125;
	
	// 280 -> 300
	offset[6][0] = 110;
	offset[6][1] = 69;
	offset[6][2] = 79;
	offset[6][3] = 99;
	offset[6][4] = 115;
	offset[6][5] = 120;
	offset[6][6] = 125;
	offset[6][7] = 120;
	offset[6][8] = 130;
	offset[6][9] = 130;
	
	// 300 -> 420 -> 540
	offset[8][0] = 40;
	offset[8][1] = 40;
	offset[8][2] = 40;
	offset[8][3] = 40;
	offset[8][4] = 40;
	offset[8][5] = 40;
	offset[8][6] = 40;
	offset[8][7] = 40;
	offset[8][8] = 40;
	offset[8][9] = 40;
	
	//if (1)alert("w="+width+" d="+depth +" y="+ p_y+" 0=" + offset[width][depth]);

	// space between court and div edges
	var preCourtY = 107; // 137px addj for a latest style change margin-top 30px;
	var behindBsasket = 30;
    //design stats: width and hight of the marker
    var markerH = 20;
    var markerW = 37;
	
	//transition between flat court and one-point perspective
	var Cy = 0.36;
	var negCy = 0.50;
	var Co = 0;
	
	if (p_y >= 0) Co = Cy;
	else Co = negCy;
	
	p_x = Math.round(p_x * 0.96);
	
	//alert(offset[width][depth]);
	l_aResult[1] = ( behindBsasket - markerW/2 ) +  p_x + offset[width][depth];
	l_aResult[0] = ( preCourtY - markerH/2 ) -  Math.round(p_y*Co);
	//alert(l_aResult[0]);
	if (or_tm != tm){
		//flip back to the original team type 990 - length of the court div (eqv to 840 in NBA coords)
		l_aResult[1] = 990 - markerW - l_aResult[1];
	}
	
	return l_aResult;
		
	}
};
