// JavaScript Document

var POPULAR="POP";
  var SINGLE_FORM="SIN";
  var BIGTHUMB_FORM="BTH";
  var AGGREGATE_FORM="AGG";
  var AGGREGATE_FORM_VID_WIDTH=4;

 sd_undo_object=function()
  {
    this.module_="";
    this.undo_function_=null;
  };
  
  sd_support=function(xsrf_token,site_read_only_mode,logged_out_mode)
  {
    this.default_ordering_=[];
    this.ordering_=[];
    this.xsrf_token_=xsrf_token;
    this.site_read_only_mode=site_read_only_mode;
    this.logged_out_mode=logged_out_mode;
    this.waiting_on_order_ajax_=false;
    this.delayed_ordering_requests_=[];
    this.num_times_popular_refreshed=0;
    this.undo_object_=new sd_undo_object();
    this.ANIMATE_MODULE_SWAP_TIME=0.4;
    this.ANIMATE_SLIDE_TIME=0.25;
    this.ANIMATE_SLIDE_RIGHT_TIME=0.25;
    this.ANIMATE_MODULE_FADE_TIME=0.75;
  };
  
  window.sd_support=sd_support;
  
  sd_support.prototype.add_module=function(mod)
  {
    this.default_ordering_.push(mod);
  };
  
   sd_support.prototype.init=function()
  {
    this.update_ordering();
    this.reset_updown_buttons();
    if(!this.logged_out_mode)
    {
      _gel("feed_undo_delete_link").href="javascript:sd.undo();";
    }
  };

 sd_support.prototype.init_drag_drop=function()
  {
    if(!this.logged_out_mode&&!this.site_read_only_mode)
    {
      this.ddr=new Sortable.create('dragdrop',{tag:'div',handle:'feedmodule-modheader',hoverclass:'drag_placeholder',onUpdate:updateList});
    }
    this.reset_updown_buttons();
  };

  sd_support.prototype.undo=function()
  {
    if(this.undo_object_.undo_function_)
    {
      this.undo_object_.undo_function_();
    }
  };

  sd_support.prototype.animate_length_preference=function(cmd,selector,form_type)
  {
    showDiv(cmd+'-loading-icn');
    this.open_loading_pane(cmd+'-loading-msg');
    this.set_length_preference(cmd,selector,form_type);
  };

  sd_support.prototype.set_length_preference=function(module,selector,form_type)
  {
    select=_gel(selector);
    var idx=select.options.selectedIndex;
    var selected=select.options[idx];
    var num=selected.value;
    if(form_type==AGGREGATE_FORM)
    {
      num=num*AGGREGATE_FORM_VID_WIDTH;
    }
    var url="/index";
    var data="alter=true&module="+module+"&num="+num+"&session_token="+this.xsrf_token_;
    var callback=function(xmlHttpRequest)
    {
      var module_anchor=_gel("feedmodule-"+module);
      module_anchor.innerHTML=xmlHttpRequest.responseText;
      this.reset_updown_buttons();
//      this.ddr.lookup_dragger_by_id("feedmodule-"+module).set_drag_handle(_gel(module+"-titlebar"));
      showDiv(module+'-options');
      hideDiv(module+'-loading-icn');
      hideDiv(module+'-loading-msg');
    }
    .bind(this);
//    ajaxRequest(url,{ postBody:data,onComplete:callback });
  };

  sd_support.prototype.set_form_preference=function(module,form_type)
  {
    var url="/index";
    var data="alter=true&module="+module+"&ftype="+form_type+"&session_token="+this.xsrf_token_;
    var callback=function(xmlHttpRequest)
    {
      var module_anchor=_gel("feedmodule-"+module);
 //     module_anchor.innerHTML=xmlHttpRequest.responseText;
      this.reset_updown_buttons();
      showDiv(module+'-options');
      hideDiv(module+'-loading-icn');
      hideDiv(module+'-loading-msg');
    }
    .bind(this);
    new Ajax.Updater('pop-data', 'url', {
    parameters: { onComplete:callback },
    insertion: Insertion.Bottom
    });
    //ajaxRequest(url,{postBody:data,onComplete:callback});
  };

    sd_support.prototype.set_form_preference=function(module,form_type)
  {
    var url="/index";
    var data="alter=true&module="+module+"&ftype="+form_type+"&session_token="+this.xsrf_token_;
    var callback=function(xmlHttpRequest)
    {
      var module_anchor=_gel("feedmodule-"+module);
 //     module_anchor.innerHTML=xmlHttpRequest.responseText;
      this.reset_updown_buttons();
      showDiv(module+'-options');
      hideDiv(module+'-loading-icn');
      hideDiv(module+'-loading-msg');
    }
    .bind(this);
    new Ajax.Updater('pop-data', 'url', {
    parameters: { onComplete:callback },
    insertion: Insertion.Bottom
    });
    //ajaxRequest(url,{postBody:data,onComplete:callback});
  };

  sd_support.prototype.update_ordering=function()
  {
    this.ordering_=[];
    for(o in this.default_ordering_)
    {
      var order=this.default_ordering_[o];
      var module_anchor=_gel("feedmodule-"+order);
      if(module_anchor)
      {
        this.ordering_.push(order);
      }
    }
  };
  
  sd_support.prototype.scan_active_modules=function()
  {
    var modules=getElementsByTagNameAndClass('div','feedmodule-anchor');
    this.ordering_=[];
    for(i in modules)
    {
      var module=modules[i];
      if(module.id.substring(0,5)!="UNDO-"&&module.id.substring(module.id.length-6,module.id.length)!="-clone")
      {
        var id_components=module.id.split("-");
        var order=id_components[1];
        this.ordering_.push(order);
      }
    }
  };

  sd_support.prototype.indexOf=function(arr,obj)
  {
    var arr_length=arr.length;
    for(var i=0;i<arr_length;i++)
    {
      if(arr[i]==obj)
      {
        return i;
      }
    }
    return-1;
  };

  sd_support.prototype.reset_updown_buttons=function()
  {
    var order_len=this.ordering_.length;
    for(var i in this.ordering_)
    {
      var order=this.ordering_[i];
      var up_btn=_gel("mup-"+order);
      var disabled_up_btn=_gel("mup-disabled-"+order);
      var down_btn=_gel("mdown-"+order);
      var disabled_down_btn=_gel("mdown-disabled-"+order);
      if(!this.site_read_only_mode&&!this.logged_out_mode)
      {
        var close_btn=_gel("mclose-"+order);
        var disabled_close_btn=_gel("mclose-disabled-"+order);
        if(close_btn&&disabled_close_btn)
        {
          close_btn.style.display="inline";
          disabled_close_btn.style.display="none";
        }
      }
      if(i==0||this.site_read_only_mode||this.logged_out_mode)
      {
        up_btn.style.display="none";
        disabled_up_btn.style.display="inline";
      }
      else
      {
        up_btn.style.display="inline";
        disabled_up_btn.style.display="none";
      }
      if(i==(order_len-1)||this.site_read_only_mode||this.logged_out_mode)
      {
        down_btn.style.display="none";
        disabled_down_btn.style.display="inline";
      }
      else
      {
        down_btn.style.display="inline";
        disabled_down_btn.style.display="none";
      }
    }
  };

  sd_support.prototype.move_module=function(type,dir)
  {
    var cur_pos=this.indexOf(this.ordering_,type);
    var next=cur_pos+1;
    if(dir=='up')
    {
      next=cur_pos-1;
    }
    var anchor=_gel("feedmodule-"+type);
    var next_anchor=_gel("feedmodule-"+this.ordering_[next]);
    if(!anchor)return;
    if(!next_anchor)return;
    var swap=this.ordering_[cur_pos];
    this.ordering_[cur_pos]=this.ordering_[next];
    this.ordering_[next]=swap;
    if(next==this.ordering_.length-1)
    {
      this.adjust_module_order(type,type,'arrow');
    }
    else
    {
      this.adjust_module_order(type,this.ordering_[next+1],'arrow');
    }
    this.reset_updown_buttons();
    ani.swapDivs(anchor,next_anchor,this.ANIMATE_MODULE_SWAP_TIME);
  }
  ;

  sd_support.prototype.change_form_type=function(module,type)
  {
    hideDiv(module+'-options-'+AGGREGATE_FORM);
    hideDiv(module+'-options-'+SINGLE_FORM);
    hideDiv(module+'-options-'+BIGTHUMB_FORM);
    showDiv(module+'-options-'+type);
    showDiv(module+'-loading-icn');
    this.open_loading_pane(module+'-loading-msg');
    this.set_form_preference(module,type);
  }
  ;

  sd_support.prototype.process_disabled_button=function(module_id)
  {
    if(this.logged_out_mode||this.site_read_only_mode)
    {
      this.open_options_pane(module_id+"-options");
    }
  }
  ;

  sd_support.prototype.open_options_pane=function(pane)
  {
    var el=_gel(pane);
    if(el.style.display=='block')
    {
      return this.close_options_pane(pane);
    }
    el.style.display='block';
    if(isIE)
    {
      showDiv(pane);
    }
    else
    {
      ani.ajaxSlideOpen(el,0.0,this.ANIMATE_SLIDE_TIME);
    }
  }
  ;

  sd_support.prototype.close_options_pane=function(pane)
  {
    if(isIE)
    {
      hideDiv(pane);
    }
    else
    {
      ani.fadeDuringSlideUp(_gel(pane),0.0,this.ANIMATE_SLIDE_TIME)
    }
  }
  ;

  sd_support.prototype.open_loading_pane=function(pane)
  {
    showDiv(pane);
  }
  ;

  sd_support.prototype.show_undo_remove_msg=function()
  {
    var status_msg=_gel("mundo-remove");
    status_msg.style.opacity='0.0';
    status_msg.style.filter='alpha(opacity=100)';
    status_msg.style.display='block';
    if(isIE)
    {
      window.setTimeout("hideDiv('mundo-remove')",30000);
    }
    else
    {
      ani.fade(status_msg,'0.0','1.0',0,this.ANIMATE_MODULE_FADE_TIME);
      ani.fadeDuringSlideUp(status_msg,30.0,this.ANIMATE_SLIDE_TIME)
    }
  }
  ;
  
  
  sd_support.prototype.hide_undo_remove_msg=function()
  {
    var status_msg=_gel("mundo-remove");
    ani.fadeDuringSlideUp(status_msg,0,this.ANIMATE_MODULE_FADE_TIME);
    if(isIE)
    {
      status_msg.style.display='none';
    }
    else
    {
      delaySetProperty(status_msg,status_msg.style,'display','none',this.ANIMATE_MODULE_FADE_TIME);
    }
  }
  ;
  
  
  sd_support.prototype.remove_module=function(cmd)
  {
    var url="/index";
    var data="remove=true&module="+cmd+"&session_token="+this.xsrf_token_;
    var callback=function(xmlHttpRequest)
    {
    }
    .bind(this);
    ajaxRequest(url,
    {
      postBody:data,onComplete:callback
    }
    );
    this.animate_module_destruction(cmd);
    this.undo_object_.undo_function_=function()
    {
      this.undo_remove_module(cmd);
    }
    .bind(this);
  }
  ;
  
  
  sd_support.prototype.animate_module_destruction=function(type)
  {
    var id="feedmodule-"+type;
    var anchor=_gel(id);
    if(isIE)
    {
      hideDiv(id);
    }
    else
    {
      ani.fadeDuringSlideUp(anchor,0,this.ANIMATE_MODULE_FADE_TIME);
    }
    var undo_id="UNDO-"+id;
    anchor.id=undo_id;
    this.scan_active_modules();
    this.reset_updown_buttons();
    this.show_undo_remove_msg();
  }
  ;
  
  
  sd_support.prototype.undo_remove_module=function(cmd)
  {
    var url="/index";
    var data="remove=true&undo=true&module="+cmd+"&session_token="+this.xsrf_token_;
    var callback=function(xmlHttpRequest)
    {
    }
    .bind(this);
//    ajaxRequest(url,{postBody:data,onComplete:callback});
    this.animate_module_recreation(cmd);
    this.undo_object_.undo_function_=null;
  }
  ;
  
  
  
  sd_support.prototype.animate_module_recreation=function(type)
  {
    var id="feedmodule-"+type;
    var undo_id="UNDO-"+id;
    var anchor=_gel(undo_id);
    anchor.style.display="block";
    if(isIE)
    {
      showDiv(undo_id);
    }
    else
    {
      ani.ajaxSlideOpen(anchor,0.0,this.ANIMATE_MODULE_FADE_TIME);
    }
    anchor.id=id;
    this.scan_active_modules();
    this.reset_updown_buttons();
    this.hide_undo_remove_msg();
  }
  ;
  
		Event.observe(document,'dom:loaded',init);
		function init() {

			Sortable.create('dragdrop',{tag:'div',handle:'feedmodule-modheader',hoverclass:'drag_placeholder',onUpdate:updateList});
		$('medit-POP').observe('click', function(event) { 
		var elt = Event.element(event).name; 
		var module=elt.substr(6,3);
		var els = module+"-options";
		var elq = $('POP-options'); 
		els.hide(); });
		//	$('medit-POP').observe('click',  sd_test('POP') );
	//	sd_test('POP');
			
		}
		function updateList(container) {
			var url = 'ajax.php';
			var params = Sortable.serialize(container.id);
			var ajax = new Ajax.Request(url,{
				method: 'post',
				parameters: params,
				onLoading: function(){$('workingMsg').show()},
				onLoaded: function(){$('workingMsg').hide()}
			});
		}
		


 function sd_test(module)
  {			$('mundo-remove').show();

    var url="/index";
 //   var data="alter=true&module="+module+"&ftype="+form_type+"&session_token="+this.xsrf_token_;
    var callback=function(xmlHttpRequest)
    {
      var module_anchor=_gel("feedmodule-"+module);
 //     module_anchor.innerHTML=xmlHttpRequest.responseText;
      this.reset_updown_buttons();
      showDiv(module+'-options');
      hideDiv(module+'-loading-icn');
      hideDiv(module+'-loading-msg');
    }
   // .bind(this);
    new Ajax.Updater('pop-data', 'url', {
    parameters: { onComplete:callback },
    insertion: Insertion.Bottom
    });
    //ajaxRequest(url,{postBody:data,onComplete:callback});
  };
		function init2(eld) {
		//	$('mundo-remove').show();
			new Effect.Parallel(
				[
					new Effect.SlideDown(eld,{sync:true}),
					new Effect.Opacity(eld,{sync:true, from: 0, to: 1 })
				],
				{
					duration:1.4,
					fps:40
				}
			)
		}