/* Loads style sheets for dynamic content */
document.write("<link rel=\"stylesheet\" type=\"text/css\" media=\"screen,projection\" href=\"/site/css/inet_dynamic.css\" />");

function adjustSpots() {
	var maxh = 0;
	var arrSpots = $("#newsspots div.newsspot");
	if (arrSpots.length > 0) {
		$.each(arrSpots, function(i, n) {
			var h = $(n).height();
			maxh = (maxh < h ? h : maxh);
		});

		$.each(arrSpots, function(i, n) {
			var d = maxh - $(n).height();
			if (d > 0) {
				$(n).find("div.content").css("padding", "0 0 "+ d +"px 0"); 	
			}
		});
	}
}


$.urlParam = function(name){
	var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
	if (results){
		return results[1] || "";
	}
	else {
		return "";
	}
}


function checkTopEvents(cb) {
	var id = $.urlParam("ref");
	if ((id != "") && $.jsonp) {

		$.jsonp({
			url : "http://www.kulturspot.dk/site/scripts/event.json.asp",	
			data : { id : id },
			callbackParameter : "callback",
			error : function() {
				if ($.isFunction(cb)) {
					cb();
				}
			},
			success : function(data, textStatus) {
				$("#events").html(data.s);
				if ($.isFunction(cb)) {
					cb();
				}
			}
		});
	}
	else {
		if ($.isFunction(cb)) {
			cb();
		}
	}
};

function initTopEvents() {
	var objDiv = $("#events").get(0);
	if (objDiv) {
		var id = "";
		var objSubDiv = $(objDiv).children("div:first").get(0);
		if (objSubDiv) {
			id = $(objSubDiv).attr("id").replace("events-", "");

			$.each($(objDiv).find("a"), function(i, n) {
				var href = $(n).attr("href");
				href += (href.indexOf("?") > -1 ? "&" : "?") + "ref=" + id;

				$(n).attr("href", href);
			});
		}
	}
}

$(document).ready(function() {
	adjustSpots();

	checkTopEvents(function() {
		initTopEvents();	
	});
});
