/* requires jquery */

var proxybase = "/oebv2005pr.php?";
var extbase = "http://oebv2005.basketballaustria.at/" 

function loadext(relurlext, containerid) {
    $('div#'+containerid).html('<img src="http://basketballaustria.at/include/spinner_000099.gif" />');
	$.get(proxybase+relurlext, function(responseText, textStatus, XMLHttpRequest) {
		  newhtml = rewrite_hrefs(responseText, containerid);
      	  $('div#'+containerid).html("");
		  $('div#'+containerid).append(newhtml);
	});
}

// rewrites links, refs to images, css and script files
function rewrite_hrefs(html, containerid) {
	// rewrite "a" tags
	newtree = $("<div></div>").html(html);
	$("a", newtree).each(function() {
	  var beforelink = $(this).attr("href");
	  $(this).attr("href", "javascript:loadext('"+beforelink+"', '"+containerid+"')");
	  
	});
	// rewrite image src
	$("img", newtree).each(function() {
		beforesrc = $(this).attr("src");
		if (beforesrc != undefined) {
			$(this).attr("src", extbase+beforesrc);
		};
	});
	// for js scripts
	$("script", newtree).each(function() {
		beforesrc = $(this).attr("src");
		if (beforesrc != undefined) {
			$(this).attr("src", extbase+beforesrc);
		};
	});
	// for css "link" elements
	$("link", newtree).each(function() {
		beforehref = $(this).attr("href");
		if (beforehref != undefined) {
			$(this).attr("href", extbase+beforehref);
		}
	});
	return newtree; 
}

// init ajaxcontainer
$(document).ready(function() {
  i = 0;
  $("a.oebv2005embed").each(function() {
      containerid = "ajaxcontainer"+i;
      i = i + 1;
      oldhref = $(this).attr('href');
      containerref = $('<div id="'+containerid+'"><img src="http://basketballaustria.at/include/spinner_000099.gif" /></div>');
      replacedlinkref = $(this);
      $(this).replaceWith(containerref);
      loadext(oldhref, containerid);
      
  });
});


