//var carousel_ajaxReq = undefined;
var headline_detail_ajaxReq = new AjaxRequest();
var carousel_ajaxReq = new Ajax2Request();
var mp3A_ajaxReq = new Ajax3Request();
var mp3B_ajaxReq = new Ajax4Request();
// Initialise global variables!
// Sad to be using global variables but a mixture of the various browsers implementation of ajax,
// the limitations of JavaScript as a language and the fact that the amazon widgets
// are only designed to be rendered as the page loads - not on the fly when the user
// clicks a headline - as here - means that to work around these factors I cannot 
// change / control I need to use global variables to pass parameters.
// The global variables as a whole represent the state of the system at an given moment in
// time - the basic repeating loop is the cycle of items selected then loaded, another item headline
// selected then loaded etc - so the global variables are re-initialised based on that cycle -
// hense as the toggle function that is fired when a news item is selected this function 
// will re-initialise these global variables each time an item opens.
var $g_current_item_number = 0;
var $g_current_locale = ' ';
var $g_carousel_operating = 0;
var $g_mp3A_operating = 0;
var $g_mp3B_operating = 0;
var $g_amazon_mp3 = 0;

function sleep($naptime){
  $naptime = $naptime * 1000;
  var $sleeping = true;
  var $now = new Date();
  var $alarm;
  var $startingMSeconds = $now.getTime();
  //alert("starting nap at timestamp: " + startingMSeconds + "\nWill sleep for: " + naptime + " ms");
  while($sleeping){
      $alarm = new Date();
      $alarmMSeconds = $alarm.getTime();
      if($alarmMSeconds - $startingMSeconds > $naptime)
      { 
	$sleeping = false;
      }
  }
}

// Get the text content of an HTML element
function getText(elem) {
  var text = "";
  if (elem) {
    if (elem.childNodes) {
      for (var i = 0; i < elem.childNodes.length; i++) {
	var child = elem.childNodes[i];
	if (child.nodeValue)
	  text += child.nodeValue;
	else {
	  if (child.childNodes[0])
	    if (child.childNodes[0].nodeValue)
	      text += child.childNodes[0].nodeValue;
	}
      }
    }
  }
  return text;
}

function simpletoggle($toggler,$toggle_string) {

	if(document.getElementById) {
		$targetElement = $toggler.parentNode.nextSibling;
	}

	if($targetElement.className == undefined) {
		$targetElement = $toggler.parentNode.nextSibling.nextSibling;	
	}

	if($targetElement.className == undefined) {
		$targetElement = $toggler.parentNode.nextSibling.nextSibling.nextSibling;
	}

	if ($targetElement.style.display == "")
	{
		$targetElement.style.display = "block";
	}

	if ($targetElement.style.display == "block")
	{
		$targetElement.style.display = "none";
		$toggler.firstChild.nodeValue = 'Show ' + $toggle_string;
	}
	else
	{
		$targetElement.style.display = "block";
		$toggler.firstChild.nodeValue = 'Hide ' + $toggle_string;
	}
}

function randomString() {
	var $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var $string_length = 8;
	var $randomstring = '';
	for (var $i=0; $i < $string_length; $i++) {
		var $rnum = Math.floor(Math.random() * $chars.length);
		$randomstring += $chars.substring($rnum,$rnum+1);
	}
	return $randomstring;
}

function ajaxcall_load_item_text($item_number,$timeline_item_ref){
        // Send the new blog entry data as an Ajax request
	$version = "Vbbh1029";
	//headline_detail_ajaxReq = new AjaxRequest();
	//alert("ajax_item_text_request no2 timeline_item_ref = " + $timeline_item_ref);
        headline_detail_ajaxReq.send("POST", "hypermap_ajax_detail.php", handle_ajax_item_text_request, "application/x-www-form-urlencoded; charset=UTF-8", "timeline_item_ref=" + escape($timeline_item_ref) + "&item_version=" + escape($version));
        //headline_detail_ajaxReq.send("POST", "test2.xml", handle_ajax_item_text_request($item_number), "application/x-www-form-urlencoded; charset=UTF-8", "timeline_item_ref=" + $timeline_item_ref + "&item_version=" + $version);
	//sleep(1);
}
function handle_ajax_item_text_request(){
$item_number = $g_current_item_number;
//alert("handle_ajax_carousel_request - item_number:" + $item_number + " state:" + carousel_ajaxReq.getReadyState() + " status:" + carousel_ajaxReq.getStatus());
    if (headline_detail_ajaxReq.getReadyState() == 4 && headline_detail_ajaxReq.getStatus() == 200) {
	    //alert("handle_ajax_item_text_request successfully received! no3");
	    // get xml data from ajax call
	    var xmlData = headline_detail_ajaxReq.getResponseXML().getElementsByTagName("text_head")[0];
	    
	    //alert(xmlData.childNodes[0].textContent || xmlData.childNodes[0].text);
	    //var $item_data = getText(xmlData.getElementsByTagName("item_text")[0]);
	    
	    var $item_data = (xmlData.childNodes[0].textContent || xmlData.childNodes[0].text);
	    var $item_text = "item_text_" + $item_number;
	    var $item_text_node = document.getElementById($item_text);
	    // try
	    //$item_text_node.childNodes[0].data = $item_data;
	    //
	    /////$y=$item_text_node.childNodes[0];
	    if ($item_text_node.hasChildNodes())
	    {
	    	$y=$item_text_node.childNodes.item(0);
	    	$item_text_node.removeChild($y);
	    }
	    var newnode = document.createTextNode($item_data);
	    $item_text_node.appendChild(newnode);
	}
}

var wsPreview = undefined;

function timeoutMP3A(){
$item_number = $g_current_item_number;
$locale = $g_current_locale;
    var xmlData = mp3A_ajaxReq.getResponseXML().getElementsByTagName("mp3_head")[0];
    var $amazon_mp3 = getText(xmlData.getElementsByTagName("amazon_mp3")[0]);	      
    var $amazon_mp3_title1 = getText(xmlData.getElementsByTagName("amazon_mp3_title1")[0]);
    var $amazon_mp3_str1 = getText(xmlData.getElementsByTagName("amazon_mp3_str1")[0]);	      
    var $amazon_mp3A_id = "amazon_mp3_" + $item_number + "A";
    var $marketplace = "GB"; // for mp3s the market place is hard coded to GB atm

    //building the $newInnerHTML string
    var $newInnerHTML = new String();
    $newInnerHTML = " var amzn_wdgt={widget:'MP3Clips'};";
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.tag='hypermapcom-21';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.widgetType='SearchAndAdd';");
    var $keywords = " amzn_wdgt.keywords='" + $amazon_mp3_str1 + "';";
    $newInnerHTML = $newInnerHTML.concat($keywords);
    if (($g_amazon_mp3 == 1) || ($g_amazon_mp3 == 3) || ($g_amazon_mp3 == 4)){
	$browsenode = "118609031";
    }
    else
    {
	$browsenode = "118610031";
    }
    $newInnerHTML = $newInnerHTML.concat("amzn_wdgt.browseNode='" + $browsenode + "';"); 
    var $titles = " amzn_wdgt.title='" + $amazon_mp3_title1 + "';";
    $newInnerHTML = $newInnerHTML.concat($titles);
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.width='336';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.height='280';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.outerBackgroundColor='E9E9E9';"); 
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.bgcolor='E9E9E9';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.shuffleTracks='False';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.maxResults='30';");
    var $marketplaces = " amzn_wdgt.marketPlace='" + $marketplace + "'; ";
    $newInnerHTML = $newInnerHTML.concat($marketplaces);
    //alert("MP3 A newInnerHTML =" + $newInnerHTML + " --- no5");
    $node_hook = document.getElementById($amazon_mp3A_id);
    //$node_hook = document.getElementById($node_id);
    $node_id = $node_hook.id;
    //temporarily set the node id to 'preview_section_demo', this is hard coded in the amazon widget js code
    $node_hook.id = 'preview_section_demo';
    $g_last_selected_mp3 = $item_number + "A";

    var sc1=document.createElement("script");
    sc1.type = "text/javascript";
    sc1.text=$newInnerHTML;
    $node_hook.appendChild(sc1);
    wsPreview=true;
    var sc2=document.createElement("script"); 
    sc2.type = "text/javascript";
    sc2.src= "http://wms.assoc-amazon.co.uk/20070822/US/js/swfobject_1_5.js";
    $node_hook.appendChild(sc2);
    //$node_hook.id = $node_id;
    //delaying($node_hook,$node_id);
    $g_carousel_operating = 0;
    $g_mp3A_operating = 0;
    $g_mp3B_operating = 0;
}

