var RMRS = {
	"Version": "1.0.0",
	"Humans": {
		"Johnny Green": "NoiseFloorDesign.com / @bemusedjohnny"
	},
	"init": function() {
		if($.browser.msie) RMRS.fixIE();
		$(window).load(RMRS.addLight());
	},
	"addLight": function() {
		if($(window).height() > $("body").height()) {
			$("body").height($(window).height());
		}
			
		$("<div>").height($("body").height()).addClass("light").fadeTo(0, 0.15).appendTo("body");
	},
	"fixIE": function() {
		$("#studio_list .shadow_box:last-child").css({"margin-right": "0px"});
		$("#studio_info td:nth-child(3)").css({"width": "110px"});
		$("#studio_info td:nth-child(2)").css({"width": "440px"});
		$(".list > li").css({"color": "black", "padding-left": "10px", "margin-bottom": "5px", "font-size": "30px"});
	}
};

RMRS.Home = function() {
	function init() {
		$("#tweet").html(linkify($("#tweet").text()));
	};
	
	function linkify(inputText) {
		var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
			replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>'),
			replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim,
			replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>'),
			replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim,
			replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1" target="_blank">$1</a>');
		return replacedText
	}; 	
			
	return {
		"init": init
	};
}();

RMRS.Information = function() {
	var ajax_block = false;
	
	function init() {
		$("#contact_form input, #contact_form textarea").bind("click focus", function() {
			if($.trim($(this).val()) == $(this).attr("default"))
				$(this).val("").addClass("focused");
		}).bind("blur", function() {
			if($.trim($(this).val()) == "")
				$(this).val($(this).attr("default")).removeClass("focused");
		});
		
		$("button[name='submit']").bind("click", handleSubmit, false);
	};
	
	function resetForm() {
		$("[name^='Message']").each(function() {
			$(this).val("").blur().blur();
		});
	}
	
	function handleSubmit() {
		if(!ajax_block) {
			$("button[name='submit']").text("SENDING...");
			
			$.post("/includes/services/email.php", $.param($(this).closest("form").serializeArray()), function(data) {
				if(data && data.success) {
					resetForm();
					$("button[name='submit']").text("MESSAGE SENT");
				} else {
					$("button[name='submit']").text("SEND FAILED!");
				}
				
				ajax_block = false;
			}, "json");
		}
		
		return false;
	};
	
	return {
		"init": init
	};
}();

