/* common
 */
try{document.execCommand('BackgroundImageCache',false,true);}catch(e){}if(!window.console){window.console={log:function(){alert(arguments[0]);}};}if(typeof app=='undefined'){app={};}app.common={box:function(){var i=jQuery('img',this);jQuery('li',this).each(function(){var d=jQuery(this).metadata();if(!d.file_url_box){return;}var pl=new Image();pl.src=d.file_url_box;jQuery(this).hover(function(){var c=i.attr('src');if(d.file_url_box!=c){i.attr('src',d.file_url_box);}jQuery(this).addClass('over').data('curr_file_url_box',c);},function(){var c=jQuery(this).data('curr_file_url_box');if(c){i.attr('src',c);}jQuery(this).removeClass('over').data('curr_file_url_box',null);}).click(function(e){var t=$('a',this).attr('target');if(t){window.open($('a',this).attr('href'),t);}else{location.href=$('a',this).attr('href');}e.stopPropagation();e.preventDefault();return false;}).css('cursor','pointer');});},isFullscreen:function(){return $('#body').is('.fullscreen');},toggleFullscreen:function(after,before,noani){if(after&&!jQuery.isFunction(after)){noani=after;after=null;}if(before&&!jQuery.isFunction(before)){noani=before;before=null;}if($('#body').is('.fullscreen')){$('#body').removeClass('fullscreen');if(before)before('fullscreen');$('#sidebar')[noani?'show':'fadeIn'](function(){if(after)after();});}else{if(before)before();$('#sidebar').css('display','none');$('#body').addClass('fullscreen');if(after)after('fullscreen');}return false;},simpleTabs:function(s,cb){var c=jQuery(s);var cid=s.replace(/[^a-zA-Z0-9\-_]/g,'');jQuery('.nav li a',c).each(function(){var e=jQuery(this);var f=jQuery('#'+jQuery(this).attr('href').split('#')[1]);f.hide();e.bind('click',function(){var curr=c.find('.visible');curr.hide().removeClass('visible');f.show().addClass('visible');c.find('.active').removeClass('active');jQuery(this).addClass('active');jQuery.cookie('simpleTabs_'+cid,jQuery(this).attr('href'),{path:'/'});if(jQuery.isFunction(cb)){cb.apply(this,[f,curr]);}jQuery(this).blur();return false;});});var cookie=jQuery.cookie('simpleTabs_'+cid);if(cookie){var e=jQuery('.nav li a[href='+cookie+']',c);if(e.length){e.trigger('click');return;}}var a=jQuery('.nav li a.active',c);if(a.length){a.trigger('click');}else{jQuery('.nav li a:eq(0)',c).trigger('click');}},toInt:function(n){n=parseInt(n);return isNaN(n)?0:n;},encode:function(str){return jQuery('<div/>').text(str).html();},decode:function(str){return jQuery('<div/>').html(str).text();},truncate:function(str,length,suffix){if(length==undefined){length=80;}if(str.length<=length){return str;}if(suffix==undefined){suffix='...';}return str.substr(0,length-suffix.length).replace(/\s+?(\S+)?$/g,'')+suffix;},appendParam:function(url,param){return url+(url.indexOf('?')==-1?'?':'&')+param;},overlayLoading:function(el,pos){var loading=jQuery('<div class="loading loading-overlay" style="position:absolute;z-index:999;"><div class="bg"></div><div class="text">Loading...</div></div>');jQuery('body').addClass('hasLoading').append(loading);if(pos==undefined||pos==null||pos.length==0){var p=jQuery(el).position();pos={centerTop:parseInt(p.top+(jQuery(el).height()/2)),centerLeft:parseInt(p.left+(jQuery(el).width()/2))};}if(pos.centerTop!=undefined){var top=pos.centerTop-(loading.height()/2);loading.css('top',parseInt(top)+'px');}if(pos.centerLeft!=undefined){var left=pos.centerLeft-(loading.width()/2);loading.css('left',parseInt(left)+'px');}if(pos.top!=undefined){loading.css('top',parseInt(pos.top)+'px');}if(pos.bottom!=undefined){loading.css('top',parseInt(pos.bottom-loading.height())+'px');}if(pos.left!=undefined){loading.css('left',parseInt(pos.left)+'px');}if(pos.right!=undefined){loading.css('left',parseInt(pos.right-loading.width())+'px');}},contentLoading:function(el){jQuery(el).append(jQuery('<span class="loading loading-content"/>'));},appendLoading:function(e,css){var el=jQuery(e);var pos=el.position();css=jQuery.extend({},{position:'absolute',top:pos.top+2,left:pos.left+el.outerWidth()+5,display:'block'},css||{});el.after(jQuery('<span class="loading loading-append"/>').css(css));if(el.length>0&&el[0].tagName!=undefined&&el[0].tagName=='SELECT'){el[0].options.length=0;el[0].options[0]=new Option('Loading...','');}},insertLoading:function(e,css){var el=jQuery(e);if(el.is(':input')){if(el.is('select')){el[0].options.length=0;el[0].options[0]=new Option('Loading...','');}el.addClass('loading').attr('disabled',true);el.css('backgroundPosition',el.height()/2+'px '+el.height()-2+'px')}else{el.parent().css({'position':'relative',display:'block'});css=jQuery.extend({},{position:'absolute',top:'50%',right:'50%'},css||{});el.append(jQuery('<span class="loading loading-insert"/>').css(css));}},removeLoading:function(e){var el=jQuery(e);if(el.length>0&&el[0].tagName!=undefined&&el[0].tagName=='SELECT'){var o=jQuery('option:first',el);if(o.text()=='Loading...'){o.remove();}}if(el.next().is('.loading')){el.next().fadeOut('fast',function(){jQuery(this).remove();},200);}else if(jQuery('body').is('.hasLoading')){jQuery('body').removeClass('hasLoading').find('.loading').fadeOut('fast',function(){jQuery(this).remove();},200);}else{el.find('.loading').fadeOut('fast',function(){jQuery(this).remove();},200);}if(el.is(':input')){el.attr('disabled',false);}el.removeClass('loading');},textlimit:function(el,val){var limit=jQuery(el).data('textlimit_limit'),max=jQuery(el).data('textlimit_max');if(!limit){var d=jQuery(el).parent().find('.description');if(d==null||d.length==0){return;}var m=/(\d+)/.exec(d.text());if(!m){return;}var max=parseInt(m[1]);d.html(d.html().replace(/(\d+)/g,'<span class="limit">'+max+'</span>'));limit=d.find('.limit');jQuery(el).data('textlimit_limit',limit);jQuery(el).data('textlimit_max',max);}if(val==undefined){val=jQuery(el).val();}var s=jQuery.trim((val).replace(/<(?:.|\s)*?>/g,"").replace(/&nbsp;/g," ").replace(/\s+/g," "));limit.text(max-s.length)[s.length>max?'addClass':'removeClass']('limit-error');}};jQuery(function(){jQuery('.jsconfirm').live('click',function(){var text=null;if(jQuery.fn.metadata){var data=jQuery(this).metadata();if(data.jsconfirmtext!=undefined){text=data.jsconfirmtext;}}return confirm(app.common.decode(text));});jQuery('textarea:not(.rte)').each(function(){jQuery(this).keyup(function(){app.common.textlimit(this)});app.common.textlimit(this);});jQuery('.topbox').each(app.common.box);});

