// cookie handling
//if(document.cookie != undefined){
//document.cookie = 'score=100; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'
//}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    
	return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
  ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  ((path == null) ? "" : ("; path=" + path)) +
  ((domain == null) ? "" : ("; domain=" + domain)) +
  ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function savegold(){
  var count = GetCookie('count');
  //alert(count);

  if(count == null) {
    SetCookie('count','250')
    return 1
    }
    else {
    var newcount = document.getElementById("total_gold").value;
	
    DeleteCookie('count')
    SetCookie('count',newcount,exp)
    return count
  }
}


function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
  endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
/*
function printOut() {
  document.write("You've been here <b>" + amt() + "</b> times.");
}
*/




// core system controls
function resetPrefs(){
	//showInformation('<br> <br>Resetting preferences...',true, 1, true); // text, fade in, extraicon, closebutton	
	corereset = true;
	resetHolders();
	holded = 1;
	rolling = false;
	fruitcount[2] = 0; // reset strawberries
	
	if(tempgold < startgold || resetfix == false){
		tempgold = startgold;
		document.getElementById("total_gold").value	 =  tempgold;
	}
	betvalue = 10;
	currentspeed = 1;
	currencyselection = 4;
	roll_count = 0;

	personality = true;
	document.getElementById('personalityswitch').style.backgroundImage = 'none';	
	// timeout
	
	
	document.getElementById('resetprefs').style.backgroundImage = 'url(images/switch_up.png)';	
	switchani = setTimeout("resswitchback()",350);

	var dropdown = document.getElementById('currencyChanger');
	dropdown.selectedIndex = 4;			
	
	var dropdown = document.getElementById('autoUpdates');
	dropdown.selectedIndex = 0;

	var dropdown = document.getElementById('speedChanger');
	dropdown.selectedIndex = 1;				
	
	
	if(window.widget){
		showInformation('<br> <br>Fruitz has been reset...',true, 1, true); // text, fade in, extraicon, closebutton	
		extra_anitimer = setTimeout("extra_fadeout()", 1000);
		savePrefs();
		getPrefs();	
	}else{
		showInformation('<br> <br>Not a widget, so no save...',true, 1, true); // text, fade in, extraicon, closebutton	
		extra_anitimer = setTimeout("extra_fadeout()", 1000);
	}
	
}


function setSpeed(){
	var dropdown = document.getElementById('speedChanger');
    var myindex  = dropdown.selectedIndex;
    var SelValue = dropdown.options[myindex].value;

	currentspeed = SelValue;
	max_counter_d = speeds[currentspeed];
	
	if(initialized == false){

	}else {
	setMessage('Speed changed to '+dropdown.options[myindex].text);			
	}
	setMessage('Speed changed to '+dropdown.options[myindex].text);	
	
//	document.getElementById("currency_overlay").innerHTML  =currency[currencyselection];
//	savePrefs()	;
}


function setCurrency(){
	//currency_overlay
	
	var dropdown = document.getElementById('currencyChanger');
    var myindex  = dropdown.selectedIndex;
    var SelValue = dropdown.options[myindex].value;
	currencyselection = SelValue;
	
	if(initialized == false){
		initialized = true;	
	}else {
		setMessage('Currency changed to '+currency[currencyselection])			
	}
	
	document.getElementById("currency_overlay").innerHTML  =currency[currencyselection];
	savePrefs()	;
}


function setAutoUpdate(){
	//currency_overlay
//	alert('set auto update')
	var dropdown = document.getElementById('autoUpdates');
    var myindex  = dropdown.selectedIndex;
    var SelValue = dropdown.options[myindex].value;
	update_selection = SelValue;
	//alert('update_selection: '+update_selection)
	if(initialized == false){
		if(update_selection == 'false'){
			setMessage('Im really paranoid, so no updates please');
		}else{
			setMessage('Gimme updates!');
		}	
	}else {
		if(update_selection == 'false'){
			setMessage('Im really paranoid, so no updates please');
		}else{
			setMessage('Gimme updates!');
		}
	}
	
	savePrefs()	;
}


// big_extra - information window
function showInformation(text, fadein, extraicon, closebutton, type){
	var boxsize = document.getElementById('big_extra');
	var stripped = text.replace(/(<([^>]+)>)/ig,""); 
	if(stripped.length < 75 && type != true){
		document.getElementById('big_extra').className = "big_extra_small";
	}else{
		document.getElementById('big_extra').className = "big_extra_large";		
	}
	//text = '('+stripped.length+')'+text;
	
	
	if(extra_anitimer_a != 0){
		clearTimeout(extra_anitimer_a);
	}
	if(extra_wait_timer != 0){
		clearTimeout(extra_wait_timer);
	}
	holderElement = boxsize;	
	textElement = document.getElementById('big_extra_text');
	if(fadein){
		extra_fadein();	
	//alert('show info')
		
	}
	// setup buttons
	if(closebutton == true){
		document.getElementById('big_extra_close').style.display = 'block';
	}
	
	// show icon
	if(extraicon == 1){
		document.getElementById('extra_icon').style.display = 'block';		
	 	document.getElementById('extra_icon').style.backgroundImage = 'url(images/info.png)';		
	}else if(extraicon == 2){
		document.getElementById('extra_icon').style.display = 'block';				
		document.getElementById('extra_icon').style.backgroundImage = 'url(images/accept.png)';		
	}else{
		
		document.getElementById('extra_icon').style.display = 'none';
	}
	
//	textElement.innerHTML = 'length: '+text.length+' - '+text;
	textElement.innerHTML = text;
	holderElement.style.display = 'block';

	
}

