//
import utility_classes_01.*;
//
class azijaReljefne
{
	private var target_mc:MovieClip;
	//
	private var puzle_arr:Array;
	private var source_arr:Array;
	//
	private var zastor_mc:MovieClip;
	//
	private var puzleNum_num:Number;
	private var sourceNum_num:Number;
	private var leftNum_num:Number;
	//
	private var sens_num:Number;
	//
	private var tekst_tf:TextField;
	//
	private var izbornik_mc:MovieClip;
	private var grid_mc:MovieClip;
	private var pomoc_mc:MovieClip;
	private var otvoriPomoc_mc:MovieClip;
	private var frstPress_bool:Boolean = true;
	//
	private var sec_num:Number = 0;
	private var min_num:Number = 0;
	private var hour_num:Number = 0;
	private var sec_str:String;
	private var min_str:String;
	private var hour_str:String;
	private var timerStart_bool:Boolean = true;
	//
	private var zapocni_mc:MovieClip;
	//
	private var klik_sound:Sound;
	//
	private var textLoader:LoadVars;
	//
	private var loadSwf_str:String;
	//
	private var vrsta_str:String;
	//
	private var pomoc_bool:Boolean;
	//
	private var timer_mc:MovieClip;
	//
	//
	public function azijaReljefne(target:MovieClip, puzleNum:Number, sourceNum:Number, sens:Number, loadSwf:String, vrsta:String, pomoc:Boolean)
	{
		//
		target_mc = target;
		//
		sens_num = sens;
		//
		vrsta_str = vrsta;
		//
		pomoc_bool = pomoc;
		//
		loadSwf_str = loadSwf;
		//
		zastor_mc = target.zastor;
		zastor_mc.swapDepths(1100);
		//
		zapocni_mc = target.zapocni;
		//
		izbornik_mc = target.izbornik;
		izbornik_mc._alpha = 0;
		izbornik_mc.swapDepths(1050);
		//
		pomoc_mc = target.pomoc;
		pomoc_mc.swapDepths(1010);
		otvoriPomoc_mc = target.pomoc.otvori;
		//
		grid_mc = target.grid;
		grid_mc._alpha = 100;
		//
		klik_sound = new Sound();
		//
		puzle_arr = new Array();
		source_arr = new Array();
		//
		puzleNum_num = puzleNum;
		sourceNum_num = sourceNum;
		leftNum_num = puzleNum;
		// 
		target_mc.kraj_mc._alpha = 0;
		target_mc.kraj_mc.uspjeh_tf.autoSize = true;
		//
		popuniPolja();
	}
	//
	//
	//
	//
	private function popuniPolja()
	{
		//
		var thisTarget:azijaReljefne = this;
		//
		for (var i = 0; i < puzleNum_num; i++)
		{
			puzle_arr[i] = target_mc["p" + i];
			puzle_arr[i].i = i;
			puzle_arr[i].depth = puzle_arr[i].getDepth();
			// Novi dio koda
			// Pohranjivanje početne pozicije puzla
			puzle_arr[i].oldX = puzle_arr[i]._x;
			puzle_arr[i].oldY = puzle_arr[i]._y;
			//
			puzle_arr[i].tocno = false;
			//
			puzleActions(puzle_arr[i]);
		}
		//
		for (var i = 0; i < sourceNum_num; i++)
		{
			source_arr[i] = target_mc["s" + i];
			source_arr[i].i = i;
			source_arr[i].iskoristen = false;
			source_arr[i]._alpha = 0;
		}
		//
		zapocni_mc.onPress = function()
		{
			_root.main.ucitajNovi(thisTarget.loadSwf_str);
		};
		//
	}
	//
	//
	private function puzleActions(target:MovieClip)
	{
		//
		var thisTarget:azijaReljefne = this;
		//
		target.distance = 100;
		target.xdist = 100;
		target.ydist = 100;
		//
		target.onPress = function()
		{
			thisTarget.pressActions(this);
		};
		//
		target.onRelease = function()
		{
			thisTarget.releaseActions(this);
		};
		target.onReleaseOutside = function()
		{
			thisTarget.releaseActions(this);
		};
	}
	//
	//
	private function releaseActions(target:MovieClip)
	{
		var najbliziSource_mc:MovieClip;
		//
		stopDrag();
		delete (target.onEnterFrame);
		//
		//
		for (var i = 0; i < source_arr.length; i++)
		{
			if (!source_arr[i].iskoristen)
			{
				source_arr[i].xdist = target._x - source_arr[i]._x;
				source_arr[i].ydist = target._y - source_arr[i]._y;
				//
				target.distance = Math.sqrt((source_arr[i].xdist * source_arr[i].xdist) + (source_arr[i].ydist * source_arr[i].ydist));
				//
				//trace("(" + target.i + ":" + i + ") : " + source_arr[i].distance + "<" + sens_num);
				//
				if (target.distance < sens_num)
				{
					source_arr[i].iskoristen = true;
					najbliziSource_mc = source_arr[i];
					break;
				}
			}
		}
		//
		najbliziSource_mc._alpha = 0;
		//
		if (target.i == najbliziSource_mc.i)
		{
			target.tocno = true;
		}
		//                                          
		if (target.distance < sens_num)
		{
			target.gotoAndStop(3);
			target._x = najbliziSource_mc._x;
			target._y = najbliziSource_mc._y;
			//
			klik_sound.attachSound("klik");
			klik_sound.start();
			//
			delete (target.onPress);
			delete (target.onRelease);
			delete (target.onReleaseOutside);
			//
			leftNum_num--;
		}
		else
		{
			target.gotoAndStop(1);
		}
		if (leftNum_num == 0)
		{
			delete (target_mc.onEnterFrame);
			daljeActions();
			//izbornik_mc._alpha = 100;
			if (vrsta_str != "end")
			{
				//delay(2);
			}
			else
			{
				//grid_mc._alpha = 0;
				//daljeActions();
			}
		}
	}
	//
	//
	private function delay(sec:Number)
	{
		//
		var thisTarget:azijaReljefne = this;
		//
		var secDelay:Number;
		var now:Number;
		var then:Number;
		var timer:Number = 0;
		//
		now = Math.round(getTimer() / 1000);
		secDelay = sec;
		then = now;
		target_mc.onEnterFrame = function()
		{
			now = Math.round(getTimer() / 1000);
			timer = now - then;
			//
			if (timer == secDelay)
			{
				thisTarget.prikaziIzbornik();
			}
		};
	}
	//
	//
	private function prikaziIzbornik()
	{
		Ease.startEase(izbornik_mc, 1, izbornik_mc._alpha, 100, 0.5, "easeOutSin", "alpha");
		//izbornik_mc.gotoAndStop(2);
		grid_mc._alpha = 0;
		daljeActions();
	}
	//
	//
	//
	//
	private function pressActions(target:MovieClip)
	{
		//
		var thisTarget:azijaReljefne = this;
		//
		//trace("frstPress_bool= " + frstPress_bool);
		//trace("pomoc_bool= " + pomoc_bool);
		//
		if (frstPress_bool == true and pomoc_bool == true)
		{
			Ease.startEase(target_mc.pocetni_mc, 1, target_mc.pocetni_mc._alpha, 0, 0.3, "easeInSin", "alpha");
			frstPress_bool = false;
		}
		//trace("timerStart_bool= " + timerStart_bool);                                                               
		if (timerStart_bool == true)
		{
			timerActions();
			timerStart_bool = false;
		}
		//                                                                          
		for (var i = 0; i < puzleNum_num; i++)
		{
			puzle_arr[i].swapDepths(puzle_arr[i].depth);
		}
		//
		target.swapDepths(1000);
		//
		target.gotoAndStop(2);
		//
		target.startDrag(false);
		//
		target.onEnterFrame = function()
		{
			thisTarget.testDistance(this);
		};
	}
	//
	//
	private function testDistance(target:MovieClip)
	{
		for (var i = 0; i < source_arr.length; i++)
		{
			if (!source_arr[i].iskoristen)
			{
				source_arr[i].xdist = target._x - source_arr[i]._x;
				source_arr[i].ydist = target._y - source_arr[i]._y;
				//
				target.distance = Math.sqrt((source_arr[i].xdist * source_arr[i].xdist) + (source_arr[i].ydist * source_arr[i].ydist));
				//
				//trace("(" + target.i + ":" + i + ") : " + source_arr[i].distance + "<" + sens_num);
				//
				if (target.distance < sens_num)
				{
					source_arr[i]._alpha = 80;
				}
				else
				{
					source_arr[i]._alpha = 0;
				}
			}
		}
	}
	//
	//
	private function timerActions()
	{
		//
		var thisTarget:azijaReljefne = this;
	}
	//
	//
	private function formatirajVrijeme(timer:Number)
	{
	}
	//
	//
	private function daljeActions()
	{
		var thisTarget:azijaReljefne = this;
		var pogesno_num:Number = 0;
		var pogesno_bool:Boolean = false;
		//
		for (var i = 0; i < puzle_arr.length; i++)
		{
			//trace("puzle_arr[" + i + "].tocno= " + puzle_arr[i].tocno);
			if (!puzle_arr[i].tocno)
			{
				//
				puzle_arr[i].gotoAndStop(1);
				pogesno_bool = true;
				pogesno_num++;
				//
				EaseExtended.startEase(puzle_arr[i], 1, puzle_arr[i]._x, puzle_arr[i]._y, puzle_arr[i].oldX, puzle_arr[i].oldY, 1, "easeOutSin", "xy");
				puzle_arr[i]._x = puzle_arr[i].oldX;
				puzle_arr[i]._y = puzle_arr[i].oldY;
			}
		}
		//
		//
		if (pogesno_bool)
		{
			if (pogesno_num == 1)
			{
				target_mc.kraj_mc.uspjeh_tf.text = "Pokušaj ponovno. Pogrešno si složio " + pogesno_num + " dio.";
			}
			if (pogesno_num > 1 and pogesno_num < 5)
			{
				target_mc.kraj_mc.uspjeh_tf.text = "Pokušaj ponovno. Pogrešno si složio " + pogesno_num + " dijela.";
			}
			if (pogesno_num > 4)
			{
				target_mc.kraj_mc.uspjeh_tf.text = "Pokušaj ponovno. Pogrešno si složio " + pogesno_num + " dijelova.";
			}
		}
		else
		{
			target_mc.kraj_mc.uspjeh_tf.text = "Čestitamo. Uspješno složeno";
		}
		//
		target_mc.kraj_mc.swapDepths(1500);
		//
		target_mc.krajSANJA_mc.swapDepths(1501);
		target_mc.krajSANJA_mc.play();
		//
		Ease.startEase(target_mc.kraj_mc, 1, target_mc.kraj_mc._alpha, 100, 0.5, "easeInSin", "alpha");
		//
		target_mc.kraj_mc.ponovi_mc.onPress = function()
		{
			thisTarget.resetPuzle();
		};
		//
	}
	//
	//
	private function resetPuzle()
	{
		//
		//promjesaj();
		target_mc.kraj_mc._alpha = 0;
		//
		for (var i = 0; i < source_arr.length; i++)
		{
			EaseExtended.startEase(puzle_arr[i], 1, puzle_arr[i]._x, puzle_arr[i]._y, puzle_arr[i].oldX, puzle_arr[i].oldY, 1, "easeOutSin", "xy");
			puzle_arr[i].tocno = false;
			//
			source_arr[i].iskoristen = false;
			source_arr[i]._alpha = 0;
			//
			puzleActions(puzle_arr[i]);
			puzle_arr[i].gotoAndStop(1);
			//
		}
		//
		leftNum_num = puzle_arr.length;
		//
	}
	//
	//
	private function promjesaj()
	{
		var switchFirst:Number;
		var switchSecond:Number;
		var itemHolder:MovieClip;
		//
		//
		for (var i = 0; i < puzle_arr.length; i++)
		{
			switchFirst = random(puzle_arr.length);
			switchSecond = random(puzle_arr.length);
			//
			if (switchFirst == switchSecond)
			{
				switchSecond = random(puzle_arr.length);
			}
			//         
			itemHolder = puzle_arr[switchFirst];
			puzle_arr[switchFirst].oldX = puzle_arr[switchSecond].oldX;
			puzle_arr[switchFirst].oldY = puzle_arr[switchSecond].oldY;
			//puzle_arr[switchSecond] = itemHolder;
			puzle_arr[switchSecond].oldX = itemHolder.oldX;
			puzle_arr[switchSecond].oldY = itemHolder.oldY;
			//
			trace(i + ":" + puzle_arr[i].i);
			//
		}
	}
}