/* form
 */
app.form={clones:{},initPostForm:function(){jQuery("#fieldset-post_geo_group").find("dt").each(function(a){var b=jQuery(this).next(),c=jQuery(this).wrap("<div/>").parent().append(b).css({"float":"left",width:"150px"});a%2&&c.after('<div style="clear:both"></div>')}).end().append('<hr/><div id="post_geo_group_map"><input type="text" class="text small lat"/> <input type="text" class="text small lng"/> <input type="button" class="submit set" value="&Uuml;bernehmen"/><div class="map" style="width:600px;height:400px;margin:10px 0;"/><input type="text" class="text address" style="width:200px;"/> <input type="button" class="submit search" value="Suchen"/></div>').find("#post_geo_group_map").each(function(){var a=this,b=$("#latitude_correction").val()||$("#latitude").val(),c=$("#longitude_correction").val()||$("#longitude").val(),d,e;b!=""&&c!=""?(e=17,d=new google.maps.LatLng(b,c)):(e=6,d=new google.maps.LatLng(51,10)),$(".lat",this).val(d.lat()),$(".lng",this).val(d.lng());var f={zoom:e,center:d,mapTypeId:google.maps.MapTypeId.ROADMAP},g=new google.maps.Map($(".map",this).get(0),f),h=new google.maps.Marker({position:d,map:g,clickable:!1,draggable:!0});google.maps.event.addListener(h,"drag",function(b){$(".lat",a).val(b.latLng.lat()),$(".lng",a).val(b.latLng.lng())}),google.maps.event.addListener(h,"dragend",function(b){$(".lat",a).val(b.latLng.lat()),$(".lng",a).val(b.latLng.lng())});var i=new google.maps.Geocoder;$(".search").click(function(){i.geocode({address:$(".address",a).val()},function(b,c){c==google.maps.GeocoderStatus.OK?(g.setCenter(b[0].geometry.location),h.setPosition(b[0].geometry.location),$(".lat",a).val(b[0].geometry.location.lat()),$(".lng",a).val(b[0].geometry.location.lng())):alert("Geocode was not successful for the following reason: "+c);return!1})}),$(".set").click(function(){$("#latitude_correction").val($(".lat",a).val()),$("#longitude_correction").val($(".lng",a).val())})}),app.form.initCategorySelect(),app.form.initControlcenterSelect()},initVehicleForm:function(){app.form.initYearFields(),app.form.initEnginepowerFields(),app.form.initGroupSelect(),app.form.initVehicletypeSelect(),app.form.initCategorySelect(),app.form.initControlcenterSelect(),app.form.initPostIdElementAutocomplete()},initAdvancedSearchForm:function(){app.form.initGroupSelect("group-"),app.form.initVehicletypeSelect("group-"),app.form.initYearRangeFields(),app.form.initEnginepowerRangeFields("tech-"),app.form.initCubiccapacityRangeFields("tech-"),app.form.initGrossvehicleweightRangeFields("tech-"),app.form.initCategorySelect("geo-"),app.form.initControlcenterSelect("geo-"),app.form.initPostIdElementAutocomplete("geo-","postname"),app.form.initUserIdElementAutocomplete("misc-"),app.form.initUserIdElementAutocomplete("misc-","photographername","photographer_id")},initYearFields:function(){jQuery("#yearofconstruction-label,#yearofregistration-label,#yearofcommissioning-label,#yearofwithdrawal-label").each(function(a){var b=jQuery(this).next();b.find("select").css("width","auto").css("min-width","60px");var c=jQuery(this).wrap("<div/>").parent().append(b).css({"float":"left",width:"120px"});a%2&&c.after('<div style="clear:both"></div>')})},initYearRangeFields:function(){jQuery("#yearofconstruction_from-label,#yearofregistration_from-label,#yearofcommissioning_from-label,#yearofwithdrawal_from-label").each(function(a){var b="150px",c=jQuery(this).css("width",b).next().css("width",b),d=jQuery("#"+$(this).attr("id").replace(/from-label/,"to-element")+" select").css({width:"60px","float":"left","margin-left":"5px"});c.find("select").css({width:"auto",_width:"60px","min-width":"60px","float":"left"}).after('<div style="clear:both"></div>').after(d);var e=jQuery(this).wrap("<div/>").parent().append(c).css({"float":"left",width:b});a%2&&e.after('<div style="clear:both"></div>')}),jQuery("#yearofconstruction_to-label,#yearofregistration_to-label,#yearofcommissioning_to-label,#yearofwithdrawal_to-label").remove(),jQuery("#yearofconstruction_to-element,#yearofregistration_to-element,#yearofcommissioning_to-element,#yearofwithdrawal_to-element").remove()},initEnginepowerFields:function(a){a=a||"",$("#"+a+"enginepower_value").css({width:"auto","min-width":"225px"}).placeholder().after($("#"+a+"enginepower_unit").css({width:"auto","min-width":"60px","margin-left":"10px"})),$("#enginepower_unit-label,#enginepower_unit-element").remove()},initEnginepowerRangeFields:function(a){a=a||"";var b=$("#"+a+"enginepower_to_value").css({width:"75px","margin-left":"10px"}).placeholder(),c=$("#"+a+"enginepower_to_unit").css({width:"auto","min-width":"60px","margin-left":"5px"});$("#"+a+"enginepower_from_value").css({width:"75px"}).placeholder().after(b).after($("#"+a+"enginepower_from_unit").css({width:"auto","min-width":"60px","margin-left":"5px"})),b.after(c),$("#enginepower_from_unit-label,#enginepower_from_unit-element").remove(),$("#enginepower_to_value-label,#enginepower_to_value-element,#enginepower_to_unit-label,#enginepower_to_unit-element").remove()},initCubiccapacityRangeFields:function(a){a=a||"";var b=$("#"+a+"cubiccapacity_to").css({width:"135px","margin-left":"10px"}).placeholder();$("#"+a+"cubiccapacity_from").css({width:"135px"}).placeholder().after(b),$("#cubiccapacity_to-label,#cubiccapacity_to-element").remove()},initGrossvehicleweightRangeFields:function(a){a=a||"";var b=$("#"+a+"grossvehicleweight_to").css({width:"135px","margin-left":"10px"}).placeholder();$("#"+a+"grossvehicleweight_from").css({width:"135px"}).placeholder().after(b),$("#grossvehicleweight_to-label,#grossvehicleweight_to-element").remove()},initGroupSelect:function(a){a=a||"",jQuery("#"+a+"group_id").parents("form").bind("submit",function(){jQuery(this).find("select:hidden,input:text:hidden").remove()}),app.form.clones[a+"organization_id"]=jQuery("#"+a+"organization_id").clone().removeAttr("id"),app.form.clones[a+"classification_id"]=jQuery("#"+a+"classification_id").clone().removeAttr("id"),jQuery("#"+a+"group_id").bind("change",function(){var b=jQuery(this).find(":selected").text();app.form.selectDependent(a,"organization_id",b),app.form.selectDependent(a,"classification_id",b)}).trigger("change")},initVehicletypeSelect:function(a){a=a||"",jQuery("#"+a+"vehicletype_id").parents("form").bind("submit",function(){jQuery(this).find("select:hidden,input:text:hidden").remove()}),app.form.clones[a+"vehiclemaker_id"]=jQuery("#"+a+"vehiclemaker_id").clone().removeAttr("id"),app.form.clones[a+"vehicleupgrader_id"]=jQuery("#"+a+"vehicleupgrader_id").clone().removeAttr("id"),app.form.clones[a+"vehiclemodel_id"]=jQuery("#"+a+"vehiclemodel_id").clone().removeAttr("id"),app.form.clones[a+"custom_maker"]=jQuery("#"+a+"custom_maker").clone().removeAttr("id"),app.form.clones[a+"custom_model"]=jQuery("#"+a+"custom_model").clone().removeAttr("id"),jQuery("#"+a+"vehicletype_id").bind("change",function(){var b=jQuery(this).find(":selected").text();app.form.selectDependent(a,"vehiclemaker_id",b,function(b){jQuery("#custom_maker-label,#custom_maker-element")[!b&&jQuery("#"+a+"vehicletype_id").val()?"show":"hide"]()}),app.form.selectDependent(a,"vehicleupgrader_id",b)}).trigger("change"),jQuery("#"+a+"vehiclemaker_id").bind("change",function(){var b=jQuery(this).find(":selected").text();app.form.selectDependent(a,"vehiclemodel_id",b,function(b){jQuery("#custom_model-label,#custom_model-element")[!b&&jQuery("#"+a+"vehicletype_id").val()?"show":"hide"]()})}).trigger("change")},initCategorySelect:function(a){a=a||"",jQuery("#"+a+"category_id").each(app.form.selectSearch)},initControlcenterSelect:function(a){a=a||"",jQuery("#"+a+"controlcenter_id").each(app.form.selectSearch)},initPostIdElementAutocomplete:function(a,b,c){a=a||"",b=b||"postautocomplete",c=c||"post_id",jQuery("#"+a+b).autocomplete("/post/autocomplete",{width:300,minChars:1,highlight:!1,max:20,scroll:!0,scrollHeight:300,formatItem:function(a,b,c,d){var e=a[0];parseInt(a[1])==0?e="<em>"+e+"</em>":e="<strong>"+e+"</strong>",a[2]!=undefined&&(e+="<br/><small>"+a[2]+"</small>");return e}}).bind("result",function(b,d,e){parseInt(d[1])==0?(jQuery(this).val(""),jQuery("#"+a+c).val("")):jQuery("#"+a+c).val(d[1])}).blur(function(){jQuery(this).val().length==0&&jQuery("#"+a+c).val("")}).keyup(function(){jQuery(this).val().length==0&&jQuery("#"+a+c).val("")})},initUserIdElementAutocomplete:function(a,b,c){a=a||"",b=b||"username",c=c||"user_id",jQuery("#"+a+b).autocomplete("/user/autocomplete",{width:300,minChars:1,highlight:!1,max:20,scroll:!0,scrollHeight:300,formatItem:function(a,b,c,d){var e=a[0];parseInt(a[1])==0?e="<em>"+e+"</em>":e="<strong>"+e+"</strong>",a[2]!=undefined&&(e+="<br/><small>"+a[2]+"</small>");return e}}).bind("result",function(b,d,e){parseInt(d[1])==0?(jQuery(this).val(""),jQuery("#"+a+c).val("")):jQuery("#"+a+c).val(d[1])}).blur(function(){jQuery(this).val().length==0&&jQuery("#"+a+c).val("")}).keyup(function(){jQuery(this).val().length==0&&jQuery("#"+a+c).val("")})},selectSearch:function(){var a=this,b,c=jQuery('<input class="text" placeholder="Suche..." type="text" name="__'+this.id+'_search" id="__'+this.id+'_search">').css({"margin-top":"5px"}).placeholder().keyup(function(){if(!$(a).data("searching")){b&&clearTimeout(b);var c=$(this),d=c.val().toLowerCase().replace(/[^a-zA-Z0-9\s]+/g,"");if(jQuery.trim(d).length<=3)return;c.addClass("ac_loading"),b=setTimeout(function(){$(a).data("searching",!0),c.addClass("ac_loading"),$("option",a).each(function(b){var c=app.common.decode($(this).text()).toLowerCase().replace(/[^a-zA-Z0-9\s]+/g,"");if(c.indexOf(d)!=-1){a.selectedIndex=b;return!1}}),c.removeClass("ac_loading"),$(a).data("searching",!1)},750)}});jQuery(this).after(c).after("<br>")},selectDependent:function(a,b,c,d){var e=jQuery("#"+a+b).empty(),f=app.form.clones[a+b].find('option[value=""]');f.length&&e.append(f.clone());var g=!1;app.form.clones[a+b].find("optgroup").each(function(){var a=jQuery(this).attr("label");a==c&&(g=!0,e.append(jQuery("option",this).clone()))}),e.trigger("change"),jQuery("#"+b+"-label,#"+b+"-element")[g?"show":"hide"](),d&&d(g);return g}}

