/**
 * zudolab jqPopWin
 *
 * @version    1
 * @copyright    (c)2008 Takeshi Takatsudo (http://zudolab.net/)
 * @license    MIT (http://www.opensource.org/licenses/mit-license.php)
 */
(function($){

	jqPopWin = function(settings)
	{
		this.selector = settings.selector ? settings.selector : null;
		this.focusWin = (settings.focusWin===false) ? false : true;
		this.resizeWin = settings.resizeWin ? settings.resizeWin : false;
		this.name = settings.name ? settings.name : "_blank";
		this.style = settings.style ? settings.style : null;
		this.$anchors = [];
		this.styleStr = null;
		
		/* exec setup when onload */
		var self = this;
		$(function(){ self.setup(); });
	}
	
	jqPopWin.prototype.setup = function()
	{
		this.$anchors = $(this.selector);
		if(this.$anchors.length==0) return;
		this.generateStyleStr();
		this.setEvents();
	}
	jqPopWin.prototype.setEvents = function()
	{
		var self = this;
		this.$anchors.click(openWindow);
		function openWindow()
		{
			/* prepare */
			var $anchor = $(this);
			var href= this.href;
			var target = (this.target && this.target!="_blank") ? this.target : (self.name ? self.name : "_blank");
			var thisStyleStr = self.styleStr ? self.styleStr : null;
			
			/* replace or add width,height style if direct width,height was specified */
			var directWH = self.getDirectWH($anchor);
			if(directWH) thisStyleStr = self.replaceOrAddWH(thisStyleStr, directWH);
			
			/* window open */
			var popWindow = thisStyleStr ?
				window.open(href, target, thisStyleStr) :
				window.open(href, target);
			
			/* try to resize, focus */
			self.tryToResize(popWindow,directWH);
			self.tryToFocus(popWindow);
			
			return false;
		}
	}
	/*
	 * window manipulation
	 */
		jqPopWin.prototype.tryToFocus = function(popWindow)
		{
			if(!this.focusWin) return;
			try{ popWindow.focus(); }catch(e){}
		}
		jqPopWin.prototype.tryToResize = function(popWindow, directWH)
		{
			if(!this.resizeWin) return;
			var styleW = this.getSpecifiedStyleValue("width");
			var styleH = this.getSpecifiedStyleValue("height");
			if(!(styleW && styleH) && !directWH) return; // return if width/height was not specified
			var width = directWH ? directWH.w : styleW;
			var height = directWH ? directWH.h : styleH;
			try{ popWindow.resizeTo(width,height); }catch(e){}
		}
	/*
	 * style string manipulation
	 */
		jqPopWin.prototype.generateStyleStr = function()
		{
			if(!this.style) return;
			var styleStr = "";
			for(var i=0,current; current=this.style[i]; i++){
				if(i!=0){
					styleStr = styleStr + ",";
				}
				var styleName = current[0];
				var styleValue = current[1];
				styleStr = styleStr + styleName + "=" + styleValue;
			}
			this.styleStr =  styleStr;
		}
		jqPopWin.prototype.replaceOrAddWH = function(styleStr, WH)
		{
			if(WH.w){
				if(!styleStr){
					styleStr = "width="+WH.w;
				}else{
					if(styleStr.indexOf("width=")>-1 && WH.w){
						styleStr = styleStr.replace(/width=[0-9]+/,"width="+WH.w);
					}else{
						styleStr = styleStr + "width="+WH.w;
					}
				}
			}
			if(WH.h){
				if(!styleStr){
					styleStr = "height="+WH.h;
				}else{
					if(styleStr.indexOf("height=")>-1 && WH.h){
						styleStr = styleStr.replace(/height=[0-9]+/,"height="+WH.h);
					}else{
						styleStr = styleStr + "height="+WH.h;
					}
				}
			}
			return styleStr;
		}
		jqPopWin.prototype.getSpecifiedStyleValue = function(styleName)
		{
			for(var i=0,currentSet; currentSet=this.style[i]; i++){
				if(currentSet[0]==styleName) return currentSet[1];
			}
			return null;
		}
	/*
	 * localized width, height getter
	 */
		jqPopWin.prototype.getDirectWH = function($anchor)
		{
			if(!$anchor.is("[class*= w_]")) return false;
			var classes = $anchor.attr("class").split(" ");
			for(var i=0,current; current=classes[i]; i++){
				if(current.indexOf("w_")==0){
					var strArray = current.split("_");
					var wVal = strArray[1] ? strArray[1] : null;
					var hVal = strArray[3] ? strArray[3] : null;
					break;
				}
			}
			return { w: wVal, h: hVal };
		}
	
})(jQuery);


new jqPopWin({
	selector: "a.fdbk",
	style: [
		["width", 256],
		["height", 260],
		["top", 150],
		["left", 300],
		["directories", "no"],
		["location", "no"],
		["menubar", "no"],
		["resizable", "no"],
		["scrollbars", "no"],
		["status", "no"],
		["toolbar", "no"]
	]
});
new jqPopWin({
	selector: "a.cont",
	style: [
		["width", 262],
		["height", 216],
		["top", 150],
		["left", 300],
		["directories", "no"],
		["location", "no"],
		["menubar", "no"],
		["resizable", "no"],
		["scrollbars", "no"],
		["status", "no"],
		["toolbar", "no"]
	]
});
