/*  =DROP DOWN MENU
    ---------------------------------------------------- */
    
// Controls the subnavigation elements on the page.
var DropdownMenu = Class.create({
  initialize: function(element) {
    this.element = element;
    this.openDelayTime = 250;
    this.closeDelayTime = 250;
    
    // Set the proper element to the class.
    this.subNavigation = this.element.down('ul');
    this.trigger = this.element.down('a');
    
    // If we got a subnavigation element let's hide it and add observers.  We'll also add an ID if it doesn't exist on the element.
    if(this.subNavigation) {
      if (!this.subNavigation.id) {
          this.subNavigation.id = this.element.id+"_subnavigation";
      }
      if (!this.trigger.id) {
          this.trigger.id = this.element.id+"_trigger";
      }
            
      // Here we build a call out so that we can hide and deactivate the menu item from a global call.
      this.hideCallOut = "$('"+this.subNavigation.id+"').up().removeClassName('active'); $('"+this.trigger.id+"').removeClassName('active');";
      this.showCallOut = "$('"+this.subNavigation.id+"').show();";
      this.subNavigation.hide();
    }   
    Event.observe(this.element, 'mouseover', this.showMenu.bindAsEventListener(this));
    Event.observe(this.element, 'mouseout', this.closeMenu.bindAsEventListener(this));
  },
  
  showMenu: function(e) {
    this.activate();
    $$('ul.nested_category_navigation').invoke('hide');
    if(this.subNavigation) {
      if (this.closeDelayTimer) window.clearTimeout(this.closeDelayTimer);
      this.subNavigation.show();
      //if (!this.subNavigation.visible()) this.openDelayTimer = window.setTimeout(this.showCallOut, this.openDelayTime);
    }
  },
  
  closeMenu: function(e) {
    if(this.subNavigation) {
      if (this.subNavigation.visible()) this.closeDelayTimer = window.setTimeout(this.hideCallOut, this.closeDelayTime);
    }
  },
  
  hideMenu: function(e) {
    this.subNavigation.hide();
    this.element.removeClassName('active');
  },
  
  activate: function() {
    this.element.siblings().each(function(sibling) {
      if(sibling.hasClassName('active')) {
          sibling.removeClassName('active'); 
          sibling.down('a').removeClassName('active');
      }
    });
    if (this.subNavigation) {
        this.element.addClassName('active');
        this.element.down('a').addClassName('active');
    }
  }
});


// Extending all elements to support special interactive functionality.
function addDropDowns(e){  
    $('primary_navigation').childElements().each(function(element) {
        element.dropdown = new DropdownMenu(element);  
        return element;
    });  
}

Event.observe(window, 'load', addDropDowns);


/*  =SLIDE SHOW
    ---------------------------------------------------- */
