var Calc = {
  
  dom_loaded: function() {
    if ($("surebetcalculator"))
      var calculator = new Calc.Surebets;
	},
	
  Surebets: Class.create({
    values: { p1: null, p2: null, p3: null, s1: 0, s2: 0, s3: 0, a: 1000, b1: null, b2: null, b3: null, m: null},
    results: { value1: 0,value2: 0,value3: 0,ret: 0,win: 0 },
    permalink: { url: window.location.toString() },
    
    initialize: function(){
      this.calculate();
      $$(".calculate").invoke("observe","change",this.calculate.bind(this));
      Event.observe('surebetcalculator', 'submit', this.calculate.bind(this));
    },
    
    update_permalink: function(){
      var permalink = "http://www.betmasters.gr/surebet-calculator?popup=true";
      permalink+= "&s1="+this.values.s1;
      permalink+= "&s2="+this.values.s2;
      permalink+= "&s3="+this.values.s3;
      permalink+= "&p1="+this.values.p1;
      permalink+= "&p2="+this.values.p2;
      permalink+= "&p3="+this.values.p3;
      permalink+= "&a="+this.values.a;
      permalink+= "&b1="+this.values.b1;
      permalink+= "&b2="+this.values.b2;
      permalink+= "&b3="+this.values.b3;
      permalink+= "&m="+this.values.m;
      this.permalink.url = permalink;
      $("permalink").value = permalink;

    },
    
    calculate: function(){
      this.update_values();
      this.update_permalink();
      
      if (this.values.a > 0) {
        if (this.values.s1 > 0 && this.values.s2 > 0 && this.values.s3 > 0) {
          this.results.value1 = this.to_f((this.values.a/this.values.s1)*(1/((1/this.values.s1)+(1/this.values.s2)+(1/this.values.s3))));
          this.results.value2 = this.to_f((this.values.a/this.values.s2)*(1/((1/this.values.s1)+(1/this.values.s2)+(1/this.values.s3))));
          this.results.value3 = this.to_f((this.values.a/this.values.s3)*(1/((1/this.values.s1)+(1/this.values.s2)+(1/this.values.s3))));
          $("calculatorerrors").update("");
        }else if(this.values.s1 > 0 && this.values.s2 > 0){
          this.results.value1 = this.to_f((this.values.a/this.values.s1)*(1/((1/this.values.s1)+(1/this.values.s2))));
          this.results.value2 = this.to_f((this.values.a/this.values.s2)*(1/((1/this.values.s1)+(1/this.values.s2))));
          this.results.value3 = 0;
          $("calculatorerrors").update("");
        }else{
          $("calculatorerrors").update("Εισάγετε τις αποδόσεις των εταιρειών (δύο ή τρεις) και το συνολικό ποσό που επιθυμείτε να παίξετε.");
        };
        
        this.results.ret = this.to_f((this.results.value1*this.values.s1));
        this.results.win = this.to_f((this.results.ret - this.values.a));
        
        $("value1").value = this.to_pretty_f(this.results.value1);
        $("value2").value = this.to_pretty_f(this.results.value2);
        $("value3").value = this.to_pretty_f(this.results.value3);
        $("return").value = this.to_pretty_f(this.results.ret);
        $("win").value =    this.to_pretty_f(this.results.win);
        if (this.results.ret <= this.values.a) {
          $("calculatorerrors").update("Η πιθανότητα επιτυχίας όλων των αποτελεσμάτων πρέπει να είναι λιγότερο του 100% για να έχετε κέρδος");
        };
      } else {
        $("calculatorerrors").update("Παρακαλώ εισάγετε το συνολικό ποντάρισμα");
      };
      return false;
    },
    
    update_values: function(){
      this.values.s1 = this.to_f($F("selection1"));
      this.values.s2 = this.to_f($F("selection2"));
      this.values.s3 = this.to_f($F("selection3"));
      this.values.b1 = $F("b1");
      this.values.b2 = $F("b2");
      this.values.b3 = $F("b3");
      this.values.p1 = $F("p1");
      this.values.p2 = $F("p2");
      this.values.p3 = $F("p3");
      this.values.m = encodeURIComponent($F("event"));
      this.values.a =  this.to_f($F("amount"));
      
      var shares = new Array();
      var url = "http://www.addthis.com/bookmark.php?pub=xa-4a2593ce40cf3e29&v=250&source=tbx-250&s=[[share]]&url=[[url]]&title=[[title]]&content=";
      var current_url = "";
      
      shares[1]="google";
      shares[2]="facebook";
      shares[3]="twitter";
      
      for (var i = shares.length - 1; i >= 0; i--){
        if($("add_"+shares[i])){
          current_url = url.replace("[[share]]",shares[i]);
          current_url = current_url.replace("[[url]]",encodeURIComponent(this.permalink.url));
          current_url = current_url.replace("[[title]]",encodeURIComponent("Το SureBet μου στο Betmasters.gr"));
          $("add_"+shares[i]).href = current_url;
        }
      };
    },
    
    to_f: function(value){
      var myfloat = parseFloat(value.toString().replace(",","."));
      return myfloat == NaN ? 0 : myfloat;
    },
    
    to_pretty_f: function(value){
    	var stringNumber = value.toString(), lastDec, posFinal, numberMore, result, decimalType, decimals;
      var nDecimals = 2;
    	var decimalTypes = [".",","];
    	for(var i = 0; i < decimalTypes.length; i++){
    		pos = stringNumber.indexOf(decimalTypes[i]);
    		if(pos != -1){
    			decimalType = decimalTypes[i];
    			break;
    		}
    	};

    	decimals = (stringNumber.length-1)-pos;
    	if(nDecimals > 0){
    		decimals = nDecimals;
    	}
    	posFinal = pos+(decimals+1);
    	if(pos != -1){
    		lastDec = stringNumber.substr(posFinal,1);
    		stringNumber = stringNumber.substr(0,posFinal);
    		if(lastDec >= 5){
    			numberMore = stringNumber.substr(stringNumber.length-1,1);
    			if(numberMore == decimalType){
    				stringNumber = (stringNumber.substr(0,stringNumber.length-1)*1)+1;
    			}else{
    				stringNumber = stringNumber.substr(0,stringNumber.length-1)+((numberMore*1)+1);
    			}

    		}					
    	} 
    	myfloat = parseFloat(stringNumber);
    	return myfloat;
    }
  }),
  
  open_bookers: function(){
    var bookers = new Array();
    bookers[1]=$F("b1");
    bookers[2]=$F("b2");
    bookers[3]=$F("b3");
    for (var i = bookers.length - 1; i >= 0; i--){
      if (typeof bookers[i]!="undefined" && bookers[i]!=0){
        window.open("/m/"+bookers[i]);
      };
    };
    return false;
  }
};

Event.observe(window, "load", Calc.dom_loaded);