/* Jasongreen's jquery popup
 * @author jasongreen
 * use what the fuck of license
 */

$.jjpopup = $.jjpopup || {}
$.jjpopup.st = 0;
$.jjpopup.el = []
$.fn.popup=function(){
	 return this.each(function(){
		//check if backgroudPopup exists
		if(!document.getElementById('backgroundPopup')){
			//$(this).after('<div id="backgroundPopup"></div>')
			$(this).after('<div id="backgroundPopup" style="background:#000000;display:none;position:fixed;_position:absolute;height:100%;width:100%;top:0;left:0;background:#000000;border:1px solid #cecece;z-index:1;"></div>')
		}
		
		//loads popup only if it is disabled
		if($.jjpopup.st==0){
			
			//request data for centering
			var windowWidth = document.documentElement.clientWidth;
			var windowHeight = document.documentElement.clientHeight;
			var popupHeight = $(this).height();
			var popupWidth = $(this).width();
			//centering
			$(this).css({
				"position": "absolute",
				"top": windowHeight/2-popupHeight/2,
				"left": windowWidth/2-popupWidth/2,
				"z-index": 100
			});
			
			if('transparent'==$(this).css('background-color')) $(this).css('background-color','#ffffff')
			//only need force for IE6
			
			$("#backgroundPopup").css({
				"height": windowHeight
			});
			
			//popup ...
			$("#backgroundPopup").css({
				"opacity": "0.6"
			});
			
			$("#backgroundPopup").fadeIn("slow");
			$(this).fadeIn("slow");
			$.jjpopup.el.push(this)
			$.jjpopup.st = 1;
		}
	})
}

$.fn.disablePopup=function(){
	this.each(function(){
		//disables popup only if it is enabled
		if($.jjpopup.st==1){
			$("#backgroundPopup").fadeOut("slow");
			$(this).fadeOut("slow");
			$.jjpopup.st = 0;
		}
	})
}

disablePopups = function(){
	$("#backgroundPopup").fadeOut("slow");
	for(x in $.jjpopup.el){
		$($.jjpopup.el[x]).fadeOut("slow");
	}
	$.jjpopup.st = 0;	
}

$.fn.bindPopup = function(c){
	this.each(function(){
		$(this).click(function(){
			$('#'+c).popup()
		})
	})
}

$.fn.bindClosePopup = function(){
	this.each(function(){
		$(this).click(function(){
			disablePopups()
		})
	})
}

