$(document).ready( function() {

	fancy();

	cache_all_odds();

	calculate_odds();

	$("#query").autocomplete("libs/search.php", {
		minChars: 0,
		max: 10,
		autoFill: false,
		mustMatch: true,
		scrollHeight: 220
		});

});

var moneyline_cache=new Array();

var decimal_cache=new Array();

function cache_all_odds(){

	$(".odds_value").each(function(){

		var odds=$(this).html();

		var odds_for_insert;

		var extract=new RegExp(/[0-9EV]+/);

		var temp= extract.exec(odds);


 		if (temp == null) {

    		temp_odds='';;
  		}
  		else {

  			temp_odds=temp[0];

  		}

		var formatted_odds=parseFloat(temp_odds);

		moneyline_cache.push(odds);

		if (temp_odds == "EV")
        {
            odds_for_insert = "2";
        }
        else if (parseFloat(odds)<0)
        {
            odds_for_insert = Math.round(((100 / formatted_odds) + 1) * 100) / 100;
            odds_for_insert = odds_for_insert.toString();
        }
        else if (parseFloat(odds)>0)
        {
            odds_for_insert = Math.round(((formatted_odds / 100) + 1) * 100) / 100;
            odds_for_insert = odds_for_insert.toString();
        }
        else
        {
            odds_for_insert = '';
        }

        decimal_cache.push(odds_for_insert);
	})


}

function fancy(){

	$("a.odds_value,a.past_odds").fancybox({
		        'autoScale'     	: false,
		        'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'easingIn'		    : 'swing',
				'easingOut'		    : 'swing',
				'autoDimensions'    : true,
				'showNavArrows'     : false,
				'titleShow'         : false,
				"type" 				: "iframe" ,
				"width" 			: 516,
				"height"			:316


	});

	$("a.alert_link").fancybox({
		        'autoScale'     	: false,
		        'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'easingIn'		    : 'swing',
				'easingOut'		    : 'swing',
				'autoDimensions'    : true,
				'showNavArrows'     : false,
				'titleShow'         : false


	});

}

function  IsEmail(email)

{


	var reg = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);



   if(reg.test(email) == false)


   {


      return false;


   }


   return true;


}

var fancybox_settings={'transitionIn': 'fade','transitionOut':'fade','easingIn':'swing','easingOut': 'swing','autoDimensions': true,"hideOnOverlayClick":true,"hideOnContentClick":true,"showCloseButton":false};

function message(type){

	$("<a id='testing'></a>").attr("href", 'libs/ajax.Messages.php?type='+type).fancybox(fancybox_settings).trigger("click").remove();

}

function calculate_odds() {

	var option=$("#odds_selector").val();

	switch(option){

		case "1":

			$("#return_container").fadeOut(300);

			$(".odds_value").each(function(i){

					$(this).html(moneyline_cache[i]);

			})

		break;

		case "2":

			$("#return_container").fadeOut(300);

			$(".odds_value").each(function(i){

					var decimal_ammount_formatted=decimal_cache[i]?jQuery.sprintf("%01.2f",decimal_cache[i]):'';

					$(this).html(decimal_ammount_formatted);

			})

		break;

		case "3":

			$("#return_container").fadeIn(300);

		break;
	}

}

function calculate_return(){

	if(isNumeric($("#return_ammount").val()))
		calculate();
	else {
		window.alert("Enter ammount in correct format !");
		$("#return_ammount").focus();
	}
}

function calculate(){

	var ammount=$("#return_ammount").val();

	$(".odds_value").each(function(i){

		var calculated_ammount=Math.round((parseFloat(decimal_cache[i])-1)*100*ammount)/100;


		if(!isFinite(calculated_ammount))
			calculated_ammount='';

		$(this).html(calculated_ammount);

	})

}

function isNumeric(val){

	return(parseFloat(val,10)==(val*1));

}

function open_alert(fight_id,type){



}

function process_alert_response(response){

	var obj=$("#alert_message");

	obj.html('');

	switch(response){

		case "1":

			obj.html("Fight doesn't exists");

		break;

		case "2":

			obj.html("Fight has already finished");


		break;

		case "3":

			obj.html("Selected bookie doesn't provide odds for selected fight");

		break;

		case "4":

			obj.html("Selected odds trashhold has already been reached");

		break;

		case "5":

			obj.html("Please enter email in correct format");

		break;

		case "6":

			obj.html("You already got max 10 alerts");

		break;

		case "7":

			obj.html("There was error adding alerts");

		break;

		case "8":

			message("alert_success");

		break;
	}

}

function validate_alert(){

	return true;

}
