// this script fills an array with small ads taken from a given folder,
// calcutates any free space down the sides of long pages and places the ads accordingly
// written by Pat Edison 2008. All rights reserved.

var extra_ads = [];// array holding ads to stick in any free space at the sides


	function get_side_ads(){setTimeout("place_ads()",1000)}; // delay to allow DOM to seat so we can get offsetHeights
	
	
	function place_ads(){
	extra_ads.sort(function() {return 0.5 - Math.random()}) // Randomise array
	var right_column_height = document.getElementById('right_column').offsetHeight;// height of right column
	var left_column_height = document.getElementById('left_column').offsetHeight;// height of right column
	var extra_content_right = document.getElementById('extra_content_right');// div in which we place our ads
	var extra_content_left = document.getElementById('extra_content_left');// div in which we place our ads
	for(var i=0; i<extra_ads.length; i++){extra_ads[i].imageobj.used = false;}// re-set for new page

	var available_space_right = (right_column_height - extra_content_right.offsetTop);

	var available_space_left = (left_column_height - extra_content_left.offsetTop);

	// now get and place our ads until space runs out
	for(var i=0; i<extra_ads.length; i++){

	if (i % 2){
	    // we go right
		available_space_right -= find_suitable_ad(available_space_right, extra_content_right);  // gets and places ad then returns its height
		}else{
		// we go left
		available_space_left -= find_suitable_ad(available_space_left, extra_content_left);  // gets and places ad then returns its height
		}
	
      }
	
	}
	
	function find_suitable_ad(space, column){
	for(var i=0; i<extra_ads.length; i++){
	var this_height = extra_ads[i].imageobj.its_height;
	if((this_height <= space) && (extra_ads[i].imageobj.used == false)){
	extra_ads[i].imageobj.used = true; // tick it off as used
			var mydiv = document.createElement("div");
			mydiv.style.cssText = "margin-top: 2px; margin-bottom: 0;";
			if (extra_ads[i].imageobj.fromserver){
			// we need to get HTML from the server
			if (extra_ads[i].imageobj.url) mydiv.onclick = function(){parse_menu_item(extra_ads[i].imageobj.url, false);};// attach onclick
			if (extra_ads[i].imageobj.url) mydiv.onmouseover = function(){this.style.cursor="pointer";}; // and pointer
			get_ads_from_server(extra_ads[i].imageobj.src, mydiv);
			}else{
			// its an image already in object array
			mydiv.appendChild(extra_ads[i].imageobj);
			}
			column.appendChild(mydiv);
			return this_height;
		    }
	  }
			return 0; // no dice
  
	}
	
	
	/* this is quite complicated and could be improved
	   some of the ads are simply images, in which case we attach
	   onclicks to their image object here. The rest are HTML which need to be
	   fetched from the server and have their onclicks attached when the ad
	   is placed in the page - see find_suitable_ad() function above
	*/
	
	function fill_ads_array(){
		
	var ads = [
	{source: "web_shop_ad.png", height: 220, html: false, url: "", externalURL: "http://rcsocietysales.co.uk/"},		   
	{source: "top15_ad.jpg", height: 220, html: false, url: "top15_page.html", externalURL: ""},
	{source: "piggy_bank_ad.jpg", height: 193, html: false, url: "support_group.html", externalURL: ""},
	{source: "todd_slaughter.html", height: 193, html: true, url: "", externalURL: ""},
	//{source: "advertising.html", height: 220, html: true, url: "advertising.html", externalURL: ""},
	//{source: "CE20_speakers_ad.jpg", height: 250, html: false, url: "", externalURL: ""},
	{source: "daily_quiz_teaser_160.jpg", height: 150, html: false, url: "quiz.html", externalURL: ""}
			  ]
	//make array of ad objects
	for(var i=0; i<ads.length; i++){
	extra_ads.push(new make_ad_obj(ads[i]));
	}
	}

	
	
	// make obj for each ad so we can easily get its url and height
	function make_ad_obj(ad){
	this.imageobj = new Image(); // used as obj to carry other properties
	this.imageobj.src = 'side_ads/'+ad.source;
	if (ad.html){
	// its HTML - we'll need to get it later from the server
	this.imageobj.fromserver = true;
	this.imageobj.url = ad.url; 
	}else{
	this.imageobj.fromserver = false; // it's a simple image
	}
	this.imageobj.its_height = ad.height;
	this.imageobj.used = false;
	if (!ad.html) {
	this.imageobj.onmouseover = function(){this.style.cursor="pointer";};
		if (ad.externalURL){
		this.imageobj.onclick = function(){parse_menu_item(false, ad.externalURL);};// attach onclick
		}else{
		this.imageobj.onclick = function(){parse_menu_item(ad.url, false);};// attach onclick	
		}
	}
	}
	
	// delete existing content
	function delete_ads(){
	var extra_content_left = document.getElementById('extra_content_left');// div in which we place our ads
	deleteprior(extra_content_left);// delete any existing content
	var extra_content_right = document.getElementById('extra_content_right');// div in which we place our ads
	deleteprior(extra_content_right);// delete any existing content
	}
	
	
	//this gets ads set up as HTML
	function get_ads_from_server(url,div){
	var ajaxRequest = setupAJAX();
  	bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
	ajaxRequest.open("GET",url+bustcacheparameter, true);
	//read returned data from server and place it in divs
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
		if (ajaxRequest.status == 200) {
		    div.innerHTML = ajaxRequest.responseText;
			}
	      }
	    }
	   ajaxRequest.send(null); 
	  }
	  
	function log(text){
	var logDiv=document.getElementById("logDiv");
	//if (delete_me) logDiv.innerHTML = "";
	var mytextnode = document.createTextNode(text);
	logDiv.appendChild(mytextnode);// append
	var br = document.createElement("br"); 
	logDiv.appendChild(br);// append
	}