var service = "http://matchstickmixup.com/galindo/service/";

$.fn.getPrice = function( complex, plan ) {
	var self = this;
	$.support.cors = true;
	url = service+"locations/"+complex+"/plans/"+plan;
	function displayPrice(output) {
		var price = output.price;
		self.text("$ "+price);
	}
	// This is because IE8 is a bitch.
	if ($.browser.msie && parseInt($.browser.version, 10) >= 8 && window.XDomainRequest) {
		var xdr = new XDomainRequest();
		xdr.open("get", url);
		xdr.onload = function() {
			displayPrice($.parseJSON(xdr.responseText));
		}
		xdr.send();
	} else {
		$.ajax(url, {
			crossDomain:true,
			success:function(data) {
				displayPrice($.parseJSON(data));
			}
		});
	}
}

$.fn.getCheapest = function( complex ) {
	var self = this;
	$.support.cors = true;
	url = service+"locations/"+complex+"/plans/cheapest";
	function displayPrice(output) {
		var price = output.price;
		self.text("$ "+price);
	}
	// This is because IE8 is a bitch.
	if ($.browser.msie && parseInt($.browser.version, 10) >= 8 && window.XDomainRequest) {
		var xdr = new XDomainRequest();
		xdr.open("get", url);
		xdr.onload = function() {
			displayPrice($.parseJSON(xdr.responseText));
		}
		xdr.send();
	} else {
		$.ajax(url, {
			crossDomain:true,
			success:function(data) {
				displayPrice($.parseJSON(data));
			}
		});
	}
}

function Time() {
	this.to24hours = function(time) {
		if(this.is12hour(time)) {
			var period = time.slice(-2);
			var arr = time.slice(0, -2).split(":");
			time = period === "pm" ? (parseInt(arr[0])+12).toString() : arr[0];
			return time += ":"+arr[1];
		}
	}

	this.to12hours = function(time) {
		if(this.is24hour(time) && !this.is12hour(time)) {
			var arr = time.split(":");
			var hour = parseInt(arr[0]);
			var period = "am";
			if(hour > 12) {
				time = (hour%12).toString();
				period = "pm";
			} else {
				time = arr[0];
			}
			return time += ":"+arr[1]+period;
		}
	}

	this.is12hour = function(time) {
		return time.match(/^(0?[1-9]|1[0-2]):[0-5]\d(am|pm)$/);
	}

	this.is24hour = function(time) {
		return time.match(/^[0-2]?\d:[0-5]\d$/);
	}
}

function createTimeWidget(day, open, close) {
	var time = new Time();
	var name = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
	var widget = $("<div id='"+name[day]+"' class='hours'>");
	var day = $("<span class='day'>").text(name[day]);
	var str = ": "+time.to12hours(open)+" - "+time.to12hours(close);
	widget.append(day, str);
	
	return widget;
}

function loadTimes(data) {
	var area = $("#hours > .content");
	area.empty();
	for(var i=0; i<data.length; i++) {
		var time = data[i];
		area.append(createTimeWidget(time.day, time.open, time.close));
	}
}

$(document).ready(function() {
	$.support.cors = true;
	url = "http://matchstickmixup.com/galindo/service/locations/WestUniversityGardens/hours"; 
	// This is because IE8 is a bitch.
	if ($.browser.msie && parseInt($.browser.version, 10) >= 8 && window.XDomainRequest) {
		var xdr = new XDomainRequest();
		xdr.open("get", url);
		xdr.onload = function() {
			loadTimes($.parseJSON(xdr.responseText));
		}
		xdr.send();
	} else {
		$.ajax(url, {
			crossDomain:true,
			success:function(data) {
				loadTimes($.parseJSON(data));
			}
		});
	}
});