// Load preferences
function getPrefs(){
	var counter = GetCookie('count');
	
	if(counter != null ){
		if(parseInt(counter) > 50 ){
		totalgold = parseInt(counter);
		tempgold = parseInt(counter);
		document.getElementById("total_gold").value	 =  tempgold;			
		setgold();
		}else{
			resetPrefs();	
		}
	}
	
	
	
	
	if(window.widget){

		//showInformation('<br> <br>Getting preferences...',true, 1, true); // text, fade in, extraicon, closebutton	
		sg  = widget.preferenceForKey("SG");
		bv  = widget.preferenceForKey("BV");
		cs  = widget.preferenceForKey("CS");
		csp = widget.preferenceForKey("CSPEED");
		roll_count = widget.preferenceForKey("ROLLCOUNT");
		percheck = widget.preferenceForKey("PERSONALITY");
		corereset =  widget.preferenceForKey("CORERESET");		
		
		
		if(widget.preferenceForKey("SG") != undefined && corereset == undefined && resetfix == true){
			setMessage('Score is retrieved.');
			totalgold = widget.preferenceForKey("SG");
			tempgold = 	widget.preferenceForKey("SG");
			document.getElementById("total_gold").value	 =  tempgold;	
			setBet(0); // setting bet
			setgold();	
		}
//		setMessage('sg: '+sg+' bv: '+bv+' cs: '+cs+' csp: '+csp);
		if(corereset == undefined){
			setMessage('Resetting core. This must be done in this release.');
		}
//		if (sg && sg.length > 0 && csp && csp.length > 0 && bv && bv.length > 0 && cs && cs.length > 0){
		if (sg != undefined &&  bv != undefined && cs != undefined && csp != undefined){
			
			//setMessage('Get prefs: '+csp.length)							
			
			tempgold = widget.preferenceForKey("SG");
			betvalue = widget.preferenceForKey("BV");	
			currencyselection = widget.preferenceForKey("CS");			
			speedselection = widget.preferenceForKey("CSPEED");
			max_counter_d = speeds[speedselection];
			autoUpdates =  widget.preferenceForKey("AUPDATES");
			corereset =  widget.preferenceForKey("CORERESET");
			
			if(percheck != undefined){
				personality =  widget.preferenceForKey("PERSONALITY");			
			}
			
			//showInformation('Got preferences: '+currencyselection,false, 0, true); // text, fade in, extraicon, closebutton				
			
			document.getElementById("total_gold").value	 =  tempgold;			
			
			//setMessage('Preferences loaded. Welcome back to version '+version)			

			//setMessage('Currency changed to '+currency[currencyselection])			
			document.getElementById("currency_overlay").innerHTML  = currency[currencyselection];			

			var dropdown = document.getElementById('currencyChanger');
			dropdown.selectedIndex = currencyselection;			
		
			var dropdown = document.getElementById('speedChanger');
			dropdown.selectedIndex = speedselection;							


			setBet(0); // setting bet
			setgold();
			//extra_anitimer = setTimeout("extra_fadeout()", 1000);
			
			if(autoUpdates == true){
				//alert('auto updates')
				updateChecker = setTimeout("dailyUpdateCheck()",checktime); // check every minute	
			}

		}else {
			//showInformation('<br> <br>No preferences...',true, 1, true); // text, fade in, extraicon, closebutton	
			if(counter != null){
			
			}else{
			resetPrefs();	
			}

//			setMessage('Preferences where not yet defined.')						
			
		}
	}
}

// Save preferences
function savePrefs(){
	//alert('currentspeed: '+currentspeed)
	//alert('tempgold: '+tempgold)
	//alert('betvalue: '+betvalue)
	//alert('currencyselection: '+currencyselection)
	savegold();
	if(window.widget){
		//showInformation('<br> <br>Preferences saved...',true, 1, true); // text, fade in, extraicon, closebutton	
		//setMessage('Saving preferences, tg, bv, cs, cpe '+tempgold+' '+betvalue+''+currencyselection+'');
		widget.setPreferenceForKey(tempgold, "SG");
		widget.setPreferenceForKey(betvalue, "BV");
		widget.setPreferenceForKey(currencyselection, "CS");		
		widget.setPreferenceForKey(currentspeed, "CSPEED");		
		widget.setPreferenceForKey(roll_count, "ROLLCOUNT");	
		widget.setPreferenceForKey(update_selection, "AUPDATES");	
		widget.setPreferenceForKey(personality, "PERSONALITY");	
		widget.setPreferenceForKey(corereset, "CORERESET");	
		//corereset
//		personality
		//extra_anitimer = setTimeout("extra_fadeout()", 1000);		
//currencyselection		
		//setMessage('Core reset:  '+widget.preferenceForKey("CORERESET"));	
	}
}

function resetScore(){
	totalgold = startgold;
	tempgold = totalgold;
	setgold();
	//document.getElementById("total_gold").value = startgold;
	//document.getElementById("gold_score").innerHTML = totalgold;
	savePrefs();
	hideInformation();
}


var currentSubmit = 0;

function checkMaxscore(){

	scoresubber = document.getElementById('score_typer');
	
	if(scoresubber.value > tempgold || IsNumeric(scoresubber.value) == false){
		scoresubber.value = tempgold;
	}
	if(IsNumeric(scoresubber.value)){
		currentSubmit = roundFive(scoresubber.value);
		
	}else{
		currentSubmit = 1500;
		scoresubber.value = 1500;
	}
	
}

function roundFive(input){
	var checker = input/10;
	var result =Math.round(checker);
	result *= 10;

	return result; 
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }