/*
 * Various classes for displaying and interacting with 
 * DHTML popups. Could include simple one screen popups
 * or multistep subscription popups. Expects calling code
 * to supply markup, style, and dimensions. Can optionally
 * use an element to black out the screen while the popup
 * is being displayed.
 *
 */
(function(a){DHTMLPopup=Object.subClass({cookie_name:null,init:function(c,b){this.popup=a("#"+c);if(a.isNullOrUndefined(b)){b=false}this.id=c;this.use_iframe=b;a("#"+c+" .closePopup").each(a.proxy(function(e,f){var d=$j(f).closest(".dhtml_popup");if(d.length<=0||d.attr("id")==c){d=this}$j(f).click(function(g){g.stopPropagation();g.preventDefault();d.hide();return false})},this));a.data(this.popup.get(0),"dhtmlpopup",this);if(this.popup.hasClass("showNow")){this.showNow()}this.initialized=true},setCookieName:function(b){this.cookie_name=b},setIframe:function(b){},visible:function(){return a(this.popup).is(":visible")},setPageMask:function(b){this.page_mask=a("#"+b).length>0?a("#"+b):false},show:function(c,b){if(this.visible()){return false}if(this.cookie_name){if(!BookingBuddy.getCookie(this.cookie_name)){BookingBuddy.createCookie(this.cookie_name,true,60*24)}else{return false}}if(this.page_mask&&(!$j.browser.msie||$j.browser.version!=6)){if(document.body){this.page_mask.css({height:(a(document.body).outerHeight())+"px",width:(a(document.body).outerWidth())+"px"})}if(this.popup.css("zIndex")){a(this.page_mask).css("zIndex",this.popup.css("zIndex")-1)}a(this.page_mask).show()}var e=null;if(a.isBoolean(c)){e={top:0,left:0}}else{if(a.isString(c)){c=(/[\w\d]+/).exec(c);e=c?a("#"+c).position():this._getCenterPos()}else{e=c?c.position():this._getCenterPos()}}if(b){e.top=Math.max(e.top+b.top,0);e.left=Math.max(e.left+b.left,0)}this.popup.css({top:e.top+"px",left:e.left+"px"});this.popup.show();this.popup.trigger("dhtmlpopup:show");if(!this.iframe&&this.use_iframe){var d=this.popup.attr("id")+"_iframe";this.iframe=this._createIframe(d)}if(this.use_iframe){this.iframe.show()}return true},showNow:function(){this.show()},hide:function(b){if(this.iframe&&this.use_iframe){this.iframe.hide()}if(this.page_mask){this.page_mask.hide()}this.popup.hide();this.popup.trigger("dhtmlpopup:hide")},_createIframe:function(d){var c=a("iframe",{id:d,src:"",frameBorder:"0",scrolling:"no"});var b=this.page_mask?this.page_mask.css("zIndex"):this.popup.css("zIndex");c.css({"background-color":"transparent",display:"none",position:"absolute",top:parseInt(this.popup.css("top"),10)+10+"px",left:parseInt(this.popup.css("left"),10)+10+"px",height:(this.popup.outerHeight()-20)+"px",width:(this.popup.outerWidth()-20)+"px",border:"0","z-index":b-1});a(c).insertAfter(this.popup);return c},_getCenterPos:function(){var b=(a(window).height()-this.popup.outerHeight())/2;var c=(a(window).width()-this.popup.outerWidth())/2;return{top:b,left:c}}});DHTMLPopup_Sub=DHTMLPopup.subClass({api_url:"/ajax/sub.php",cookie_name:"BBDHTMLPopup_Sub",init:function(c,b){this._super(c,b);this.popup_form=a(this.popup).find("form")},setAPIURL:function(b){this.api_url=b},submit:function(b){if(!this.submitting){var c=this.popup_form.serialize();a.post(this.api_url,c,a.proxy(function(d){if("error"==d.status){this.popup.trigger("dhtmlpopup:sub:error",[d])}else{this.popup.trigger("dhtmlpopup:sub:success",[d])}this.submitting=false},this),"json")}return false}});DHTMLPopup_Affiliate=DHTMLPopup.subClass({api_url:"/ajax/af.php",setAPIURL:function(b){this.api_url=b},submit:function(e,d,f){var g=$j("#"+e);var b=g.find("input[name='"+d+"']");var c=g.find("input[name='"+f+"']");var h={af:b.val(),sc:c.attr("checked")};a.post(this.api_url,h,a.proxy(function(i){if("error"==i.status){this.popup.trigger("dhtmlpopup:af:error")}else{this.popup.trigger("dhtmlpopup:af:success",[i.url])}},this),"json");return false}});DHTMLPopup_CheckRates=DHTMLPopup.subClass({init:function(c,b){this._super(c,b);this.triggers=a(".checkHotelRatesTrigger");this.triggers.click(a.proxy(function(f){var g=a(f.currentTarget).attr("id");var d=g.substr(g.lastIndexOf("_")+1);$j("#hotelcheckrates_form_aderrors").hide();this.show(d)},this))},show:function(d,f){var b="check_rates_popup_location_"+d;if(!a.emptyString(d)){this.hide();var e=a("#check_rates_popup_location_"+d);var c=a("#hotel_name_"+d).html();if(!a.emptyString(c)){a("#dhtml_hotel_search_title").html(c);a("#hotelcheckrates_provider").val(c);a("#hotelcheckrates_provider").data("hotel_id",d);a("#hotelcheckrates_provider").change();a("#hotelcheckrates_provider").blur()}if(f){b=false}}else{b=false}this._super(b)},showNow:function(){var d=a(".popup_location");if(d.length>0){var b=d.attr("id");var c=b.substr(b.lastIndexOf("_")+1);this.show(c,true)}}});DHTMLPopup_Loader=DHTMLPopup.subClass({init:function(c,b){this.target=a("#"+c);if(a("#widget_overlay").length){this.overlay=a("#widget_overlay").hide()}else{this.overlay=a('<div id="widget_overlay"><div id="widget_loader"></div></div>').appendTo("body").hide()}},show:function(f,e){var c=this.target.offset();var b=this.target.height();var d=this.target.width();this.overlay.height(b).width(d);this.overlay.css({top:c.top+"px",left:c.left+"px"});this.overlay.show()},hide:function(b){if(!b){b=0}this.overlay.delay(b).hide(0)}});DHTMLPopup_Factory={types:{base:DHTMLPopup,sub:DHTMLPopup_Sub,affiliate:DHTMLPopup_Affiliate,checkRates:DHTMLPopup_CheckRates,loader:DHTMLPopup_Loader},create:function(c){var d=null;if(!a.emptyString(c.id)){var b=this.types[c.type];if(a.isNullOrUndefined(b)){b=this.types.base}d=new b(c.id,c.use_iframe)}return d}}})(jQuery);