$ = function(e){
	switch(typeof e){
		case "string":
			return document.getElementById(e);
		break;
		default:
			return e;
		break;
	}
}


Glry = {
	Linky:function(e){
		var Container,Item;
		if(e == undefined){
			Container = document.links;
		}else{
			Container = e.getElementsByTagName("A");
		}
		for(var Index = 0;Index < Container.length;Index++){
			Item = Container[Index];
			if(Item.getAttribute('enabled') !== null){
				if(Item.getAttribute('enabled') == "true"){
					Container[Index].onclick = Glry.Clicky;
				}
			}
		}
	},
	Clicky:function(e){
		switch(this.getAttribute('action')){
			case "GET":
				Ajax.Handlers.before = Glry.CreateLoader;
				Ajax.Handlers.after = Glry.ParseLoadedContent;
				Ajax.Get(this.rel);
				return false;
			break;
			case "POST":
				return false;
			break;
			default:
				new Function("return "+this.getAttribute('action'))();
				return;
			break
		}
	},
	Browser:function(){
		if(navigator.appName == "Microsoft Internet Explorer"){
			return "MS";
		}else{
			return -1;
		}
	}
}
Glry.Cart = {
	Container:new Object(),
	Summary:function(State,Position){
		Glry.Cart.Container  = $('cart-container');
		if(Position == undefined){
			Glry.Cart.Container.style.display = "none";
			return;
		}
		var Coordinates = Glry.Util.Position(Position);
		Glry.Cart.Container.style.top = Coordinates.y+"px";
		Glry.Cart.Container.style.left = Coordinates.x+"px";
		Glry.Cart.Container.style.display = "block";
	}
}
Glry.Gallerize = {
	Files:Object(),
	Container:null,
	Enlarge:false,
	Counter:Number(0),
	Init:function(Enlarge){
		if(Enlarge !== undefined){
			Glry.Gallerize.Enlarge = true;
		}
		if(Glry.Gallerize.Container == null){
			return false;
		}
		Glry.Gallerize.Counter++;
		if(Glry.Gallerize.Counter >= (Glry.Gallerize.Files.length)){
			Glry.Gallerize.Counter = 0;
		}
		Glry.Gallerize.Display();
	},
	Display:function(){
		if(!Glry.Gallerize.Enlarge){
			ImageFile = Glry.Util.CreateObject("IMG");
			ImageFile.src = Glry.Gallerize.Files[Glry.Gallerize.Counter].src;
			Glry.Gallerize.Container.removeAttribute("style");
			if(Glry.Gallerize.Container.hasChildNodes()){
				Glry.Gallerize.Container.innerHTML = '';
			}
			Glry.Gallerize.Container.appendChild(ImageFile);
			Glry.Gallerize.Container.style.height = ImageFile.height+'px';
			Glry.Gallerize.Container.style.width = ImageFile.width+'px';
			return;
		}
		var Item,Content,Close;
		Glry.Util.Param.Attr = {
			"id":"Overlay",
			"class":"Overlay"
		}
		Item = Glry.Util.CreateObject();
		Item.style.width = Glry.Util.DocumentWidth()+"px";
		Item.style.height = Glry.Util.ClientHeight()+"px";
		window.onresize = function(){
			$("Overlay").style.width = Glry.Util.DocumentWidth()+"px";
			$("Overlay").style.height = Glry.Util.DocumentHeight()+"px";
		}
		Glry.Util.Param.Attr = {
			"id":"selected",
			"src":Glry.Gallerize.Files[Glry.Gallerize.Counter].large
		}
		Image = Glry.Util.CreateObject("IMG");
		Item.appendChild(Image);
		document.body.appendChild(Item)
		$('selected').style.position = "absolute";
		$('selected').style.left = Glry.Util.GetCenter(Image).x+"px";
		$('selected').style.top = Glry.Util.GetCenter(Image).y+"px";
	}
}
Glry.Display = {
	Item:Object(),
	Position:Boolean(false),
	Show:function(Item,Position){
		Glry.Display.Item = Item;
		Glry.Display.Item.style.display = "block";
		if(Position !== undefined){
			
		}
	},
	Hide:function(Item){
		Glry.Display.Item = Item;
		Glry.Display.Item.style.display = "none";
	},
	Swap:function(Item,Position){
		Glry.Display.Item = Item;
		if(Glry.Display.Item.style.display == "block"){
			Glry.Display.Item.style.display = "none";
		}else{
			Glry.Display.Item.style.display = "block";
		}
		if(Position !== undefined){
			Glry.Display.Item.style.position = "absolute";
			Params = Glry.Util.Position(Position);
			Correction = Glry.Util.Position(Glry.Display.Item);
			Glry.Display.Item.style.left = (Params.x+Params.width)-Correction.width+"px";
			Glry.Display.Item.style.top = Params.y+Params.height+"px";
		}
	}
}

Glry.Util = {
	Param:Object(),
	RandomString:function(){
		var Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var Result = '';
		for (var Index = 0; Index < 10; Index++) {
			var RNum = Math.floor(Math.random() * Chars.length);
			Result += Chars.substring(RNum,RNum+1);
		}
		return Result;
	},
	CreateObject:function(Tag){
		if(Tag == undefined){
			Tag = "DIV";
		}
		var Item = document.createElement(Tag);
		if(Glry.Util.Param.Attr !== undefined){
			for(var Index in Glry.Util.Param.Attr){
				Item.setAttribute(Index,Glry.Util.Param.Attr[Index]);
			}
		}
		Glry.Util.Param = new Object();
		return Item;
	},
	GetCenter:function(Item){
		var Position = {
			scrollX : Glry.Util.ScrollX(),
			scrollY : Glry.Util.ScrollY(),
			viewPortWidth : Glry.Util.ClientWidth(),
			viewPortHeight : Glry.Util.ClientHeight(),
			elementHeight : Item.offsetHeight,
			elementWidth : Item.offsetWidth
		}
		this.x = (Position.viewPortWidth / 2) - (Position.elementWidth / 2) + Position.scrollX;
		this.y = (Position.viewPortHeight / 2) - (Position.elementHeight / 2) + Position.scrollY;
		if(Position.elementHeight >= Position.viewPortHeight){
			this.y = Position.scrollY;
		}
		return this;
	},
	GetMouse:function(){
		this.x = 0;
		this.y = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY){
			this.x = e.pageX;
			this.y = e.pageY;
		}else if (e.clientX || e.clientY) 	{
			this.x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			this.y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		return this
	},
	DocumentWidth:function(){
		return document.documentElement.offsetWidth || document.body.offsetWidth;
	},
	DocumentHeight:function(){
		return document.documentElement.offsetHeight || document.body.offsetHeight;
	},
	ClientWidth:function(){
		return window.innerWidth || document.documentElement.clientWidth;
	},
	ClientHeight:function(){
		return window.innerHeight || document.documentElement.clientHeight;
	},
	ScrollX:function(){
		return document.documentElement.scrollLeft || document.body.scrollLeft;
	},
	ScrollY:function(){
		return document.documentElement.scrollTop || document.body.scrollTop;
	},
	Position:function(Item){
		var left = 0,top = 0;
		var parent = Item;
		while (parent) {
			left += parent.offsetLeft;
			top += parent.offsetTop;
			parent = parent.offsetParent;
		}
		return {
			x:left,
			y:top,
			width:Item.offsetWidth,
			height:Item.offsetHeight
		};
	}
}

Glry.Confirm = {
	Delete:function(Url){
		if(Url == undefined){
			return;
		}
		var msg = confirm("Press OK to Delete.\nPress Cancel to return");
		if (msg){
			window.location = Url;
		}
	},
	Signout:function(Url){
		if(Url == undefined){
			return;
		}
		var msg = confirm("Oled kindel, et soovid välja logida?");
		if (msg){
			window.location = Url;
		}
	}
}