$(document).ready(function(){
	if((speed = $.cookie("pachma-speed")) == 2){
		loadHighSpeed();
		      addSpeedLink();
	} else if(speed == 1) {
		//low speed was set, do nothing
		      addSpeedLink();
	} else {
		//speed not set. check speed
		var st = new SpeedTest();
		st.run({
		  onStart: function() {
		    //alert('Before Running Speed Test');
		  }
		  ,onEnd: function(speed) {
		    //alert( 'Speed test complete:  ' + speed.Kbps + ' Kbps');
		    // put your logic here
		    if( speed.Kbps < 200 ){
		      //alert('Your connection is too slow');
		      //loadLowSpeed();
		      $.cookie("pachma-speed", "1", {path:'/'});
		      $.cookie("pachma-speed", "1", {path:'/index.php/'});
		      addSpeedLink();
		    } else {
			loadHighSpeed();
			$.cookie("pachma-speed", "2", {path:'/'});
		      addSpeedLink();
		    }
		  }
		});
	}
});
function loadHighSpeed(){
	$("body").addClass("hispeed");
	$("#header span#headerImage").addClass("hispeed");
	$("#container").addClass("hispeed");
	$("#compass").addClass("hispeed");
}
function loadLowSpeed(){
	$("body").removeClass("hispeed");
	$("#header span#headerImage").removeClass("hispeed");
	$("#container").removeClass("hispeed");
	$("#compass").removeClass("hispeed");
}
function changeSpeed(newspeed){
	//newspeed = 1 for low, 2 for high
	speed = $.cookie("pachma-speed");
	if(newspeed == 1 && speed  == 2){
		loadLowSpeed();
		$.cookie("pachma-speed", "1");
	} else if(newspeed == 2 && speed == 1){
		loadHighSpeed();
		$.cookie("pachma-speed", "2");
	}
	addSpeedLink();
}
function addSpeedLink(){
	if($.cookie("pachma-speed") == 2){
		link="Low-bandwidth";
		title="switch to web for slow connections"
		newspeed = 1;
	} else {
		link="High-bandwidth";
		title="switch to web for high speed connections"
		newspeed = 2;
	}
	if((speedLink = $('#speedLink')).length > 0){
		$(speedLink).html("<p id='speedLink'><a title='"+title+"' href='javascript:changeSpeed("+newspeed+");'>" + link + "</a></p>");
	} else {
		$("#footer").append("<p id='speedLink'><a title='"+title+"' href='javascript:changeSpeed("+newspeed+");'>" + link + "</a>");
		//$("#footer").append("</p>"+"<p><a href='#' onClick='resetcookie();'>" + "RESET" + "</a></p>");
	}
	return false;
}
function resetcookie(){
	$.cookie("pachma-speed", "0", {domain: 'www.pachamamahealing-north.com', path:'/'});
}
var SpeedTest = function() {
  /* 
  From:  http://techallica.com/kilo-bytes-per-second-vs-kilo-bits-per-second-kbps-vs-kbps/
  256 kbps            31.3 KBps
  384 kbps            46.9 KBps
  512 kbps            62.5 KBps
  768 kbps            93.8 KBps
  1 mbps ~ 1000kbps   122.1 KBps
  */
};
SpeedTest.prototype = {
  imgUrl: "script/speedtest.jpg"    // Where the image is located at
  ,size: 59917                // bytes
  ,run: function( options ) {
    
    if( options && options.onStart )
      options.onStart();
  
    var imgUrl = this.imgUrl + "?r=" + Math.random();
    this.startTime = (new Date()).getTime() ;

    var testImage = new Image();
    var me = this;
    testImage.onload = function() { 
      me.endTime = (new Date()).getTime();
      me.runTime = me.endTime - me.startTime;
    
      if( options && options.onEnd )
        options.onEnd( me.getResults() );
    };
    testImage.src = imgUrl; 
  }
  
  ,getResults: function() {
    if( !this.runTime ) 
      return null;
      
    return { 
      runTime: this.runTime
      ,Kbps: ( this.size * 8 / 1024 / ( this.runTime / 1000 ) )
      ,KBps: ( this.size / 1024 / ( this.runTime / 1000 ) )
    };
  }
}
