/****************************************************************************
 *  PopupWin
 ****************************************************************************/
$(document).ready(function(){
	$("body#home").append(
		'<div id="PopWin" class="unpopped"><a class="control" href="#open">Open</a><div style="display:none;" class="popcont"></div></div>'
	);
	if($("#PopWin").length>0){
		initpopwin($("#PopWin"));
	}
	
});
function startcss(){
	return {
				'width': "40px",
				'height': "50px",
				'right': "0px",
				'top': "120px"
			};
};
function endcss(win){
	return {
		'width': "300px",
		'height': "270px",
		'right': (($(document).width()/2)-($("#inner").width()/2)-(win.width()-100))+"px",
		'top': "120px"
	};
};
function initpopwin(win){
	$.ajax({
		type: "POST",
		url: "/includes/popwin.php",
		data: "page="+$('body').attr('id'),
		success: function(msg){
			if(msg != ''){
				win.css(startcss())			
				   .find('.popcont')
				   .html(msg)
				   .hide(500);
				setTimeout(function(){openpop(win,true);},3000);
			}
		}
	});
};

function openpop(win,autoclose){
	win.animate(endcss(win),1000).find('.popcont').show(500);
	win.find("a.control").html('Close');
	if(autoclose) timedclose(win);
	linkClose(win);
};

function closepop(win){
	win.animate(startcss(win),1000).find('.popcont').hide(500);
	linkOpen(win);
};

function timedclose(win){
	setTimeout(function(){
		closepop(win);
		linkOpen(win);
		win.find("a.control").html('Open');
	},20000);
	win.addClass("unpopped").removeClass("popped");
};

function linkOpen(win){
	win.find("a.control").unbind("click").click(function(){
		$(this).html('Close');
		openpop(win);
		return false;
	});
	win.addClass("unpopped").removeClass("popped");	
};

function linkClose(win){
	win.find("a.control").unbind("click").click(function(){
		$(this).html('Open');
		closepop(win);
		return false;
	});
	win.addClass("popped").removeClass("unpopped");
};

