//Hits the server to generate the widget in the iframe
//var styleServer = "http://192.168.1.41:8080/widgets"  //development
var styleServer = "http://www.kalendi.com"  //production
function populateWidget(rssURL, imgURL, linkURL, cssURL) {
		var styleSheet = styleServer + "/new_widgets/rss_widget/widget.xsl" 
		cssParam = "param=css:" + encodeURIComponent(cssURL) //link to widget's css
		imgParam = "param=img:" + encodeURIComponent(imgURL) //link to widget's top image
		linkParam = "param=link:" + encodeURIComponent(linkURL) //link from the bottom link (event calendar)
		rssParam = "param=rss:" + encodeURIComponent(rssURL) //url from the rss button
		transParam = "transformOnServer=true" //The transformation from xml->html should take place on the server 
		var loc = location.href
		var hashPos = loc.lastIndexOf("#");
		if (hashPos > 0)
			loc = loc.substr(0,hashPos)
		var locParam = "param=loc:" + encodeURIComponent(loc) //So that the child can properly set the fragment of the parent in all browsers
		styleParam = "extStyleSheet=" + encodeURIComponent(styleSheet)
	  var url = rssURL + "?" + transParam + "&" + styleParam + "&" + cssParam + "&" + imgParam + "&" + linkParam  + "&" + rssParam + "&" + locParam
		var rss_iframe = document.getElementById("rss_widget")
		rss_iframe.src = url //load the widget
}

//parses a string of the form name1=val1&name2=val2...etc. into a hash
function parseQueryString(query) {
   var params = {}
   if (query && query.length > 1) {
      query = query.substr(1) //skip the '#' 
      var pairs = query.split('&')
      for (var i = 0; i < pairs.length; i++) {
         var pair = pairs[i].split('=')
         if (pair.length < 2)
            continue
         params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1])
      }
   }
   return params
}

//looks at the fragment of the iframe src string for the dimensions of the widget 
function checkForMessages(){
	var rss_iframe = document.getElementById("rss_widget")
	var src = self.location.hash
	var fragStart = src.lastIndexOf('#')
	if (fragStart > -1) {
		var frag = src.substr(fragStart)
		var dimensions = parseQueryString(frag)
		var width = dimensions.w
		var height = dimensions.h
	  if (width && height) {
	  	rss_iframe.style.width = width + "px"
	  	rss_iframe.style.height = height + "px"
	  	//no need to poll for the dimensions any longer
	  	window.location.hash = "#"
	  	clearInterval(dim_id)
	  }	  		
	}
}

//poll for the dimensions of the widget
var dim_id = setInterval(checkForMessages, 200);
