/* 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(){location.href=$('a',this).attr('href');}).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(app.common.decode(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:{},initVehicleForm:function(){jQuery('#group_id, #vehicletype_id').parents('form').bind('submit',function(){jQuery(this).find('select:hidden,input:text:hidden').remove();});jQuery('#fieldset-vehicle_date_group').find('dt').each(function(i){var dd=jQuery(this).next();var p=jQuery(this).wrap('<div/>').parent().append(dd).css({float:'left',width:'150px'});if(i%2){p.after('<div style="clear:both"></div>');}}).end().find('select').css('width','auto').css('min-width','60px');function select(id,sel,cb){var el=jQuery('#'+id).empty();var empty=app.form.clones[id].find('option[value=""]');if(empty.length){el.append(empty.clone());}var found=false;app.form.clones[id].find('optgroup').each(function(){var l=jQuery(this).attr('label');if(l==sel){found=true;el.append(jQuery('option',this).clone());}});el.trigger('change');jQuery('#'+id+'-label,#'+id+'-element')[found?'show':'hide']();if(cb){cb(found)}return found;}app.form.clones.organization_id=jQuery('#organization_id').clone().removeAttr('id');app.form.clones.classification_id=jQuery('#classification_id').clone().removeAttr('id');jQuery('#group_id').bind('change',function(){var sel=jQuery(this).find(':selected').text();select('organization_id',sel);select('classification_id',sel);}).trigger('change');app.form.clones.vehiclemaker_id=jQuery('#vehiclemaker_id').clone().removeAttr('id');app.form.clones.vehicleupgrader_id=jQuery('#vehicleupgrader_id').clone().removeAttr('id');app.form.clones.vehiclemodel_id=jQuery('#vehiclemodel_id').clone().removeAttr('id');app.form.clones.custom_maker=jQuery('#custom_maker').clone().removeAttr('id');app.form.clones.custom_model=jQuery('#custom_model').clone().removeAttr('id');jQuery('#vehicletype_id').bind('change',function(){var sel=jQuery(this).find(':selected').text();select('vehiclemaker_id',sel,function(found){jQuery('#custom_maker-label,#custom_maker-element')[!found&&jQuery('#vehicletype_id').val()?'show':'hide']();});select('vehicleupgrader_id',sel);}).trigger('change');jQuery('#vehiclemaker_id').bind('change',function(){var sel=jQuery(this).find(':selected').text();select('vehiclemodel_id',sel,function(found){jQuery('#custom_model-label,#custom_model-element')[!found&&jQuery('#vehicletype_id').val()?'show':'hide']();});}).trigger('change');app.form.initCategorySelect();app.form.initControlcenterSelect();app.form.initPostIdElementAutocomplete();},initCategorySelect:function(){jQuery('#category_id').each(app.form.selectSearch);},initControlcenterSelect:function(){jQuery('#controlcenter_id').each(app.form.selectSearch);},initPostIdElementAutocomplete:function(){jQuery('#postautocomplete').autocomplete("/post/autocomplete",{width:300,minChars:1,highlight:false,max:20,scroll:true,scrollHeight:300,formatItem:function(data,i,n,value){var r=data[0];if(parseInt(data[1])==0){r='<em>'+r+'</em>';}else{r='<strong>'+r+'</strong>';}if(data[2]!=undefined){r+='<br/><small>'+data[2]+'</small>';}return r;}}).bind('result',function(event,data,formatted){if(parseInt(data[1])==0){jQuery(this).val('');jQuery('#post_id').val('');}else{jQuery('#post_id').val(data[1]);}}).blur(function(){if(jQuery(this).val().length==0){jQuery('#post_id').val('');}}).keyup(function(){if(jQuery(this).val().length==0){jQuery('#post_id').val('');}});},selectSearch:function(){var select=this,timeout;var ip=jQuery('<input class="text" type="text" name="__'+this.id+'_search" id="__'+this.id+'_search">').css({'margin-top':'5px','font-style':'italic','color':'#999'}).val('Suche...').focus(function(){$(this).css({'font-style':'normal','color':'#000'});if($(this).val()=='Suche...'){$(this).val('');}}).blur(function(){if($(this).val()==''){$(this).val('Suche...').css({'font-style':'italic','color':'#999'});}}).keyup(function(){if($(select).data('searching')){return;}if(timeout){clearTimeout(timeout);}var el=$(this);var s=el.val().toLowerCase().replace(/[^a-zA-Z0-9\s]+/g,'');if(jQuery.trim(s).length<=3){return;}el.addClass('ac_loading');timeout=setTimeout(function(){$(select).data('searching',true);el.addClass('ac_loading');$('option',select).each(function(i){var t=app.common.decode($(this).text()).toLowerCase().replace(/[^a-zA-Z0-9\s]+/g,'');if(t.indexOf(s)!=-1){select.selectedIndex=i;return false;}});el.removeClass('ac_loading');$(select).data('searching',false);},750);});jQuery(this).after(ip).after('<br>');}};

/* 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);}}};
