using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Util 
{
	public static GameObject FindChild( GameObject parent, string strChildName )
	{
		int iCount = parent.transform.GetChildCount();
		
		for(int i=0; i<iCount; i++)
		{
			GameObject child = parent.transform.GetChild(i).gameObject;			
			if ( child.name == strChildName )
			{
				return child;
			}
			
			GameObject res = FindChild( child, strChildName );
			if ( res!=null )
			{
				return res;
			}					
		}
		
		return null;
	}		
	
	
	
	// from:  http://entitycrisis.blogspot.co.uk/2010/11/automagic-gui-scaling-in-unity3d.html
	
    static float WIDTH  = 960;
    static float HEIGHT = 640;

    static List<Matrix4x4> stack = new List<Matrix4x4>();
	
	static float s_fLeft  = 0.0f;
	static float s_fRight = 0.0f;

    static public void BeginGUI() 
	{
        stack.Add (GUI.matrix);
        Matrix4x4 m = new Matrix4x4 ();
        var scale = 1f;
        var offset = Vector3.zero;        
		scale = (Screen.height/HEIGHT);
        offset.x += (Screen.width-(WIDTH*scale))*0.5f;        
        m.SetTRS(offset,Quaternion.identity,Vector3.one*scale);
        GUI.matrix *= m;
		
		float fVirtualW = Screen.width/(Screen.height / 640.0f);
		s_fLeft = -0.5f*(fVirtualW-WIDTH);		
		s_fRight = s_fLeft+fVirtualW;
    }

    static public void EndGUI() 
	{
        GUI.matrix = stack[stack.Count - 1];
        stack.RemoveAt (stack.Count - 1);
    }
	
	public static float GetLeft()
	{
		return s_fLeft;
	}
	
	public static float GetRight()
	{
		return s_fRight;
	}
}
