var Message = {
	
	$iBox:null,
	func:function(){},
	
	show:function(str,type,func)
	{
		type = type || 0;
		func = func || 0;
		
		var img = "";
		if(type==1)
			var img = "<img src='/img/new2/icons/success_48.png' />";
		else if(type==2)
			var img = "<img src='/img/new2/icons/error_48.png' />";
		
		$(".infoBox").remove();
		this.$iBox=$("<div class='infoBox'></div>");
		this.$iBox.html(img+str+"<br/><br/>");
		
		if(type!=-1)
		{
			var $button = $("<button type='button' class='grayButton'>Ok</button>");		
			if(func!=0) {
				$button.click(function(){
					Message.remove();
					func();
				});
			}
			else {
				$button.click(function(){
					Message.remove();
				});
			}
			this.$iBox.append($button);
		}
		this.add();
	},
	
	showWithButton:function(str,type,buttonText,func)
	{
		window.location="#";
		var img = "";
		if(type==1)
			var img = "<img src='/img/new2/icons/success_48.png' />";
		else if(type==2)
			var img = "<img src='/img/new2/icons/error_48.png' />";
		
		$(".infoBox").remove();
		this.$iBox=$("<div class='infoBox'></div>");
		this.$iBox.html(img+str+"<br/><br/><div class='buttons'><button onclick='Message.remove();' type='button' class='button'>Ok</button></div>");		
		
		var button = $("<button style='margin-left:5px;' type='button' class='button'>"+buttonText+"</button>");
		this.$iBox.find(".buttons").append(button);
		button.click(func);
		this.add();
	},
	
	display:function(){
		
	},
	
	confirm:function(str,func)
	{
		Message.func = func || function(){};
		
		var img = "<img src='/img/new2/icons/confirm_48.png' />";
		$(".infoBox").remove();
		this.$iBox=$("<div class='infoBox'></div>");
		this.$iBox.html(img+str+"<br/><br/><button onclick='Message.execute();' type='button' class='button'>Confirm</button> <button onclick='Message.remove();' type='button' class='button'>Cancel</button>");		
		this.add();
	},
	
	execute:function(){
		Message.remove();
		Message.func();
	},
	
	remove:function()
	{
		$(".infoBox").remove();
	},
	
	add:function()
	{
		$("body").append(this.$iBox);
		var w = this.$iBox.width();
		var h = this.$iBox.height();
		var w2 = $(document).width();
		var h2 = $(document).height();
		
		var x = (w2-w)/2;
		var y = (h2-h)/2 -90;
				
		this.$iBox.css({"left":x,"top":y}).show();
	}
};

function InfoBox (obj,timeOut) {
	
	this.obj=obj;
	this.timeOut=timeOut;
	this.posX;
	this.posY;
	this.$iBox;
	
	this.show=function(str,x,y) {
		
		if(x==undefined || x==null)
			x=0;
		if(y==undefined || y==null)
			y=0;
		
		var pos=$(this.obj).offset();
		this.posX=pos.left+x;
		this.posY=pos.top+y;
		
		this.$iBox=$("<div class='infoBox oldInfoBox'></div>").html(str);
		this.$iBox.css({top:this.posY,left:this.posX});
		this.$iBox.hide();
		
		$("body").append(this.$iBox);
		this.$iBox.fadeIn();
		if(this.timeOut!=0 && this.timeOut!=undefined && this.timeOut!=null)
		{
			this.$iBox.oneTime(this.timeOut*1000,function(){$(this).remove();});
		}
	};
	
	this.set=function(str) {
		this.$iBox.html(str);
	};
	
	this.remove=function() {
		var th=this;
			th.$iBox.remove();
	};
}

var Pop = {
	
	x:0,
	y:0,
	$pop:null,
	title:null,
	
	initPositions: function(width)
	{
		var windowWidth = $(window).width();
		var scrollTop = $(window).scrollTop();
		
		this.x=(windowWidth-width-20)/2;
		this.y=scrollTop+100;
	},
	
	setStyle: function(css)
	{
		$(this.$pop).children(".content").css(css);
	},
	
	show:function(title,width,draggable) {
		
		width = width || 500;
		draggable = draggable || true;
		
		Pop.close();
		
		this.title=title;
		this.initPositions(width);
		
		var $pop = $("<div id='mainPop' class='pop'></div>");
		var $content = $("<div class='popContent'></div>");
		var $title = $("<h2>"+title+"</h2>");
		$title.append($("<img class='close' src='/img/close2.png' onclick='Pop.close();' />"));
		
		$pop.append($title);
		$pop.append($content);

		$content.css("width",width);
		$pop.css("left",this.x);
		$pop.css("top",this.y);
		
		this.$pop=$pop;
		$("body").append($pop);
		
		if(draggable)
			$pop.draggable({handle:'h2'});
	},
	
	loading:function(){
		Pop.insert("<img style='margin:15px;' alt='Loading...' src='/img/loading.gif' />");
	},
	
	insert:function(data)
	{
		
		if(typeof(data) != 'object')
			$(this.$pop).find(".popContent").html(data);
		else
			$(this.$pop).find(".popContent").append($(data));
	},
	close:function()
	{
		if($(".popContent").find("#publisherContainer").length==1)
		{
			$("#publisherContainer").hide().appendTo("body");
		}
		$("#mainPop").remove();
	}
};

function toTop(obj)
{
	var index=$(obj).css("z-index");
	$(".pop").each(function(){
		if($(this).css("z-index")>=index)
		{
			var i=Number($(this).css("z-index"));
			var newIndex=i+1;
			$(obj).css("z-index",newIndex);
		}
	});
}
