//website_root_address = "http://www.canon-eos-450d.co.uk";
//website_root_address = "http://192.168.1.40/goldenriverfarms.co.uk";
website_root_address = "http://www.goldenriverfarms.co.uk";

// This function checks that Ajax is allowed
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;
}

/* An ajax function */
function load_tweets ( div_id, root_dir )
{
	/* save the loading stuff */
	document.getElementById(div_id).innerHTML='<h1 class="title1 border1">Latest Tweets</h1><center><img src = "' + root_dir + '/images/loading.gif" /></center>';

	/* Check that ajax is allowed */
	xmlHttp=GetXmlHttpObject();
	
	/* Define the path to the PHP page */
	var url=website_root_address+"/_modules/tweet-list/load.php";

	/* If successful, display the output in the following function  */
	xmlHttp.onreadystatechange=function () { complete_load_tweets ( div_id ) };

	/* Send the ajax call */
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

/* Function which catches the return */
function complete_load_tweets ( div_id )
{                        
	/* If the page has been successful and has loaded, display the output into #contentbox */
	if (xmlHttp.readyState==4)
        {
		document.getElementById(div_id).innerHTML=xmlHttp.responseText;
	}
}

/* An ajax function */
function load_homepage_tweets ( div_id, root_dir )
{
	/* save the loading stuff */
	document.getElementById(div_id).innerHTML='<h2 class="title1 border1">Latest Tweet <span></span></h2><class="tLarge mbtm20 border2"><center><img src = "' + root_dir + '/images/loading.gif" /></center></p>';

	/* Check that ajax is allowed */
	xmlHttp=GetXmlHttpObject();
	
	/* Define the path to the PHP page */
	var url=website_root_address+"/_modules/tweet-item/load.php";

	/* If successful, display the output in the following function */
	xmlHttp.onreadystatechange=function () { complete_load_homepage_tweets ( div_id ) };
	
	/* Send the ajax call */
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

/* Function which catches the return */
function complete_load_homepage_tweets ( div_id )
{                        
	/* If the page has been successful and has loaded, display the output into #contentbox */
	if (xmlHttp.readyState==4)
        {
		document.getElementById(div_id).innerHTML=xmlHttp.responseText;
	}
}

/* Function which will add an item to the shopping cart */
function add_to_cart ( product_id, qty )
{
	/* set up the ajax */
	cart_xmlHttp = GetXmlHttpObject ( );
	var url=website_root_address+"/add-to-cart.php?pid="+product_id+"&qty="+qty;
	cart_xmlHttp.onreadystatechange=complete_add_to_cart;
	cart_xmlHttp.open("GET",url,true);
	cart_xmlHttp.send(null);
}

/* Function which completes adding to cart */
function complete_add_to_cart ( )
{
	if ( cart_xmlHttp.readyState==4 )
	{
		/* split up the things returned */
		var return_list = cart_xmlHttp.responseText.split ( "|" );
		if ( return_list[0] == "success" )
		{
			/* update the cart */
			document.getElementById('scrollMe').innerHTML = return_list[1];
			document.getElementById('cart_total').innerHTML = "&pound;" + return_list[2];
			//document.getElementById('scrollMe').scrollTop = document.getElementById('scrollMe').scrollHeight;
			/* scroll to top of page and fade in cart */

			if ( document.getElementById('allow_scroll').value == "true" )
			{
				document.getElementById('allow_scroll').value = "false";
				scroll_to_top_show_cart ( );
			}
		}
		check_cart_scroller ( );
	}
	
}

/* Function which will step a scroll to the top of the page */
function scroll_to_top_show_cart_old ( current_step )
{
	var just_starting = false;
	if ( current_step == null )
	{
		just_starting = true;
		current_step = 1;
	}

	var step_increase = 4;
	var step_max = 40;
	var wait_timer = 50;
	var slow_at = 0;
	for ( i=0;i<step_max;i+=step_increase )
		slow_at += i;
	slow_at += step_max;

	window.scrollBy(0,(0-current_step));

	if (window.pageYOffset)
		ScrollTop = window.pageYOffset;
	else
		ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;


	if ( current_step < step_max && ScrollTop > slow_at )
		current_step += step_increase;
	else if ( ScrollTop < slow_at )
		current_step -= step_increase;

      //alert ( "scroll top = "+ScrollTop+"\nstep_max = "+step_max+"\ncurrent step = "+current_step+"\nslow at = "+slow_at );
	if ( ScrollTop > step_max && current_step > 0 )
		setTimeout ( "scroll_to_top_show_cart ( " + current_step + " );", wait_timer );
	else
		show_cart ( );
}

function scroll_to_top_show_cart ( )
{

	var step = 50;    // 30
	var wait_timer = 5; //50

	window.scrollBy(0,(0-step));


	if (window.pageYOffset)
		ScrollTop = window.pageYOffset;
	else
		ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;


	if ( ScrollTop > 5 )
		setTimeout ( "scroll_to_top_show_cart ( );", wait_timer );
	else
		show_cart ( );
}

/* Function which will fade in the shopping cart */
function show_cart ( )
{
  if ( document.getElementById('cart1').style.opacity < 1 )
  {
    opacity('cart1',0,100,500);
  }
}

function opacity(id, opacStart, opacEnd, millisec) {
    /* speed for each frame */
    var speed = Math.round(millisec / 100); 
    var timer = 0; 
    document.getElementById(id).style.display='block';
    /* determine the direction for the blending, if start and end are the same nothing happens */
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

/* change the opacity for different browsers */
function changeOpac(opacity, id) { 
    //window.scrollBy(0,-1);
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}


/* ----------------------------------------------------------------------- */
/*                          MENU HOVER FADE CODE                           */
/* ----------------------------------------------------------------------- */

/* global settings */
  var step = 10;
  var timer = 43;

/* start the fade */
  function start_fade ( divname  )
  {

    // check for span
    if ( !document.getElementById(divname + '_span') )
    {
      // span doesnt exist, create it
      document.getElementById(divname).innerHTML = document.getElementById(divname).innerHTML + '<span id = "' + divname +'_span"></span><input id = "' + divname +'_txt" type = "hidden" name = "" />';
    }
    // check for hidden text box
    if ( !document.getElementById(divname + '_txt') )
    {
      // span doesnt exist, create it
      document.getElementById(divname).innerHTML = document.getElementById(divname).innerHTML + '<input id = "' + divname +'_txt" type = "hidden" name = "" />';
    }


    document.getElementById(divname + "_txt" ).value = "fade in";
    fade_in ( divname + "_span", divname + "_txt" );

  }

/* end the fade */
  function end_fade ( divname  )
  {

    document.getElementById(divname + "_txt").value = "fade out";
    fade_out ( divname + "_span", divname + "_txt" );

  }

/* fade in one step */
  function fade_in ( divname, inputname )
  {

    if ( document.getElementById(inputname).value == "fade in" )
    {
      var opacity = ( 100 * document.getElementById(divname).style.opacity );


      var new_opacity = ( 0 + opacity + step );



      document.getElementById(divname).style.opacity = ( new_opacity / 100 );
      document.getElementById(divname).style.MozOpacity = ( new_opacity / 100 );
      document.getElementById(divname).style.KhtmlOpacity = ( new_opacity / 100 );
      document.getElementById(divname).style.filter = "alpha(opacity=" + new_opacity + ")";

      if ( new_opacity < 100 )
      {
        setTimeout ( "fade_in('" + divname + "', '" + inputname + "')", timer );
      }
      else
      {
        document.getElementById(inputname).value = '';
      }

    }

  }

/* fade out one step */
  function fade_out ( divname, inputname )
  {

    if ( document.getElementById(inputname).value == "fade out" )
    {
      var opacity = ( 100 * document.getElementById(divname).style.opacity );



      var new_opacity = ( 0 + opacity - step );



      document.getElementById(divname).style.opacity = ( new_opacity / 100 );
      document.getElementById(divname).style.MozOpacity = ( new_opacity / 100 );
      document.getElementById(divname).style.KhtmlOpacity = ( new_opacity / 100 );
      document.getElementById(divname).style.filter = "alpha(opacity=" + new_opacity + ")";

      if ( new_opacity > 0 )
      {
        setTimeout ( "fade_out('" + divname + "', '" + inputname + "')", timer );
      }
      else
      {
        document.getElementById(inputname).value = '';
      }

    }

  }


/* ----------------------------------------------------------------------- */
/*                           CART DIV SCROLLER                             */
/* ----------------------------------------------------------------------- */

/* global variables */
var scrollstep = 1;
var go_stop = "stop";

/* start the scroll down */
function start_scroll_down ( )
{
  go_stop = "go";
  scroll_step ( "down" );
}

/* start the scroll up */
function start_scroll_up ( )
{
  go_stop = "go";
  scroll_step ( "up" );
}

/* stop the scroll */
function stop_scroll ( )
{
  go_stop = "stop";
}

/* scroll one step */
function scroll_step ( direction )
{
  if ( go_stop == "go" )
  {

    if ( direction == "down" )
      document.getElementById('scrollMe').scrollTop += scrollstep;
    else
      document.getElementById('scrollMe').scrollTop -= scrollstep;
    setTimeout ( "scroll_step('"+direction+"')", 2 );

  }
}

/* check if we need to show the scroll bar on the cart */
function check_cart_scroller ( )
{
  if ( document.getElementById('scrollMe').scrollHeight>80 )
    document.getElementById('cartScroller').style.display='block';
}

/* this function will show/hide the info box next to the add to cart buttons */
function show_hide_info ( div_id )
{
  opacity(div_id,0,100,500);
  setTimeout('opacity(\'' + div_id + '\',100,0,500)',3700);
  setTimeout ( 'check_show_hide_info(\''+div_id+'\')', ( timer * 4 ) );
}

/* this function will check if the box has been hidden */
function check_show_hide_info ( div_id )
{
  var opacity = ( 100 * document.getElementById(div_id).style.opacity );
  if ( opacity < 5 )
    document.getElementById(div_id).style.display='none';
  else
    setTimeout ( 'check_show_hide_info(\''+div_id+'\')', timer );
}


/* ----------------------------------------------------------------------- */
/*                           CHECK CART CHANGES                            */
/* ----------------------------------------------------------------------- */

function check_cart ( num, original_val, current_val )
{
  if ( original_val == current_val )
    document.getElementById('item-update-'+num).style.display='none';
  else
    document.getElementById('item-update-'+num).style.display='block';
}


/* ----------------------------------------------------------------------- */
/*                   SWAP OUT EACH CHECK BOX FOR IMAGES                    */
/* ----------------------------------------------------------------------- */

function swap_check_pics ( )
{
  for(i=0;i<document.getElementById('cart2').elements.length;i++)
  {
    if ( document.getElementById('cart2').elements[i].type=="checkbox" )
    {
      document.getElementById('cart2').elements[i].style.display="none";
      document.getElementById(document.getElementById('cart2').elements[i].id+'-lbl').innerHTML='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    }
  }
}

function checkbox_changed(chk_id)
{
  root_dir = "http://192.168.1.40/goldenriverfarms.co.uk";
  
  var version = getInternetExplorerVersion ( );
  if ( version > 0 && version < 7 )
  {
    if ( document.getElementById(chk_id).checked )
      document.getElementById(chk_id).checked = false;
    else
      document.getElementById(chk_id).checked = true;
  }

  if ( document.getElementById(chk_id).checked )
    document.getElementById(chk_id+"-lbl").style.background='url(' + root_dir + '/images/radio-check1.jpg) center center no-repeat';
  else
    document.getElementById(chk_id+"-lbl").style.background='url(' + root_dir + '/images/radio-check2.jpg) center center no-repeat';
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}


/* ----------------------------------------------------------------------- */
/*                       FADE IN/OUT OVERLAY IMAGES                        */
/* ----------------------------------------------------------------------- */

function show_popup_photo ( photo_div_id )
{
  document.getElementById(photo_div_id).style.display='block';
  document.getElementById('photo_fade').value = 'fade in';
  fade_in ( photo_div_id, 'photo_fade' );
  return false;
}

function hide_popup_photo ( photo_div_id )
{
  document.getElementById('photo_fade').value = 'fade out';
  fade_out ( photo_div_id, 'photo_fade' );
  setTimeout ( 'test_hide_popup_photo(\''+photo_div_id+'\')', timer );
  return false;
}

function test_hide_popup_photo ( photo_div_id )
{
  var opacity = ( 100 * document.getElementById(photo_div_id).style.opacity );
  if ( opacity < 5 )
    document.getElementById(photo_div_id).style.display='none';
  else
    setTimeout ( 'test_hide_popup_photo(\''+photo_div_id+'\')', timer );
}