function ajaxcall_load_mp3A($item_number,$timeline_item_ref){
        // Send the new blog entry data as an Ajax request
	$version = "Vbbh4673";
	//alert("mp3A_ajaxReq.send no2 timeline_item_ref = " + $timeline_item_ref);
	$g_mp3A_operating = 1;
        mp3A_ajaxReq.send("POST", "hypermap_ajax_mp3.php", handle_ajax_mp3A_request, "application/x-www-form-urlencoded; charset=UTF-8", "timeline_item_ref=" + escape($timeline_item_ref) + "&item_version=" + escape($version));
        //mp3A_ajaxReq.send("POST", "test.xml", handle_ajax_carousel_request($item_number,$locale), "application/x-www-form-urlencoded; charset=UTF-8", "timeline_item_ref=" + $timeline_item_ref + "&item_version=" + $version);
	$g_carousel_operating = 0;
	$g_mp3B_operating = 0;
}
function handle_ajax_mp3A_request(){

//alert("handle_ajax_mp3A_request - item_number:" + $item_number + " state:" + mp3A_ajaxReq.getReadyState() + " status:" + mp3A_ajaxReq.getStatus());
//if (mp3A_ajaxReq.getReadyState() == 4 && mp3A_ajaxReq.getStatus() == 200 && $g_mp3A_operating == 1) {
if (mp3A_ajaxReq.getReadyState() == 4 && mp3A_ajaxReq.getStatus() == 200) {
    setTimeout("timeoutMP3A()",1250);
    //alert("handle_ajax_mp3A_request successfully received! no3");
    // get xml data from ajax call & set variables
}
}

function timeoutMP3B(){
$item_number = $g_current_item_number;
$locale = $g_current_locale;
//alert("mp3B handle_ajax_carousel_request - item_number:" + $item_number + " state:" + mp3B_ajaxReq.getReadyState() + " status:" + mp3B_ajaxReq.getStatus());
//if (mp3B_ajaxReq.getReadyState() == 4 && mp3B_ajaxReq.getStatus() == 200 && $g_mp3B_operating == 1) {
if (mp3B_ajaxReq.getReadyState() == 4 && mp3B_ajaxReq.getStatus() == 200) {
    //alert("mp3B handle_ajax_carousel_request successfully received! no3");
    // get xml data from ajax call

    // Fix the id for this items mp3 A id as this will be preview_section_demo
    if (document.getElementById('preview_section_demo')){
	  $change_element_id = document.getElementById('preview_section_demo');
	  $last_selected_mp3_now = $item_number + "A";
	  if ($g_last_selected_mp3 == $last_selected_mp3_now){
	      //This will almost certainly be executed if there is a B mp3 player,
	      // as a B mp3 player does not exist until immediately after the A player has been 
	      // created.
	      $amazon_mp3_target_id = "amazon_mp3_" + $item_number + "A";
	      if (document.getElementById($amazon_mp3_target_id)){
		  $then_the_status_is = "All is OK";
	      }
	      else
	      {
		  $change_element_id.id = $amazon_mp3_target_id;
	      }
	  }
    }

    var xmlData = mp3B_ajaxReq.getResponseXML().getElementsByTagName("mp3_head")[0];
    var $amazon_mp3 = getText(xmlData.getElementsByTagName("amazon_mp3")[0]);	      
    var $amazon_mp3_title2 = getText(xmlData.getElementsByTagName("amazon_mp3_title2")[0]);
    var $amazon_mp3_str2 = getText(xmlData.getElementsByTagName("amazon_mp3_str2")[0]);	      
    var $amazon_mp3B_id = "amazon_mp3_" + $item_number + "B";
    var $marketplace = "GB"; // for mp3s the market place is hard coded to GB atm

    //building the $newInnerHTML string
    var $newInnerHTML = new String();
    $newInnerHTML = " var amzn_wdgt={widget:'MP3Clips'};";
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.tag='hypermapcom-21';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.widgetType='SearchAndAdd';");
    var $keywords = " amzn_wdgt.keywords='" + $amazon_mp3_str2 + "';";
    $newInnerHTML = $newInnerHTML.concat($keywords);
    if ($g_amazon_mp3 == 4){
	$browsenode = "118609031";
    }
    else
    {
	$browsenode = "118610031";
    }
    $newInnerHTML = $newInnerHTML.concat("amzn_wdgt.browseNode='" + $browsenode + "';"); 
    var $titles = " amzn_wdgt.title='" + $amazon_mp3_title2 + "';";
    $newInnerHTML = $newInnerHTML.concat($titles);
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.width='336';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.height='280';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.outerBackgroundColor='E9E9E9';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.bgcolor='E9E9E9';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.shuffleTracks='False';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.maxResults='30';");
    var $marketplaces = " amzn_wdgt.marketPlace='" + $marketplace + "'; ";
    $newInnerHTML = $newInnerHTML.concat($marketplaces);

    //alert("got MP3B newInnerHTML =" + $newInnerHTML + " --- no5");

    $node_hook = document.getElementById($amazon_mp3B_id);
    //$node_hook = document.getElementById($node_id);
    $node_id = $node_hook.id;
    //temporarily set the node id to 'preview_section_demo', this is hard coded in the amazon widget js code
    $node_hook.id = 'preview_section_demo';
    $g_last_selected_mp3 = $item_number + "B";

    var sc1=document.createElement("script");
    sc1.type = "text/javascript";
    sc1.text=$newInnerHTML;
    $node_hook.appendChild(sc1);
    
    // hard coded in the amazon widget code is:
    /*
    if(typeof wsPreview == 'undefined'){
    document.write(swfObject.getSWFHTML());
    }else{
    document.getElementById("preview_section_demo").innerHTML = swfObject.getSWFHTML();
    }
    hence setting the node id to preview_section_demo and wsPreview=true; enables the else part to work
    --- note wsPreview=true; simply creates a boolean object - it could be wsPreview=false; and this would
    still work as wsPreview would still be created as an object - wsPreview=undefined; kills the wsPreview object
    */
    wsPreview=true;
    var sc2=document.createElement("script"); 
    sc2.type = "text/javascript";
    sc2.src= "http://wms.assoc-amazon.co.uk/20070822/US/js/swfobject_1_5.js";
    $node_hook.appendChild(sc2);
    //$node_hook.id = $node_id;
    //delaying($node_hook,$node_id);
    $g_carousel_operating = 0;
    $g_mp3A_operating = 0;
    $g_mp3B_operating = 0;

}
}

function ajaxcall_load_mp3B($item_number,$timeline_item_ref){
        // Send the new blog entry data as an Ajax request
	$version = "Vbbh4673";
	//alert("handle_ajax_mp3B_request - item_number:" + $item_number + " state:" + mp3A_ajaxReq.getReadyState() + " status:" + mp3A_ajaxReq.getStatus());
	$g_mp3B_operating = 1;
        mp3B_ajaxReq.send("POST", "hypermap_ajax_mp3.php", handle_ajax_mp3B_request, "application/x-www-form-urlencoded; charset=UTF-8", "timeline_item_ref=" + escape($timeline_item_ref) + "&item_version=" + escape($version));
        //mp3B_ajaxReq.send("POST", "test.xml", handle_ajax_carousel_request($item_number,$locale), "application/x-www-form-urlencoded; charset=UTF-8", "timeline_item_ref=" + $timeline_item_ref + "&item_version=" + $version);
	$g_carousel_operating = 0;
	$g_mp3A_operating = 0;
}
function handle_ajax_mp3B_request(){
if (mp3B_ajaxReq.getReadyState() == 4 && mp3B_ajaxReq.getStatus() == 200) {
    setTimeout("timeoutMP3B()",3000);
    //alert("handle_ajax_mp3B_request successfully received! no3");
    // get xml data from ajax call & set variables
}
}

