var OrderCatalog=function(params){if(typeof jQuery=="undefined"){throw new Error("OrderCatalog requires 'jQuery'.")}if(typeof jQuery.tmpl=="undefined"){throw new Error("OrderCatalog requires 'jQuery templates plugin'.")}if(typeof jQuery.colorbox=="undefined"){throw new Error("OrderCatalog requires 'ColorBox'.")}if(typeof jQuery.mask=="undefined"){throw new Error("OrderCatalog requires 'jQuery mask plugin'.")}if(typeof jQuery.validator=="undefined"){throw new Error("OrderCatalog requires 'jQuery validation plugin'.")}var URL_FORM="/order_catalog/form.html";var URL_GET_CATALOGS="/cgi/api/catalogs.cgi";var URL_SUBMIT_ORDER="/cgi/api/catalogs/order.cgi";var COLORBOX={CSS_CLASS:"order_catalog default",TRANSITION:"elastic",SPEED:350,OPACITY:0.85,HEIGHT:false,MAX_HEIGHT:"98.5%",WIDTH:"715px",MAX_WIDTH:"98.5%",OVERLAY_CLOSE:true,TITLE:"Infomaterial und Kataloge"};var MARKUP_CATALOG=['<div class="catalog ${position}">','<input name="catalogs" id="${uid}" class="field checkbox" type="checkbox" value="${id}" title="${title}" />','<label for="${uid}">','<span class="title">${title}</span>','<span class="description">${description}</span>',"</label>","</div>"].join("");$.template("catalogTemplate",MARKUP_CATALOG);var MARKUP_SUCCESS=['<div class="success">',"<h1>Vielen Dank für Ihre Katalogbestellung.</h1>","<p>Wir haben diese erhalten und werden in Kürze folgende Kataloge an Sie verschicken:</p>",'<ul class="catalogs"></ul>',"</div>"].join("");$.template("successTemplate",MARKUP_SUCCESS);var MARKUP_ORDERED_CATALOG=['<li class="catalog">${title}</li>'].join("");$.template("orderedCatalogTemplate",MARKUP_ORDERED_CATALOG);var MARKUP_ERROR_EXPLANATION=['<div class="error_explanation">',"<h1>Bei Ihrer Anfrage ist ein Problem aufgetreten</h1>","</div>"].join("");$.template("errorExplanationTemplate",MARKUP_ERROR_EXPLANATION);var MARKUP_ERRORS=['<ul class="errors"></ul>'].join("");$.template("errorsTemplate",MARKUP_ERRORS);var MARKUP_ERROR=['<li class="error">${error}</li>'].join("");$.template("errorTemplate",MARKUP_ERROR);if(params.target==undefined){throw new Error("OrderCatalog constructor requires a 'target'.")}var self=this;var country=params.country?params.country:"de-DE";var checkboxSettings=params.checkboxSettings?params.checkboxSettings:new Object({});var mask=params.mask?params.mask:"99999";var colorbox;var colorBoxLoadedContent;var loading;var messsages;var form;this.init=function(){_init()};var _init=function(){if(params.target){if($.isArray(params.target)){$.each(params.target,function(index,element){var target=$(element);target.click(function(){_onClick(target)})})}else{var target=$(params.target);target.click(function(){_onClick(target)})}}};var _onClick=function(target){$.colorbox.style("colorbox-default");colorbox=$("div#colorbox");target.colorbox({href:URL_FORM,transition:COLORBOX.TRANSITION,speed:COLORBOX.SPEED,opacity:COLORBOX.OPACITY,innerHeight:COLORBOX.HEIGHT,maxHeight:COLORBOX.MAX_HEIGHT,width:COLORBOX.WIDTH,maxWidth:COLORBOX.MAX_WIDTH,overlayClose:COLORBOX.OVERLAY_CLOSE,title:function(){return COLORBOX.TITLE},onOpen:function(){colorbox.addClass(COLORBOX.CSS_CLASS)},onComplete:function(){_onComplete()},onClosed:function(){colorbox.removeClass(COLORBOX.CSS_CLASS)}})};var _onComplete=function(){colorBoxLoadedContent=colorbox.find("div#cboxLoadedContent");colorBoxLoadedContent.css({overflow:"hidden"});loading=colorBoxLoadedContent.find("div.loading");messages=colorBoxLoadedContent.find("div.messages");form=colorBoxLoadedContent.find("form");_getCatalogs()};var _getCatalogs=function(){try{$.ajax({url:URL_GET_CATALOGS,type:"GET",dataType:"json",success:function(data){if(data){if(data.result){var elements=form.find("fieldset.catalogs div.elements");$.each(data.result,function(i,catalog){var uid=_uid("uid");var position=(i%2==0)?"left":"right";$.tmpl("catalogTemplate",{uid:uid,id:catalog.id,title:catalog.title,description:catalog.description,position:position}).appendTo(elements)});_onCatalogsLoaded()}else{$.each(data.errors,function(i,error){throw new Error(error.description)})}}}})}catch(error){}};var _onCatalogsLoaded=function(){_prepareForm();form.show();loading.hide();$.colorbox.resize()};var _prepareForm=function(){form.find("input:checkbox").checkbox(checkboxSettings).bind("disable",function(){this.checked=false});form.find("input#zip_code").mask(mask,{placeholder:""});$.validator.addMethod("atLeastOneCatalogIsSelected",function(value,element){return false});$.validator.addMethod("zipCodeAndCityMissing",function(value,element){return false});form.validate({submitHandler:function(){_onFormSubmit()},errorClass:"error",errorElement:"span",errorPlacement:function(error,element){if(element.parent().hasClass("catalog")){error.prependTo(form.find("fieldset.catalogs div.elements")).hide().css("height","auto").slideDown("fast",function(){$.colorbox.resize()})}else{error.insertAfter(element).hide().css("height","auto").slideDown("fast",function(){$.colorbox.resize()})}},rules:{catalogs:{atLeastOneCatalogIsSelected:{depends:function(element){return((form.find("input[name=catalogs]:checkbox:checked").length>=1)?0:1)}}},zip_code:{required:"input#zip_code:filled",zipCodeAndCityMissing:{depends:function(element){return form.find("input#zip_code:blank").length&&form.find("input#city:blank").length}}},city:{required:"input#zip_code:filled",zipCodeAndCityMissing:{depends:function(element){return form.find("input#zip_code:blank").length&&form.find("input#city:blank").length}}}},messages:{catalogs:{atLeastOneCatalogIsSelected:"Bitte wählen Sie mindestens einen Katalog aus."},person:"Bitte geben Sie Ihren Namen ein.",email:{required:"Bitte geben Sie Ihre E-Mail-Adresse ein.",email:"Bitte geben Sie eine gültige E-Mail-Adresse ein."},address:"Bitte geben Sie Ihre Anschrift ein.",zip_code:{required:"Bitte geben Sie Ihre Postleitzahl ein.",zipCodeAndCityMissing:"Bitte geben Sie Ihre Postleitzahl und den Wohnort ein."},city:{required:"Bitte geben Sie Ihren Wohnort ein.",zipCodeAndCityMissing:""}}});form.find("a.submit").click(function(){form.submit();return false})};var _onFormSubmit=function(){form.hide();loading.show();$.colorbox.resize();var orderedCatalogs=[];$.each(form.find("input[name=catalogs]:checked"),function(i,catalog){var catalog=$(catalog);orderedCatalogs.push({title:catalog.attr("title")})});var action="action=order&";var data=action+form.serialize();data=data.replace(/catalogs=/g,"c=");$.ajax({type:"post",url:URL_SUBMIT_ORDER,data:data,success:function(result){var result=eval("("+result+")");if(result.error){var errors=$.tmpl("errorsTemplate",{});$.each(result.error,function(i,error){$.tmpl("errorTemplate",{error:error}).appendTo(errors)});var errorExplanation=$.tmpl("errorExplanationTemplate",{}).appendTo(messages);errors.appendTo(errorExplanation)}else{var success=$.tmpl("successTemplate",{}).appendTo(messages);var catalogs=$.tmpl("orderedCatalogTemplate",orderedCatalogs).appendTo(success.find("ul.catalogs"))}loading.hide();messages.show();$.colorbox.resize();$.colorbox.resize()}});return false};var _uid=function(prefix){if(!prefix){prefix="uid"}var date=new Date().getMilliseconds();var nummber=Math.random();var random=Math.round(nummber*100000);return prefix+date+random};_init()};
