var popup_mp3 = "assets/mp3/pop.mp3";
var popup_ogg = "assets/mp3/pop.ogg";
var popup_player_src = "assets/mp3/niftyplayer.swf";
var popup_embed= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="0" height="0" id="niftyPlayerPopup%id%" align=""><param name="movie" value="'+popup_player_src+'?file='+popup_mp3+'&as=0"><param name=quality value=high><embed src="'+popup_player_src+'?file='+popup_mp3+'&as=0" quality=high bgcolor=#FFFFFF width="0" height="0" name="niftyPlayerPopup%id%" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
var popup_audio_embed = "<audio id='popup_sound' src='%audio_file%' preload='auto'></audio>";

var popOuts = {
    init : function(){
        
        var scope = this;
        
        this.popups  =  $$("div.popup");
        
        if(clientSettings.hasAudioSupport){
                
            var formatted_popup_embed = popup_audio_embed.replace(/\%audio_file\%/g,clientSettings.isSafari?popup_mp3:popup_ogg);

            new Element("span",{
                "html" : formatted_popup_embed,
                "class" : "audio_player"
            }).inject($("popups"));
                
        }
        
        var i=1;
        
        this.popups.each(function(el){
            
            el.popupOpen = false;
            el.closeTimeout = null;
            
            if(clientSettings.hasFlashSupport){
                
                var formatted_popup_embed = popup_embed.replace(/\%id\%/g,i);

                new Element("span",{
                    "html" : formatted_popup_embed,
                    "class" : "flash_player"
                }).inject(el);
            
            }
            
            
            if(clientSettings.isIphone){
                
             
                el.getElements("a")[0].addEvent("click",function(e){

                  
                    if(!el.popupOpen){
                        
                        e.stop();
                            
                        scope.popups.each(function(check_el){
                        
                            if(check_el!=el && check_el.popupOpen){
                                
                                clearTimeout(check_el.closeTimeout);
                                scope.slideDown(check_el);
                                
                            }
                        
                        })
                        
                        scope.slideUp(el); 

                        el.closeTimeout=setTimeout(function(){ 
                            scope.slideDown(el);
                        },8000);
                        
                    }
                })
                
            }
            else {
                
                el.addEvent("mouseenter",function(){

                    scope.slideUp(el);

                })

                el.addEvent("mouseleave",function(){

                    scope.slideDown(el);

                })
                
            }
            
            i++;
            
        })
        
    },
    slideUp: function(el){
          if(el.fx){el.fx.cancel();}
          var max_height = 221;
          var min_height = 59;
          var current_height = el.getSize().y;
          el.fx = new Fx.Tween(el, {
              duration: (max_height-current_height)/(max_height-min_height)*200,
              property: 'height'
          });
          el.popupOpen = true;
          this.playSound(el);
          el.fx.start(current_height,221);
    },
    slideDown: function(el){
          if(el.fx){el.fx.cancel();}
          el.popupOpen = false;
          var max_height = 221;
          var min_height = 59;
          var current_height = el.getSize().y;
          el.fx = new Fx.Tween(el, {
              duration: (current_height-min_height)/(max_height-min_height)*200,
              property: 'height'
          });
          el.fx.start(el.getSize().y,59);
    },
    playSound : function(el){
        if(clientSettings.hasAudioSupport){
           soundPlayer.play_sound("popup_sound");
        }
        else if(clientSettings.hasFlashSupport) {
           var player_id = el.getElements("object")[0].id;
           niftyplayer(player_id).play();
        }
    }
}



