var actnum=1;
var actnum_int = 1;
var jstiparr = new Array(0,0,0,0,0,0,0);
var image_width = 48; 
var image_height = 40; 
var selected_image_width = 58; 
var selected__height = 58; 

$(document).ready(function() {
	init_tiparea();
	$(".tipfield").find("img.nr").each(function(i) {
	 	$(this).click(function() {
			moveTip($(this));
			});
	 });
	$("#results").find(".choosennum").each(function(i) {
		$(this).css("width", selected_image_width + "px");
		$(this).css("height", selected_image_width + "px");
		});
	});

function init_tiparea(resettip)	{
	if(resettip) {
		TIPPED=false;
		jstiparr = new Array(0,0,0,0,0,0,0);
		}
	actnum = 1;
	actnum_int = 1;
	for(i = 1;i <= 7;i++)	{
		$("#tipped"+i).remove();
		}
	tleft = 5;
	ttop = 5;
	for(i = 1;i <= 49;i++)	{
		$("#num"+i).attr("src",NUMBERS_BIG + "/" + i + ".png");
		$("#num"+i).attr("alt",i);
		$("#num"+i).css("left",tleft+"px");
		$("#num"+i).css("top",ttop+"px");
		$("#num"+i).css('cursor','pointer');
		if(i % 7 == 0 && i > 1)	{
			tleft = 5;
			ttop = (ttop + image_height);
			}
		else {
			tleft = (tleft + image_width);
			}
		}

	if(TIPPED != false)	{
		for(i = 1;i <= 7;i++)	{
			u = (i - 1);
			nr = TIPPED[u];
			jstiparr[u]=nr;
			$("#tip"+i).append('<img id="tipped' + i + '" class="newtipped" src="' + NUMBERS_BIG + '/' + nr + '.png"/>');
			$("#num"+nr).attr("src",NUMBERS_BIG + "/empty.png");
			}
		}

	return true;
	}

function moveTip(elm)	{
	if(actnum>7 || $(elm).attr("src") == NUMBERS_BIG + "/empty.png")return;
	$(elm).clone().attr("id","tipped" + actnum).insertBefore($(elm));
	$(elm).attr("src",NUMBERS_BIG + "/empty.png");
	$("#tip"+actnum).css('zIndex',1);
	$("#tipped"+actnum).css('zIndex',32);
	$("#tipped"+actnum).addClass("newtipped");
	$("#tipped"+actnum).animate({'left':$("#tip"+actnum).position().left,'top':$("#tip"+actnum).position().top}, "slow", function() {
		jstiparr[actnum_int-1] = $("#tipped"+actnum_int).attr("alt");
		actnum_int++;
		}).attr("src",NUMBERS_BIG + "/" + $("#tipped"+actnum).attr("alt") + ".png");
	$(elm).css('cursor','none');
	actnum++;
	}


function moveRand(cnrs,used)    {
	if(used <= 6)	{
		nr = cnrs[used];
		used++;
		elm = "#num" + nr;
		$(elm).clone().attr("id","tipped" + actnum).insertBefore($(elm));
		$(elm).attr("src",NUMBERS_BIG + "/empty.png");
		$("#tip"+actnum).css('zIndex',1);
		$("#tipped"+actnum).css('zIndex',32);
		$("#tipped"+actnum).addClass("newtipped");
		$("#tipped"+actnum).animate({'left':$("#tip"+actnum).position().left,'top':$("#tip"+actnum).position().top}, "slow").attr("src",NUMBERS_BIG + "/" + $("#tipped"+actnum).attr("alt") + ".png");;
		setTimeout(function() {
		jstiparr[actnum_int-1] = $("#tipped"+actnum_int).attr("alt");
		actnum_int++;
		moveRand(cnrs,used);
		},200);
		$(elm).css('cursor','none');
		actnum++;
		}
	}


function randomTip() {
	init_tiparea(1);
	var rnr=0;
	var rcheck = false;
	for(i=0;i<7;i++) {
		do {
			rcheck=false;
			rnr = Math.floor((Math.random()*49) + 1);
			for(a=0;a<i;a++) if(jstiparr[a] == rnr) rcheck = true;
			} while(rcheck == true);
		jstiparr[i] = rnr;
		}
	moveRand(jstiparr,0);
	}
	
function sendTip() {
	for(i=0;i<7;i++) {
		if(jstiparr[i]==0) {
			alert(choose_numbers_first);
			return;
			}
		}
	var num = $("#num").val();
	senddata = {"numbers": jstiparr, "num": num};
	var dataString = JSON.stringify(senddata);
	$.post("/content/dyn_settip/", {Dyndata: dataString}, function(retstatement) {
		if(retstatement.msg == "Set")	{
				if(retstatement.nexturl != "")	{
					getPage(retstatement.nexturl);
					}
				else {
					flashdiv=document.getElementById('flashnrs');
					flashdiv.style.display="none";
					fodiv.style.display="block";
					}
			}
		}, "json");
	}

function getPage(url)	{
	document.location.href=url;
	}

