/* *********************************************** */
/*  this function will return an array of all      */
/*  elements with the specified class              */
/* *********************************************** */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* *********************************************** */
/*  ajax build function  */
/* *********************************************** */
function GetXmlHttpObject() {
	
	var xmlHttp=null;
	
	try {
		xmlHttp=new XMLHttpRequest();
	}
	
	catch (e) {
		
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return xmlHttp;
}

/* *********************************************** */
/*  show/hide the text in send us your facts form  */
/* *********************************************** */
function set_show_hide_fields ( )
{
	// find an array of all elements
	element_list = getElementsByClass('show-hide-text');
	// loop through all elements
	for (i=0;i<element_list.length;i++)
	{
		current_text = element_list[i].value;
		element_list[i].onfocus = function ()
		{
			if(this.value==current_text){this.value='';}
		};
		element_list[i].onblur = function ()
		{
			if(this.value==''){this.value=current_text;}
		};
	}
}

/* *********************************************** */
/*  replace h2 text with image equivilant  */
/* *********************************************** */
var heading_element_list = [];
function replace_headings_with_images ( )
{
  heading_element_list = document.getElementsByTagName('h2');
  replace_single_heading_with_image ( 0 );
}
function replace_single_heading_with_image ( element_no )
{
	if ( heading_element_list.length > element_no )
	{                                                                                              
		heading_text = heading_element_list[element_no].innerHTML;
		preload_image = new Image ();
		preload_image.onload = function ( ) { replace_single_heading_with_image_success ( element_no ); };
		preload_image.onerror = function ( ) { replace_single_heading_with_image_fail ( element_no ); };
		preload_image.src = 'http://www.davidcameronfacts.co.uk/images/title-'+heading_text.replace(/\s/g,'-')+'.png';
	}
}
function replace_single_heading_with_image_success ( element_no )
{
	heading_text = heading_element_list[element_no].innerHTML;
	heading_element_list[element_no].innerHTML = '<img src = "http://www.davidcameronfacts.co.uk/images/title-'+heading_text.replace(/\s/g,'-')+'.png" title = "'+heading_text+'" alt = "'+heading_text+'" />';
	replace_single_heading_with_image ( ( element_no + 1 ) );
}
function replace_single_heading_with_image_fail ( element_no )
{
	replace_single_heading_with_image ( ( element_no + 1 ) );
}

/* *********************************************** */
/*  replace number text with image equivilant  */
/* *********************************************** */
var number_element_list = [];
function replace_numbers_with_images ( )
{
  number_element_list = getElementsByClass('number');
  replace_single_number_with_image ( 0 );
}
function replace_single_number_with_image ( element_no )
{
	if ( number_element_list.length > element_no )
	{                                                                                              
		number_text = number_element_list[element_no].innerHTML;
		preload_image = new Image ();
		preload_image.onload = function ( ) { replace_single_number_with_image_success ( element_no ); };
		preload_image.onerror = function ( ) { replace_single_number_with_image_fail ( element_no ); };
		preload_image.src = 'http://www.davidcameronfacts.co.uk/images/numbers-'+number_text.replace(/\s/g,'-')+'png';
	}
}
function replace_single_number_with_image_success ( element_no )
{
	number_text = number_element_list[element_no].innerHTML;
	number_element_list[element_no].innerHTML = '<img src = "http://www.davidcameronfacts.co.uk/images/numbers-'+number_text.replace(/\s/g,'-')+'png" title = "'+number_text+'" alt = "'+number_text+'" />';
	replace_single_number_with_image ( ( element_no + 1 ) );
}
function replace_single_number_with_image_fail ( element_no )
{
	replace_single_number_with_image ( ( element_no + 1 ) );
}

/* *********************************************** */
/*  these functions are used for the widget code   */
/* *********************************************** */
function build_wiget_code_output ( )
{
  
  /* get the data from all fields */
  if      ( document.getElementById('fact-type-fact') && document.getElementById('fact-type-fact').checked ) { fact_type = 'fact'; }
  else if ( document.getElementById('fact-type-fotd').checked ) { fact_type = 'fotd'; }
  if      ( document.getElementById('fact-type-random').checked ) { fact_type = 'random'; }    
  widget_width = document.getElementById('widget-width').value;                                  
  widget_height = document.getElementById('widget-height').value;                                  
  fact_id = document.getElementById('this-fact-id').value;
  homepage_title = document.getElementById('homepage-meta-title').value;

  if ( widget_width == '500' ) { widget_width = ''; }
  if ( widget_height == '90' ) { widget_height = ''; }

  /* build the script string */
  additional_string = '';
  if ( fact_type != 'random' )
    additional_string = additional_string+"&s="+fact_type;
  if ( fact_type == 'fact' )
    additional_string = additional_string+"&f="+fact_id;
  if ( widget_width != '' )
    additional_string = additional_string+"&w="+widget_width;
  if ( widget_height != '' )
    additional_string = additional_string+"&h="+widget_height;
  if ( homepage_title != '' )
    homepage_title = 'Learn more about David Cameron - The Conservative Party Leader - Election Fun Website - Should be in the Conservative Party Election Manifesto';

  if ( additional_string != '' )
    additional_string = '?'+additional_string.substring(1);

  output_script = '<script src = "http://www.davidcameronfacts.co.uk/widget/david-cameron-facts.js'+additional_string+'"></script><noscript><a href = "http://www.davidcameronfacts.co.uk" title = "'+homepage_title+'">Visit David Cameron Facts for your daily fix of facts about the man</a></noscript>';

  document.getElementById('embed-script-output').value = output_script;

}
if ( document.getElementById('fact-type-random') )
{

  /* add the events to all inputs */
  if ( document.getElementById('fact-type-fact') )
    document.getElementById('fact-type-fact').onchange = build_wiget_code_output;
  document.getElementById('fact-type-fotd').onchange = build_wiget_code_output;
  document.getElementById('fact-type-random').onchange = build_wiget_code_output;
  document.getElementById('widget-width').onkeyup = function ( ) {
    if ( !isNaN ( parseFloat ( this.value.substring ( this.value.length-1 ) ) ) )
    {
      build_wiget_code_output ( );
    }else{
      this.value = this.value.substring ( 0, ( this.value.length-1 ) )
    }
  };
  document.getElementById('widget-height').onkeyup = function ( )  {
    if ( !isNaN ( parseFloat ( this.value.substring ( this.value.length-1 ) ) ) )
    {
      build_wiget_code_output ( );
    }else{
      this.value = this.value.substring ( 0, ( this.value.length-1 ) )
    }
  };
  document.getElementById('embed-script-output').onclick = function() { document.getElementById('embed-script-output').select(); };
  document.getElementById('show-share-blog-advanced').onclick = function () { document.getElementById('advanced-output').style.display='block';return false; };

  build_wiget_code_output ( ); 

  document.getElementById('hide-share-on-my-blog').onclick = function() {
    document.getElementById('show-share-on-my-blog').style.display = 'block';
    document.getElementById('hide-share-on-my-blog').style.display = 'none';
    return false;
  }
  document.getElementById('hide-share-on-my-blog').style.display='block';
}

/* *********************************************** */
/*               save a user's vote                */
/* *********************************************** */
all_affected_votes = [];
function save_vote ( span_id, user_choice )
{
	/* get list of spans that are affected */
	all_affected_votes = getElementsByClass(span_id);
	for ( i=0;i<all_affected_votes.length;i++)
	{
		all_affected_votes[i].innerHTML = "Wait!";
	}
	voting_http = GetXmlHttpObject ();
	url = 'http://www.davidcameronfacts.co.uk/_inc/save-vote.php?fact=' + span_id.substring ( 5 ) + '&vote=' + user_choice;
	url  =url + "&sid=" + Math.random();
	voting_http.onreadystatechange = function() { complete_save_vote ( span_id ); };
	voting_http.open ( "GET", url, true );
	voting_http.send ( null );
}

function complete_save_vote ( span_id )
{
	if ( voting_http.readyState == 4 )
        {
		for ( i=0;i<all_affected_votes.length;i++)
		{
			all_affected_votes[i].innerHTML = "Thanks";
		}
	}
}

/* *********************************************** */
/*  set document onload                            */
/* *********************************************** */
window.onload = function ( )
{       /*
	replace_headings_with_images ( );
	replace_numbers_with_images ( );
	set_show_hide_fields ( );   */
};        

	replace_headings_with_images ( );
	replace_numbers_with_images ( );
	set_show_hide_fields ( );
