
(function($){$.fn.box=function(options){var defaults={display:'none',width:'300px',height:'200px',zindex:'3000',onclose:false,beforeopen:false,beforeclose:false,txtback:'&lt;',txtnext:'&gt;',txtclose:'x',txttitle:'Info',dpltitle:true,iframe:false,ajax:false,json:false,waitonly:false,gallery:false,boxborder:"",iebugsrc:'/common/img/leer.gif',clickable:true,closealways:true}
return this.each(function(){defaults.display=$(this).css('display');$(this).data('box',$.extend(defaults,options));$.box.init(this);});};jQuery.box={div:"<div><\/div>",pspan_o:"<p><span>",pspan_c:"<\/span><\/p>",curr_obj:false,init:function(el){el=this.get_el(el);el.opt=$(el).data('box');el.opt.box_window=false;el.opt.box_overlay=false;el.opt.viewpreload=false;el.opt.closable=true;if(el.opt.gallery){el.opt.gallery=this.preload(el);}},preload:function(el){el=this.get_el(el);var view,tmp=[];$(el).find("img").each(function(i){view=new Image;view.src=$(this).parents('a').attr("href");view.title=this.title;view.index=i;view.load=0;this.index=tmp.length;$(this).click(function(){$.box.open(el,this.index);});$(this).parents('a').click(function(){return false;});tmp.push({view:view});});return tmp;},open:function(el){el=this.get_el(el);el.opt=$(el).data('box')||false;if(!el.opt)return;if(el.opt.beforeopen){el.opt.beforeopen($(el).get(0));}
if(!this.curr_obj_check(el))return;el.opt.closable=false;this.overlay(el);if(el.opt.gallery){if(typeof arguments[1]==="undefined"){return false;}
this.init_gallery(el,arguments[1]);}else if(el.opt.ajax){if(typeof arguments[1]==="undefined"){this.init_box(el);return false;}
this.init_ajax(el,arguments[1]);}else if(el.opt.json){if(typeof arguments[1]==="undefined"){this.init_box(el);return false;}
this.init_json(el,arguments[1]);}else if(el.opt.iframe){if(typeof arguments[1]!=="undefined"){el.opt.iframe=arguments[1];}
this.init_box(el);}else{this.init_box(el);}},init_box:function(el){el=this.get_el(el);el.opt=$(el).data('box')||false;if(!el.opt)return;if(el.opt.box_window){el.opt.box_window_past=el.opt.box_window;el.opt.box_window=false;$(el.opt.box_window_past).css('zIndex',(el.opt.zindex+1)).fadeOut(function(){$(el.opt.box_window_past).remove()});}else{el.opt.box_holder=$(this.div).css('display','none');$(el).before($(el.opt.box_holder));}
el.opt.box_window=$(this.div).appendTo(document.body).attr('id','box-window').css({'zIndex':el.opt.zindex,'display':'none','margin':'0'});if(el.opt.boxborder){$(el.opt.box_window).css('border',el.opt.boxborder);}
if(el.opt.gallery){if(el.opt.dpltitle){var idx=(typeof arguments[1]!=="undefined")?arguments[1]:0;var view=el.opt.gallery[idx].view;var image_next=(idx<el.opt.gallery.length-1)?idx+1:0;var image_back=(idx>0)?idx-1:el.opt.gallery.length-1;el.opt.box_header=$(this.div).appendTo(el.opt.box_window).attr('id','box-image-header');el.opt.box_close=$(this.pspan_o+el.opt.txtclose+this.pspan_c).appendTo(el.opt.box_header).attr('id','box-close').click(function(){$.box.close(el)});el.opt.box_next=$(this.pspan_o+el.opt.txtnext+this.pspan_c).appendTo(el.opt.box_header).attr('id','box-next').click(function(){$.box.init_gallery(el,image_next)});el.opt.box_back=$(this.pspan_o+el.opt.txtback+this.pspan_c).appendTo(el.opt.box_header).attr('id','box-back').click(function(){$.box.init_gallery(el,image_back)});}
el.opt.box_content=$(this.div).appendTo(el.opt.box_window).attr('id','box-image-content');el.opt.box_image=$("<img />").appendTo(el.opt.box_content).attr({src:view.src}).css({width:view.width+"px",height:view.height+"px"});el.opt.box_title=$(this.div).appendTo(el.opt.box_content).attr('id','box-image-title').css({width:view.width+"px"}).html(view.title);el.opt.box_image.click(function(){$.box.init_gallery(el,image_next)});}else{if(el.opt.dpltitle){el.opt.box_header=$(this.div).appendTo(el.opt.box_window).attr('id','box-header');el.opt.box_close=$(this.pspan_o+el.opt.txtclose+this.pspan_c).appendTo(el.opt.box_header).attr('id','box-close').click(function(){$.box.close(el)});el.opt.box_title=$("<h3>"+el.opt.txttitle+"</h3>").appendTo(el.opt.box_header).attr('id','box-title');}
if(el.opt.width){$(el.opt.box_window).css('width',el.opt.width);}
if(el.opt.iframe){el.opt.box_iframe=$("<iframe border='0' frameborder='0'></iframe>").appendTo(el).attr({'id':'box-iframe','src':el.opt.iframe}).css({'height':el.opt.height});}
el.opt.box_content=$(this.div).appendTo(el.opt.box_window).attr('id','box-content').css({'height':el.opt.height});$(el).appendTo(el.opt.box_content).show();}
this.set_box_pos(el);this.overlay_size(el);if(el.opt.waitonly){el.opt.closable=true;}else{$(el.opt.box_window).fadeIn(function(){$(el.opt.box_overlay).css('backgroundImage','none');el.opt.closable=true;});}},init_gallery:function(el,idx){el=this.get_el(el);el.opt=$(el).data('box')||false;if(!el.opt)return;var view=el.opt.gallery[idx].view;clearTimeout(el.opt.viewpreload);el.opt.viewpreload=false;if(!view.height){if(view.load<30){view.load++;el.opt.viewpreload=setTimeout(function(){$.box.init_gallery(el,idx)},1000);}else{alert("unavailable: "+view.src);}
return;}
this.init_box(el,idx);},init_ajax:function(el,url){el=this.get_el(el);el.opt=$(el).data('box')||false;if(!el.opt||typeof url!=="string")return;$.ajax({url:url,error:function(){alert("No data available!");$.box.close(el,true);},success:function(response){$(el).html(response);$.box.init_box(el);}});},init_json:function(el,url){el=this.get_el(el);el.opt=$(el).data('box')||false;if(!el.opt||typeof el.opt.json!=="function")return;$.ajax({url:url,dataType:'json',error:function(){alert("No data available!");$.box.close(el,true);},success:function(response){el.opt.json($(el).get(0),response);$.box.init_box(el);}});},overlay:function(el){el=this.get_el(el);el.opt=$(el).data('box')||false;if(!el.opt)return;if(el.opt.box_overlay){return false;}
el.opt.box_overlay=$(this.div).appendTo(document.body).attr('id','box-overlay').css('zIndex',(el.opt.zindex-3));if(el.opt.clickable){$(el.opt.box_overlay).click(function(){$.box.close(el);});}
this.ie6fix(el.opt.box_overlay,el.opt.iebugsrc);this.overlay_size(el);},overlay_size:function(el){el=this.get_el(el);el.opt=$(el).data('box')||false;if(!el.opt)return;var overlay_height=($.browser.msie&&$.browser.version<7)?"height":"minHeight";$(el.opt.box_overlay).css(overlay_height,$.box.client().height+'px').css('backgroundPosition','50% '+($(window).height()/2+$.box.client().scrolltop-50)+'px');},close:function(el){var forceclose=(typeof arguments[1]!=="undefined")?arguments[1]:false;el=this.get_el(el);el.opt=$(el).data('box')||false;if(!el.opt||!el.opt.box_overlay||(!el.opt.closable&&!forceclose))return;if(el.opt.beforeclose){el.opt.beforeclose($(el).get(0));}
if(el.opt.gallery){clearTimeout(el.opt.viewpreload);el.opt.viewpreload=false;}else{if(el.opt.iframe){$(el.opt.box_iframe).remove();}
if(el.opt.display=="none"){$(el).hide();}
if(el.opt.box_window){$(el.opt.box_holder).before($(el));$(el.opt.box_holder).remove();}}
if(el.opt.box_window){$(el.opt.box_window).remove();el.opt.box_window=false;}
$(el.opt.box_overlay).remove();el.opt.box_overlay=false;if(el.opt.onclose){el.opt.onclose($(el).get(0));}
this.curr_obj=false;},set_box_pos:function(el){el=this.get_el(el);el.opt=$(el).data('box')||false;if(!el.opt||!el.opt.box_window)return;var left=($(window).width()/2-$(el.opt.box_window).outerWidth()/2+$.box.client().scrollleft)+"px";var top=($(window).height()/2-$(el.opt.box_window).outerHeight()/2+$.box.client().scrolltop)+"px";if($(window).height()>$(el.opt.box_window).outerHeight()){$(el.opt.box_window).css("top",top).css("left",left);}else{$(el.opt.box_window).css("top",0).css("left",left);}},curr_obj_check:function(el){var ret=true;el=this.get_el(el);if(this.curr_obj&&this.curr_obj!==el){this.curr_obj.opt=$(this.curr_obj).data('box')||false;if(!this.curr_obj.opt)return true;if(this.curr_obj.opt.closealways){this.close(this.curr_obj,true);this.curr_obj=el;ret=true;}else{ret=false;}}else{this.curr_obj=el;}
return ret;},client:function(){var height,width;var doc_el=document.documentElement;var doc_bd=document.body;var scrolltop=Math.max(doc_el.scrollTop,doc_bd.scrollTop);var scrollleft=Math.max(doc_el.scrollLeft,doc_bd.scrollLeft);if($.browser.msie&&$.browser.version<7){var scrollHeight=Math.max(doc_el.scrollHeight,doc_bd.scrollHeight);var scrollWidth=Math.max(doc_el.scrollWidth,doc_bd.scrollWidth);var offsetHeight=Math.max(doc_el.offsetHeight,doc_bd.offsetHeight);var offsetWidth=Math.max(doc_el.offsetWidth,doc_bd.offsetWidth);height=(scrollHeight<offsetHeight)?$(window).height():scrollHeight;width=(scrollWidth<offsetWidth)?$(window).width():scrollWidth;}else{height=$(document).height();width=$(document).width();}
return{width:width,height:height,scrolltop:scrolltop,scrollleft:scrollleft}},jalert:function(str_msg,str_butt,str_title){var id="jalert-box-window";var div="<div><\/div>";var p="<p><\/p>";var ht=(typeof arguments[3]!=="undefined"&&arguments[3])?parseInt(arguments[3]):100;var wd=(typeof arguments[4]!=="undefined"&&arguments[4])?parseInt(arguments[4]):220;var is_avail=$("#"+id).length;if(!is_avail){$(div).appendTo($(document.body)).css('display','none').attr('id',id);}else{$.box.close(id);$("#"+id).html('');}
var msg=$(p).appendTo($("#"+id)).html(str_msg);var butt_wrap=$(div).appendTo($("#"+id)).addClass('jalert-button-wrapper');var butt=$(p).appendTo($(butt_wrap)).html(str_butt).addClass('jalert-button-gray').click(function(){$.box.close(id);});$("#"+id).box({width:wd,height:ht,txttitle:str_title});$.box.open(id);},jwait:function(){var id="jwait-box-window";var div="<div><\/div>";var is_avail=$("#"+id).length;if(!is_avail){$(div).appendTo($(document.body)).css('display','none').attr('id',id);$("#"+id).box({waitonly:true});}
$.box.open(id);},get_el:function(el){return(typeof el==="string")?$("#"+el):$(el);},ie6fix:function(parent,src){if(!($.browser.msie&&$.browser.version<7)||!src){return;}
if(src.indexOf("http")==-1){src=self.location.protocol+"\/\/"+self.location.host+src;}
var iframe=document.createElement("iframe");iframe.src=src;iframe.style.height="100%";iframe.style.width="100%";iframe.style.filter="alpha(opacity=0)";$(parent).append(iframe);iframe.contentWindow.document.onclick=function(){iframe.parentNode.click();}}}})(jQuery);