//function mran(ma,mi) {return(Math.round(Math.random()*(ma-mi))+mi);}

	var Slideshow = Class.create({
		 initialize: function(delay, elmtName, markers) {
		    
			this.delay = delay;
			this.paused = 0;			
			this.arrSlideElmt = $(elmtName).select('img.rotate');
			this.totalElmt = this.arrSlideElmt.size();
			this.arrMarkerElmt = [this.totalElmt];
			this.curElmtNum = 1; //mran(totalElmt, 1); //randomize first element
			this.run = (this.totalElmt > 0);
			
			var ul = '<ul id="' + markers + '" />';
			$(elmtName).insert(ul, '');
			
			for (i = 0; i < this.totalElmt; i++) {			    
			    var li = '<li class="' + ((i<1) ? "marker current" : "marker") + '" id="marker'+(i+1)+'"><span id="markerspan'+(i+1)+'" title="Show Image '+(i+1)+'"></span></li>';
			    $(markers).insert(li, '');
			    this.arrMarkerElmt[i] = $("marker"+(i+1));
			    
				this.arrMarkerElmt[i].observe('click', function(event){
				    var id = (Event.element(event).identify());
				    id = (id.substring(id.length-1));				   
				    this.showSlide(id);
					this.executor.stop();
				}.bind(this));
				
				this.arrSlideElmt[i].setStyle({
				    position : 'absolute',
				    left : 0,
				    top : 0});
			}
					
		 },		 
		start: function() {
		    if(!this.run)
		    {
		        return;
		    }
			//show first element without effect
			this.arrSlideElmt[this.curElmtNum-1].setStyle({
			  display: 'block'
			});
			this.executor = new PeriodicalExecuter(function() { 
				this.next(); //start slidehow
			}.bind(this), this.delay);		
		},		
		next: function(){
			if (!this.paused) {
				this.update();
			}
		},		
		update: function() {
		    //alert(this.curElmtNum);
		    this.arrMarkerElmt[this.curElmtNum-1].removeClassName('current');
			new Effect.Fade(this.arrSlideElmt[this.curElmtNum-1],{duration: 2.0,beforeStart:function(){ //afterFinish
				this.checkSlide();
				new Effect.Appear(this.arrSlideElmt[this.curElmtNum-1]);
				this.arrMarkerElmt[this.curElmtNum-1].addClassName('current');
				this.arrSlideElmt[this.curElmtNum-1].setStyle({
			        width : '667px', 
                    height : '301px'
			    });
			    //alert(this.arrSlideElmt[this.curElmtNum-1].getStyle('width'));
			}.bind(this)});
			
		},
		showSlide: function(nextElmtNum) {
		    if(this.curElmtNum == nextElmtNum)
		    {
		        return;
		    }
		    this.arrMarkerElmt[this.curElmtNum-1].removeClassName('current');
			new Effect.Fade(this.arrSlideElmt[this.curElmtNum-1],{duration: 2.0,beforeStart:function(){ //afterFinish
				this.setSlide(nextElmtNum);
				new Effect.Appear(this.arrSlideElmt[this.curElmtNum-1]);
				this.arrMarkerElmt[this.curElmtNum-1].addClassName('current');
			}.bind(this)});
		},
		checkSlide: function() {
		    if (this.curElmtNum == this.totalElmt) { this.curElmtNum = 1; }
			else { this.curElmtNum ++; }
		},
		setSlide: function(nextElmtNum)
		{
		    if (nextElmtNum > this.totalElmt) { this.curElmtNum = 1; }
			else { this.curElmtNum = nextElmtNum; }
		}
	});

    // Extending all elements to support special interactive functionality.
    function createSlideShow(e){  
        var speed = 7;
        var container = "featured_content";
        var dynamicMarkerId = "rotatorMarker"; //Added dynamically to the container.
        var slideshow = new Slideshow(speed, container, dynamicMarkerId);
		slideshow.start();
    };
    
    Event.observe(window, 'load', createSlideShow);

