
//--- browser checking
userAgent = navigator.userAgent.toLowerCase();
ns4 = (document.layers) ? true:false
ie4 = (document.all && userAgent.indexOf("mac")==-1) ? true:false
w3c = (document.documentElement) ? true:false;

//--- init function for each page
function initialize()
{
	//--- get window size
	if (ns4)
	{	windowwidth  = window.innerWidth;
		windowheight = window.innerHeight;
	}
	else if (ie4)
	{	windowwidth  = document.body.clientWidth;
		windowheight = document.body.clientHeight;
	}
}

function layerWrite(id,text,nestref)
{
	if (ns4)
	{
		var lyr = (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : document.layers[id].document;
      lyr.open();
      lyr.write(text);
      lyr.close();
	}
	else if (ie4) 
	{
		document.all[id].innerHTML = text;
	}
}

function moveBy(obj,x,y) 
{
	obj.xpos += x;
	obj.left = obj.xpos;
	obj.ypos += y;
	obj.top = obj.ypos;
}

function moveTo(obj,x,y)
{
	obj.xpos = x;
	obj.left = obj.xpos;
	obj.ypos = y;
	obj.top = obj.ypos;
}


function clipValues(obj,which) {
        if (ns4) {
                if (which=="t") return obj.clip.top
                if (which=="r") return obj.clip.right
                if (which=="b") return obj.clip.bottom
                if (which=="l") return obj.clip.left
        }
        else if (ie4) 
		{
                var clipv = obj.clip.split("rect(")[1].split(")")[0].split("px")
                if (which=="t") return Number(clipv[0])
                if (which=="r") return Number(clipv[1])
                if (which=="b") return Number(clipv[2])
                if (which=="l") return Number(clipv[3])
        }
}

function clipTo(obj,t,r,b,l) {
        if (ns4) {
                obj.clip.top = t
                obj.clip.right = r
                obj.clip.bottom = b
                obj.clip.left = l
        }
        else if (ie4) obj.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
}

function clipBy(obj,t,r,b,l) {
        if (ns4) {
                obj.clip.top = clipValues(obj,'t') + t
                obj.clip.right = clipValues(obj,'r') + r
                obj.clip.bottom = clipValues(obj,'b') + b
                obj.clip.left = clipValues(obj,'l') + l
        }
        else if (ie4) 
		{	
			obj.clip = "rect("+Number(this.clipValues(obj,'t')+t)+"px "+Number(this.clipValues(obj,'r')+r)+"px "+Number(this.clipValues(obj,'b')+b)+"px "+Number(this.clipValues(obj,'l')+l)+"px)";
		}
}

function showObject(obj)
{
	if (ns4) obj.visibility	 = "show";
    else if (ie4) obj.visibility = "visible";
}

function hideObject(obj)
{
	if (ns4) obj.visibility = "hide";
   else if (ie4) obj.visibility = "hidden";
}


//--- custom functions / objects

function TextEffect(pTxtArray)
{
	this.rnd = -1;
	while (this.rnd < 0 || this.rnd > pTxtArray.length-1)
		this.rnd = Math.round(Math.random() * pTxtArray.length - 1) + 1;
	this.txtString = pTxtArray[this.rnd];
	this.len = this.txtString.length;
	this.pos = 0;
	this.txtStringOut = "";
	this.tmpArray = this.txtString.split('');
	this.textEffectWrite = textEffectWrite;	
	this.textEffectWrite();
}

function textEffectWrite()
{	
	if (this.pos == this.len) { clearTimeout("textEffectWrite()"); return; }
			
	this.txtStringOut += this.tmpArray[this.pos];

	this.pos++;
	setTimeout('myText.textEffectWrite()',1);
}

//----------------------------


var delay               = 800;
var tfieldsize          = 30; //needs to be an even number
var numberofpairs       = tfieldsize/2;
var field_contains_piece= new Array(tfieldsize+5);

//the following variables are set in initgame()
var foundnumberofpairs	= 0; 
var first				   = 0;
var second				   = 0;
var moves				   = 0;
var UcantMove			   = true
for (n=1;n<(tfieldsize+1);n++)  field_contains_piece[n]=Math.floor(n/2+0.5)

function pic_is(imgname)
{
	// eval("fn=window.document.p"+imgname+".src")
	fn=document.images["p"+imgname].src;
	l=fn.length;
	filename=fn.substring(l-5,l);
	return filename;
}


function display_piece(piec)
{
	eval("window.document.p"+piec+".src='images/"+field_contains_piece[piec]+".gif'");
}

function hide_piece(piec)
{
	eval("window.document.p"+piec+".src='images/0.gif'");
}

function hide()
{
	UcantMove=false;
	hide_piece(first);
	hide_piece(second);
	second=0;
	first=0;
}

function clickon(field)
{
	if (UcantMove) return;
	if (!(pic_is(field)=="0.gif")) return;
	display_piece(field)
	if (first==0 || field==first)
	{
      first=field;
      return;
	}
	UcantMove=true
	second=field
	moves++
	if(field_contains_piece[first]!=field_contains_piece[second]) 
	{
   	setTimeout("hide()",delay)
   	return;
	}

	first=0
	second=0
	foundnumberofpairs++
	if (foundnumberofpairs==numberofpairs)
	{
		var msg = "";
		if ( moves == 15 )         msg = "Glaub ich nicht.";
		else if ( moves <= 20 )    msg = "Superstar!";
		else if ( moves <= 30 )    msg = "Sensationell!";
		else if ( moves <= 35 )    msg = "Hitverdächtig!";
		else if ( moves <= 40 )    msg = "Nicht schlecht.";
   		else if ( moves <= 45 )    msg = "Ganz ok.";
	   	else if ( moves <= 50 )    msg = "Na ich weiß nicht.";
	   	else if ( moves <= 55 )    msg = "Ist die Maus kaputt?";
   		else if ( moves > 55 )     msg = "Hoffnungslos.";
   	
//		top.fmemory.versuche.value = msg+" "+moves+" Versuche.";   	   
		document.getElementById("versuche").value = moves+" Versuche.";
//		top.fmemory.versuche.value = moves+" Versuche.";   	   		
	}
	UcantMove=false;
}

function randomise()
{
	for (n=1;n<(tfieldsize+1);n++)
	{
		tmp=Math.floor(Math.random()*tfieldsize+1)
		swaptemp=field_contains_piece[tmp]
		field_contains_piece[tmp]=field_contains_piece[n]
		field_contains_piece[n]=swaptemp
	}
}


function initgame()
{
   initialize();
   
	foundnumberofpairs=0; //reset in init
	first  = 0;
	second = 0;
	moves  = 0;

	for (n=1;n<(tfieldsize+1);n++)
	{
		field_contains_piece[n]=Math.floor(n/2+0.5);
		hide_piece(n);
	}
	randomise();
	UcantMove=false;
	top.fmemory.versuche.value = "";   	   	
}

function showall()
{
   for (n=1;n<(tfieldsize+1);n++)
	{
		display_piece(n);
	}
}

function hideall()
{
   for (n=1;n<(tfieldsize+1);n++)
	{
		hide_piece(n);
	}
}

function eye()
{
   msg1 = "Ich kann ja auch<br>nichts dafür!<br>Probiers eben<br>nocheinmal!";
   msg2 = "Aua, Du hast<br>mir ins Auge<br>geklickt!";
   msg3 = "Bei 15 Versuchen<br>bekommst Du<br>meinen Hut!";
   msg4 = "Ich glaub Dir<br>ist ziemlich fad!";
   var textArray = new Array(msg1,msg2,msg3,msg4);
	myText = new TextEffect(textArray);
}

function preload()
{
	var imgPath = "images/";

	if (document.images)
	{   
	   //reference of images (img-tag: name)
	   theImages = new Array( 'i0','i1','i2','i3','i4','i5','i6','i7','i8','i9','i10','i11','i12','i13','i14','i15' );
 
	   //real filenames without extension
	   imgFiles  = new Array( '0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15' );

	   // path were images are stored (must have trailing slash)
	   if (imgPath == null) imgPath = '';

	   var maxImgs = theImages.length;
	   for ( i=0; i<maxImgs; i++ ) 
	   {
		  eval( theImages[i] + ' = new Image();' );         
		  eval( theImages[i] + '.src = "'+ imgPath + imgFiles[i] + '.gif";' );  
	   }
	}
}