RMRS.Find = function() {
	var map, markers;

	function init() {
		markers = {
			"Rivington Music": {
				"name": "Rivington Music Rehearsal Studios",
				"address": "188 Stanton, New York, NY",
				"lat": 40.7199977,
				"lng": -73.98341119999998
			}, 
			"The Thompson": {
				"name": "The Thompson",
				"address": "190 Allen, New York, NY",
				"lat": 40.7223325, 
				"lng": -73.98874519999998
			},
			"Hotel on Rivington": {
				"name": "Hotel on Rivington",
				"address": "107 Rivington, New York, NY",
				"lat": 40.7199694,
				"lng": -73.98801789999999
			},
			"Blue Moon Hotel": {
				"name": "Blue Moon Hotel",
				"address": "100 Orchard, New York, NY",
				"lat": 40.7186194,
				"lng": -73.98990449999997
			},
			"Off Soho Suites": {
				"name": "Off Soho Suites",
				"address": "11 Rivington, New York, NY",
				"lat": 40.72147,
				"lng": -73.99251200000003
			},
			"East Houston Hotel": {
				"name": "East Houston Hotel",
				"address": "151 East Houston, New York, NY",
				"lat": 40.7229585,
				"lng": -73.98940319999997
			},
			"Edison ParkFast": {
				"name": "Edison ParkFast",
				"address": "167-177 Essex, New York, NY",
				"lat": 40.7217885,
				"lng": -73.98654850000003
			},
			"Central Parking Systems": {
				"name": "Central Parking Systems",
				"address": "135 Delancey, New York, NY",
				"lat": 40.716748572144155,
				"lng": -73.98623954100037
			},
			"Parking Systems": {
				"name": "Parking Systems",
				"address": "105 Essex, New York, NY",
				"lat": 40.718996813186195,
				"lng": -73.98807506686478
			},
			"Arlene's Grocery": {
				"name": "Arlene's Grocery",
				"address": "95 Stanton, New York, NY",
				"lat": 40.721264066028084,
				"lng": -73.98826076319465
			},
			"The Bowery Ballroom": {
				"name": "The Bowery Ballroom",
				"address": "6 Delancey, New York, NY",
				"lat": 40.720364,
				"lng": -73.99354199999999
			},
			"The Bowery Electric": {
				"name": "The Bowery Electric",
				"address": "327 Bowery, New York, NY",
				"lat": 40.7257548,
				"lng": -73.99181909999999
			},
			"Cake Shop": {
				"name": "Cake Shop",
				"address": "152 Ludlow, New York, NY",
				"lat": 40.72083253949573,
				"lng": -73.98790186069586
			},
			"Crash Mansion": {
				"name": "Crash Mansion",
				"address": "199 Bowery, New York, NY",
				"lat": 40.720933,
				"lng": -73.99354199999999
			},
			"The Delancey": {
				"name": "The Delancey",
				"address": "168 Delancey, New York, NY",
				"lat": 40.717827,
				"lng": -73.98530199999999
			},
			"Fat Baby": {
				"name": "Fat Baby",
				"address": "112 Rivington, New York, NY",
				"lat": 40.71996,
				"lng": -73.987774
			},
			"Fontana’s": {
				"name": "Fontana’s",
				"address": "105 Eldridge, New York, NY",
				"lat": 40.718105,
				"lng": -73.99228399999998
			},
			"The Lakeside Lounge": {
				"name": "The Lakeside Lounge",
				"address": "162 Avenue B, New York, NY",
				"lat": 40.7271002,
				"lng": -73.97975489999999
			},
			"The Living Room": {
				"name": "The Living Room",
				"address": "154 Ludlow, New York, NY",
				"lat": 40.7208858,
				"lng": -73.9878534
			},
			"Lucky Jacks": {
				"name": "Lucky Jacks",
				"address": "129 Orchard, New York, NY",
				"lat": 40.7196422,
				"lng": -73.9895596
			},
			"The Mercury Lounge": {
				"name": "The Mercury Lounge",
				"address": "217 E Houston, New York, NY",
				"lat": 40.7221646,
				"lng": -73.9867142
			},
			"Mic-Club": {
				"name": "Mic-Club",
				"address": "129 Orchard, New York, NY",
				"lat": 40.7196422,
				"lng": -73.9895596
			},
			"The National Underground": {
				"name": "The National Underground",
				"address": "159 East Houston, New York, NY",
				"lat": 40.7228996,
				"lng": -73.98905009999999
			},
			"The Parkside Lounge": {
				"name": "The Parkside Lounge",
				"address": "317 East Houston, New York, NY",
				"lat": 40.7210741,
				"lng": -73.98314540000001
			},
			"Pianos NYC": {
				"name": "Pianos NYC",
				"address": "158 Ludlow, New York, NY",
				"lat": 40.721108,
				"lng": -73.987751
			},
			"Rockwood Music Hall": {
				"name": "Rockwood Music Hall",
				"address": "196 Allen, New York, NY",
				"lat": 40.722482,
				"lng": -73.98869289999999
			}
		};
		
		gmap(document.getElementById("map"), markers["Rivington Music"], 16);
		addmarkers(markers);
		
		$(".list a").bind("click", function() { 
			$(".list a.map_selected").removeClass("map_selected");
			$(this).addClass("map_selected")
			
			var name = $(this).text();
			map.panTo(new google.maps.LatLng(markers[name].lat, markers[name].lng));
			markers[name].marker.setAnimation(google.maps.Animation.BOUNCE);
			showInfo(name, $(this).attr("href"));
			
			setTimeout(function() {
				markers[name].marker.setAnimation(null);
			}, 2000);
			
			return false;
		});
		
		$(function() { $(".list a:contains('Rivington Music')").click(); });
	};
	
	function showInfo(name, url) {
		var marker = markers[name];
		var map_info = $("#map_info");
		map_info.fadeTo("slow", 0.8)
		map_info.find("div:first-child").html($("<a>", {"href": url, "target": "_blank"}).text(marker.name));
		map_info.find("div:last-child").text(marker.address);
	}
	
	function addmarkers(markers) {
		for(var key in markers) {
			var opts = {
				title: markers[key].name + ", " + markers[key].address,
            	map: map,
	            position: new google.maps.LatLng(markers[key].lat, markers[key].lng)
	        };
	        
			if(key == "Rivington Music")
				opts['icon'] = new google.maps.MarkerImage("/includes/img/logo.png", null, null, null, new google.maps.Size(80, 22));
			
			markers[key].marker = new google.maps.Marker(opts);
		}
	};
	
	function gmap(element, start, zoom) {
		var	style = [{
				"featureType": "all",
				"elementType": "all",
				"stylers": [{"hue": "#282828"},{"lightness": -20},{"saturation": -100}]
			}, {
				"featureType": "water",
				"elementType": "all",
				"stylers": [{"hue": "#ffffff"},{"lightness": -40},{"saturation": -100}]
			},{
				"featureType": "landscape",
				"elementType": "labels",
				"stylers": [{"visibility": "off"}]
			},{
				"featureType": "landscape",
				"elementType": "all",
				"stylers": [{"hue": "#cccccc"},{"lightness": 0},{"saturation": -50}]
			}],
			latlng = new google.maps.LatLng(start.lat, start.lng),
			opts = {
				"zoom": zoom,
				"center": latlng,
				"disableDefaultUI": true,
				"mapTypeId": google.maps.MapTypeId.TERRAIN,
				"scaleControl": false,
				"scrollwheel": false
			},
			styledMapOptions = {
			  	"map": map
			},
			rmrs =  new google.maps.StyledMapType(style, styledMapOptions);
			
		map =  new google.maps.Map(element, opts);
		map.mapTypes.set('rmrs', rmrs);
		map.setMapTypeId('rmrs');
	};
	
	return {
		"init": init
	};
}();