/* ajax
 */
app.ajax={toggle:function(sel){jQuery(sel).live('click',function(){var self=this;app.common.appendLoading(self);jQuery.getJSON(app.common.appendParam(jQuery(self).attr('href'),'format=json'),function(data){if(!data)return;app.common.removeLoading(self);if(data.stat=='ok'){jQuery(self).removeClass('type-success').removeClass('type-rejected').removeClass('type-error').removeClass('type-new').removeClass('type-pending').removeClass('type-owner').addClass('type-'+data.type).html(data.text);}else{if(data.error&&data.error.msg){app.ajax.handleStatFail(data.error.msg);}else{app.ajax.handleStatFail(data.message);}}});return false;});},handleStatFail:function(msg){alert(app.common.decode(msg));}};

/* uploadprogress
 */
app.uploadprogress={finished:false,timer:null,useiframe:jQuery.browser.safari,detect:function(imgBaseUri){var pk=jQuery('#progress_key');var form=pk.parents('form');jQuery('#uploadprogressbar').remove();form.find('input[type=file]:last').after('<span class="uploadprogressbar" id="uploadprogressbar"></span>');jQuery("#uploadprogressbar").progressBar({boxImage:imgBaseUri+'/img/progressbar.gif',barImage:imgBaseUri+'/img/progressbg_green.gif'}).hide();if(app.uploadprogress.useiframe&&top.document==document){var iframe=document.createElement('iframe');iframe.name="uploadprogressbariframe";jQuery(iframe).css({width:'0',height:'0',position:'absolute',top:'-3000px'});document.body.appendChild(iframe);var d=iframe.contentWindow.document;d.open();d.write('<html><head>'+jQuery('head').html()+'</head><body></body></html>');d.close();}form.bind('submit',function(e){var run=false;form.find('#submit').attr('disabled','disabled');form.find('input[type=file]').each(function(){if(jQuery(this).val()!=''){run=true;}});if(run){jQuery("#uploadprogressbar").parents('dd').find('.errors').remove();app.uploadprogress.finished=false;jQuery("#uploadprogressbar").show().progressBar(0,'Starting...');var uploadProgress=app.uploadprogress.useiframe?uploadprogressbariframe.app.uploadprogress.show:app.uploadprogress.show;var uuid=pk.val();window.setTimeout(function(){uploadProgress(uuid)},10);}});},show:function(uuid){if(app.uploadprogress.finished)return;jQuery.getJSON("/uploadprogress.php?progress_key="+uuid,function(response){if(app.uploadprogress.finished)return;var bar=app.uploadprogress.useiframe?jQuery("#uploadprogressbar",parent.document):jQuery("#uploadprogressbar");if(!response||response.finished){app.uploadprogress.finished=true;bar.progressBar(100,'');}else{bar.progressBar(response['percent'],response['text']);}});if(!app.uploadprogress.finished){window.setTimeout(function(){app.uploadprogress.show(uuid)},500);}}};

