
SK.UI.Progress=new Class({Implements:[Options],disturbing_elements:[],id:Math.round(Math.random()*new Date().getTime())%1000,ie6:document.all&&navigator.appVersion.indexOf("MSIE 6")!=-1?true:false,ie7:document.all&&navigator.appVersion.indexOf("MSIE 7")!=-1?true:false,ff2:navigator.userAgent.toLowerCase().indexOf("firefox/2")!=-1?true:false,ff1:navigator.userAgent.toLowerCase().indexOf("firefox/1")!=-1?true:false,mac:navigator.userAgent.toLowerCase().indexOf("macintosh")!=-1?true:false,ff2mac:this.ff2&&this.mac,ff1mac:this.ff1&&this.mac,safari:navigator.appVersion.indexOf("Safari")!=-1?true:false,WRAPPER_OPACITY:1.0,BG_OPACITY:0.7,DEFAULT_THEME_NAME:"black",isDisplayed:false,isInitialized:false,SPINNER_COLORS:{light:'#000000',black:'#ffffff'},options:{callbacks:{},theme:null,placeholder:$(document),spinner:{lines:10,length:10,width:3,radius:10,color:'#ffffff',speed:1,trail:50,shadow:false}},placeholders:{},initialize:function(options){this.setOptions(options);if(!$defined(this.SPINNER_COLORS[this.options.theme])){this.options.theme=this.DEFAULT_THEME_NAME;}
this.collectPlaceholders();var active_progress=this.placeholders.wrapper.retrieve('instance');if(active_progress){active_progress.executeCallback('beforehide');active_progress.executeCallback('afterhide');active_progress.setOptions(this.options);this.placeholders.wrapper.set('class',this.options.theme);return active_progress;}
this.bounds={center:this.center.bind(this)}
this.placeholders.wrapper.store('instance',this);return this;},collectPlaceholders:function(){['progress_ui_wrapper','progress_ui_content_animation','progress_ui_message','progress_ui_background','skwaiting_stripe'].each(function(el_id){this.placeholders[el_id.replace('progress_ui_','')]=$(el_id);},this);},init:function(message,callbacks){this.message=message;if(typeof(callbacks)!='undefined'){this.options.callbacks=$extend(this.options.callbacks,callbacks);}
if(this.isInitialized){return;}
this.create();this.reset();window.addEvent('scroll',this.bounds.center);window.addEvent('resize',this.bounds.center);this.isInitialized=true;},create:function(){if(this.options.zindex){this.placeholders.wrapper.setStyles({'z-index':this.options.zindex});}},destroy:function(){window.removeEvent('scroll',this.bounds.center);window.removeEvent('resize',this.bounds.center);this.isInitialized=false;this.isDisplayed=false;this.placeholders.wrapper.store('instance',null);if(this.options.zindex){this.placeholders.wrapper.setStyles({'z-index':''});}},reset:function(){this.disturbing_elements=[];},display:function(){if(this.isDisplayed){this.setMessage(this.message);this.executeCallback('beforedisplay');this.callAfterDisplay();this.executeCallback('afterdisplay');return;}
this.isDisplayed=true;this.collectDisturbingElements();this.hideDisturbingElements();this.center();this.executeCallback('beforedisplay');this.displayContents();this.executeCallback('afterdisplay');},hide:function(){this.executeCallback('beforehide');this.hideContents();this.displayDisturbingElements();this.destroy();this.executeCallback('afterhide');},getCallback:function(event_id){if(typeof SK=='undefined'||typeof SK.Singletons=='undefined'||typeof SK.Singletons.env=='undefined'){return null;}
var callback=SK.Singletons.env.get('SK.UI.Progress.on'+event_id);return callback==''?null:eval(callback);},executeCallback:function(event_id){var callback=this.getCallback(event_id);if(callback!=null){callback(this);}},getContentPlaceholder:function(){return this.placeholders.content_animation;},setMessage:function(message){this.placeholders.message.innerHTML=message;},callAfterDisplay:function(){if(this.options.callbacks.onafterdisplay){this.options.callbacks.onafterdisplay();}},displayContents:function(){this.setMessage(this.message);this.placeholders.wrapper.setStyles({'opacity':'0.0','display':'block'});this.placeholders.wrapper.set('class',this.options.theme);var wrapper_opacity=this.WRAPPER_OPACITY;var bg_opacity=this.BG_OPACITY;this.options.duration=0;if(this.options.duration==0){this.placeholders.wrapper.setStyle('opacity',wrapper_opacity);this.placeholders.background.setStyle('opacity',bg_opacity);this.startImagesAnimation();this.callAfterDisplay();}else{new Fx.Morph(this.placeholders.wrapper,{duration:this.options.duration?this.options.duration:200,transition:Fx.Transitions.Sine.easeOut,onComplete:function(){this.startImagesAnimation();this.callAfterDisplay.bind(this);}.bind(this)}).start({opacity:wrapper_opacity});this.placeholders.background.setStyles({'opacity':'0.0','display':'block'});new Fx.Morph(this.placeholders.background,{duration:this.options.duration?this.options.duration:200,transition:Fx.Transitions.Sine.easeOut}).start({opacity:bg_opacity});}},startImagesAnimation:function(){var spinner_diameter=(this.options.spinner.radius+this.options.spinner.length)*2;this.placeholders.skwaiting_stripe.setStyles({width:spinner_diameter,height:spinner_diameter});this.options.spinner.color=this.SPINNER_COLORS[this.options.theme];this.spinner=new Spinner(this.options.spinner).spin(this.placeholders.skwaiting_stripe);if(Browser.Engine.trident){$(this.spinner.el).setStyles({'margin':null,'top':null,'left':null});}},stopImagesAnimation:function(){if(this.spinner)this.spinner.stop();},hideContents:function(){this.placeholders.wrapper.setStyle('display','none');this.stopImagesAnimation();},isFullOfBugsIE:function(){return((document.compatMode=='BackCompat'&&Browser.Engine.trident)||(document.compatMode!='BackCompat'&&Browser.Engine.trident4==true));},center:function(){var p=$(this.options.placeholder);var size=p.getSize();var scroll=p.getScroll();var pos=p.getCoordinates();if(this.isFullOfBugsIE()){this.placeholders.wrapper.setStyles({'left':scroll.x+pos.left,'top':scroll.y+pos.top});}else{var position=!p.get||p.get('tag').toLowerCase()=='body'?'fixed':p.getStyle('position')=='fixed'?'fixed':'absolute';this.placeholders.wrapper.setStyles({'position':position,'left':0+pos.left,'top':0+pos.top});}
$$('#progress_ui_wrapper, #progress_ui_background, #progress_ui_foreground, #progress_ui_content').setStyles({width:size.x,height:size.y});},hideDisturbingElements:function(){for(var i=0;i<this.disturbing_elements.length;i++){if($(this.disturbing_elements[i])&&$(this.disturbing_elements[i]).setStyle){$(this.disturbing_elements[i]).setStyle('display','none');}}},displayDisturbingElements:function(){for(var i=0;i<this.disturbing_elements.length;i++){if($(this.disturbing_elements[i])&&$(this.disturbing_elements[i]).setStyle){$(this.disturbing_elements[i]).setStyle('display','');}
if(this.safari){this.patchSafariFlashElements(this.disturbing_elements[i],false);}}},collectDisturbingElements:function(){var target_tags=['object','embed','param'];if(this.ie6)target_tags.push('select');for(var j=0;j<target_tags.length;j++){var objs=document.getElementsByTagName(target_tags[j]);for(var i=0;i<objs.length;i++){this.disturbing_elements.push(objs[i]);if(this.safari){this.patchSafariFlashElements(objs[i],false);}}}},patchSafariFlashElements:function(e,undo){if(undo){var old_wmode=e.getAttribute('old_wmode');if(old_wmode)e.setAttribute('wmode',old_wmode);else e.removeAttribute('wmode');}else{var old_wmode=e.getAttribute('wmode');e.setAttribute('wmode','opaque');if(old_wmode)e.setAttribute('old_wmode',old_wmode);}},changeTheme:function(theme_name){this.options.theme=theme_name||this.DEFAULT_THEME_NAME;},changeMessage:function(message){this.message=message;},getPlaceholder:function(placeholder_name){return this.placeholders[placeholder_name];}});