function timeoutCarousel(){
$item_number = $g_current_item_number;
$locale = $g_current_locale;
    if (document.getElementById('preview_section_demo')){
	  $change_element_id = document.getElementById('preview_section_demo');
	  $last_selected_mp3_now = $item_number + "A";
	  if ($g_last_selected_mp3 == $last_selected_mp3_now){
	      //This will almost certainly be executed if there is a B mp3 player
	      $amazon_mp3_target_id = "$amazon_mp3_" + $item_number + "A";
	      if (document.getElementById($amazon_mp3_target_id)){
		  $then_the_status_is = "old mp3A is OK";
	      }
	      else
	      {
		  $change_element_id.id = $amazon_mp3_target_id;
	      }
	  }
	  $last_selected_mp3_now = $item_number + "B";
	  if ($g_last_selected_mp3 == $last_selected_mp3_now){
	      //This will almost certainly be executed if there is a B mp3 player
	      $amazon_mp3_target_id = "$amazon_mp3_" + $item_number + "B";
	      if (document.getElementById($amazon_mp3_target_id)){
		  $then_the_status_is = "old mp3B is OK";
	      }
	      else
	      {
		  $change_element_id.id = $amazon_mp3_target_id;
	      }
	  }
    }

    var xmlData = carousel_ajaxReq.getResponseXML().getElementsByTagName("carousel_head")[0];
    var $title = getText(xmlData.getElementsByTagName("title")[0]);	      
    var $search_string = getText(xmlData.getElementsByTagName("search_string")[0]);	
    var $amazon_carousel_id = "amazon_carousel_" + $item_number + "X";
    var $marketplace = $locale;

    //alert("got xmlData=" + xmlData + " --- no4");

    var $newInnerHTML = new String();
    $newInnerHTML = " var amzn_wdgt={widget:'Carousel'};";
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.tag='hypermapcom-21';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.widgetType='SearchAndAdd';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.searchIndex='All';");
    var $keywords = " amzn_wdgt.keywords='" + $search_string + "';";
    $newInnerHTML = $newInnerHTML.concat($keywords);
    var $titles = " amzn_wdgt.title='" + $title + "';";
    $newInnerHTML = $newInnerHTML.concat($titles);
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.width='600';");
    $newInnerHTML = $newInnerHTML.concat(" amzn_wdgt.height='200';");
    var $marketplaces = " amzn_wdgt.marketPlace='" + $marketplace + "'; ";
    $newInnerHTML = $newInnerHTML.concat($marketplaces);

    //alert("got newInnerHTML =" + $newInnerHTML + " --- no5");

    $node_hook = document.getElementById($amazon_carousel_id);
    //$node_hook = document.getElementById($node_id);
    $node_id = $node_hook.id;
    //temporarily set the node id to 'preview_section_demo', this is hard coded in the amazon widget js code
    $node_hook.id = 'preview_section_demo';
    $g_last_selected_carousel = 1

    var sc1=document.createElement("script");
    sc1.type = "text/javascript";
    //sc1.text="amzn_wdgt={widget:'Carousel'}; amzn_wdgt.tag='hypermapcom-21'; amzn_wdgt.widgetType='SearchAndAdd'; amzn_wdgt.searchIndex='All'; amzn_wdgt.keywords='Pink Floyd'; amzn_wdgt.title='Pink Floyd'; amzn_wdgt.width='600'; amzn_wdgt.height='200'; amzn_wdgt.marketPlace='US';";
    sc1.text=$newInnerHTML;
    $node_hook.appendChild(sc1);
    
    wsPreview=true;
    var sc2=document.createElement("script"); 
    sc2.type = "text/javascript";
    sc2.src= "http://wms.assoc-amazon.co.uk/20070822/US/js/swfobject_1_5.js";
    $node_hook.appendChild(sc2);
    //$node_hook.id = $node_id;
    //delaying($node_hook,$node_id);
    $g_carousel_operating = 0;
    $g_mp3A_operating = 0;
    $g_mp3B_operating = 0;
}