/*  =LISTING MANAGER
    ---------------------------------------------------- */
   var ListingManager = Class.create({
		 initialize: function() {
		    if($('member_listing') == null)
		    {
		        this.arrMembers = [0];
		        return;
		    }
			this.arrMembers = $('member_listing').select('tr.member_detail');
			this.totalElmt = this.arrMembers.size();
			this.debug = false;
			
			for (i = 0; i < this.totalElmt; i++) {
			    var item = this.arrMembers[i];
			    
			    var id = $(item).down('span.listing_control').identify();
			        $(id).down('span').observe('click', function(event){
				        var id = (Event.element(event).identify());				        				   
				        this.update(id);
				    }.bind(this));
				    
				    //Show Controller Buttons.
			        new Effect.Appear($(id), {duration: 2.0});
			}			
		 },		 
		start: function() {
			//hide all elements
			this.arrMembers.each(function(item) {
			    try{
			        $(item).down('span.shrink').hide();
			        $(item).down('ul.listing_actions').hide();
			        $(item).down('div.member_logo').hide();
			        $(item).down('div.member_photos').hide();
			    }
			    catch(e)
			    {
			        if(this.debug)
	                {
	                    alert(e);
	                }
			    }
			});
		},
		showDetail: function(memberRow) {
		    try
		    {
		        if(!$(memberRow).hasClassName('expanded'))
		        {
		            new Effect.Appear($(memberRow).down('span.shrink'), { duration: 2.0,beforeStart:function(){
				        $(memberRow).addClassName('expanded');     
			        }.bind(this)});
                    new Effect.Appear($(memberRow).down('ul.listing_actions'), { duration: 3.0 });
                    var id = $(memberRow).down('div.member_logo').identify();
                    getMemberImages(id, memberRow, 'div.member_logo', 'div.member_photos', true);                                                   
                }
			}
			catch(e)
			{
			    if(this.debug)
	            {
	                alert(e);
	            }
			}
		},
		hideDetail: function(memberRow) {
		    try
		    {
		        if($(memberRow).hasClassName('expanded'))
		        {
			        new Effect.Fade($(memberRow).down('span.shrink'), { duration: 1.0,afterFinish:function(){
				        $(memberRow).removeClassName('expanded');  
			        }.bind(this)});
                    new Effect.Fade($(memberRow).down('ul.listing_actions'), { duration: 1.0 });
                    new Effect.Fade($(memberRow).down('div.member_logo'), { duration: 1.0 });
                    new Effect.Fade($(memberRow).down('div.member_photos'), { duration: 1.0 });
			    }
			}
			catch(e)
			{
			    if(this.debug)
	            {
	                alert(e);
	            }
			} 
			
		},
		toggle: function (memberRow) {
		    try
		    {
		        if($(memberRow).hasClassName('expanded'))
		        {
		            this.hideDetail(memberRow);
		        }
		        else
		        {
		            this.showDetail(memberRow);
		        }
		    }
		    catch(e)
		    {
		        if(this.debug)
		        {
		            alert(e);
		        }
		    }
		},			
		update: function(control) {
		    try
		    {
		        
		        var memberRow = $(control).up('tr.member_detail');
		        this.toggle(memberRow);
		    }
		    catch(e)
		    {
		        if(this.debug)
	            {
	                alert(e);
	            }
		    }
		}
	});
	
	
    // Extending all elements to support special interactive functionality.
    function createListingManager(e){  
        var listingManager = new ListingManager();
		listingManager.start();
    };
    
    //Add this to the member listing page/control.
    //Event.observe(window, 'load', createListingManager);

    //use this function on pages with member detail to get the images for that member. 
    //used in ListingManager class!
    function getMemberImages(id, container, logo_holder, photo_holder, replace)
    {
        var debug = false;
        
        try
	    {	
	        logo_holder = $(container).down(logo_holder);
	        photo_holder = $(container).down(photo_holder);
	        
	        if(!replace)
	        {   
	            $(logo_holder).insert({bottom:new Element('div', { id: 'logo' })});
	            $('logo').hide();
	            logo_holder = $('logo');
	            
	            $(photo_holder).insert({bottom:new Element('div', { id: 'photos' })});
	            $('photos').hide();
	            photo_holder = $('photos');
	            
	        }
	    
            var url ='/memberimages.ashx?id=' + id;
            var element = new Element('div');
            new Ajax.Request(url, {   method: 'get', onSuccess: function(xhrResponse) {                            
                if(xhrResponse.responseText == null || xhrResponse.responseText == '')
                {
                    if(debug)
                    {
                        alert('response empty; id: ' + id + '; logo_holder: ' + logo_holder + '; photo_holder: ' + photo_holder + '; replace: ' + replace + ';');
                    }
                }
                else
                {
                    element.update(xhrResponse.responseText);
					$(logo_holder).update(element.down('img.member_logo'));
					if(element.down('ul.member_photos') != null && $(photo_holder) != null)
					{
					    $(photo_holder).update(element.down('ul.member_photos'));
					}
					new Effect.Appear($(logo_holder), { duration: 3.0 });
					new Effect.Appear($(photo_holder), { duration: 3.0 });
				}
			  }
			});	        
	    }
	    catch(e)
	    {
	        if(debug)
            {
                alert(e);
            }
	    }
    }    

/*  =BLOG ENTRY LOADER
    ---------------------------------------------------- */
   var BlogEntryLoader = Class.create({
		 initialize: function() {
		    if($('blog_entries') == null)
		    {
		        return;
		    }
			this.debug = true;		    
		 },
		 start: function() {
		    new Ajax.Updater('blog_entries', '/BlogEntries.ashx');
		 }
	});	
	
    // Extending all elements to support special interactive functionality.
    function createBlogEntryLoader(e){  
        var blogEntryLoader = new BlogEntryLoader();
		blogEntryLoader.start();
    };
    
    //Add this to the blog entries control.
    //Event.observe(window, 'load', createBlogEntryLoader);
        
        
