(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;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()
{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;var directWH=self.getDirectWH($anchor);if(directWH)thisStyleStr=self.replaceOrAddWH(thisStyleStr,directWH);var popWindow=thisStyleStr?window.open(href,target,thisStyleStr):window.open(href,target);self.tryToResize(popWindow,directWH);self.tryToFocus(popWindow);return false;}}
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;var width=directWH?directWH.w:styleW;var height=directWH?directWH.h:styleH;try{popWindow.resizeTo(width,height);}catch(e){}}
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;}
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);