function ajaxcall_load_carousel($item_number,$timeline_item_ref){
        // Send the new blog entry data as an Ajax request
	$version = "Vbbh4673";
	//alert("carousel_ajaxReq.send no2 timeline_item_ref = " + $timeline_item_ref);
	$g_carousel_operating = 1;
        carousel_ajaxReq.send("POST", "hypermap_ajax_carousel.php", handle_ajax_carousel_request, "application/x-www-form-urlencoded; charset=UTF-8", "timeline_item_ref=" + escape($timeline_item_ref) + "&item_version=" + escape($version));
        //carousel_ajaxReq.send("POST", "test.xml", handle_ajax_carousel_request($item_number,$locale), "application/x-www-form-urlencoded; charset=UTF-8", "timeline_item_ref=" + $timeline_item_ref + "&item_version=" + $version);
	$g_mp3A_operating = 0;
	$g_mp3B_operating = 0;
}
function handle_ajax_carousel_request(){

//alert("handle_ajax_carousel_request - item_number:" + $item_number + " state:" + carousel_ajaxReq.getReadyState() + " status:" + carousel_ajaxReq.getStatus());
//if (carousel_ajaxReq.getReadyState() == 4 && carousel_ajaxReq.getStatus() == 200 && $g_carousel_operating == 1) {
if (carousel_ajaxReq.getReadyState() == 4 && carousel_ajaxReq.getStatus() == 200) {
    //alert("handle_ajax_carousel_request successfully received! no3");
    // get xml data from ajax call
    // clean up the possible existance of a preview_section_demo from an amazon mp3 player widget
    setTimeout("timeoutCarousel()",4500);
}
}

function isMSIE() {
	  return '\v' == 'v';
	}


function toggle($toggler,$video_searchstring,$text_searchstring,$video_available,$item_number,$timeline_item_ref,$amazon_carousel_status,$amazon_search,$locale,$amazon_mp3) {

	if ((XMLHttpRequest) && (isMSIE()))
	{
		// Only for Internet Explorer - delay running the code in this function for 2 seconds - ie every time an item is clicked - the reason is that IE 8 will crash with out this delay 
		// as it tries to copy the Amazon carousel before it has been created. Firefox, Chrome, Safari and Opera do not have this problem / operate perfectly - hence the check for the
		// Internet Explorer specific ajax XMLHttpRequest object before calling this sleep function.
		sleep(2);
	}
	//alert ("timeline_item_ref:" + $timeline_item_ref);
	//The called back ajax function cannot be passed parameters!
	//the parameter passed in the ajax send call must be simply the callback function name with no brackets
	//as in:handle_ajax_item_text_request
        //headline_detail_ajaxReq.send("POST", "hypermap_ajax_detail.php", handle_ajax_item_text_request, "application/x-www-form-urlencoded; charset=UTF-8", "timeline_item_ref=" + escape($timeline_item_ref) + "&item_version=" + escape($version));
	//wierd but true! - hence the two following global variables
	$g_current_item_number = $item_number;
	$g_current_locale = $locale;
	$g_amazon_mp3 = $amazon_mp3;
	// Stop all amazon based ajax calls asap
	g_carousel_operating = 0;
	g_mp3A_operating = 0;
	g_mp3B_operating = 0;

	//clean up if a node is left with an id of preview_section_demo from the previously selected item 
	//something which is highly likely - the last ajax'd amazon item will still have preview_section_demo
	//as the id - this needs to be restored to it's original id

	if (document.getElementById('preview_section_demo')){
	      $change_element_id = document.getElementById('preview_section_demo');
	      if ($g_last_selected_carousel == 1){
		  $amazon_carousel_target_id = "amazon_carousel_" + $g_last_selected_item_number + "X";
		  if (document.getElementById($amazon_carousel_target_id)){
		      $then_the_status_is = "amazon_carousel is OK";
		  }
		  else
		  {
		      $change_element_id.id = $amazon_carousel_target_id;
		  }
	      }
	      $last_selected_mp3_now = $g_last_selected_item_number + "A";
	      if ($g_last_selected_mp3 == $last_selected_mp3_now){
		  //This will almost certainly be executed if there is a B mp3 player
		  $amazon_mp3_target_id = "mp3_" + $g_last_selected_item_number + "A";
		  if (document.getElementById($amazon_mp3_target_id)){
		      $then_the_status_is = "old mp3A is OK";
		  }
		  else
		  {
		      $change_element_id.id = $amazon_mp3_target_id;
		  }
	      }
	      $last_selected_mp3_now = $g_last_selected_item_number + "B";
	      if ($g_last_selected_mp3 == $last_selected_mp3_now){
		  //This will almost certainly be executed if there is a B mp3 player
		  $amazon_mp3_target_id = "mp3_" + $g_last_selected_item_number + "B";
		  if (document.getElementById($amazon_mp3_target_id)){
		      $then_the_status_is = "old mp3B is OK";
		  }
		  else
		  {
		      $change_element_id.id = $amazon_mp3_target_id;
		  }
	      }
	}

	$g_last_selected_carousel = 0;
	$g_last_selected_mp3 = 0;

	var $targetElement;

	if(document.getElementById) {
		$targetElement = $toggler.parentNode.nextSibling;
	}
	
	if($targetElement.id == undefined) {
		$targetElement = $toggler.parentNode.nextSibling.nextSibling;
	}

	if($targetElement.id == undefined) {
		$targetElement = $toggler.parentNode.nextSibling.nextSibling.nextSibling;
	}	

	if (document.getElementById("clickheadline")){
		$current_clickheadline_element = document.getElementById("clickheadline");
		$current_clickheadline_element.id = "clickheadline" + randomString()
	}

	if ($targetElement.style.display == "block")
	{
	  $targetElement.style.display = "none";
	  //$current_clickheadline_element = document.getElementById("clickheadline");
	  //$current_clickheadline_element.id = "clickheadline" + randomString();

	}
	else
	{
	
		$toggler.id = 'clickheadline';

		$targetElement.style.display = "block";

		ajaxcall_load_item_text($item_number,$timeline_item_ref);
		
		// The Video ...
		/* Move the YouTube video to the new location also removing it from the old location */ 
		var $videoBar = document.getElementById("videoBar-bar");
		var $newvideoBar = $videoBar.cloneNode( true );
		
		var $expandableid = $videoBar.parentNode;
		$expandableid.removeChild( $videoBar );
		/* Hide the previous draw down display */
		$expandableid.style.display = "none";

		$targetElement.appendChild($newvideoBar);

		$targetElement.style.display = "block";
		//LoadVideoBar("Beatles");
		// If no videos are found for the $video_searchstring then simply no video short cuts are shown
		// So if the db indicates there are no suitable videos passing an empty string instead of the video_searchstring
		if ($video_available == 0){
			$empty_string = "gjhghjughjgyugfhjfhjfyuyfhjfjhf";
			LoadVideoBar($empty_string);
		}
		else
		{
			LoadVideoBar($video_searchstring);			
		}

		//delaying();
		
		// The Search Bar
		/* Move the Search Bar to the new location also removing it from the old location */ 
		$searchBar = document.getElementById("searchControl");
		var $newsearchBar = $searchBar.cloneNode( true );
		var $expandableid = $searchBar.parentNode;
		$expandableid.removeChild( $searchBar );
		$targetElement.appendChild($newsearchBar);
		/* Finished moving search bar */

		$searchResults = document.getElementById("searchResults");
		var $newsearchResults = $searchResults.cloneNode( true );
		var $expandableid = $searchResults.parentNode;
		$expandableid.removeChild( $searchResults );
		$targetElement.appendChild($newsearchResults);

		//amazon
		// amazon_mp3 widget handling
		if ($amazon_mp3 > 0){
		    var $amazon_mp3_id = "amazon_mp3_" + $item_number;
		    var $amazonMp3Player = document.getElementById($amazon_mp3_id);
		    $amazonMp3Player.style.display = "block";
		    // make a copy, remove the original and append the copy to the end
		    //var $newamazonMp3Player = $amazonMp3Player.cloneNode( true );
		    var $expandableid = $amazonMp3Player.parentNode
		    $expandableid.removeChild($amazonMp3Player);
		    $targetElement.appendChild($amazonMp3Player);

		    $amazon_mp3_target_id = "amazon_mp3_" + $item_number + "A";
		    if (document.getElementById($amazon_mp3_target_id)){
			$then_the_status_is = "All is OK";
		    }
		    else
		    {
			if (document.getElementById('preview_section_demo')){
			      $change_element_id = document.getElementById('preview_section_demo');
			      $change_element_id.id = $amazon_mp3_target_id;
			}
		    }
		    //sleep(1);
		    //if (($g_carousel_operating == 0) && ($g_mp3A_operating == 0) && ($g_mp3B_operating == 0)){
			ajaxcall_load_mp3A($item_number,$timeline_item_ref);
		    //}
		    //stop for 1 second to allow time for the mp3 amazon call to complete so freeing up
		    //the preview_section_demo id to be used by the amazon_carousel below!
		    //sleep(1);
		    
		    if ($amazon_mp3 > 2){
			  $amazon_mp3_target_id = "amazon_mp3_" + $item_number + "B";
			  if (document.getElementById($amazon_mp3_target_id)){
			      $then_the_status_is = "All is OK";
			  }
			  else
			  {
			      if (document.getElementById('preview_section_demo')){
				    $change_element_id = document.getElementById('preview_section_demo');
				    $change_element_id.id = $amazon_mp3_target_id;
			      }
			  }
			  //if (($g_carousel_operating == 0) && ($g_mp3A_operating == 0) && ($g_mp3B_operating == 0)){
			      //sleep(1);
			      //alert("amazon_mp3:" + $amazon_mp3 + " Does this ever get called ajaxcall_load_mp3B!");
			      ajaxcall_load_mp3B($item_number,$timeline_item_ref);
			  //}
			  //stop for 1 second to allow time for the mp3 amazon call to complete so freeing up
			  //the preview_section_demo id to be used by the amazon_carousel below!
			  //sleep(2);
		    }
		    
		}
		// amazon_search widget handling 
		//if ($amazon_search == 'None'){
		    var $amazon_search = "amazon_search_" + $g_last_item_number;
		    var $amazonsearchPlayer = document.getElementById($amazon_search);
		    $amazonsearchPlayer.style.display = "block";
		    // make a copy of the last item amazon search widget
		    var $newamazonsearchPlayer = $amazonsearchPlayer.cloneNode( true );
		    var $expandableid = $amazonsearchPlayer.parentNode
		    $expandableid.removeChild($amazonsearchPlayer);
		    $targetElement.appendChild($newamazonsearchPlayer); 
		//}
		//else
		/*{
		    var $amazon_search = "amazon_search_" + $item_number;
		    var $amazonsearchPlayer = document.getElementById($amazon_search);
		    $amazonsearchPlayer.style.display = "block";
		    // make a copy, remove the original and append the copy to the end
		    //var $newamazonsearchPlayer = $amazonsearchPlayer.cloneNode( true );
		    var $expandableid = $amazonsearchPlayer.parentNode
		    $expandableid.removeChild($amazonsearchPlayer);
		    $targetElement.appendChild($amazonsearchPlayer); 
		}*/

		if ($amazon_carousel_status != 'None'){
		    // amazon_carousel widget handling - same as for amazon_mp3 widget above
		    var $amazon_carousel = "amazon_carousel_" + $item_number;
		    var $amazoncarouselPlayer = document.getElementById($amazon_carousel);
		    $amazoncarouselPlayer.style.display = "block";
		    // make a copy, remove the original and append the copy to the end
		    //var $newamazoncarouselPlayer = $amazoncarouselPlayer.cloneNode( true );
		    var $expandableid = $amazoncarouselPlayer.parentNode
		    $expandableid.removeChild($amazoncarouselPlayer);
		    $targetElement.appendChild($amazoncarouselPlayer);

		    $amazon_carousel_target_id = "amazon_carousel_" + $item_number + "X";
		    if (document.getElementById($amazon_carousel_target_id)){
			$then_the_status_is = "All is OK";
		    }
		    else
		    {
			if (document.getElementById('preview_section_demo')){
			      $change_element_id = document.getElementById('preview_section_demo');
			      $change_element_id.id = $amazon_carousel_target_id;
			}
		    }
		    //if (($g_carousel_operating == 0) && ($g_mp3A_operating == 0) && ($g_mp3B_operating == 0)){
			//sleep(1);
			ajaxcall_load_carousel($item_number,$timeline_item_ref);
		    //}
		}
		
		// Hide the previous draw down display
		$expandableid.style.display = "none";

		/* End - Hide the previous draw down display */
		$targetElement.style.display = "block";

		/* Added ignore youtube in the search string */
		//$text_searchstring = $text_searchstring + " -youtube";
		LoadSearchBar($text_searchstring);
	}
	$g_last_selected_item_number = $item_number;
}


