(function($) {

  this.GLOBAL = {
    debug: true,

    // Глобальный объект для переинициализации всего,
    // в ответ на аякс например
    aInit: [],
    reInit: function(func){
      try{
        if(func && $.isFunction(func)){
          func.call();
          this.aInit.push(func);
        } else {
          $.each(this.aInit, function(){
            this.call();
          })
        }
      } catch(e){
        debug(e);
      }
    }
  }

  this.debug = function(str) {
  	if (window.console && window.console.log && GLOBAL.debug)
  	window.console.log("[GLOBAL: '" + str + "']\n" + debug.caller.toString());
  };

})(jQuery);

(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
  // The base Class implementation (does nothing)
  this._Class = function(){};
  
  // Create a new Class that inherits from this class
  _Class.extend = function(prop) {
    var _super = this.prototype;
    
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
    
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" && 
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
            
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
            
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);        
            this._super = tmp;
            
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
    
    // The dummy class constructor
    function _Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
    
    // Populate our constructed prototype object
    _Class.prototype = prototype;
    
    // Enforce the constructor to be what we expect
    _Class.constructor = _Class;

    // And make this class extendable
    _Class.extend = arguments.callee;
    
    return _Class;
  };
})();

// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed

(function(){
  var cache = {};
  
  this.tmpl = function tmpl(str, data){
    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = !/\W/.test(str) ?
      cache[str] = cache[str] ||
        tmpl(str) :
      
      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +
        
        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +
        
        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");
    
    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
  };
})();


/*
  Required:
    — jQuery
    — jQuery Easing plugin
    — John Resig Simple JavaScript Inheritance (object: "_Class")
*/

(function( $ ){
  
  this.classAnimate = _Class.extend({
    init: function( step, duration, easing, callback ){
    	this.opt = {
    		step: step,
    		complete: callback ? callback : function(){}
    	};
    	if( $.isFunction(easing) ){
    	  this.opt.complete = easing;
    	}

    	this.now = 0;
    	this.duration = duration;

    	this.easing = !$.isFunction(easing) ? easing : "swing";

    	this._init();
    },

  	_init: function(){
  		var self = this;
  		this.startTime = ( new Date() ).getTime();
  		this.timerId = setInterval(function(){
  			self.next();
  		}, 13);
  	},

  	next: function(){
  		this.prev = this.now;
  		var t = ( new Date() ).getTime();
  		this.step = t - this.startTime;

  		if( this.step > this.duration ){
  			this.step = this.duration;
  			this.exec();
  			clearInterval(this.timerId);
  			this.opt.complete.apply(this);

  			return;
  		}
		
  		this.exec();
  	},

  	exec: function(){
  		this.state = this.step / this.duration;
  		this.now = $.easing[this.easing](this.state, this.step, 0, 1, this.duration);
  		this.opt.step.apply(this, [this.now, this.prev]);
  	},

  	stop: function(){
  		clearInterval(this.timerId);
  	}
    
  })

})( jQuery );


/*
 * Depend Class v0.1b : attach class based on first class in list of current element
 * File: jquery.dependClass.js
 * Copyright (c) 2009 Egor Hmelyoff, hmelyoff@gmail.com
 */


(function($) {
	// Init plugin function
	$.baseClass = function(obj){
	  obj = $(obj);
	  return obj.get(0).className.match(/([^ ]+)/)[1];
	};
	
	$.fn.addDependClass = function(className, delimiter){
		var options = {
		  delimiter: delimiter ? delimiter : '-'
		}
		return this.each(function(){
		  var baseClass = $.baseClass(this);
		  if(baseClass)
    		$(this).addClass(baseClass + options.delimiter + className);
		});
	};

	$.fn.removeDependClass = function(className, delimiter){
		var options = {
		  delimiter: delimiter ? delimiter : '-'
		}
		return this.each(function(){
		  var baseClass = $.baseClass(this);
		  if(baseClass)
    		$(this).removeClass(baseClass + options.delimiter + className);
		});
	};

	$.fn.toggleDependClass = function(className, delimiter){
		var options = {
		  delimiter: delimiter ? delimiter : '-'
		}
		return this.each(function(){
		  var baseClass = $.baseClass(this);
		  if(baseClass)
		    if($(this).is("." + baseClass + options.delimiter + className))
    		  $(this).removeClass(baseClass + options.delimiter + className);
    		else
    		  $(this).addClass(baseClass + options.delimiter + className);
		});
	};

	// end of closure
})(jQuery);

/* Global browsers definition */

(function($) {

	$(document).ready(function(){
		if($.browser.safari)
			$(document.documentElement)
			  .addClass("b-khtml")
			  .addClass("b-safari");

		if($.browser.mozilla)
			$(document.documentElement).addClass("b-gecko");

		if($.browser.opera)
			$(document.documentElement).addClass("b-opera");
	});

})(jQuery);

/* Global browsers definition (end) */

/* Global base events definition */

(function($) {

	$(document).ready(function(){

	  $(".hovered, j-hovered")
  	  .live("mouseover", function(){
    		$(this).addClass(this.className.match(/([^ ]+)/)[1] + "-hover");
  	  })
  	  .live("mouseout", function(){
    		$(this).removeClass(this.className.match(/([^ ]+)/)[1] + "-hover");
  	  })
  	  
  	$(".j-collapse")
  	  .live("click", function(){
  	    $(this).parents(".j-collapsed").toggleDependClass("collapsed");
  	  })

	});

})(jQuery);

/* Global base events definition (end) */

/* Placeholder for inputs */

(function($) {
  
  $(document).ready(function(){
    GLOBAL.reInit(function(){
      $("input[placeholder], textarea[placeholder]").not("[prettysearch=yes]").each(function(){
        new g_placeholder(this);
      })
    })
  })
  
  this.g_placeholder = function(input){
  	return this.init.apply(this, arguments);
  }
  
  g_placeholder.prototype = {
    init: function(obj){
      this.ptr = $(obj);

      this.placeholder = this.ptr.attr("placeholder");
      this.ptr.removeAttr("placeholder");

      this.create();
      this.bindEvents();
      
      if(this.ptr.val() != '')
        this.f_placeholder(false);
    },
    create: function(){
      this.domNode = $("<span>");
      this.domNode.css({
        display: "inline-block",
        position: "relative"
      })
      this.ptr.wrap(this.domNode);

      this.placeholderNode = $("<span>");
      this.placeholderNode
        .text(this.placeholder)
        .css({ position: "absolute", left: 3, top: 3, height: "1.5em", color: "gray", zIndex: 1 })
      this.ptr.after(this.placeholderNode);
    },
    bindEvents: function(){
      var $this = this;
      this.ptr
        .focus(function(){
          $this.f_placeholder(false);
        })
        .blur(function(){
          if($this.ptr.val() == '')
            $this.f_placeholder(true);
        })
      this.placeholderNode
        .mouseup(function(){
          $this.ptr.focus();
        })
    },
    f_placeholder: function(status){
      if(status)
        this.placeholderNode.show()
      else
        this.placeholderNode.hide()
    }
  }
  
})(jQuery);

/* Placeholder for inputs (end) */


/* Button object */

/* Атрибут checkedLabel надо показывать в нажатом состоянии */

(function($) {

  var buttons = new Array();

  this.b_button_OPTIONS = {
    defaultName: "button",
    className: "b-button",
    formClass: "b-button_form",
    template:
      '<div class="b-button" bindEvents="mousedown,mouseup,mouseover,mouseout">' +
        '<table><tr><td>' +
          '<div class="b-button-bg">' +
            '<i class="l g-png"></i><i class="r g-png"></i>' +
          '</div>' +
          '<div class="b-button-text" attachData="value">' +
            '<i class="b-button-icon g-png"></i>' +
          '</div>' +
        '</td></tr></table>' +
      '</div>'
  };

  this.b_button_IDs = 0;

  $(document).ready(function(){
    b_button_init();
  })
  
  this.b_button_init = function(){
    $("*[buttonType=group]").each(function(){
      new b_button_group(this);
    })
    $("button[buttonType]").each(function(){
      new b_button(this);
    })
  }


  this.b_button = function(){
  	return this.init.apply(this, arguments);
  }

  b_button.prototype = {
    init: function(obj, options){
      this.options = $.extend(b_button_OPTIONS, options ? options : {});

      obj.buttonHandler = this;
      this.domNode = $(obj);
      
      var b = this.isExist();
      if(b){ return b; }
      
      this.name = this.domNode.attr("name") ? this.domNode.attr("name") : this.options.defaultName + b_button_IDs++;
      this.value = this.domNode.html();
      this.checked = false;
      
      this.old = {} // save here old states if it changes
      
      this.is = {
        prevent: true,
        stateBubble: true
      };

      this.o = {
        type: this.domNode.attr("buttonType")
      };
      
      // data for append
      this.d = {
        value: this.value
      }

      var checkedLabel = this.domNode.attr("checkedLabel");
      if(checkedLabel && checkedLabel.length){
        this.d.value = '' +
          '<span class="off">' + this.value + '</span>' +
          '<span class="on">' + checkedLabel + '</span>'
      }

      
      // events for append
      this.e = {
        list: ["mousedown", "mouseup", "mouseover", "mouseout"],
        onstatechange: this.domNode.attr("onstatechange") ? this.domNode.attr("onstatechange") : function(){}
      };
      
      this.domNode
        .removeAttr("buttonType")
        .addClass(this.options.formClass)
        .html(this.parse(this.options.template)) // parse nodes and append events and data

      this.bindEvents();
        
      if(this.domNode.get(0).disabled) // define disabled state
        this.setClass(this.domNode, "disabled") 

      var checked = this.domNode.attr("checked");
      if(checked && (checked == 'true' || checked == 'checked') && this.o.type != "simple"){ // define checked state
        this.is.stateBubble = false;
        this.state("checked", true)
      }
      
      this.postCreate();

      buttons.push(this);
    },
    
    isExist: function(){
      for (var i=0; i < buttons.length; i++) {
        if(buttons[i].domNode.get(0) == this.domNode.get(0))
          return buttons[i];
      };
      return false;
    },
    
    postCreate: function(){

      var _on = this.o.value.find(".on");
      var _off = this.o.value.find(".off");
      var w = {
        on: _on.css({ display: "inline-block" }).width()+1,
        off: _off.css({ display: "inline-block" }).width()+1
      }
      _on.removeAttr("style");
      _off.removeAttr("style");

      if(w.on >= w.off){
        _on.css({ width: w.on });
        _off.css({ width: w.on });
      } else {
        _on.css({ width: w.off });
        _off.css({ width: w.off });
      }

    },
    
    bindEvents: function(){
      var $this = this;

      for (var i=0; i < this.e.list.length; i++) {
        var _event = this.e.list[i];
        if(this[_event]){
          
          (function(_event){
            $this.domNode.bind(_event, function(){
              $this[_event].apply($this, $this.e[_event] ? [$this.e[_event]] : null)
            });

            if(_event == 'mouseup')
              $(document).bind(_event, function(){
                $this[_event].apply($this, $this.e[_event] ? [$this.e[_event]] : null)
              })

          })(_event)

        }
      };

    },
    
    mousedown: function(node){
      var node = node ? node : this.domNode;

      switch(this.o.type){
        case 'trigger':
          if(this.checked){
            this.is.prevent = false;
          } else {
            this.state("checked", true);
            this.is.prevent = true;
          }
          break;

        case 'switcher':
          if(!this.checked)
            this.state("checked", true);
          
          break;

        case 'simple':
        default:
          this.is.prevent = false;
          this.setClass(node, "down");
          break;
      }

    },

    mouseup: function(node){
      var node = node ? node : this.domNode;

      if(!this.is.prevent){
        switch(this.o.type){
          case 'trigger':
            if(this.checked) this.state("checked", false);
            break;

          case 'trigger':
            
            break;

          case 'simple':
          default:
            this.setClass(node, "down", false);
            break
        }
      }

    },

    mouseover: function(node){
      var node = node ? node : this.domNode;
      this.setClass(node, "hover");
    },

    mouseout: function(node){
      var node = node ? node : this.domNode;
      this.setClass(node, "hover", false);
    },
    
    onstatechange: function(){
    },
    
    _onstatechange: function(){
      this.onstatechange.apply(this);
      if($.isFunction(this.e.onstatechange))
        this.e.onstatechange.apply(this)
      else
        eval(this.e.onstatechange)
    },
    
    parse: function(tpl){
      var $this = this;
      
      var tpl = $(tpl);
      tpl.filter("[bindEvents]").add(tpl.find("*[bindEvents]")).each(function(){
        var node = $(this);
        jQuery.each(node.attr("bindEvents").split(/ *, */), function(){
          var _event = this.toString();
          $this.e[_event] = node;
        });
        $(this).removeAttr("bindEvents");
      })

      tpl.filter("[attachData]").add(tpl.find("*[attachData]")).each(function(){
        var node = $(this);
        jQuery.each(node.attr("attachData").split(/ *, */), function(){
          var _type = this.toString();
          node.append($this.d[_type]);
          $this.o[_type] = node;
        });
        $(this).removeAttr("attachData");
      })
      
      return tpl;

    },
    
    setClass: function(node, sClass, b){
      if(b === false) node.removeClass(this.options.className + "_" + sClass);
      else node.addClass(this.options.className + "_" + sClass);
    },
    
    state: function(name, value){
      this.is.prevent = true;

      this.old[name] = this[name];
      this[name] = value;
      switch(name){
        case 'checked':
          if(this.old[name] != this[name] && this.is.stateBubble)
            this._onstatechange()
          if(!this.is.stateBubble)
            this.is.stateBubble = true;

          if(!value)
            this.setClass(this.e.mouseup, "down", false);
          else
            this.setClass(this.e.mouseup, "down");
          
          break;

        case 'disabled':
          if(value){
            this.setClass(this.domNode, name)
            this.domNode.attr(name, true)
          } else {
            this.setClass(this.domNode, name, false)
            this.domNode.removeAttr(name)
          }
          
          break;
      }
    }
    
    
  }

  function b_button_group(){
  	return this.init.apply(this, arguments);
  }

  b_button_group.prototype = {
    init: function(obj){
      var $this = this;
      this.buttons = new Array();
      var buttons = $(obj).addClass(b_button_OPTIONS.className + "_group").find("button[buttonType]");

      this.e = {
        onstatechange: $(obj).attr("onstatechange") ? $(obj).attr("onstatechange") : function(){}
      };

      buttons.each(function(i){
        var button = new b_button(this);
        var sClass = "center";
        if(i == 0) sClass = "left";
        if(i == buttons.length - 1) sClass = "right";
        
        if(buttons.length != 1)
          button.domNode.wrap($("<div>").addClass(b_button_OPTIONS.className + "_" + sClass))
        
        $this.buttons.push(button);
      })
      
      if(buttons.length != 1)
        this.bindEvents();
    },
    
    bindEvents: function(){
      $this = this;
      $.each(this.buttons, function(){
        this.onstatechange = function(){
          $this.onclick(this)
        };
      })
    },
    
    onclick: function(obj){
      var $this = this;

      state = {};
      state[obj.name] = true;
      state.new_state = obj.name;

      $.each(this.buttons, function(){
        if(this != obj){
          this.is.stateBubble = false;
          this.state("checked", false);
          if(this.old.checked == true && this.checked == false) {
            state[this.name] = false;
            state.old_state = this.name;
          }
        }
      })
      obj.is.prevent = true;
      

      if($.isFunction(this.e.onstatechange))
        this.e.onstatechange.apply(this)
      else
        (function(state){
          eval($this.e.onstatechange)
        }).call(obj, state)
      
    }
    
  };

})(jQuery);

/* Button object (end) */


/**************************************************
	Class:  Pretty Search
	Author: Egor Hmelyoff (hmelyoff@gmail.com)
	TODO:
		— allow safari to use own input[type=search]
		— find and return object if exist
	
**************************************************/
/*global document, jQuery */

var _classPrettySearchInit = true;
var _classPrettySearchSave = [];


function PrettySearch(){
	return this.init.apply(this, arguments);
}

jQuery(document).ready(function(){
	if(_classPrettySearchInit){
		jQuery("input[prettysearch=yes]").each(function(){
			_classPrettySearchSave.push(new PrettySearch(jQuery(this)));
		});
	}
});

PrettySearch.prototype = {
	init: function(){
		
		// Define default boolean vars
		this.is = {
			input: false
		};

		this._init.apply(this, arguments);
		
		return this;
	},
	
	_init: function(){
		var ptr = jQuery(arguments[0]);
		if(!ptr.is("input")) {ptr = ptr.find("input[type=text]");}

		if(ptr.is("input")){
			this.hSettings = {
				// base vars
				baseClass: "b-search",
				ptr: arguments[0],

				// default attributes
				id: ptr.attr("id") ? ptr.attr("id") : null,
				sClass: ptr.attr("class") ? ptr.attr("class"): null,
				placeholder: ptr.attr("placeholder") ? ptr.attr("placeholder") : null,
				incremental: ptr.attr("incremental") ? ptr.attr("incremental") : 'no',
				results: ptr.attr("results") ? ptr.attr("results") : 0,
				autocomplete: ptr.attr("autocomplete") ? ptr.attr("autocomplete") : 'on',
				
				// form attributes
				name: ptr.attr("name") ? ptr.attr("name") : 'q',
				value: ptr.attr("value") ? ptr.attr("value") : null,
				
				width: ptr.width()-13,
				
				// events
				onsearch: ptr.attr("onsearch") ? ptr.attr("onsearch") : function(){}
			};
			
			jQuery.extend(this.hSettings, arguments[1] ? arguments[1] : {});
			
			
			var ps = jQuery('<div></div>')
				.addClass(this.hSettings.sClass)
				.addClass(this.hSettings.baseClass);
				// .css({ width: this.hSettings.width });
			
			if(this.hSettings.id) {ps.attr("id", this.hSettings.id);}
			
			ps.append(
				'<div class="' + this.hSettings.baseClass +'_left g-png"></div>' +
				'<div class="' + this.hSettings.baseClass +'_container">' +
				  '<div class="' + this.hSettings.baseClass +'_right g-png"></div>' + 
				  '<div class="' + this.hSettings.baseClass +'_container">' +
  					'<span class="' + this.hSettings.baseClass +'_placeholder">' + (this.hSettings.placeholder ? this.hSettings.placeholder : '') + '</span>' +
  					'<span class="' + this.hSettings.baseClass +'_spinner"></span>' +
  					'<span class="' + this.hSettings.baseClass +'_clear"></span>' +
  				'</div>'+
				'</div>');

			var _ptr = ptr.clone(true);
			if(!jQuery.browser.msie) {
				_ptr.attr("type", "text");
			}
			
			ps.find("span." + this.hSettings.baseClass +"_placeholder").after(_ptr);

			ps.find("div." + this.hSettings.baseClass +"_container input")
				.removeAttr("placeholder")
				.removeAttr("incremental")
				.removeAttr("prettysearch")
				.addClass(this.hSettings.baseClass + "_input")
				.css({ outlineWidth: 0 });
				
			ptr.replaceWith(ps);
			this.ptr = ps;

			if(!this.hSettings.value && this.hSettings.placeholder) {
				this.placeholder(true);
			}

			if(this.hSettings.value) {
				this.clear(true);
			}
			this._events();
			
		} else {return false;}
	},
	
	_events: function(){
		var oThis = this;
		this.ptr.mousedown(function(){
			oThis.placeholder(false);
			oThis.ptr.addClass(oThis.hSettings.baseClass + "-focus");
			if(!oThis.is.input) {
				return false;
			} else {
				oThis.is.input = false;
			}
		})
		.mouseup(function(){
			oThis.ptr.find("input." + oThis.hSettings.baseClass + "_input").focus();
		});
		this.ptr.find("input." + this.hSettings.baseClass + "_input")
			.focus(function(){
				oThis.ptr.addClass(oThis.hSettings.baseClass + "-focus");
				oThis.placeholder(false);
			})
			.blur(function(){
				var $this = jQuery(this);
				oThis.ptr.removeClass(oThis.hSettings.baseClass + "-focus");
				if(!$this.val()) {
					oThis.placeholder(true);
				}
			})
			.keyup(function(){
				var $this = jQuery(this);
				if($this.val()) {
					oThis.clear(true);
				} else {
					oThis.clear(false);
				}
				
				oThis.value = $this.val();
				if(oThis.hSettings.incremental == 'yes') {
					oThis.onsearch();
				}
			})
			.mousedown(function(){
				oThis.is.input = true;
			})
			.change(function(){
				oThis.value = jQuery(this).val();
				if(oThis.hSettings.incremental == 'yes') {
					oThis.onsearch();
				}
			})
			.parents("form").submit(function(){
				oThis.onsubmit();
			});
			
		this.ptr.find("span." + this.hSettings.baseClass + "_clear")
			.mousedown(function(){
				jQuery(this).addClass(oThis.hSettings.baseClass + "_clear_down");
				oThis.ptr.find("input." + oThis.hSettings.baseClass + "_input").focus().select();
				return false;
			})
			.click(function(){
				oThis.value = oThis.ptr.find("input." + oThis.hSettings.baseClass + "_input").val("").focus().val();
				jQuery(this).removeClass(oThis.hSettings.baseClass + "_clear_down").hide();
				if(oThis.hSettings.incremental == 'yes') {
					oThis.onsearch();
				}
			});
			
		this.onsearch = function(){
			if(jQuery.isFunction(this.hSettings.onsearch)) {
				this.hSettings.onsearch.apply(oThis);
			} else {
				eval(this.hSettings.onsearch);
			}
		};
		
		this.events();
			
	},
	
	events: function(){
		
	},
	
	onsearch: function(){
		
	},
	
	onsubmit: function(){
		this.onsearch();
	},
	
	placeholder: function(b){
		var p = this.ptr.find("span." + this.hSettings.baseClass + "_placeholder");
		if(b) {p.show();}
		else {p.hide();}
	},
	
	clear: function(b){
		var p = this.ptr.find("span." + this.hSettings.baseClass + "_clear");
		if(b) {p.show();}
		else {p.hide();}
	},
	
	loading: function(b){
		if(b) {
			this.ptr.addClass(this.hSettings.baseClass + "-loading");
		} else {
			this.ptr.removeClass(this.hSettings.baseClass + "-loading");
		}
	}

};


/* Button object */

var b_notice_OPTIONS = {
  template:
    '<span class="b-notice">' +
      '<i class="icon"></i>' +
      '<span class="b-notice-animate">' +
        '<span class="hide">скрыть</span>' +
        '<span class="b-notice-text"></span>' +
      '</span>' +
    '</span>',
    
  selectorHTML: ".b-notice-text",
  selectorAnimate: ".b-notice-animate",
  selectorEventShow: "i.icon",
  selectorEventHide: ".hide"
};

(function($) {
  
  $(document).ready(function(){
    $(".b-notice").not(".b-notice_show").each(function(){
      new b_notice(this)
    })
  })

  function b_notice(){
  	return this.init.apply(this, arguments);
  }

  b_notice.prototype = {
    init: function(obj, options){
      this.options = $.extend(b_notice_OPTIONS, options ? options : {});
      
      this.is = {
        display: false,
        animate: false
      }

      this.domNode = $(obj);
      this.className = this.domNode.attr("class");
      this.html = this.domNode.html();
      
      var domNode = $(this.options.template)
        .addClass(this.className);

      domNode.find(this.options.selectorHTML).html(this.html);
        
      this.domNode.replaceWith(domNode);
      this.domNode = domNode;

      this.bindEvents();

      domNode.show();
      
    },
    
    bindEvents: function(){
      var $this = this;
      this.domNode.find(this.options.selectorEventShow)
        .click(function(){
          if(!$this.is.animate){
            var node = $this.domNode.find($this.options.selectorAnimate);

            if($this.is.display){
              hide(node)
            } else {
              show(node);
            }
          }
        })

      this.domNode.find(this.options.selectorEventHide)
        .click(function(){
          if(!$this.is.animate)
            hide($this.domNode.find($this.options.selectorAnimate));
        })
        
      var show = function(node){
        $this.is.display = true;
        $this.is.animate = true;

        var h = node.css({ display: "block" }).height();
        node.hide();
        
        node.css({ height: 0, overflow: "hidden", position: "relative", opacity: 0 })
          .animate({ height: h, opacity: 1 }, function(){
            $this.is.animate = false;
          })
      }
      
      var hide = function(node){
        $this.is.display = false;
        $this.is.animate = true;
        node
          .animate({ height: 0, opacity: 0 }, function(){
            $(this).removeAttr("style");
            $this.is.animate = false;
          })
      }
    }
    
  };

})(jQuery);

/* Button object (end) */


/* Subscription object */

(function($) {
	$(document).ready(function(){
		if($('div.l-subscriptions').length != 0){
			var subscription = new Subscription();
			subscription.init();
		}
	});

	function Subscription(){
	
	}

	Subscription.prototype = {
		init:function(){
			var _my = this;
			this.flows = $('div.l-subscriptions ul.list li');
			this.flowNameBlock = $('div.l-subscriptions ul.list li div.short');
			this.flowName = $('div.l-subscriptions ul.list li div.short p b');
			this.flowSubscrIcon = $('div.l-subscriptions ul.list li div.short p span');
			this.flowNameBlock.hover(function(){
				$(this).parent().addClass('hover');
			}, function(){
				$(this).parent().removeClass('hover');
			});
			this.flowName.click(function(){
				_my.closeFlow(_my.flows)
				if($(this).parents('li').is('.open')){
					_my.closeFlow($(this).parents('li'));
				} else {
					_my.openFlow($(this).parents('li'));
				}
			});
			this.flowSubscrIcon.click(function(){
				if(!$(this).parents('li').is('.open')){
					_my.closeFlow($(this).parents('li'));
				}
			});
		},
		closeFlow:function(elem){
				var open = $('li.open');
				toClose = open.find('div.full')
				$( '.links .update', toClose).hide()
				toClose.animate({height:0}, 'fast', function(){
					open.removeClass('open');
				});
		},
		openFlow:function(elem){
			if(!elem.is('.open')){
				elem.addClass('open');
				elem.find('div.full').animate({height:105}, 'fast');
			}
		}
	}
})(jQuery);

/* Subscription object (end) */



/* Micro in header */

(function($) {
  
	function debug(str) {
		if (window.console && window.console.log && GLOBAL.debug)
		window.console.log("[header.micro: '" + str + "']\n" + debug.caller.toString());
	};

  this.header_micro_OPTION = {
    selectorRoot: ".b-header .b-header_micro",
    selectorUpdateHTML: ".j-header-micro-update",
    selectorStatus: ".j-header-micro-status",

    selectorPlaceholder: ".placeholder",
    selectorPlaceholderType: {
      success: ".success"
    },

    selectorInput: "input",
    selectorSubmit: "span.submit",

    colorFrom: "#ffffff",
    colorTo: "#ffef95"
  };
  
  $(document).ready(function(){
    header.micro.init({ debug: true });
  })
  
  if(!this.header)
    this.header = new Object();
  
  this.header.micro = {
    init: function(options){
      this.options = $.extend(header_micro_OPTION, options ? options : {});
      this.ptr = $(this.options.selectorRoot);
      
      this.is = {
        init: false,
        input: false,
        submited: false
      }
      
      if(this.ptr.length){
        this.is.init = true;
        this.bindEvents();
      }
      
    },
    
    bindEvents: function(){
      var $this = this;
      this.ptr
        .mousedown(function(){
          focus();
    			if(!$this.is.input) return false;
    			else $this.is.input = false;
        })
        .mouseup(function(){
          $this.ptr.find($this.options.selectorInput).focus();
        })
        
      this.ptr.find(this.options.selectorInput)
        .focus(function(){
          focus();
        })
        .blur(function(){
          $this.ptr.removeDependClass("focused");
          if(this.value == "")
            $this.placeholder(true);
        })
        .mousedown(function(){
          $this.is.input = true;
        })
        
        var focus = function(){
          $this.ptr.addDependClass("focused");
          $this.placeholder(false);
        }
        
    },
    
    placeholder: function(b){
      var type;
      if(this.is.submited) type = "success";

      var p = this.ptr.find(this.options.selectorPlaceholder)
      if(b){
        p.show();
        if(type && this.options.selectorPlaceholderType[type]){
          p.hide().filter(this.options.selectorPlaceholderType[type]).show();
        } else {
          $.each(this.options.selectorPlaceholderType, function(){
            p.filter(this.toString()).hide();
          })
        }
      } else
        p.hide();
    },
    
    update: function(html){
      if(!this.is.init) return false;

      try{
        $(this.options.selectorUpdateHTML).replaceWith(html);
      }catch(e){
        debug(e);
      }
    },
    
    before: function(){
      if(!this.is.init) return false;
      if(this.ptr.find(this.options.selectorInput).attr("value") == "") { return false; }
      this.ptr.addDependClass("loading");
      return true;
    },
    
    success: function(){
      if(!this.is.init) return false;
      var $this = this;

      this.is.submited = true;
      this.ptr.find(this.options.selectorInput).val("").blur();
      this.ptr.removeDependClass("loading");
      $(this.options.selectorStatus)
        .css({ backgroundColor: this.options.colorTo })
        .animate({ backgroundColor: this.options.colorFrom }, 1000)
    }
  }

})(jQuery);

/* Micro in header (end) */


/*global clearTimeout, document, jQuery, setTimeout*/
(function($){
  function Cache(options, persistent_data) {
    this.options = options;
  	if(!options.cacheLength || options.cacheLength < 1) {
  	  this.options.cacheLength = 1;
  	}

  	this.persistent = {};
  	if(this.options.data){
  	  this.initPersistent(this.options.data);
  	}
    this.flush();
  }

  Cache.prototype = {
    flush : function() {
  		this.data = {};
    	this.length = 0;
    },

    initPersistent : function(data) {
  		var sFirstChar = "", stMatchSets = {}, row = [];

  		// loop through the array and create a lookup structure
  		for( var i=0; i < data.length; i++ ){
  			row = data[i].strip();

  			// if the length is zero, don't add to list
  			if( row.length > 0 ){
  				// get the first character
  				sFirstChar = row.substring(0, 1).toLowerCase();
  				// if no lookup array for this character exists, look it up now
  				if( !stMatchSets[sFirstChar] ) {stMatchSets[sFirstChar] = [];}
  				// if the match is a string
  				stMatchSets[sFirstChar].push(row);
  			}
  		}

  		// add the data items to the cache
  		for(var k in stMatchSets){ 
  			// add to the cache
  			this.addToPersistent(k, stMatchSets[k]);
  		}
    },

  	addToPersistent : function(q, data) {
  		if (!data || !q) {return;}
  		this.persistent[q] = data;
  	},

  	lookup : function(q) {
  		return this.merge(this.lookupPersistent(q), this.lookupTemp(q));
  	},

  	merge : function(cache1, cache2) {
  		var result = [];
  		if(cache1) {for(var i = 0; i < cache1.length; i++) {
  			result[i] = cache1[i];
  		}}
  		if(cache2) {for(i = 0; i < cache2.length; i++) {
  			if(!result.include(cache2[i])) {
  				result[result.length] = cache2[i];
  			}
  		}}
  		return result.length > 0 ? result : null;
  	},

  	lookupTemp : function(q) {
  		return this.lookupCache(q, this.data);
  	},

  	lookupPersistent : function(q) {
  		return this.lookupCache(q, this.persistent);
  	},

  	lookupCache : function(q, cache) {
  		if (!q || !cache) {return null;}
  		if (cache[q]) {return cache[q];}
  		if (this.options.matchSubset) {
  			var csub = [];
  			for (var i = q.length - 1; i >= this.options.minChars; i--) {
  				var qs = q.substr(0, i);
  				var c = cache[qs];
  				if (c) {
  					for (var j = 0; j < c.length; j++) {
  						var x = c[j];
  						if (csub.indexOf(x) == -1 && this.matchSubset(x, q)) {
  							csub[csub.length] = x;
  						}
  					}
  				}
  			}

  			csub = csub.uniq();
  			if(csub.length > 0) {
  				cache[q] = csub;
  				return csub;
  			}
  		}
  		return null;
  	},

  	matchSubset : function(s, sub) {
  	  if(!s) {return false;}
  		if (!this.options.matchCase) {s = s.toLowerCase();}
  		var i = s.indexOf(sub);
  		if (i == -1) {return false;}
  		return i === 0 || this.options.matchContains;
  	},

  	store : function(q, data) {
  		if (!data || !q) {return;}
  		if (!this.length) {
  			this.flush();
  			this.length++;
  		} else if (!this.data[q]) {
  			this.length++;
  		}
  		this.data[q] = data;
  	}

  };
  
  
  function Results(input, results, options) {
    var _res = results.get(0);
    document.body.appendChild(_res);
    this.results = $(_res);
    this.timeout = null;
    this.input = input;
    
  	this.results.hide();
  	this.options = options;
  	this.active = -1;
  	if(typeof(this.options.width) == "string") {
  	  this.options.width = parseInt(this.options.width, 10);
	  }
  	if( this.options.width > 0 ) {this.results.css("width", this.options.width);}
  }
  
  Results.prototype = {
    moveUp : function() {
      this.moveSelect(-1);
    },
    
    moveDown : function() {
      this.moveSelect(1);
    },
    
    moveSelect : function(step) {
  		var lis = $("li", this.results);
  		if (!lis) {return;}

  		this.active += step;

  		if (this.active < 0) {
  			this.active = 0;
  		} else if (this.active >= lis.size()) {
  			this.active = lis.size() - 1;
  		}

  		lis.removeClass("selected");

  		$(lis[this.active]).addClass("selected");

  		if(this.options.autoFill) {
  		  var li = $(lis[this.active]).get(0);
  		  this.input.autoFill(li.selectData || li.selectValue);
  		}

  		// Weird behaviour in IE
  		// if (lis[active] && lis[active].scrollIntoView) {
  		// 	lis[active].scrollIntoView(false);
  		// }

  	},
  	
  	hideNow : function() {
  		if (this.timeout) {clearTimeout(this.timeout);}
  		if (this.results.is(":visible")) {
  			this.results.hide();
  		}
  		if(this.options.mustMatch) {
  			var v = this.input.value();
  		  if (v != this.input.lastSelected) {
  				this.selectItem(null);
  			}
      }
  	},

  	hide : function() {
  		if (this.timeout) {clearTimeout(this.timeout);}
  		var self = this;
  		this.timeout = setTimeout(function() {
  		  self.hideNow();
  		}, 200);
  	},
  	
   	selectCurrent : function() {
  		var li = $("li.selected", this.results)[0];
  		if (!li) {
  			var $li = $("li", this.results);
  			if (this.options.selectOnly) {
  				if ($li.length == 1) {li = $li[0];}
  			} else if (this.options.selectFirst) {
  				li = $li[0];
  			}
  		}
  		if (li) {
  			this.selectItem(li);
  			return true;
  		} else {
  			return false;
  		}
  	},
  	
  	show : function() {

  		// top : 50, width: 168; left: 0; position: absolute
  		var top = (this.input.input.offset().top + this.input.input.get(0).offsetHeight);
  		var left = this.input.input.offset().left;
  		
  		this.results.css({
  			position: 'absolute',
  			width: (this.options.width + 6) + "px",
  			top: top + "px",
  			left : left +"px",
  			zIndex: 999
  		}).
  		show().
  		find("li:first").
  		  addClass("selected").
  		end();
  	},
  	
  	selectItem : function(li) {
  		if (!li) {
  			li = document.createElement("li");
  			li.extra = [];
  			li.selectValue = "";
  		}
  		var v = $.trim(li.selectValue ? li.selectValue : li.innerHTML);
  		this.results.html("");
  		this.input.trigger('valueselected', [v]);

  		if (this.options.onItemSelect) {setTimeout(function() { this.options.onItemSelect(li); }, 1);}
  	},
  	
    dataToDom : function(data) {
  		var ul = document.createElement("ul");
  		var num = data.length;
  		var self = this;

  		// limited results to a max number
  		if( (this.options.maxItemsToShow > 0) && (this.options.maxItemsToShow < num) ) {num = this.options.maxItemsToShow;}

  		for (var i=0; i < num; i++) {
  			var row = data[i];
  			var li = document.createElement("li");
  			if (this.options.formatItem) {
  				li.innerHTML = this.options.formatItem(row, i, num);
    			li.selectValue = row;
  			} else if(typeof(row) != "string") {
  			  li.innerHTML = "";
  			  for(var key in row) {
  			    if(row[key]) {
    			    li.innerHTML += "<span>"+row[key]+"</span>";
  			    }
  			  }
    			li.selectValue = row[this.options.select];
    			li.selectData = row;
  			} else {
  				li.innerHTML = row;
    			li.selectValue = row;
  			}
  			ul.appendChild(li);
  		}
  		var lis = $(ul).find("li");
  		lis.hover(
				function() { 
				  lis.removeClass("selected"); 
				  $(this).addClass("selected"); 
				  self.active = lis.indexOf($(this).get(0)); },
				function() { $(this).removeClass("selected"); }
			).click(function(e) { e.preventDefault(); e.stopPropagation(); self.selectItem(this); });
			
  		return ul;
  	},
  	
  	loadData : function(data) {
  	  this.results.html("");
			if ($.browser.msie) {
				// we put a styled iframe behind the calendar so HTML SELECT elements don't show through
				//this.results.append(document.createElement('iframe'));
			}
			this.active = 0;
			this.results.append(this.dataToDom(data));
  	}
  };
  
  function Input(input, me, options) {
    this.input = $(input);
    this.input.get(0).autocompleter = me;
  	this.input.attr("autocomplete", "off");
  	this.id = '#' + this.input.attr('id');
  	
  	
  	this.spinner = $(this.id + "_auto_complete_indicator");
  	this.spinner.css('left', (parseInt(this.input.css('width'), 10) - 15) +"px"); //165px
  	
  	this.options = options;
  	if (options.inputClass) {this.input.addClass(options.inputClass);}

  	this.hasFocus = false;
  	this.lastKeyPressCode = null;
    var timeout = null;
    
    this.prev = "";
  	

		var self = this;
  	this.input.keydown(function(e) {
  		// track last key pressed
  		this.lastKeyPressCode = e.keyCode;
  		switch(e.keyCode) {
  			case 38: // up
  				e.preventDefault();
  				self.input.trigger('moveup');
  				break;
  			case 40: // down
  				e.preventDefault();
  				self.input.trigger('movedown');
  				break;
  			//case 9:  // tab
  			case 13: // return
				  //self.input.get(0).blur();
  			  self.input.trigger('selectcurrent');
					e.preventDefault();
					return false;
  				break;
  			default:
  				if (timeout) {clearTimeout(timeout);}
  				timeout = setTimeout(function(){self.change();}, options.delay);
  				break;
  		}
  	})
  	.focus(function(){
  		self.hasFocus = true;
  	})
  	.blur(function() {
  		self.hasFocus = false;
  		self.input.trigger('valuehide');
  	});
  }
  
  Input.prototype = {
    change : function() {
  		// ignore if the following keys are pressed: [del] [shift] [capslock]
  		if(this.lastKeyPressCode == 46 || (this.lastKeyPressCode > 8 && this.lastKeyPressCode < 32) ) {
  		  return this.input.trigger('hide');
  		}
  		
  		var v = this.value();
  		if (v == this.prev) {return;}
  		this.prev = v;
  		if (v.length >= this.options.minChars) {
  		  this.startLoad();
  			this.input.trigger('valuechange', [v]);
  		} else {
  		  this.stopLoad();
  			this.input.trigger('valuehide');
  		}
  	},
  	
  	startLoad : function() {
  	  this.input.addClass(this.options.loadingClass);
  	},
  	
  	stopLoad : function() {
			this.input.removeClass(this.options.loadingClass);
			this.hideSpinner();
  	},
  	
  	showSpinner : function() {
  	  this.spinner.show();
  	},
  	
  	hideSpinner : function() {
  	  this.spinner.hide();
  	},
    
    value : function() {
  		var parts = this.input.val().split(",").map(function(token) { return token.strip();});
  		return parts.length > 0 ? parts[parts.length - 1] : "";
  	},

  	setValue : function(value) {
  		var parts = this.input.val().split(",").map(function(token) { return token.strip();});
  		var current = parts.pop();
  		parts[parts.length] = value;
  		this.input.val(parts.join(",")).trigger('valueSet', value);
  	},

  	currentStartPosition : function() {
  		var index = this.input.val().lastIndexOf(",");
  		return index > 0 ? index + 1 : 0;
  	},
  	
  	// selects a portion of the input string
  	createSelection : function(start, end){
  		// get a reference to the input element
  		var field = this.input.get(0);
  		var offset = this.currentStartPosition();
  		start = offset + start;
  		end = offset + end;
  		if( field.createTextRange ){
  			var selRange = field.createTextRange();
  			selRange.collapse(true);
  			selRange.moveStart("character", start);
  			selRange.moveEnd("character", end);
  			selRange.select();
  		} else if( field.setSelectionRange ){
  			field.setSelectionRange(start, end);
  		} else if( field.selectionStart ){
  				field.selectionStart = start;
  				field.selectionEnd = end;
			}
  		field.focus();
  	},
  	
  	autoFill : function(data){
  	  var sValue = typeof(data) == "string" ? data : data[this.options.select];
  		// if the last user key pressed was backspace, don't autofill
  		if(this.lastKeyPressCode == 8 ){
  		  return false;
  		}
  		
  		if(typeof(data) != "string" && this.options.fieldPrefix) {
        for(var field in data) {
          $("#"+this.options.fieldPrefix+"_"+field).val(data[field]);
        }
  		}

			this.setValue(sValue);
			
			this.createSelection(this.prev.length, sValue.length);
  	},
  	
  	receiveData : function(q, data) {
			if(!this.hasFocus || !data.length) {return false;}
			if(this.options.autoFill && (this.value().toLowerCase() == q.toLowerCase()) ) {
			  this.autoFill(data[0]);
			}
			return true;
  	},
  	
  	trigger : function(event, data) {
  	  this.input.trigger(event, data);
  	}
  };
  
  $.autocomplete = function(input_id, options) {
  var input = new Input(input_id, this, options);
	if(!options.width > 0) {
	  options.width = input.input.css('width');
	} 
	var results = new Results(input, $(input.id + "_auto_complete"), options);
	var cache = new Cache(options, options.data);
	

	var prev_empty = false;
	var keyb = false;
	var request = null;

	function makeUrl(q) {
		var url = options.url + "/" + encodeURI(q);// q=
		for (var i in options.extraParams) {
			url += "&" + i + "=" + encodeURI(options.extraParams[i]);
		}
		if(options.with_params && typeof(options.with_params) == "function") {
		  url += options.with_params();
		}
		return url;
	}
	
	function receiveData(q, data) {
		if (data) {
			input.stopLoad();
			
			// if the field no longer has focus or if there are no matches, do not display the drop down
			if(!input.receiveData(q, data)) {return results.hideNow();}

			results.loadData(data);
			results.show();
		} else {
			results.hideNow();
		}
	}
	
	
	function requestData(event, q) {
		if (!options.matchCase) {q = q.toLowerCase();}
		var cached_data = cache.lookup(q);

		if (cached_data) {
			receiveData(q, cached_data);
		}

		if((!cached_data || cached_data.length < options.maxItemsToShow) && 
			(typeof options.url == "string") && (options.url.length > 0) ){
//			if(prev && q.indexOf(prev) == 0 && prev_empty) return;
			//if(request) {
			//	request.onreadystatechange = null;
			//}
			request = $.ajax({
				url : makeUrl(q), 
				async : true,
				beforeSend : function() {input.showSpinner();},
				dataType : 'json',
				complete: function() {input.hideSpinner();},
				success : function(data) {
          cache.store(q, data);
					receiveData(q, cache.merge(cached_data, data));
				}
			});
		// if there's been no data found, remove the loading class
		} else {
		  input.stopLoad();
		}
	}
	
	
	input.input.
	  bind('moveup', function(){results.moveUp();}).
	  bind('movedown', function(){results.moveDown();}).
	  bind('selectcurrent', function(){results.selectCurrent();}).
	  bind('valuehide', function(){results.hide();input.stopLoad();}).
	  bind('valuechange', requestData).
	  bind('valueselected', function(event, v) {
	    input.lastSelected = v;
  		this.prev = v;
  		if(request) {
  			//request.onreadystatechange = null;
  			//request.state = 0;
  			input.hideSpinner();
  			request = null;
  		}
  		input.setValue(v);
  		//results.hideNow();
  		cache.flush();
	  });


	


};

jQuery.fn.autocomplete = function(url, options, data) {
	// Make sure options exists
	options = options || {};
	// Set url as option
	options.url = url;
	// set some bulk local data
	options.data = (data && (typeof data == "object") && (data.constructor == Array)) ? data : null;

	// Set default values for required options
	options.inputClass = options.inputClass || "ac_input";
	options.resultsClass = options.resultsClass || "ac_results";
	options.lineSeparator = options.lineSeparator || "\n";
	options.cellSeparator = options.cellSeparator || "|";
	options.minChars = options.minChars || 2;
	options.delay = options.delay || 400;
	options.matchCase = options.matchCase || 0;
	options.matchSubset = options.matchSubset || true;
	options.matchContains = options.matchContains || false;
	options.cacheLength = options.cacheLength || 1;
	options.mustMatch = options.mustMatch || 0;
	options.extraParams = options.extraParams || {};
	options.loadingClass = options.loadingClass || "ac_loading";
	options.selectFirst = options.selectFirst || false;
	options.selectOnly = options.selectOnly || false;
	options.maxItemsToShow = options.maxItemsToShow || 8;
	options.autoFill = options.autoFill || true;
	options.width = parseInt(options.width, 10) || 0;

	this.each(function() {
		var input = this;
		var autocompleter = new jQuery.autocomplete(input, options);
	});

	// Don't break the chain
	return this;
};

jQuery.fn.autocompleteArray = function(data, options) {
	return this.autocomplete(null, options, data);
};

jQuery.fn.indexOf = function(e){
	for( var i=0; i<this.length; i++ ){
		if( this[i] == e ) {return i;}
	}
	return -1;
};

})(jQuery);


/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 9/11/2008
 * @author Ariel Flesler
 * @version 1.4
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);

// script.aculo.us effects.js v1.8.1, Thu Jan 03 22:07:12 -0500 2008

// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
// Contributors:
//  Justin Palmer (http://encytemedia.com/)
//  Mark Pilgrim (http://diveintomark.org/)
//  Martin Bialasinki
// 
// script.aculo.us is freely distributable under the terms of an MIT-style license.
// For details, see the script.aculo.us web site: http://script.aculo.us/ 

// converts rgb() and #xxx to #xxxxxx format,  
// returns self (or first argument) if not convertable  
String.prototype.parseColor = function() {  
  var color = '#';
  if (this.slice(0,4) == 'rgb(') {  
    var cols = this.slice(4,this.length-1).split(',');  
    var i=0; do { color += parseInt(cols[i]).toColorPart() } while (++i<3);  
  } else {  
    if (this.slice(0,1) == '#') {  
      if (this.length==4) for(var i=1;i<4;i++) color += (this.charAt(i) + this.charAt(i)).toLowerCase();  
      if (this.length==7) color = this.toLowerCase();  
    }  
  }  
  return (color.length==7 ? color : (arguments[0] || this));  
};

/*--------------------------------------------------------------------------*/

Element.collectTextNodes = function(element) {  
  return $A($(element).childNodes).collect( function(node) {
    return (node.nodeType==3 ? node.nodeValue : 
      (node.hasChildNodes() ? Element.collectTextNodes(node) : ''));
  }).flatten().join('');
};

Element.collectTextNodesIgnoreClass = function(element, className) {  
  return $A($(element).childNodes).collect( function(node) {
    return (node.nodeType==3 ? node.nodeValue : 
      ((node.hasChildNodes() && !Element.hasClassName(node,className)) ? 
        Element.collectTextNodesIgnoreClass(node, className) : ''));
  }).flatten().join('');
};

Element.setContentZoom = function(element, percent) {
  element = $(element);  
  element.setStyle({fontSize: (percent/100) + 'em'});   
  if (Prototype.Browser.WebKit) window.scrollBy(0,0);
  return element;
};

Element.getInlineOpacity = function(element){
  return $(element).style.opacity || '';
};

Element.forceRerendering = function(element) {
  try {
    element = $(element);
    var n = document.createTextNode(' ');
    element.appendChild(n);
    element.removeChild(n);
  } catch(e) { }
};

/*--------------------------------------------------------------------------*/

var Effect = {
  _elementDoesNotExistError: {
    name: 'ElementDoesNotExistError',
    message: 'The specified DOM element does not exist, but is required for this effect to operate'
  },
  Transitions: {
    linear: Prototype.K,
    sinoidal: function(pos) {
      return (-Math.cos(pos*Math.PI)/2) + 0.5;
    },
    reverse: function(pos) {
      return 1-pos;
    },
    flicker: function(pos) {
      var pos = ((-Math.cos(pos*Math.PI)/4) + 0.75) + Math.random()/4;
      return pos > 1 ? 1 : pos;
    },
    wobble: function(pos) {
      return (-Math.cos(pos*Math.PI*(9*pos))/2) + 0.5;
    },
    pulse: function(pos, pulses) { 
      pulses = pulses || 5; 
      return (
        ((pos % (1/pulses)) * pulses).round() == 0 ? 
              ((pos * pulses * 2) - (pos * pulses * 2).floor()) : 
          1 - ((pos * pulses * 2) - (pos * pulses * 2).floor())
        );
    },
    spring: function(pos) { 
      return 1 - (Math.cos(pos * 4.5 * Math.PI) * Math.exp(-pos * 6)); 
    },
    none: function(pos) {
      return 0;
    },
    full: function(pos) {
      return 1;
    }
  },
  DefaultOptions: {
    duration:   1.0,   // seconds
    fps:        100,   // 100= assume 66fps max.
    sync:       false, // true for combining
    from:       0.0,
    to:         1.0,
    delay:      0.0,
    queue:      'parallel'
  },
  tagifyText: function(element) {
    var tagifyStyle = 'position:relative';
    if (Prototype.Browser.IE) tagifyStyle += ';zoom:1';
    
    element = $(element);
    $A(element.childNodes).each( function(child) {
      if (child.nodeType==3) {
        child.nodeValue.toArray().each( function(character) {
          element.insertBefore(
            new Element('span', {style: tagifyStyle}).update(
              character == ' ' ? String.fromCharCode(160) : character), 
              child);
        });
        Element.remove(child);
      }
    });
  },
  multiple: function(element, effect) {
    var elements;
    if (((typeof element == 'object') || 
        Object.isFunction(element)) && 
       (element.length))
      elements = element;
    else
      elements = $(element).childNodes;
      
    var options = Object.extend({
      speed: 0.1,
      delay: 0.0
    }, arguments[2] || { });
    var masterDelay = options.delay;

    $A(elements).each( function(element, index) {
      new effect(element, Object.extend(options, { delay: index * options.speed + masterDelay }));
    });
  },
  PAIRS: {
    'slide':  ['SlideDown','SlideUp'],
    'blind':  ['BlindDown','BlindUp'],
    'appear': ['Appear','Fade']
  },
  toggle: function(element, effect) {
    element = $(element);
    effect = (effect || 'appear').toLowerCase();
    var options = Object.extend({
      queue: { position:'end', scope:(element.id || 'global'), limit: 1 }
    }, arguments[2] || { });
    Effect[element.visible() ? 
      Effect.PAIRS[effect][1] : Effect.PAIRS[effect][0]](element, options);
  }
};

Effect.DefaultOptions.transition = Effect.Transitions.sinoidal;

/* ------------- core effects ------------- */

Effect.ScopedQueue = Class.create(Enumerable, {
  initialize: function() {
    this.effects  = [];
    this.interval = null;    
  },
  _each: function(iterator) {
    this.effects._each(iterator);
  },
  add: function(effect) {
    var timestamp = new Date().getTime();
    
    var position = Object.isString(effect.options.queue) ? 
      effect.options.queue : effect.options.queue.position;
    
    switch(position) {
      case 'front':
        // move unstarted effects after this effect  
        this.effects.findAll(function(e){ return e.state=='idle' }).each( function(e) {
            e.startOn  += effect.finishOn;
            e.finishOn += effect.finishOn;
          });
        break;
      case 'with-last':
        timestamp = this.effects.pluck('startOn').max() || timestamp;
        break;
      case 'end':
        // start effect after last queued effect has finished
        timestamp = this.effects.pluck('finishOn').max() || timestamp;
        break;
    }
    
    effect.startOn  += timestamp;
    effect.finishOn += timestamp;

    if (!effect.options.queue.limit || (this.effects.length < effect.options.queue.limit))
      this.effects.push(effect);
    
    if (!this.interval)
      this.interval = setInterval(this.loop.bind(this), 15);
  },
  remove: function(effect) {
    this.effects = this.effects.reject(function(e) { return e==effect });
    if (this.effects.length == 0) {
      clearInterval(this.interval);
      this.interval = null;
    }
  },
  loop: function() {
    var timePos = new Date().getTime();
    for(var i=0, len=this.effects.length;i<len;i++) 
      this.effects[i] && this.effects[i].loop(timePos);
  }
});

Effect.Queues = {
  instances: $H(),
  get: function(queueName) {
    if (!Object.isString(queueName)) return queueName;
    
    return this.instances.get(queueName) ||
      this.instances.set(queueName, new Effect.ScopedQueue());
  }
};
Effect.Queue = Effect.Queues.get('global');

Effect.Base = Class.create({
  position: null,
  start: function(options) {
    function codeForEvent(options,eventName){
      return (
        (options[eventName+'Internal'] ? 'this.options.'+eventName+'Internal(this);' : '') +
        (options[eventName] ? 'this.options.'+eventName+'(this);' : '')
      );
    }
    if (options && options.transition === false) options.transition = Effect.Transitions.linear;
    this.options      = Object.extend(Object.extend({ },Effect.DefaultOptions), options || { });
    this.currentFrame = 0;
    this.state        = 'idle';
    this.startOn      = this.options.delay*1000;
    this.finishOn     = this.startOn+(this.options.duration*1000);
    this.fromToDelta  = this.options.to-this.options.from;
    this.totalTime    = this.finishOn-this.startOn;
    this.totalFrames  = this.options.fps*this.options.duration;
    
    eval('this.render = function(pos){ '+
      'if (this.state=="idle"){this.state="running";'+
      codeForEvent(this.options,'beforeSetup')+
      (this.setup ? 'this.setup();':'')+ 
      codeForEvent(this.options,'afterSetup')+
      '};if (this.state=="running"){'+
      'pos=this.options.transition(pos)*'+this.fromToDelta+'+'+this.options.from+';'+
      'this.position=pos;'+
      codeForEvent(this.options,'beforeUpdate')+
      (this.update ? 'this.update(pos);':'')+
      codeForEvent(this.options,'afterUpdate')+
      '}}');
    
    this.event('beforeStart');
    if (!this.options.sync)
      Effect.Queues.get(Object.isString(this.options.queue) ? 
        'global' : this.options.queue.scope).add(this);
  },
  loop: function(timePos) {
    if (timePos >= this.startOn) {
      if (timePos >= this.finishOn) {
        this.render(1.0);
        this.cancel();
        this.event('beforeFinish');
        if (this.finish) this.finish(); 
        this.event('afterFinish');
        return;  
      }
      var pos   = (timePos - this.startOn) / this.totalTime,
          frame = (pos * this.totalFrames).round();
      if (frame > this.currentFrame) {
        this.render(pos);
        this.currentFrame = frame;
      }
    }
  },
  cancel: function() {
    if (!this.options.sync)
      Effect.Queues.get(Object.isString(this.options.queue) ? 
        'global' : this.options.queue.scope).remove(this);
    this.state = 'finished';
  },
  event: function(eventName) {
    if (this.options[eventName + 'Internal']) this.options[eventName + 'Internal'](this);
    if (this.options[eventName]) this.options[eventName](this);
  },
  inspect: function() {
    var data = $H();
    for(property in this)
      if (!Object.isFunction(this[property])) data.set(property, this[property]);
    return '#<Effect:' + data.inspect() + ',options:' + $H(this.options).inspect() + '>';
  }
});

Effect.Parallel = Class.create(Effect.Base, {
  initialize: function(effects) {
    this.effects = effects || [];
    this.start(arguments[1]);
  },
  update: function(position) {
    this.effects.invoke('render', position);
  },
  finish: function(position) {
    this.effects.each( function(effect) {
      effect.render(1.0);
      effect.cancel();
      effect.event('beforeFinish');
      if (effect.finish) effect.finish(position);
      effect.event('afterFinish');
    });
  }
});

Effect.Tween = Class.create(Effect.Base, {
  initialize: function(object, from, to) {
    object = Object.isString(object) ? $(object) : object;
    var args = $A(arguments), method = args.last(), 
      options = args.length == 5 ? args[3] : null;
    this.method = Object.isFunction(method) ? method.bind(object) :
      Object.isFunction(object[method]) ? object[method].bind(object) : 
      function(value) { object[method] = value };
    this.start(Object.extend({ from: from, to: to }, options || { }));
  },
  update: function(position) {
    this.method(position);
  }
});

Effect.Event = Class.create(Effect.Base, {
  initialize: function() {
    this.start(Object.extend({ duration: 0 }, arguments[0] || { }));
  },
  update: Prototype.emptyFunction
});

Effect.Opacity = Class.create(Effect.Base, {
  initialize: function(element) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    // make this work on IE on elements without 'layout'
    if (Prototype.Browser.IE && (!this.element.currentStyle.hasLayout))
      this.element.setStyle({zoom: 1});
    var options = Object.extend({
      from: this.element.getOpacity() || 0.0,
      to:   1.0
    }, arguments[1] || { });
    this.start(options);
  },
  update: function(position) {
    this.element.setOpacity(position);
  }
});

Effect.Move = Class.create(Effect.Base, {
  initialize: function(element) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'relative'
    }, arguments[1] || { });
    this.start(options);
  },
  setup: function() {
    this.element.makePositioned();
    this.originalLeft = parseFloat(this.element.getStyle('left') || '0');
    this.originalTop  = parseFloat(this.element.getStyle('top')  || '0');
    if (this.options.mode == 'absolute') {
      this.options.x = this.options.x - this.originalLeft;
      this.options.y = this.options.y - this.originalTop;
    }
  },
  update: function(position) {
    this.element.setStyle({
      left: (this.options.x  * position + this.originalLeft).round() + 'px',
      top:  (this.options.y  * position + this.originalTop).round()  + 'px'
    });
  }
});

// for backwards compatibility
Effect.MoveBy = function(element, toTop, toLeft) {
  return new Effect.Move(element, 
    Object.extend({ x: toLeft, y: toTop }, arguments[3] || { }));
};

Effect.Scale = Class.create(Effect.Base, {
  initialize: function(element, percent) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      scaleX: true,
      scaleY: true,
      scaleContent: true,
      scaleFromCenter: false,
      scaleMode: 'box',        // 'box' or 'contents' or { } with provided values
      scaleFrom: 100.0,
      scaleTo:   percent
    }, arguments[2] || { });
    this.start(options);
  },
  setup: function() {
    this.restoreAfterFinish = this.options.restoreAfterFinish || false;
    this.elementPositioning = this.element.getStyle('position');
    
    this.originalStyle = { };
    ['top','left','width','height','fontSize'].each( function(k) {
      this.originalStyle[k] = this.element.style[k];
    }.bind(this));
      
    this.originalTop  = this.element.offsetTop;
    this.originalLeft = this.element.offsetLeft;
    
    var fontSize = this.element.getStyle('font-size') || '100%';
    ['em','px','%','pt'].each( function(fontSizeType) {
      if (fontSize.indexOf(fontSizeType)>0) {
        this.fontSize     = parseFloat(fontSize);
        this.fontSizeType = fontSizeType;
      }
    }.bind(this));
    
    this.factor = (this.options.scaleTo - this.options.scaleFrom)/100;
    
    this.dims = null;
    if (this.options.scaleMode=='box')
      this.dims = [this.element.offsetHeight, this.element.offsetWidth];
    if (/^content/.test(this.options.scaleMode))
      this.dims = [this.element.scrollHeight, this.element.scrollWidth];
    if (!this.dims)
      this.dims = [this.options.scaleMode.originalHeight,
                   this.options.scaleMode.originalWidth];
  },
  update: function(position) {
    var currentScale = (this.options.scaleFrom/100.0) + (this.factor * position);
    if (this.options.scaleContent && this.fontSize)
      this.element.setStyle({fontSize: this.fontSize * currentScale + this.fontSizeType });
    this.setDimensions(this.dims[0] * currentScale, this.dims[1] * currentScale);
  },
  finish: function(position) {
    if (this.restoreAfterFinish) this.element.setStyle(this.originalStyle);
  },
  setDimensions: function(height, width) {
    var d = { };
    if (this.options.scaleX) d.width = width.round() + 'px';
    if (this.options.scaleY) d.height = height.round() + 'px';
    if (this.options.scaleFromCenter) {
      var topd  = (height - this.dims[0])/2;
      var leftd = (width  - this.dims[1])/2;
      if (this.elementPositioning == 'absolute') {
        if (this.options.scaleY) d.top = this.originalTop-topd + 'px';
        if (this.options.scaleX) d.left = this.originalLeft-leftd + 'px';
      } else {
        if (this.options.scaleY) d.top = -topd + 'px';
        if (this.options.scaleX) d.left = -leftd + 'px';
      }
    }
    this.element.setStyle(d);
  }
});

Effect.Highlight = Class.create(Effect.Base, {
  initialize: function(element) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({ startcolor: '#ffff99' }, arguments[1] || { });
    this.start(options);
  },
  setup: function() {
    // Prevent executing on elements not in the layout flow
    if (this.element.getStyle('display')=='none') { this.cancel(); return; }
    // Disable background image during the effect
    this.oldStyle = { };
    if (!this.options.keepBackgroundImage) {
      this.oldStyle.backgroundImage = this.element.getStyle('background-image');
      this.element.setStyle({backgroundImage: 'none'});
    }
    if (!this.options.endcolor)
      this.options.endcolor = this.element.getStyle('background-color').parseColor('#ffffff');
    if (!this.options.restorecolor)
      this.options.restorecolor = this.element.getStyle('background-color');
    // init color calculations
    this._base  = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this));
    this._delta = $R(0,2).map(function(i){ return parseInt(this.options.endcolor.slice(i*2+1,i*2+3),16)-this._base[i] }.bind(this));
  },
  update: function(position) {
    this.element.setStyle({backgroundColor: $R(0,2).inject('#',function(m,v,i){
      return m+((this._base[i]+(this._delta[i]*position)).round().toColorPart()); }.bind(this)) });
  },
  finish: function() {
    this.element.setStyle(Object.extend(this.oldStyle, {
      backgroundColor: this.options.restorecolor
    }));
  }
});

Effect.ScrollTo = function(element) {
  var options = arguments[1] || { },
    scrollOffsets = document.viewport.getScrollOffsets(),
    elementOffsets = $(element).cumulativeOffset(),
    max = (window.height || document.body.scrollHeight) - document.viewport.getHeight();  

  if (options.offset) elementOffsets[1] += options.offset;

  return new Effect.Tween(null,
    scrollOffsets.top,
    elementOffsets[1] > max ? max : elementOffsets[1],
    options,
    function(p){ scrollTo(scrollOffsets.left, p.round()) }
  );
};

/* ------------- combination effects ------------- */

Effect.Fade = function(element) {
  element = $(element);
  var oldOpacity = element.getInlineOpacity();
  var options = Object.extend({
    from: element.getOpacity() || 1.0,
    to:   0.0,
    afterFinishInternal: function(effect) { 
      if (effect.options.to!=0) return;
      effect.element.hide().setStyle({opacity: oldOpacity}); 
    }
  }, arguments[1] || { });
  return new Effect.Opacity(element,options);
};

Effect.Appear = function(element) {
  element = $(element);
  var options = Object.extend({
  from: (element.getStyle('display') == 'none' ? 0.0 : element.getOpacity() || 0.0),
  to:   1.0,
  // force Safari to render floated elements properly
  afterFinishInternal: function(effect) {
    effect.element.forceRerendering();
  },
  beforeSetup: function(effect) {
    effect.element.setOpacity(effect.options.from).show(); 
  }}, arguments[1] || { });
  return new Effect.Opacity(element,options);
};

Effect.Puff = function(element) {
  element = $(element);
  var oldStyle = { 
    opacity: element.getInlineOpacity(), 
    position: element.getStyle('position'),
    top:  element.style.top,
    left: element.style.left,
    width: element.style.width,
    height: element.style.height
  };
  return new Effect.Parallel(
   [ new Effect.Scale(element, 200, 
      { sync: true, scaleFromCenter: true, scaleContent: true, restoreAfterFinish: true }), 
     new Effect.Opacity(element, { sync: true, to: 0.0 } ) ], 
     Object.extend({ duration: 1.0, 
      beforeSetupInternal: function(effect) {
        Position.absolutize(effect.effects[0].element)
      },
      afterFinishInternal: function(effect) {
         effect.effects[0].element.hide().setStyle(oldStyle); }
     }, arguments[1] || { })
   );
};

Effect.BlindUp = function(element) {
  element = $(element);
  element.makeClipping();
  return new Effect.Scale(element, 0,
    Object.extend({ scaleContent: false, 
      scaleX: false, 
      restoreAfterFinish: true,
      afterFinishInternal: function(effect) {
        effect.element.hide().undoClipping();
      } 
    }, arguments[1] || { })
  );
};

Effect.BlindDown = function(element) {
  element = $(element);
  var elementDimensions = element.getDimensions();
  return new Effect.Scale(element, 100, Object.extend({ 
    scaleContent: false, 
    scaleX: false,
    scaleFrom: 0,
    scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
    restoreAfterFinish: true,
    afterSetup: function(effect) {
      effect.element.makeClipping().setStyle({height: '0px'}).show(); 
    },  
    afterFinishInternal: function(effect) {
      effect.element.undoClipping();
    }
  }, arguments[1] || { }));
};

Effect.SwitchOff = function(element) {
  element = $(element);
  var oldOpacity = element.getInlineOpacity();
  return new Effect.Appear(element, Object.extend({
    duration: 0.4,
    from: 0,
    transition: Effect.Transitions.flicker,
    afterFinishInternal: function(effect) {
      new Effect.Scale(effect.element, 1, { 
        duration: 0.3, scaleFromCenter: true,
        scaleX: false, scaleContent: false, restoreAfterFinish: true,
        beforeSetup: function(effect) { 
          effect.element.makePositioned().makeClipping();
        },
        afterFinishInternal: function(effect) {
          effect.element.hide().undoClipping().undoPositioned().setStyle({opacity: oldOpacity});
        }
      })
    }
  }, arguments[1] || { }));
};

Effect.DropOut = function(element) {
  element = $(element);
  var oldStyle = {
    top: element.getStyle('top'),
    left: element.getStyle('left'),
    opacity: element.getInlineOpacity() };
  return new Effect.Parallel(
    [ new Effect.Move(element, {x: 0, y: 100, sync: true }), 
      new Effect.Opacity(element, { sync: true, to: 0.0 }) ],
    Object.extend(
      { duration: 0.5,
        beforeSetup: function(effect) {
          effect.effects[0].element.makePositioned(); 
        },
        afterFinishInternal: function(effect) {
          effect.effects[0].element.hide().undoPositioned().setStyle(oldStyle);
        } 
      }, arguments[1] || { }));
};

Effect.Shake = function(element) {
  element = $(element);
  var options = Object.extend({
    distance: 20,
    duration: 0.5
  }, arguments[1] || {});
  var distance = parseFloat(options.distance);
  var split = parseFloat(options.duration) / 10.0;
  var oldStyle = {
    top: element.getStyle('top'),
    left: element.getStyle('left') };
    return new Effect.Move(element,
      { x:  distance, y: 0, duration: split, afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x: -distance*2, y: 0, duration: split*2,  afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x:  distance*2, y: 0, duration: split*2,  afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x: -distance*2, y: 0, duration: split*2,  afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x:  distance*2, y: 0, duration: split*2,  afterFinishInternal: function(effect) {
    new Effect.Move(effect.element,
      { x: -distance, y: 0, duration: split, afterFinishInternal: function(effect) {
        effect.element.undoPositioned().setStyle(oldStyle);
  }}) }}) }}) }}) }}) }});
};

Effect.SlideDown = function(element) {
  element = $(element).cleanWhitespace();
  // SlideDown need to have the content of the element wrapped in a container element with fixed height!
  var oldInnerBottom = element.down().getStyle('bottom');
  var elementDimensions = element.getDimensions();
  return new Effect.Scale(element, 100, Object.extend({ 
    scaleContent: false, 
    scaleX: false, 
    scaleFrom: window.opera ? 0 : 1,
    scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
    restoreAfterFinish: true,
    afterSetup: function(effect) {
      effect.element.makePositioned();
      effect.element.down().makePositioned();
      if (window.opera) effect.element.setStyle({top: ''});
      effect.element.makeClipping().setStyle({height: '0px'}).show(); 
    },
    afterUpdateInternal: function(effect) {
      effect.element.down().setStyle({bottom:
        (effect.dims[0] - effect.element.clientHeight) + 'px' }); 
    },
    afterFinishInternal: function(effect) {
      effect.element.undoClipping().undoPositioned();
      effect.element.down().undoPositioned().setStyle({bottom: oldInnerBottom}); }
    }, arguments[1] || { })
  );
};

Effect.SlideUp = function(element) {
  element = $(element).cleanWhitespace();
  var oldInnerBottom = element.down().getStyle('bottom');
  var elementDimensions = element.getDimensions();
  return new Effect.Scale(element, window.opera ? 0 : 1,
   Object.extend({ scaleContent: false, 
    scaleX: false, 
    scaleMode: 'box',
    scaleFrom: 100,
    scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
    restoreAfterFinish: true,
    afterSetup: function(effect) {
      effect.element.makePositioned();
      effect.element.down().makePositioned();
      if (window.opera) effect.element.setStyle({top: ''});
      effect.element.makeClipping().show();
    },  
    afterUpdateInternal: function(effect) {
      effect.element.down().setStyle({bottom:
        (effect.dims[0] - effect.element.clientHeight) + 'px' });
    },
    afterFinishInternal: function(effect) {
      effect.element.hide().undoClipping().undoPositioned();
      effect.element.down().undoPositioned().setStyle({bottom: oldInnerBottom});
    }
   }, arguments[1] || { })
  );
};

// Bug in opera makes the TD containing this element expand for a instance after finish 
Effect.Squish = function(element) {
  return new Effect.Scale(element, window.opera ? 1 : 0, { 
    restoreAfterFinish: true,
    beforeSetup: function(effect) {
      effect.element.makeClipping(); 
    },  
    afterFinishInternal: function(effect) {
      effect.element.hide().undoClipping(); 
    }
  });
};

Effect.Grow = function(element) {
  element = $(element);
  var options = Object.extend({
    direction: 'center',
    moveTransition: Effect.Transitions.sinoidal,
    scaleTransition: Effect.Transitions.sinoidal,
    opacityTransition: Effect.Transitions.full
  }, arguments[1] || { });
  var oldStyle = {
    top: element.style.top,
    left: element.style.left,
    height: element.style.height,
    width: element.style.width,
    opacity: element.getInlineOpacity() };

  var dims = element.getDimensions();    
  var initialMoveX, initialMoveY;
  var moveX, moveY;
  
  switch (options.direction) {
    case 'top-left':
      initialMoveX = initialMoveY = moveX = moveY = 0; 
      break;
    case 'top-right':
      initialMoveX = dims.width;
      initialMoveY = moveY = 0;
      moveX = -dims.width;
      break;
    case 'bottom-left':
      initialMoveX = moveX = 0;
      initialMoveY = dims.height;
      moveY = -dims.height;
      break;
    case 'bottom-right':
      initialMoveX = dims.width;
      initialMoveY = dims.height;
      moveX = -dims.width;
      moveY = -dims.height;
      break;
    case 'center':
      initialMoveX = dims.width / 2;
      initialMoveY = dims.height / 2;
      moveX = -dims.width / 2;
      moveY = -dims.height / 2;
      break;
  }
  
  return new Effect.Move(element, {
    x: initialMoveX,
    y: initialMoveY,
    duration: 0.01, 
    beforeSetup: function(effect) {
      effect.element.hide().makeClipping().makePositioned();
    },
    afterFinishInternal: function(effect) {
      new Effect.Parallel(
        [ new Effect.Opacity(effect.element, { sync: true, to: 1.0, from: 0.0, transition: options.opacityTransition }),
          new Effect.Move(effect.element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition }),
          new Effect.Scale(effect.element, 100, {
            scaleMode: { originalHeight: dims.height, originalWidth: dims.width }, 
            sync: true, scaleFrom: window.opera ? 1 : 0, transition: options.scaleTransition, restoreAfterFinish: true})
        ], Object.extend({
             beforeSetup: function(effect) {
               effect.effects[0].element.setStyle({height: '0px'}).show(); 
             },
             afterFinishInternal: function(effect) {
               effect.effects[0].element.undoClipping().undoPositioned().setStyle(oldStyle); 
             }
           }, options)
      )
    }
  });
};

Effect.Shrink = function(element) {
  element = $(element);
  var options = Object.extend({
    direction: 'center',
    moveTransition: Effect.Transitions.sinoidal,
    scaleTransition: Effect.Transitions.sinoidal,
    opacityTransition: Effect.Transitions.none
  }, arguments[1] || { });
  var oldStyle = {
    top: element.style.top,
    left: element.style.left,
    height: element.style.height,
    width: element.style.width,
    opacity: element.getInlineOpacity() };

  var dims = element.getDimensions();
  var moveX, moveY;
  
  switch (options.direction) {
    case 'top-left':
      moveX = moveY = 0;
      break;
    case 'top-right':
      moveX = dims.width;
      moveY = 0;
      break;
    case 'bottom-left':
      moveX = 0;
      moveY = dims.height;
      break;
    case 'bottom-right':
      moveX = dims.width;
      moveY = dims.height;
      break;
    case 'center':  
      moveX = dims.width / 2;
      moveY = dims.height / 2;
      break;
  }
  
  return new Effect.Parallel(
    [ new Effect.Opacity(element, { sync: true, to: 0.0, from: 1.0, transition: options.opacityTransition }),
      new Effect.Scale(element, window.opera ? 1 : 0, { sync: true, transition: options.scaleTransition, restoreAfterFinish: true}),
      new Effect.Move(element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition })
    ], Object.extend({            
         beforeStartInternal: function(effect) {
           effect.effects[0].element.makePositioned().makeClipping(); 
         },
         afterFinishInternal: function(effect) {
           effect.effects[0].element.hide().undoClipping().undoPositioned().setStyle(oldStyle); }
       }, options)
  );
};

Effect.Pulsate = function(element) {
  element = $(element);
  var options    = arguments[1] || { };
  var oldOpacity = element.getInlineOpacity();
  var transition = options.transition || Effect.Transitions.sinoidal;
  var reverser   = function(pos){ return transition(1-Effect.Transitions.pulse(pos, options.pulses)) };
  reverser.bind(transition);
  return new Effect.Opacity(element, 
    Object.extend(Object.extend({  duration: 2.0, from: 0,
      afterFinishInternal: function(effect) { effect.element.setStyle({opacity: oldOpacity}); }
    }, options), {transition: reverser}));
};

Effect.Fold = function(element) {
  element = $(element);
  var oldStyle = {
    top: element.style.top,
    left: element.style.left,
    width: element.style.width,
    height: element.style.height };
  element.makeClipping();
  return new Effect.Scale(element, 5, Object.extend({   
    scaleContent: false,
    scaleX: false,
    afterFinishInternal: function(effect) {
    new Effect.Scale(element, 1, { 
      scaleContent: false, 
      scaleY: false,
      afterFinishInternal: function(effect) {
        effect.element.hide().undoClipping().setStyle(oldStyle);
      } });
  }}, arguments[1] || { }));
};

Effect.Morph = Class.create(Effect.Base, {
  initialize: function(element) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      style: { }
    }, arguments[1] || { });
    
    if (!Object.isString(options.style)) this.style = $H(options.style);
    else {
      if (options.style.include(':'))
        this.style = options.style.parseStyle();
      else {
        this.element.addClassName(options.style);
        this.style = $H(this.element.getStyles());
        this.element.removeClassName(options.style);
        var css = this.element.getStyles();
        this.style = this.style.reject(function(style) {
          return style.value == css[style.key];
        });
        options.afterFinishInternal = function(effect) {
          effect.element.addClassName(effect.options.style);
          effect.transforms.each(function(transform) {
            effect.element.style[transform.style] = '';
          });
        }
      }
    }
    this.start(options);
  },
  
  setup: function(){
    function parseColor(color){
      if (!color || ['rgba(0, 0, 0, 0)','transparent'].include(color)) color = '#ffffff';
      color = color.parseColor();
      return $R(0,2).map(function(i){
        return parseInt( color.slice(i*2+1,i*2+3), 16 ) 
      });
    }
    this.transforms = this.style.map(function(pair){
      var property = pair[0], value = pair[1], unit = null;

      if (value.parseColor('#zzzzzz') != '#zzzzzz') {
        value = value.parseColor();
        unit  = 'color';
      } else if (property == 'opacity') {
        value = parseFloat(value);
        if (Prototype.Browser.IE && (!this.element.currentStyle.hasLayout))
          this.element.setStyle({zoom: 1});
      } else if (Element.CSS_LENGTH.test(value)) {
          var components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/);
          value = parseFloat(components[1]);
          unit = (components.length == 3) ? components[2] : null;
      }

      var originalValue = this.element.getStyle(property);
      return { 
        style: property.camelize(), 
        originalValue: unit=='color' ? parseColor(originalValue) : parseFloat(originalValue || 0), 
        targetValue: unit=='color' ? parseColor(value) : value,
        unit: unit
      };
    }.bind(this)).reject(function(transform){
      return (
        (transform.originalValue == transform.targetValue) ||
        (
          transform.unit != 'color' &&
          (isNaN(transform.originalValue) || isNaN(transform.targetValue))
        )
      )
    });
  },
  update: function(position) {
    var style = { }, transform, i = this.transforms.length;
    while(i--)
      style[(transform = this.transforms[i]).style] = 
        transform.unit=='color' ? '#'+
          (Math.round(transform.originalValue[0]+
            (transform.targetValue[0]-transform.originalValue[0])*position)).toColorPart() +
          (Math.round(transform.originalValue[1]+
            (transform.targetValue[1]-transform.originalValue[1])*position)).toColorPart() +
          (Math.round(transform.originalValue[2]+
            (transform.targetValue[2]-transform.originalValue[2])*position)).toColorPart() :
        (transform.originalValue +
          (transform.targetValue - transform.originalValue) * position).toFixed(3) + 
            (transform.unit === null ? '' : transform.unit);
    this.element.setStyle(style, true);
  }
});

Effect.Transform = Class.create({
  initialize: function(tracks){
    this.tracks  = [];
    this.options = arguments[1] || { };
    this.addTracks(tracks);
  },
  addTracks: function(tracks){
    tracks.each(function(track){
      track = $H(track);
      var data = track.values().first();
      this.tracks.push($H({
        ids:     track.keys().first(),
        effect:  Effect.Morph,
        options: { style: data }
      }));
    }.bind(this));
    return this;
  },
  play: function(){
    return new Effect.Parallel(
      this.tracks.map(function(track){
        var ids = track.get('ids'), effect = track.get('effect'), options = track.get('options');
        var elements = [$(ids) || $$(ids)].flatten();
        return elements.map(function(e){ return new effect(e, Object.extend({ sync:true }, options)) });
      }).flatten(),
      this.options
    );
  }
});

Element.CSS_PROPERTIES = $w(
  'backgroundColor backgroundPosition borderBottomColor borderBottomStyle ' + 
  'borderBottomWidth borderLeftColor borderLeftStyle borderLeftWidth ' +
  'borderRightColor borderRightStyle borderRightWidth borderSpacing ' +
  'borderTopColor borderTopStyle borderTopWidth bottom clip color ' +
  'fontSize fontWeight height left letterSpacing lineHeight ' +
  'marginBottom marginLeft marginRight marginTop markerOffset maxHeight '+
  'maxWidth minHeight minWidth opacity outlineColor outlineOffset ' +
  'outlineWidth paddingBottom paddingLeft paddingRight paddingTop ' +
  'right textIndent top width wordSpacing zIndex');
  
Element.CSS_LENGTH = /^(([\+\-]?[0-9\.]+)(em|ex|px|in|cm|mm|pt|pc|\%))|0$/;

String.__parseStyleElement = document.createElement('div');
String.prototype.parseStyle = function(){
  var style, styleRules = $H();
  if (Prototype.Browser.WebKit)
    style = new Element('div',{style:this}).style;
  else {
    String.__parseStyleElement.innerHTML = '<div style="' + this + '"></div>';
    style = String.__parseStyleElement.childNodes[0].style;
  }
  
  Element.CSS_PROPERTIES.each(function(property){
    if (style[property]) styleRules.set(property, style[property]); 
  });
  
  if (Prototype.Browser.IE && this.include('opacity'))
    styleRules.set('opacity', this.match(/opacity:\s*((?:0|1)?(?:\.\d*)?)/)[1]);

  return styleRules;
};

if (document.defaultView && document.defaultView.getComputedStyle) {
  Element.getStyles = function(element) {
    var css = document.defaultView.getComputedStyle($(element), null);
    return Element.CSS_PROPERTIES.inject({ }, function(styles, property) {
      styles[property] = css[property];
      return styles;
    });
  };
} else {
  Element.getStyles = function(element) {
    element = $(element);
    var css = element.currentStyle, styles;
    styles = Element.CSS_PROPERTIES.inject({ }, function(results, property) {
      results[property] = css[property];
      return results;
    });
    if (!styles.opacity) styles.opacity = element.getOpacity();
    return styles;
  };
};

Effect.Methods = {
  morph: function(element, style) {
    element = $(element);
    new Effect.Morph(element, Object.extend({ style: style }, arguments[2] || { }));
    return element;
  },
  visualEffect: function(element, effect, options) {
    element = $(element)
    var s = effect.dasherize().camelize(), klass = s.charAt(0).toUpperCase() + s.substring(1);
    new Effect[klass](element, options);
    return element;
  },
  highlight: function(element, options) {
    element = $(element);
    new Effect.Highlight(element, options);
    return element;
  }
};

$w('fade appear grow shrink fold blindUp blindDown slideUp slideDown '+
  'pulsate shake puff squish switchOff dropOut').each(
  function(effect) { 
    Effect.Methods[effect] = function(element, options){
      element = $(element);
      Effect[effect.charAt(0).toUpperCase() + effect.substring(1)](element, options);
      return element;
    }
  }
);

$w('getInlineOpacity forceRerendering setContentZoom collectTextNodes collectTextNodesIgnoreClass getStyles').each( 
  function(f) { Effect.Methods[f] = Element[f]; }
);

Element.addMethods(Effect.Methods);


// script.aculo.us controls.js v1.8.1, Thu Jan 03 22:07:12 -0500 2008

// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
//           (c) 2005-2007 Ivan Krstic (http://blogs.law.harvard.edu/ivan)
//           (c) 2005-2007 Jon Tirsen (http://www.tirsen.com)
// Contributors:
//  Richard Livsey
//  Rahul Bhargava
//  Rob Wills
// 
// script.aculo.us is freely distributable under the terms of an MIT-style license.
// For details, see the script.aculo.us web site: http://script.aculo.us/

// Autocompleter.Base handles all the autocompletion functionality 
// that's independent of the data source for autocompletion. This
// includes drawing the autocompletion menu, observing keyboard
// and mouse events, and similar.
//
// Specific autocompleters need to provide, at the very least, 
// a getUpdatedChoices function that will be invoked every time
// the text inside the monitored textbox changes. This method 
// should get the text for which to provide autocompletion by
// invoking this.getToken(), NOT by directly accessing
// this.element.value. This is to allow incremental tokenized
// autocompletion. Specific auto-completion logic (AJAX, etc)
// belongs in getUpdatedChoices.
//
// Tokenized incremental autocompletion is enabled automatically
// when an autocompleter is instantiated with the 'tokens' option
// in the options parameter, e.g.:
// new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' });
// will incrementally autocomplete with a comma as the token.
// Additionally, ',' in the above example can be replaced with
// a token array, e.g. { tokens: [',', '\n'] } which
// enables autocompletion on multiple tokens. This is most 
// useful when one of the tokens is \n (a newline), as it 
// allows smart autocompletion after linebreaks.

if(typeof Effect == 'undefined')
  throw("controls.js requires including script.aculo.us' effects.js library");

var Autocompleter = { }
Autocompleter.Base = Class.create({
  baseInitialize: function(element, update, options) {
    element          = $(element)
    this.element     = element; 
    this.update      = $(update);  
    this.hasFocus    = false; 
    this.changed     = false; 
    this.active      = false; 
    this.index       = 0;     
    this.entryCount  = 0;
    this.oldElementValue = this.element.value;

    if(this.setOptions)
      this.setOptions(options);
    else
      this.options = options || { };

    this.options.paramName    = this.options.paramName || this.element.name;
    this.options.tokens       = this.options.tokens || [];
    this.options.frequency    = this.options.frequency || 0.4;
    this.options.minChars     = this.options.minChars || 1;
    this.options.onShow       = this.options.onShow ||
      function(element, update){ 
        if(!update.style.position || update.style.position=='absolute') {
          update.style.position = 'absolute';
          // Position.clone(element, update, {
          //   setHeight: false, 
          //   offsetTop: element.offsetHeight
          // });
          Element.clonePosition(update, element, {
            setHeight: false, 
            offsetTop: element.offsetHeight
          });
        }
        Effect.Appear(update,{duration:0.15});
      };
    this.options.onHide = this.options.onHide || 
      function(element, update){ new Effect.Fade(update,{duration:0.15}) };

    if(typeof(this.options.tokens) == 'string') 
      this.options.tokens = new Array(this.options.tokens);
    // Force carriage returns as token delimiters anyway
    if (!this.options.tokens.include('\n'))
      this.options.tokens.push('\n');

    this.observer = null;
    
    this.element.setAttribute('autocomplete','off');

    Element.hide(this.update);

    Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this));
    Event.observe(this.element, 'keydown', this.onKeyPress.bindAsEventListener(this));
  },

  show: function() {
    if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update);
    if(!this.iefix && 
      (Prototype.Browser.IE) &&
      (Element.getStyle(this.update, 'position')=='absolute')) {
      new Insertion.After(this.update, 
       '<iframe id="' + this.update.id + '_iefix" '+
       'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
       'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
      this.iefix = $(this.update.id+'_iefix');
    }
    if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
  },
  
  fixIEOverlapping: function() {
    Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)});
    this.iefix.style.zIndex = 1;
    this.update.style.zIndex = 2;
    Element.show(this.iefix);
  },

  hide: function() {
    this.stopIndicator();
    if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.element, this.update);
    if(this.iefix) Element.hide(this.iefix);
  },

  startIndicator: function() {
    if(this.options.indicator) Element.show(this.options.indicator);
  },

  stopIndicator: function() {
    if(this.options.indicator) Element.hide(this.options.indicator);
  },

  onKeyPress: function(event) {
    if(this.active)
      switch(event.keyCode) {
       case Event.KEY_TAB:
       case Event.KEY_RETURN:
         this.selectEntry();
         Event.stop(event);
       case Event.KEY_ESC:
         this.hide();
         this.active = false;
         Event.stop(event);
         return;
       case Event.KEY_LEFT:
       case Event.KEY_RIGHT:
         return;
       case Event.KEY_UP:
         this.markPrevious();
         this.render();
         Event.stop(event);
         return;
       case Event.KEY_DOWN:
         this.markNext();
         this.render();
         Event.stop(event);
         return;
      }
     else 
       if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN || 
         (Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return;

    this.changed = true;
    this.hasFocus = true;

    if(this.observer) clearTimeout(this.observer);
      this.observer = 
        setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
  },

  activate: function() {
    this.changed = false;
    this.hasFocus = true;
    this.getUpdatedChoices();
  },

  onHover: function(event) {
    var element = Event.findElement(event, 'LI');
    if(this.index != element.autocompleteIndex) 
    {
        this.index = element.autocompleteIndex;
        this.render();
    }
    Event.stop(event);
  },
  
  onClick: function(event) {
    var element = Event.findElement(event, 'LI');
    this.index = element.autocompleteIndex;
    this.selectEntry();
    this.hide();
  },
  
  onBlur: function(event) {
    // needed to make click events working
    setTimeout(this.hide.bind(this), 250);
    this.hasFocus = false;
    this.active = false;     
  }, 
  
  render: function() {
    if(this.entryCount > 0) {
      for (var i = 0; i < this.entryCount; i++)
        this.index==i ? 
          Element.addClassName(this.getEntry(i),"selected") : 
          Element.removeClassName(this.getEntry(i),"selected");
      if(this.hasFocus) { 
        this.show();
        this.active = true;
      }
    } else {
      this.active = false;
      this.hide();
    }
  },
  
  markPrevious: function() {
    if(this.index > 0) this.index--
      else this.index = this.entryCount-1;
    this.getEntry(this.index).scrollIntoView(true);
  },
  
  markNext: function() {
    if(this.index < this.entryCount-1) this.index++
      else this.index = 0;
    this.getEntry(this.index).scrollIntoView(false);
  },
  
  getEntry: function(index) {
    return this.update.firstChild.childNodes[index];
  },
  
  getCurrentEntry: function() {
    return this.getEntry(this.index);
  },
  
  selectEntry: function() {
    this.active = false;
    this.updateElement(this.getCurrentEntry());
  },

  updateElement: function(selectedElement) {
    if (this.options.updateElement) {
      this.options.updateElement(selectedElement);
      return;
    }
    var value = '';
    if (this.options.select) {
      var nodes = $(selectedElement).select('.' + this.options.select) || [];
      if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select);
    } else
      value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal');
    
    var bounds = this.getTokenBounds();
    if (bounds[0] != -1) {
      var newValue = this.element.value.substr(0, bounds[0]);
      var whitespace = this.element.value.substr(bounds[0]).match(/^\s+/);
      if (whitespace)
        newValue += whitespace[0];
      	this.element.value = newValue + value + this.element.value.substr(bounds[1]);
    } else {
      this.element.value = value;
    }
    this.oldElementValue = this.element.value;
    this.element.focus();
    
    if (this.options.afterUpdateElement)
      this.options.afterUpdateElement(this.element, selectedElement);
  },

  updateChoices: function(choices) {
    if(!this.changed && this.hasFocus) {
      this.update.innerHTML = choices;
      Element.cleanWhitespace(this.update);
      Element.cleanWhitespace(this.update.down());

      if(this.update.firstChild && this.update.down().childNodes) {
        this.entryCount = 
          this.update.down().childNodes.length;
        for (var i = 0; i < this.entryCount; i++) {
          var entry = this.getEntry(i);
          entry.autocompleteIndex = i;
          this.addObservers(entry);
        }
      } else { 
        this.entryCount = 0;
      }

      this.stopIndicator();
      this.index = 0;
      
      if(this.entryCount==1 && this.options.autoSelect) {
        this.selectEntry();
        this.hide();
      } else {
        this.render();
      }
    }
  },

  addObservers: function(element) {
    Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));
    Event.observe(element, "click", this.onClick.bindAsEventListener(this));
  },

  onObserverEvent: function() {
    this.changed = false;   
    this.tokenBounds = null;
    if(this.getToken().length>=this.options.minChars) {
      this.getUpdatedChoices();
    } else {
      this.active = false;
      this.hide();
    }
    this.oldElementValue = this.element.value;
  },

  getToken: function() {
    var bounds = this.getTokenBounds();
    return this.element.value.substring(bounds[0], bounds[1]).strip();
  },

  getTokenBounds: function() {
    if (null != this.tokenBounds) return this.tokenBounds;
    var value = this.element.value;
    if (value.strip().empty()) return [-1, 0];
    var diff = arguments.callee.getFirstDifferencePos(value, this.oldElementValue);
    var offset = (diff == this.oldElementValue.length ? 1 : 0);
    var prevTokenPos = -1, nextTokenPos = value.length;
    var tp;
    for (var index = 0, l = this.options.tokens.length; index < l; ++index) {
      tp = value.lastIndexOf(this.options.tokens[index], diff + offset - 1);
      if (tp > prevTokenPos) prevTokenPos = tp;
      tp = value.indexOf(this.options.tokens[index], diff + offset);
      if (-1 != tp && tp < nextTokenPos) nextTokenPos = tp;
    }
    return (this.tokenBounds = [prevTokenPos + 1, nextTokenPos]);
  }
});

Autocompleter.Base.prototype.getTokenBounds.getFirstDifferencePos = function(newS, oldS) {
  var boundary = Math.min(newS.length, oldS.length);
  for (var index = 0; index < boundary; ++index)
    if (newS[index] != oldS[index])
      return index;
  return boundary;
};

Ajax.Autocompleter = Class.create(Autocompleter.Base, {
  initialize: function(element, update, url, options) {
    this.baseInitialize(element, update, options);
    this.options.asynchronous  = true;
    this.options.onComplete    = this.onComplete.bind(this);
    this.options.defaultParams = this.options.parameters || null;
    this.url                   = url;
  },

  getUpdatedChoices: function() {
    this.startIndicator();
    
    var entry = encodeURIComponent(this.options.paramName) + '=' + 
      encodeURIComponent(this.getToken());

    this.options.parameters = this.options.callback ?
      this.options.callback(this.element, entry) : entry;

    if(this.options.defaultParams) 
      this.options.parameters += '&' + this.options.defaultParams;
    
    new Ajax.Request(this.url, this.options);
  },

  onComplete: function(request) {
    this.updateChoices(request.responseText);
  }
});

// The local array autocompleter. Used when you'd prefer to
// inject an array of autocompletion options into the page, rather
// than sending out Ajax queries, which can be quite slow sometimes.
//
// The constructor takes four parameters. The first two are, as usual,
// the id of the monitored textbox, and id of the autocompletion menu.
// The third is the array you want to autocomplete from, and the fourth
// is the options block.
//
// Extra local autocompletion options:
// - choices - How many autocompletion choices to offer
//
// - partialSearch - If false, the autocompleter will match entered
//                    text only at the beginning of strings in the 
//                    autocomplete array. Defaults to true, which will
//                    match text at the beginning of any *word* in the
//                    strings in the autocomplete array. If you want to
//                    search anywhere in the string, additionally set
//                    the option fullSearch to true (default: off).
//
// - fullSsearch - Search anywhere in autocomplete array strings.
//
// - partialChars - How many characters to enter before triggering
//                   a partial match (unlike minChars, which defines
//                   how many characters are required to do any match
//                   at all). Defaults to 2.
//
// - ignoreCase - Whether to ignore case when autocompleting.
//                 Defaults to true.
//
// It's possible to pass in a custom function as the 'selector' 
// option, if you prefer to write your own autocompletion logic.
// In that case, the other options above will not apply unless
// you support them.

Autocompleter.Local = Class.create(Autocompleter.Base, {
  initialize: function(element, update, array, options) {
    this.baseInitialize(element, update, options);
    this.options.array = array;
  },

  getUpdatedChoices: function() {
    this.updateChoices(this.options.selector(this));
  },

  setOptions: function(options) {
    this.options = Object.extend({
      choices: 10,
      partialSearch: true,
      partialChars: 2,
      ignoreCase: true,
      fullSearch: false,
      selector: function(instance) {
        var ret       = []; // Beginning matches
        var partial   = []; // Inside matches
        var entry     = instance.getToken();
        var count     = 0;

        for (var i = 0; i < instance.options.array.length &&  
          ret.length < instance.options.choices ; i++) { 

          var elem = instance.options.array[i];
          var foundPos = instance.options.ignoreCase ? 
            elem.toLowerCase().indexOf(entry.toLowerCase()) : 
            elem.indexOf(entry);

          while (foundPos != -1) {
            if (foundPos == 0 && elem.length != entry.length) { 
              ret.push("<li><strong>" + elem.substr(0, entry.length) + "</strong>" + 
                elem.substr(entry.length) + "</li>");
              break;
            } else if (entry.length >= instance.options.partialChars && 
              instance.options.partialSearch && foundPos != -1) {
              if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
                partial.push("<li>" + elem.substr(0, foundPos) + "<strong>" +
                  elem.substr(foundPos, entry.length) + "</strong>" + elem.substr(
                  foundPos + entry.length) + "</li>");
                break;
              }
            }

            foundPos = instance.options.ignoreCase ? 
              elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : 
              elem.indexOf(entry, foundPos + 1);

          }
        }
        if (partial.length)
          ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
        return "<ul>" + ret.join('') + "</ul>";
      }
    }, options || { });
  }
});

// AJAX in-place editor and collection editor
// Full rewrite by Christophe Porteneuve <tdd@tddsworld.com> (April 2007).

// Use this if you notice weird scrolling problems on some browsers,
// the DOM might be a bit confused when this gets called so do this
// waits 1 ms (with setTimeout) until it does the activation
Field.scrollFreeActivate = function(field) {
  setTimeout(function() {
    Field.activate(field);
  }, 1);
}

Ajax.InPlaceEditor = Class.create({
  initialize: function(element, url, options) {
    this.url = url;
    this.element = element = $(element);
    this.prepareOptions();
    this._controls = { };
    arguments.callee.dealWithDeprecatedOptions(options); // DEPRECATION LAYER!!!
    Object.extend(this.options, options || { });
    if (!this.options.formId && this.element.id) {
      this.options.formId = this.element.id + '-inplaceeditor';
      if ($(this.options.formId))
        this.options.formId = '';
    }
    if (this.options.externalControl)
      this.options.externalControl = $(this.options.externalControl);
    if (!this.options.externalControl)
      this.options.externalControlOnly = false;
    this._originalBackground = this.element.getStyle('background-color') || 'transparent';
    this.element.title = this.options.clickToEditText;
    this._boundCancelHandler = this.handleFormCancellation.bind(this);
    this._boundComplete = (this.options.onComplete || Prototype.emptyFunction).bind(this);
    this._boundFailureHandler = this.handleAJAXFailure.bind(this);
    this._boundSubmitHandler = this.handleFormSubmission.bind(this);
    this._boundWrapperHandler = this.wrapUp.bind(this);
    this.registerListeners();
  },
  checkForEscapeOrReturn: function(e) {
    if (!this._editing || e.ctrlKey || e.altKey || e.shiftKey) return;
    if (Event.KEY_ESC == e.keyCode)
      this.handleFormCancellation(e);
    else if (Event.KEY_RETURN == e.keyCode)
      this.handleFormSubmission(e);
  },
  createControl: function(mode, handler, extraClasses) {
    var control = this.options[mode + 'Control'];
    var text = this.options[mode + 'Text'];
    if ('button' == control) {
      var btn = document.createElement('input');
      btn.type = 'submit';
      btn.value = text;
      btn.className = 'editor_' + mode + '_button';
      if ('cancel' == mode)
        btn.onclick = this._boundCancelHandler;
      this._form.appendChild(btn);
      this._controls[mode] = btn;
    } else if ('link' == control) {
      var link = document.createElement('a');
      link.href = '#';
      link.appendChild(document.createTextNode(text));
      link.onclick = 'cancel' == mode ? this._boundCancelHandler : this._boundSubmitHandler;
      link.className = 'editor_' + mode + '_link';
      if (extraClasses)
        link.className += ' ' + extraClasses;
      this._form.appendChild(link);
      this._controls[mode] = link;
    }
  },
  createEditField: function() {
    var text = (this.options.loadTextURL ? this.options.loadingText : this.getText());
    var fld;
    if (1 >= this.options.rows && !/\r|\n/.test(this.getText())) {
      fld = document.createElement('input');
      fld.type = 'text';
      var size = this.options.size || this.options.cols || 0;
      if (0 < size) fld.size = size;
    } else {
      fld = document.createElement('textarea');
      fld.rows = (1 >= this.options.rows ? this.options.autoRows : this.options.rows);
      fld.cols = this.options.cols || 40;
    }
    fld.name = this.options.paramName;
    fld.value = text; // No HTML breaks conversion anymore
    fld.className = 'editor_field';
    if (this.options.submitOnBlur)
      fld.onblur = this._boundSubmitHandler;
    this._controls.editor = fld;
    if (this.options.loadTextURL)
      this.loadExternalText();
    this._form.appendChild(this._controls.editor);
  },
  createForm: function() {
    var ipe = this;
    function addText(mode, condition) {
      var text = ipe.options['text' + mode + 'Controls'];
      if (!text || condition === false) return;
      ipe._form.appendChild(document.createTextNode(text));
    };
    this._form = $(document.createElement('form'));
    this._form.id = this.options.formId;
    this._form.addClassName(this.options.formClassName);
    this._form.onsubmit = this._boundSubmitHandler;
    this.createEditField();
    if ('textarea' == this._controls.editor.tagName.toLowerCase())
      this._form.appendChild(document.createElement('br'));
    if (this.options.onFormCustomization)
      this.options.onFormCustomization(this, this._form);
    addText('Before', this.options.okControl || this.options.cancelControl);
    this.createControl('ok', this._boundSubmitHandler);
    addText('Between', this.options.okControl && this.options.cancelControl);
    this.createControl('cancel', this._boundCancelHandler, 'editor_cancel');
    addText('After', this.options.okControl || this.options.cancelControl);
  },
  destroy: function() {
    if (this._oldInnerHTML)
      this.element.innerHTML = this._oldInnerHTML;
    this.leaveEditMode();
    this.unregisterListeners();
  },
  enterEditMode: function(e) {
    if (this._saving || this._editing) return;
    this._editing = true;
    this.triggerCallback('onEnterEditMode');
    if (this.options.externalControl)
      this.options.externalControl.hide();
    this.element.hide();
    this.createForm();
    this.element.parentNode.insertBefore(this._form, this.element);
    if (!this.options.loadTextURL)
      this.postProcessEditField();
    if (e) Event.stop(e);
  },
  enterHover: function(e) {
    if (this.options.hoverClassName)
      this.element.addClassName(this.options.hoverClassName);
    if (this._saving) return;
    this.triggerCallback('onEnterHover');
  },
  getText: function() {
    return this.element.innerHTML;
  },
  handleAJAXFailure: function(transport) {
    this.triggerCallback('onFailure', transport);
    if (this._oldInnerHTML) {
      this.element.innerHTML = this._oldInnerHTML;
      this._oldInnerHTML = null;
    }
  },
  handleFormCancellation: function(e) {
    this.wrapUp();
    if (e) Event.stop(e);
  },
  handleFormSubmission: function(e) {
    var form = this._form;
    var value = $F(this._controls.editor);
    this.prepareSubmission();
    var params = this.options.callback(form, value) || '';
    if (Object.isString(params))
      params = params.toQueryParams();
    params.editorId = this.element.id;
    if (this.options.htmlResponse) {
      var options = Object.extend({ evalScripts: true }, this.options.ajaxOptions);
      Object.extend(options, {
        parameters: params,
        onComplete: this._boundWrapperHandler,
        onFailure: this._boundFailureHandler
      });
      new Ajax.Updater({ success: this.element }, this.url, options);
    } else {
      var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
      Object.extend(options, {
        parameters: params,
        onComplete: this._boundWrapperHandler,
        onFailure: this._boundFailureHandler
      });
      new Ajax.Request(this.url, options);
    }
    if (e) Event.stop(e);
  },
  leaveEditMode: function() {
    this.element.removeClassName(this.options.savingClassName);
    this.removeForm();
    this.leaveHover();
    this.element.style.backgroundColor = this._originalBackground;
    this.element.show();
    if (this.options.externalControl)
      this.options.externalControl.show();
    this._saving = false;
    this._editing = false;
    this._oldInnerHTML = null;
    this.triggerCallback('onLeaveEditMode');
  },
  leaveHover: function(e) {
    if (this.options.hoverClassName)
      this.element.removeClassName(this.options.hoverClassName);
    if (this._saving) return;
    this.triggerCallback('onLeaveHover');
  },
  loadExternalText: function() {
    this._form.addClassName(this.options.loadingClassName);
    this._controls.editor.disabled = true;
    var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
    Object.extend(options, {
      parameters: 'editorId=' + encodeURIComponent(this.element.id),
      onComplete: Prototype.emptyFunction,
      onSuccess: function(transport) {
        this._form.removeClassName(this.options.loadingClassName);
        var text = transport.responseText;
        if (this.options.stripLoadedTextTags)
          text = text.stripTags();
        this._controls.editor.value = text;
        this._controls.editor.disabled = false;
        this.postProcessEditField();
      }.bind(this),
      onFailure: this._boundFailureHandler
    });
    new Ajax.Request(this.options.loadTextURL, options);
  },
  postProcessEditField: function() {
    var fpc = this.options.fieldPostCreation;
    if (fpc)
      $(this._controls.editor)['focus' == fpc ? 'focus' : 'activate']();
  },
  prepareOptions: function() {
    this.options = Object.clone(Ajax.InPlaceEditor.DefaultOptions);
    Object.extend(this.options, Ajax.InPlaceEditor.DefaultCallbacks);
    [this._extraDefaultOptions].flatten().compact().each(function(defs) {
      Object.extend(this.options, defs);
    }.bind(this));
  },
  prepareSubmission: function() {
    this._saving = true;
    this.removeForm();
    this.leaveHover();
    this.showSaving();
  },
  registerListeners: function() {
    this._listeners = { };
    var listener;
    $H(Ajax.InPlaceEditor.Listeners).each(function(pair) {
      listener = this[pair.value].bind(this);
      this._listeners[pair.key] = listener;
      if (!this.options.externalControlOnly)
        this.element.observe(pair.key, listener);
      if (this.options.externalControl)
        this.options.externalControl.observe(pair.key, listener);
    }.bind(this));
  },
  removeForm: function() {
    if (!this._form) return;
    this._form.remove();
    this._form = null;
    this._controls = { };
  },
  showSaving: function() {
    this._oldInnerHTML = this.element.innerHTML;
    this.element.innerHTML = this.options.savingText;
    this.element.addClassName(this.options.savingClassName);
    this.element.style.backgroundColor = this._originalBackground;
    this.element.show();
  },
  triggerCallback: function(cbName, arg) {
    if ('function' == typeof this.options[cbName]) {
      this.options[cbName](this, arg);
    }
  },
  unregisterListeners: function() {
    $H(this._listeners).each(function(pair) {
      if (!this.options.externalControlOnly)
        this.element.stopObserving(pair.key, pair.value);
      if (this.options.externalControl)
        this.options.externalControl.stopObserving(pair.key, pair.value);
    }.bind(this));
  },
  wrapUp: function(transport) {
    this.leaveEditMode();
    // Can't use triggerCallback due to backward compatibility: requires
    // binding + direct element
    this._boundComplete(transport, this.element);
  }
});

Object.extend(Ajax.InPlaceEditor.prototype, {
  dispose: Ajax.InPlaceEditor.prototype.destroy
});

Ajax.InPlaceCollectionEditor = Class.create(Ajax.InPlaceEditor, {
  initialize: function($super, element, url, options) {
    this._extraDefaultOptions = Ajax.InPlaceCollectionEditor.DefaultOptions;
    $super(element, url, options);
  },

  createEditField: function() {
    var list = document.createElement('select');
    list.name = this.options.paramName;
    list.size = 1;
    this._controls.editor = list;
    this._collection = this.options.collection || [];
    if (this.options.loadCollectionURL)
      this.loadCollection();
    else
      this.checkForExternalText();
    this._form.appendChild(this._controls.editor);
  },

  loadCollection: function() {
    this._form.addClassName(this.options.loadingClassName);
    this.showLoadingText(this.options.loadingCollectionText);
    var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
    Object.extend(options, {
      parameters: 'editorId=' + encodeURIComponent(this.element.id),
      onComplete: Prototype.emptyFunction,
      onSuccess: function(transport) {
        var js = transport.responseText.strip();
        if (!/^\[.*\]$/.test(js)) // TODO: improve sanity check
          throw 'Server returned an invalid collection representation.';
        this._collection = eval(js);
        this.checkForExternalText();
      }.bind(this),
      onFailure: this.onFailure
    });
    new Ajax.Request(this.options.loadCollectionURL, options);
  },

  showLoadingText: function(text) {
    this._controls.editor.disabled = true;
    var tempOption = this._controls.editor.firstChild;
    if (!tempOption) {
      tempOption = document.createElement('option');
      tempOption.value = '';
      this._controls.editor.appendChild(tempOption);
      tempOption.selected = true;
    }
    tempOption.update((text || '').stripScripts().stripTags());
  },

  checkForExternalText: function() {
    this._text = this.getText();
    if (this.options.loadTextURL)
      this.loadExternalText();
    else
      this.buildOptionList();
  },

  loadExternalText: function() {
    this.showLoadingText(this.options.loadingText);
    var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
    Object.extend(options, {
      parameters: 'editorId=' + encodeURIComponent(this.element.id),
      onComplete: Prototype.emptyFunction,
      onSuccess: function(transport) {
        this._text = transport.responseText.strip();
        this.buildOptionList();
      }.bind(this),
      onFailure: this.onFailure
    });
    new Ajax.Request(this.options.loadTextURL, options);
  },

  buildOptionList: function() {
    this._form.removeClassName(this.options.loadingClassName);
    this._collection = this._collection.map(function(entry) {
      return 2 === entry.length ? entry : [entry, entry].flatten();
    });
    var marker = ('value' in this.options) ? this.options.value : this._text;
    var textFound = this._collection.any(function(entry) {
      return entry[0] == marker;
    }.bind(this));
    this._controls.editor.update('');
    var option;
    this._collection.each(function(entry, index) {
      option = document.createElement('option');
      option.value = entry[0];
      option.selected = textFound ? entry[0] == marker : 0 == index;
      option.appendChild(document.createTextNode(entry[1]));
      this._controls.editor.appendChild(option);
    }.bind(this));
    this._controls.editor.disabled = false;
    Field.scrollFreeActivate(this._controls.editor);
  }
});

//**** DEPRECATION LAYER FOR InPlace[Collection]Editor! ****
//**** This only  exists for a while,  in order to  let ****
//**** users adapt to  the new API.  Read up on the new ****
//**** API and convert your code to it ASAP!            ****

Ajax.InPlaceEditor.prototype.initialize.dealWithDeprecatedOptions = function(options) {
  if (!options) return;
  function fallback(name, expr) {
    if (name in options || expr === undefined) return;
    options[name] = expr;
  };
  fallback('cancelControl', (options.cancelLink ? 'link' : (options.cancelButton ? 'button' :
    options.cancelLink == options.cancelButton == false ? false : undefined)));
  fallback('okControl', (options.okLink ? 'link' : (options.okButton ? 'button' :
    options.okLink == options.okButton == false ? false : undefined)));
  fallback('highlightColor', options.highlightcolor);
  fallback('highlightEndColor', options.highlightendcolor);
};

Object.extend(Ajax.InPlaceEditor, {
  DefaultOptions: {
    ajaxOptions: { },
    autoRows: 3,                                // Use when multi-line w/ rows == 1
    cancelControl: 'link',                      // 'link'|'button'|false
    cancelText: 'cancel',
    clickToEditText: 'Click to edit',
    externalControl: null,                      // id|elt
    externalControlOnly: false,
    fieldPostCreation: 'activate',              // 'activate'|'focus'|false
    formClassName: 'inplaceeditor-form',
    formId: null,                               // id|elt
    highlightColor: '#ffff99',
    highlightEndColor: '#ffffff',
    hoverClassName: '',
    htmlResponse: true,
    loadingClassName: 'inplaceeditor-loading',
    loadingText: 'Loading...',
    okControl: 'button',                        // 'link'|'button'|false
    okText: 'ok',
    paramName: 'value',
    rows: 1,                                    // If 1 and multi-line, uses autoRows
    savingClassName: 'inplaceeditor-saving',
    savingText: 'Saving...',
    size: 0,
    stripLoadedTextTags: false,
    submitOnBlur: false,
    textAfterControls: '',
    textBeforeControls: '',
    textBetweenControls: ''
  },
  DefaultCallbacks: {
    callback: function(form) {
      return Form.serialize(form);
    },
    onComplete: function(transport, element) {
      // For backward compatibility, this one is bound to the IPE, and passes
      // the element directly.  It was too often customized, so we don't break it.
      new Effect.Highlight(element, {
        startcolor: this.options.highlightColor, keepBackgroundImage: true });
    },
    onEnterEditMode: null,
    onEnterHover: function(ipe) {
      ipe.element.style.backgroundColor = ipe.options.highlightColor;
      if (ipe._effect)
        ipe._effect.cancel();
    },
    onFailure: function(transport, ipe) {
      alert('Error communication with the server: ' + transport.responseText.stripTags());
    },
    onFormCustomization: null, // Takes the IPE and its generated form, after editor, before controls.
    onLeaveEditMode: null,
    onLeaveHover: function(ipe) {
      ipe._effect = new Effect.Highlight(ipe.element, {
        startcolor: ipe.options.highlightColor, endcolor: ipe.options.highlightEndColor,
        restorecolor: ipe._originalBackground, keepBackgroundImage: true
      });
    }
  },
  Listeners: {
    click: 'enterEditMode',
    keydown: 'checkForEscapeOrReturn',
    mouseover: 'enterHover',
    mouseout: 'leaveHover'
  }
});

Ajax.InPlaceCollectionEditor.DefaultOptions = {
  loadingCollectionText: 'Loading options...'
};

// Delayed observer, like Form.Element.Observer, 
// but waits for delay after last key input
// Ideal for live-search fields

Form.Element.DelayedObserver = Class.create({
  initialize: function(element, delay, callback) {
    this.delay     = delay || 0.5;
    this.element   = $(element);
    this.callback  = callback;
    this.timer     = null;
    this.lastValue = $F(this.element); 
    Event.observe(this.element,'keyup',this.delayedListener.bindAsEventListener(this));
  },
  delayedListener: function(event) {
    if(this.lastValue == $F(this.element)) return;
    if(this.timer) clearTimeout(this.timer);
    this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay * 1000);
    this.lastValue = $F(this.element);
  },
  onTimerEvent: function() {
    this.timer = null;
    this.callback(this.element, $F(this.element));
  }
});


/*
 * jQuery EasIng v1.1.2 - http://gsgd.co.uk/sandbox/jquery.easIng.php
 *
 * Uses the built In easIng capabilities added In jQuery 1.1
 * to offer multiple easIng options
 *
 * Copyright (c) 2007 George Smith
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

// t: current time, b: begInnIng value, c: change In value, d: duration

jQuery.extend( jQuery.easing,
{
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});


/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
/*global document, jQuery */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


/*global document, jQuery */
function Draggable(){
	this._init.apply(this, arguments);
}

Draggable.prototype = {
	/* Methods for re-init in child class */
	oninit: function(){},
	events: function(){},
	onmousedown: function(){
		this.ptr.css({ position: "absolute" });
	},
	onmousemove: function(evt, x, y){
		this.ptr.css({ left: x, top: y });
	},
	onmouseup: function(){},

	isDefault: {
		drag: false,
		clicked: false,
		toclick: true,
		mouseup: false
	},

	_init: function(){
		if(arguments.length > 0){
			this.ptr = jQuery(arguments[0]);
			this.outer = jQuery(".draggable-outer");

			this.is = {};
			jQuery.extend(this.is, this.isDefault);

			var _offset = this.ptr.offset();
			this.d = {
				left: _offset.left,
				top: _offset.top,
				width: this.ptr.width(),
				height: this.ptr.height()
			};
			
			this.oninit.apply(this, arguments);

			this._events();
		}
	},
	_events: function(){
		var oThis = this;

		jQuery(document)
			.mousemove(function(evt){
				if(oThis.is.drag){
					oThis._mousemove(evt);
					return false;
				}
			})
			.mouseup(function(evt){
				oThis._mouseup(evt);
			})
			.bind("dragstart", function(){
				return false;
			});

		this.ptr
			.mousedown(function(evt){
				oThis._mousedown(evt);
				return false;
			})
			.mouseup(function(evt){
				oThis._mouseup(evt);
			});
		
		this.ptr.find("a")
			.click(function(){
				oThis.is.clicked = true;
				if(!oThis.is.toclick){
					oThis.is.toclick = true;
					return false;
				}
			})
			.mousedown(function(oEvt){
				oThis._mousedown(oEvt);
				return false;
			})
			.bind("dragstart", function(){
				return false;
			});

		
		this.events();
	},
	_mousedown: function(evt){
		this.is.drag = true;
		this.is.clicked = false;
		this.is.mouseup = false;
		
		var _offset = this.ptr.offset();
		this.cx = evt.pageX - _offset.left;
		this.cy = evt.pageY - _offset.top;

		jQuery.extend(this.d, {
			left: _offset.left,
			top: _offset.top,
			width: this.ptr.width(),
			height: this.ptr.height()
		});
		
		if(this.outer && this.outer.get(0)){
			this.outer.css({ height: Math.max(this.outer.height(), jQuery(document.body).height()), overflow: "hidden" });
		}
		
		this.onmousedown(evt);
	},
	_mousemove: function(evt){
		this.is.toclick = false;
		this.onmousemove(evt, evt.pageX - this.cx, evt.pageY - this.cy);
	},
	_mouseup: function(evt){
		var oThis = this;
		if(this.is.drag){
			this.is.drag = false;

			if(this.outer && this.outer.get(0)){

				if(jQuery.browser.mozilla) {
					this.outer.css({ overflow: "hidden" });
				} else {
					this.outer.css({ overflow: "visible" });
				}
				
				if(jQuery.browser.msie && jQuery.browser.version == '6.0') {
					this.outer.css({ height: "100%" });
				} else {
					this.outer.css({ height: "auto" });
				}	
			}

			this.onmouseup(evt);
		}
	}
	
};


Function.prototype.inheritFrom = function(BaseClass, oOverride){
	var Inheritance = function() {};
	Inheritance.prototype = BaseClass.prototype;
	this.prototype = new Inheritance();
	this.prototype.constructor = this;
	this.prototype.baseConstructor = BaseClass;
	this.prototype.superClass = BaseClass.prototype;

	if(oOverride){
		for(var i in oOverride) {
			this.prototype[i] = oOverride[i];
		}
	}
};


/*global Animate, Draggable, Event, Image, _o, clearInterval, clearTimeout, counter_text, document, jQuery, setInterval, setTimeout, window, $, $A, Autocompleter, Launcher, Prototype, js, swfobject, Ajax, Class, Effect, Element, Insertion, PlaceMap, YMaps*/

var _InviteFriends;

function jsSpan(elem){
	this.span = jQuery(elem);
	this.init();
}
jsSpan.prototype = {
	init:function(){
		this.span.hover(function(){
			jQuery(this).css({textDecoration:'underline'});
		}, function(){
			jQuery(this).css({textDecoration:'none'});
		});
	}
};


var jsButton = {};
jsButton.parseIncrement = function(increments_id) {
	var counter_text = jQuery("#" + increments_id).html();
	if(!counter_text) {return null;}
	var counter = counter_text.match(/\d+/);
	if (isNaN(counter)) {
		return null;
	}
	return counter;
};
		
jsButton.writeIncrements = function(increments_id, inc_old, inc) {
	jQuery("#" + increments_id).html(counter_text.replace(inc_old, inc));
};

jsButton.increment = function(button, increments_id) {
  var counter_elem = jQuery("#" + increments_id);
	var counter_text = counter_elem.html();
	if(!counter_text) {return;}
	var counter = parseInt(counter_text.match(/\d+/), 10);
	var new_counter = 0;
	if (isNaN(counter) || counter < 0) {
		return;
	}
	new_counter = button.checked ? counter + 1 : counter - 1;
	
	counter_elem.html(counter_text.replace(counter, new_counter));
};



var preload = ['/images/icon-favorite-act.gif', '/images/icon-favorite-hov.gif', '/images/micro-right-dis.gif', '/images/micro-left-dis.gif', '/images/micro-right-hov.gif', '/images/micro-right-act.gif', '/images/micro-left-hov.gif', '/images/micro-left-act.gif', '/images/users-list-item-act.gif', '/images/users-list-arr-back-act.gif', '/images/users-list-act-bg.gif', '/images/icon-favorite-act.gif', '/images/event-past-slider.png', '/images/event-past-slider-long.png', '/images/icon-star-act.gif', '/images/embed-act-lt.gif', '/images/embed-act-rt.gif', '/images/embed-act-lb.gif', '/images/embed-act-rb.gif', '/images/icon-evt-view2.gif', '/images/icon-evt-view2-act.gif', '/images/icon-evt-view1.gif', '/images/icon-evt-view1-act.gif', '/images/button-act-right.png', '/images/button-act-left.png', '/images/button-act-bg-ie.gif', '/images/button-act-bg.png', '/images/button-hover-right.png', '/images/button-hover-left.png', '/images/button-hover-bg.png', '/images/button-hover-bg-ie.gif', '/images/drop-top-right-bg.gif', '/images/drop-top-right-bg-act.gif', '/images/drop-top-left-bg.gif', '/images/drop-top-left-bg-act.gif', '/images/drop-top-bg.gif', '/images/drop-top-bg-act.gif', '/images/drop-top-arr.gif', '/images/drop-top-arr-dis.gif', '/images/drop-bottom-right-bg.gif', '/images/drop-bottom-right-bg-act.gif', '/images/drop-bottom-left-bg.gif', '/images/drop-bottom-left-bg-act.gif', '/images/drop-bottom-bg.gif', '/images/drop-bottom-bg-act.gif', '/images/drop-bottom-arr.gif', '/images/drop-bottom-arr-dis.gif', '/images/ajax-rating-blue.gif', '/images/ajax-rating-red.gif'];
// эта картинка задрала уже своими 404 '/images/events-new-view.gif', 
// у нас все картинки в /images/, может убрать в функцию? повторение - мать заикания,
// А ТОЧНО! УБЕРУ КАК ГАЛЕРЫ СДЕЛАЮ

var Preloader = function(aImages){
	var i=0;
	if(aImages.length >0){
		var img = new Image();
		img.src = aImages[0];
		img.onload = function(){
			aImages.shift();
			Preloader(aImages);
		};
	} else {
		return true;
	}
};

function Hairvideo(){
	
}


Hairvideo.prototype = {
  initPlayer:function(){
    var _my = this;
    this.k = 0;
    this.counter = 0;
    this.timeForToggle = 150;
    this.allImgs = jQuery('.flash-outer .photo-player .imgs img');
    this.playPause = jQuery('.flash-outer .photo-player .controls .button');
    this.track = jQuery('.flash-outer .photo-player .controls .slider-wrap');
    this.slider = jQuery('.flash-outer .photo-player .controls .slider-wrap .slider');
    this.allImgs.eq(0).css({visibility:'visible'});
    for(var i=1; i < this.allImgs.length; i++){
      this.allImgs.eq(i).css({visibility:'hidden'});
    }
    this.step = (this.track.width()-this.slider.width())/this.allImgs.length;
    this.timeTemp = this.timeForToggle*this.allImgs.length;
    this.playPause.click(function(){
      _my.togglePlay();
    });
    this.slider.mousedown(function(){
      _my.dragFlag = true;
      _my.startDrag();
    });
    jQuery(window).mousemove(function(evt){
      _my.dragSlider(evt);
    }).mouseup(function(){
      _my.dragFlag = false;
    });
  },
  togglePlay:function(temp){
    var _my = this;
    if(!this.playPause.is('.pause')){
      if(this.counter === 0){
        this.slider.css({left:0});
        this.allImgs.eq(this.allImgs.length-1).css({visibility:'hidden'});
        this.allImgs.eq(0).css({visibility:'visible'});
      }
      this.playPause.addClass('pause');
      this.playPause.css({background:'url(/images/hairlooks/play_act.gif) no-repeat left top'});
      setTimeout(function(){
        _my.playPause.css({background:'url(/images/hairlooks/pause.gif) no-repeat left 1px'});
      }, 150);

      this.slider.animate({left:this.track.width()-this.slider.width()}, this.timeTemp, 'linear');

      this.playerInt = setInterval(function(){
        _my.changePicture();
      }, this.timeForToggle);
    } else {
      this.playPause.removeClass('pause');
      this.playPause.css({background:'url(/images/hairlooks/pause_act.gif) no-repeat left 1px'});
      setTimeout(function(){
        _my.playPause.css({background:'url(/images/hairlooks/play.gif) no-repeat left top'});
      }, 150);
      for(var i=0; i < this.allImgs.length; i++){
        if(this.allImgs.eq(i).css('visibility') == 'visible'){
          this.timeTemp = (this.allImgs.length - this.counter)*this.timeForToggle;
        }
      }
      clearInterval(this.playerInt);
      this.slider.stop();
      this.kickDown = this.counter*this.step;
      this.slider.css({left:this.kickDown});
    }
  },
  changePicture:function(){
    var _my = this;
    for(var i=0; i < this.allImgs.length; i++){
      if(this.allImgs.eq(i).css('visibility') == 'visible'){
        this.counter = i+1;
        this.allImgs.eq(i).css({visibility:'hidden'});
        if(i == this.allImgs.length - 1){
          this.k = 0;
          this.counter = 0;
          this.slider.stop();
          clearInterval(this.playerInt);
          this.playPause.removeClass('pause');
          this.timeTemp = this.timeForToggle*this.allImgs.length;
          this.playPause.css({background:'url(/images/hairlooks/pause_act.gif) no-repeat left 1px'});
          setTimeout(function(){
            _my.playPause.css({background:'url(/images/hairlooks/play.gif) no-repeat left top'});
          }, 150);
          this.allImgs.eq(i).css({visibility:'visible'});
        } else {
          this.k = i + 1;
          this.counter = i+1;
          this.allImgs.eq(this.k).css({visibility:'visible'});
        }
        break;
      }
    }
  },
  startDrag:function(){
    this.tempLeft = this.track.offset().left;
  },
  dragSlider:function(evt){
    var _my = this;
    if(this.dragFlag === true){
      if(evt.pageX >= this.track.offset().left && evt.pageX <= this.track.offset().left + this.track.width()){
        _my.counter = Math.round((evt.pageX - _my.tempLeft)/_my.step);
        _my.slider.css({left:_my.counter*_my.step+_my.tempLeft-_my.track.offset().left});
        for(var i=0; i < this.allImgs.length; i++){
          this.allImgs.eq(i).css({visibility:'hidden'});
        }
        this.allImgs.eq(_my.counter).css({visibility:'visible'});
      }
    }
  }	
};

var Micro = {
	init:function(){
		var _my = this;
		this.aniTime = 500;
		this.micro = jQuery('div.column div.micro');
		this.staticBlock = this.micro.find('div.micro-main');
		this.comments = this.micro.find('div.comments-counter');
		this.commentsLink = this.comments.find('p a');
		this.sayMore = this.staticBlock.find('span.say-more');
		this.items = [];
		this.staticBlock.find('div.item').each(function(i){
			_my.items[i] = {
				ptr:jQuery(this),
				height:jQuery(this).height(),
				comments:jQuery(this).find('.to-comments'),
				link:jQuery(this).find('.to-comments').attr('href'),
				is_now:false
			};
		});
		this.leftArr = this.micro.find('div.left-arr');
		this.rightArr = this.micro.find('div.right-arr');
		this.leftArrDiv = this.leftArr.find('div').css({top:(this.items[0].height+24)/2-15});
		this.rightArrDiv = this.rightArr.find('div').css({top:(this.items[0].height+24)/2-15});
		if(this.items.length == 1){
			this.leftArr.css({visibility:'hidden'});
			this.rightArr.css({visibility:'hidden'});
		}
		this.staticBlock.css({height:this.items[0].height});
		this.posArr(0);
		this.items[0].ptr.css({display:'block'});
		this.items[0].is_now = true;
		if(this.items[0].link){
			this.changeComments(0);
			this.comments.css({display:'block'});
		}
		if(jQuery('#say-intro').length > 0){
			this.comments.css({display:'none'});
		}
		this.leftArrDiv.css({display:'block'});
		this.rightArrDiv.css({display:'block'});
		this.attachEvents();
	},
	posArr:function(elem){
		this.leftArr.height(this.items[elem].height+24);
		this.rightArr.height(this.items[elem].height+24);
		this.leftArrDiv.css({top:(this.items[elem].height+24)/2-15});
		this.rightArrDiv.css({top:(this.items[elem].height+24)/2-15});
	},
	changeComments:function(elem){
		var _my = this;
		if(_my.items[elem].link){
				_my.commentsLink.attr('href', _my.items[elem].link).html(_my.items[elem].comments.html());
		}
	},
	deleteItem:function(id){
		if(jQuery('#'+id).length > 0){
			jQuery('#'+id).remove();
			this.reInit();
		}
	},
	reInit:function(){
		var _my = this;
		jQuery('span.jsSpan').unbind().each(function(){
			var jsspan = new jsSpan(this);
		});
		b_button_init();
		this.items = [];
		this.comments = this.micro.find('div.comments-counter');
		this.commentsLink = this.comments.find('p a');
		this.staticBlock.find('div.item').each(function(i){
			_my.items[i] = {
				ptr:jQuery(this),
				height:jQuery(this).height(),
				comments:jQuery(this).find('.to-comments'),
				link:jQuery(this).find('.to-comments').attr('href'),
				is_now:false
			};
		});
		this.staticBlock.find('div.item .m-favorite').unbind('hover');
		this.staticBlock.find('div.item .m-favorite').hover(function(){
			if(!jQuery(this).is('.m-favorite-act')){
				jQuery(this).css({background:'url(/images/icon-favorite-hov.gif) no-repeat'});
			}
		}, function(){
			if(!jQuery(this).is('.m-favorite-act')){
				jQuery(this).css({background:'url(/images/icon-favorite.gif) no-repeat'});
			}
		});
		if(this.items.length == 1){
			this.leftArr.css({visibility:'hidden'});
			this.rightArr.css({visibility:'hidden'});
		} else {
			this.leftArr.css({visibility:'visible'});
			this.rightArr.css({visibility:'visible'});
		}
	},
	changeHeight:function(id){
		var _my = this;
		var i;
		var now;
		for(i=0; i<this.items.length; i++){
			if(this.items[i].is_now === true){
				this.items[i].ptr.fadeOut('fast');
				break;
			}
		}
		_my.leftArr.addClass('m-dis');
		_my.rightArr.addClass('m-dis');
		this.reInit();
		if(!id){
			id = this.items[0].ptr.attr('id');
		}
		for(i=0; i<this.items.length; i++){
			if(this.items[i].ptr.is('#'+id)){
				now = i;
				this.now = i;
				break;
			}
		}
		this.items[now].is_now = true;
		this.items[now].ptr.removeClass("item-preload").css({ opacity: 1 });
		if(this.items[now].ptr.is('.item-form')){
			this.comments.fadeOut('fast', function(){
				_my.staticBlock.animate({height:_my.items[now].height}, _my.aniTime, 'easeOutQuad', function(){
					_my.items[now].ptr.fadeIn('fast');
				});
			});
		} else {
			this.posArr(now);
			this.staticBlock.animate({height:_my.items[now].height}, _my.aniTime, 'easeOutQuad', function(){
				_my.items[now].ptr.fadeIn('fast', function(){
					if(jQuery('#say-intro').length === 0){
						_my.changeComments(now);
						_my.comments.fadeIn('fast');
					}
				});
				if(now !== 0){
					_my.leftArr.removeClass('m-dis');
				} else {
					_my.leftArr.removeClass('m-hov');
				}
				if(now < _my.items.length-1 && _my.items.length > 1){
					_my.rightArr.removeClass('m-dis');
				} else {
					_my.rightArr.removeClass('m-hov');
				}
			});
		}
	},
	checkNow:function(){
		for(var i=0; i<this.items.length; i++){
			if(this.items[i].is_now === true){
				this.now = i+1;
				break;
			}
		}
	},
	switchMicro:function(dur){
		for(var i=0; i<this.items.length; i++){
			if(this.items[i].is_now === true){
				var now = i;
				break;
			}
		}
		if(dur == -1 && now > 0){
			this.hide(now, dur);
		} else if(dur == 1 && now < this.items.length){
			this.hide(now, dur);
		} else if(dur === 0){
			this.hide(now, dur);
		}
	},
	hide:function(now, dur){
		var _my = this;
		if((dur == -1 && now > 0) || (dur == 1 && now < this.items.length-1)){
			this.leftArr.height(24);
			this.rightArr.height(24);
			this.leftArrDiv.fadeOut('fast');
			this.rightArrDiv.fadeOut('fast');
			this.comments.fadeOut('fast', function(){
				_my.items[now].ptr.fadeOut('fast', function(){
					_my.show(now+dur);
				});
				_my.items[now].is_now = false;
			});
		} else if(dur === 0){
			this.leftArr.height(24);
			this.rightArr.height(24);
			this.leftArrDiv.fadeOut('fast');
			this.rightArrDiv.fadeOut('fast');
			this.comments.fadeOut('fast', function(){
				_my.items[now].ptr.fadeOut('fast', function(){
					_my.show(dur, "hide");
				});
				_my.items[now].is_now = false;
			});
		}
	},
	show:function(now, hide){
		var _my = this;
		if(this.leftArr.is('.m-dis')){
			this.leftArr.removeClass('m-dis');
		}
		if(this.rightArr.is('.m-dis')){
			this.rightArr.removeClass('m-dis');
		}
		if(now == this.items.length-1){
			this.rightArr.addClass('m-dis');
		}
		if(now === 0){
			this.leftArr.addClass('m-dis');
		}
		if(hide){
			this.leftArr.addClass('m-dis');
			this.rightArr.addClass('m-dis');
		}
		if(this.items[now].link){
			this.changeComments(now);
		}
		this.staticBlock.animate({height:this.items[now].height}, this.aniTime, 'easeOutQuad', function(){
			_my.posArr(now);
			_my.items[now].ptr.fadeIn('fast', function(){
				_my.leftArrDiv.fadeIn('fast');
				_my.rightArrDiv.fadeIn('fast');
				_my.items[now].is_now = true;
				if(!_my.items[now].ptr.is('.item-form')){
					_my.comments.fadeIn('fast');
					if(hide){
						_my.rightArr.removeClass('m-dis');
					}
				} else {
					_my.comments.css({display:'none'});
				}
			});
		});
	},
	resize:function(d){
		var delta = Math.abs(d).toString();
		if(d<0){
			this.staticBlock.animate({height:"-="+delta+"px"}, 150, 'easeOutQuad');
		} else {
			this.staticBlock.animate({height:"+="+delta+"px"}, 150, 'easeOutQuad');
		}
	},
	
	preload: function(){
	  var now;
		for(var i=0; i<this.items.length; i++){
			if(this.items[i].is_now === true){
				now = i;
				break;
			}
		}
		
		this.items[now].ptr.addClass("item-preload").animate({ opacity: 0.3 });
		return false;
	},

  // Микро через юзербар
  live_wrapper_id: '#micro-live-wrapper',
  live_input_id: '#micro-live-input',
  live_add_text: "Написать микро",
  live_add_more_text: "Готово. Написать еще...",
  live_update: function(html){
    if (jQuery(this.live_wrapper_id).size() == 0){

    } else {
      jQuery(this.live_wrapper_id).replaceWith(html);
    }
  },
  validate_live_micro: function(){
    var val = jQuery(this.live_input_id).attr('value');
    if(val == this.live_add_text || val == this.add_more_text){return false;};
  },
  after_live_submit: function(){
    jQuery(this.live_input_id).attr('value', this.live_add_more_text).blur();    
  },
  live_init: function(){
    this.live_events();
    jQuery(this.live_input_id).blur();
  },
  live_events: function(){
    jQuery(this.live_input_id).blur(
      function(){if(this.value == ""){this.value=Micro.live_add_text;}}
    ).focus(
      function(){
        if(this.value == Micro.live_add_text || this.value == Micro.live_add_more_text){
          this.value = "";
        };
      }
    );
  },
  //

	attachEvents:function(){
		var _my = this;
		this.leftArr.hover(function(){
			if(!jQuery(this).is('.m-dis')){
				_my.leftArr.addClass('m-hov');
			}
		}, function(){
			if(!jQuery(this).is('.m-dis')){
				_my.leftArr.removeClass('m-hov');
			}
		}).click(function(){
			if(!jQuery(this).is('.m-dis')){
				_my.leftArr.addClass('m-act');
				setTimeout(function(){
					_my.leftArr.removeClass('m-act');
				}, 150);
				_my.switchMicro(-1);
			}
		});
		this.rightArr.hover(function(){
			if(!jQuery(this).is('.m-dis')){
				_my.rightArr.addClass('m-hov');
			}
		}, function(){
			if(!jQuery(this).is('.m-dis')){
				_my.rightArr.removeClass('m-hov');
			}
		}).click(function(){
			if(!jQuery(this).is('.m-dis')){
				_my.rightArr.addClass('m-act');
				setTimeout(function(){
					_my.rightArr.removeClass('m-act');
				}, 150);
				_my.switchMicro(1);
			}
		});
		this.staticBlock.find('div.item .m-favorite').hover(function(){
			if(!jQuery(this).is('.m-favorite-act')){
				jQuery(this).css({background:'url(/images/icon-favorite-hov.gif) no-repeat'});
			}
		}, function(){
			if(!jQuery(this).is('.m-favorite-act')){
				jQuery(this).css({background:'url(/images/icon-favorite.gif) no-repeat'});
			}
		});
	}
};


////////////////////////////////

/*
  Bubble plugin.
  Written by Egor Hmelyoff (hmelyoff@gmail.com)
  
  Global object:
    bubble
  
  Api:
    bubble.show(@data, @settings) — show bubble

    bubble.close(@settings) — close bubble

    bubble.append(node, @settings) — append data to bubble
      — return: uid:Number to show bubble later
      
    bubble.onload(func:Function) — execute func after bubble loading
    
  Params:
    @data:
      — "remote:url/page"  :String     — load page by ajax
      — "image:url/image]" :String     — preload and show any image
      — "html data string" :String     — create object from html data and show bubble with it (not working now)
      — "any text"         :String     — show bubble with this text (not working now)
    
      — uid                :Number     — show node generated by bubble.append

      — node               :DOM/Object — append node and show it
        
    @settings:Object
      — from               :DOM/Object — animate bubble from this node/size object (undefined — show from center or current state)
        { width: px, height: px, left: px, top: px }

      — to                 :DOM/Object — animate bubble to this node/size object (undefined — show to center, normal mode)
        { width: px, height: px, left: px, top: px }

      — message            :String     — Preload message
      — success            :Boolean    — Set preload icon to success

      — event              :Event      — to be able catch users pressed keys and so on (now using for make animation slower with Alt key)
      — reload             :Boolean    — if true reload ajax data for each "remote" bubble show
      — serialize          :FROM_ID    — ID of form to serialize and send with "remote" request
      — ajax               :Object     — object to extend ajax request object (look jQuery.ajax) (not working now)

      — oncreate(@node)    :Function   — call after data append by bubble.append in bubble context
      — onclose()          :Function   — call after bubble closed
  
  Example:
    bubble.show("remote:/login", { event: event, from: this })
    
  TODO:
    — centering content over animation
    — finishing list above
    — initialize bubble on show if it still does not initialized

*/

(function($) {
  
  var onload = [];
  var $this = this;

  this.bubble = {};
  this.bubble.onload = function(func){
    onload.push(func);
  }

  $(window).load(function(){
    $this.bubble = new b_bubble();
    bubble.postCreate();
  })
  
  var b_bubble_OPTIONS = {
    className: "b-bubble",
    classFixed: "b-bubble-fixed",

    opacityUnderLayer: 0.3,

    speed: 400, //ms
    speedSlow: 4000, //ms
    
    shadowSize: 43,
    alwaysOnTop: true,

    animation: "swing",
    animationClose: "easeInBack",
    
    preload: {
      width: 250,
      height: 150
    },
    
    template: 
  		'<div>' +
  			'<div class="b_c">' +
  				'<div class="b_t"><div><div></div></div></div>' +
  				'<div class="b_r"><div><div></div></div></div>' +
  				'<div class="b_b"><div><div></div></div></div>' +
  				'<div class="b_l"><div><div></div></div></div>' +
  				'<div class="b_content_out">' +
			      '<div class="b_close" title="Esc"></div>'+
  					'<div class="b_content">' +
  					'</div>' +
  				'</div>' +
  			'</div>' +
  		'</div>'
  }

  this.b_bubble = function(){
  	return this.init.apply(this, arguments);
  }
  
  b_bubble.prototype = {
    init: function(options){
      this.options = $.extend(b_bubble_OPTIONS, options ? options : {});
      
      this.is = {
        created: false,
        opened: false,
        closing: false,
        animate: false,
        loading: false
      }
      
      this.queue = [];
      this.cache = [];
      this.events = [];
      
      this.create();
    },
    create: function(){
      if(!this.is.created){

        this.ptr = $(this.options.template)
          .css({ left: -9999, top: -9999 })
          .addClass(this.options.className)

        this.ptr.prependTo(document.body);

        this.o = {
          content: this.ptr.find(".b_content"),
          close: this.ptr.find(".b_close"),
          bc: this.ptr.find(".b_c"),
    			eh: this.ptr.find(".b_t div").height(),
    			ew: this.ptr.find(".b_l div").width(),
            
    			oh: this.ptr.find(".b_t, .b_b"),
    			ot: this.ptr.find(".b_r div div, .b_l"),
    			ob: this.ptr.find(".b_r div, .b_l div").not(".b_r div div, .b_l div div"),
            
    			ow: this.ptr.find(".b_r, .b_l"),
    			or: this.ptr.find(".b_t div, .b_b div").not(".b_t div div, .b_b div div"),
    			ol: this.ptr.find(".b_t div div, .b_b")
        }
        this.o.pt = parseInt(this.o.bc.css("padding-top"));
  			this.o.pl = parseInt(this.o.bc.css("padding-left"));
  			
  			//this.ptr.hide();

        if($.browser.msie && $.browser.version < 7)
          this.ptr.wrap($("<div>").addClass(this.options.classFixed).hide())

  			this.underLayer = $("<div>").addClass("b-bubble-under").hide();
  			this.underLayer.insertAfter(this.ptr);

  			
  			var $this = this;
  			this.o.close.add(this.underLayer).click(function(evt){
  			  $this.close({ event: evt });
  			})
  			
  			$(document).keypress(function( event ){
  			  if(event.keyCode == 27 && $this.is.opened){
  			    $this.close({ event: event });
  			    return false;
  			  }
  			})
  			
  			
  			
        this.is.created = true;
      }
    },
    
    onload: function(func){
      if($.isFunction(func))
        func.call(window);
    },
    postCreate: function(){
			if(onload.length){
			  for (var i=0; i < onload.length; i++) {
			    if($.isFunction(onload[i]))
			      onload[i].call(window);
			  };
			  onload = [];
			}
    },


    
    addQueue: function(func){
      if(!this.queueCount)
        this.queueCount = 0;
        
      this.queue.push(func);
      this.queueCount++;
      this.check();
    },
    check: function(){
      if(!this.is.animate && this.queue.length){
        this.queue[0].call(this);
        this.queue.shift();
      }
    },
    inCache: function(data){
      for (var i=0; i < this.cache.length; i++) {
        if(this.cache[i].uid && this.cache[i].uid == data)
          return this.cache[i];

        if(this.cache[i].src && this.cache[i].src == data)
          return this.cache[i];

        if(this.cache[i].url && this.cache[i].url == data)
          return this.cache[i];

        if(this.cache[i].node && this.cache[i].node == data)
          return this.cache[i];
      };
      return false;
    },
    cleanCache: function(cache){
      for (var i=0; i < this.cache.length; i++) {
        if(this.cache[i] == cache)
          break;
      }
      this.cache.splice(i, 1);
    },


    animate: function(settings){
      var $this = this;

      if(!this.is.animate){
        this.is.animate = true;
        
        if(!this.is.opened)
          this.ptr.css({ left: -9999, top: -9999 }).show();
        
        if(this.is.opened)
          settings.from = null;

        from = this.normalize(settings.from);
        if(this.is.opened){
          $.extend(from, { width: this.sizes.w, height: this.sizes.h })
        }

  			this.sizes = {
  				w: from.width,
  				h: from.height,
  				l: from.left,
  				t: from.top
  			}

  			this.o.content.css({
  				width: Math.round(this.sizes.w),
  				height: Math.round(this.sizes.h)
  			});

				var top = from.top - this.ptr.height()/2;
				if(this.options.alwaysOnTop && this.options.shadowSize)
  				if(top < -this.options.shadowSize) top = -this.options.shadowSize;

  			this.ptr.css({
  				left: from.left - this.ptr.width()/2,
  				top: top
  			})
  			
  			if(!$.browser.msie && !this.is.opened)
  			  this.ptr.css({ opacity: 0 })

  			this.setLimit();

        to = this.normalize(settings.to);
        
        this.underLayer.css({ opacity: this.options.opacityUnderLayer }).show();

        var speed = this.options.speed;
        try{
          if(settings.event && settings.event.altKey === true)
            speed = this.options.speedSlow;
        } catch(e){}
          

        var animation = this.options.animation;
        if(this.is.closing)
          animation = this.options.animationClose;
          
  			this.oAni = new classAnimate(function(now, prev){
  				$this.sizes = {
  					w: $this.sizes.w + ((to.width-from.width)*(now-prev)),
  					h: $this.sizes.h + ((to.height-from.height)*(now-prev))
  				}

  				$this.o.content.css({
  					width: Math.round($this.sizes.w),
  					height: Math.round($this.sizes.h)
  				});
  				
  				var top = from.top + (to.top-from.top)*now - $this.ptr.height()/2;
  				if($this.options.alwaysOnTop)
    				if(top < -$this.options.shadowSize) top = -$this.options.shadowSize;

  				$this.ptr.css({
  					left: from.left + (to.left-from.left)*now - $this.ptr.width()/2,
  					top: top
  				})

  				if(!$.browser.msie && !$this.is.opened)
  					$this.ptr.css({ opacity: to.opacity*now })
  					
  				if(!$.browser.msie && $this.is.closing)
					  $this.ptr.css({ opacity: (1-now) });

  				$this.setLimit();

  			}, speed, animation, function(){

  			  if(settings.message && $this.is.loading){
  			    $this.o.content.append("<span>" + settings.message + "</span>");
  			  }

          $this.is.animate = false;
          $this.is.opened = true;
          $this.check();
  			})

      }
      
      
    },
		setLimit: function(){
			var t = Math.round(this.o.pt + this.sizes.h/2);
			if(t > this.o.eh) t = this.o.eh;
			
			var l = Math.round(this.o.pl + this.sizes.w/2);
			if(l > this.o.ew) l = this.o.ew;
			
			this.o.oh.css({ height: t });
			this.o.ot.css({ top: t });
			if($.browser.msie && $.browser.version < 7 && this.o.ob.parent().height() % 2) t--;
			this.o.ob.css({ bottom: t });

			this.o.ow.css({ width: l });
			this.o.ol.css({ left: l });
			if($.browser.msie && $.browser.version < 7 && this.o.or.parent().width() % 2) l--;
			this.o.or.css({ right: l });
			
		},
    normalize: function(obj){
      var defs = { // default coords and sizes
        width: 30,
        height: 30,
        left: $(window).width()/2,
        top: $(window).height()/2,
        opacity: 1
      }
      
      if(obj){
        var jobj = $(obj);
        if(obj.length > 0 || obj.tagName){ //domNode
          defs.width = jobj.width() > defs.width ? jobj.width() : defs.width;
          defs.height = jobj.height() > defs.height ? jobj.height() : defs.height;
          
          var _offset = jobj.offset();
          defs.left = _offset.left+defs.width/2;
          defs.top = _offset.top+defs.height/2-$(window).scrollTop();
          
        } else { // size object
          if(obj.width < 30) delete obj.width;
          if(obj.height < 30) delete obj.height;
          $.extend(defs, obj)
        }
      }
      return defs;
    },

    
    show: function( data, settings ){
      var $this = this;
      var jData = $(data);
      
      if( $.browser.msie && $.browser.version < 7 )
        this.ptr.parents("." + this.options.classFixed).show();
        
      if( settings && settings.onclose && $.isFunction(settings.onclose) )
        this.events.onclose = settings.onclose;
      else
        this.events.onclose = null;

      switch( typeof data ){
        case "number":
          this.showByCacheId(data, settings);
          break;
          
        case "string":

          if(jData.length > 0){
            // console.log("TODO: animate -> string object")

          } else if(data.split(":")[0] == "remote"){
            var url = data.split(":")[1];
            this.showRemote(url, settings);

          } else if(data.split(":")[0] == "image"){
            var src = data.split(":")[1];
            this.showImage(src, settings);

          } else {
            this.animate(from, to ? to : { width: 200, height: 200 });
            // console.log("TODO: animate -> text")

          }

          break;

        case "object":
          if(jData.length > 0){
            this.showNode(jData, settings);
          }

          break;

      }

    },
    close: function(settings){
      if(!this.is.closing && this.is.opened){
        this.addQueue(function(){
          this.is.closing = true;

          if(this.is.loading)
            this.preload(false);

          if(!settings.to) settings.to = {};
          $.extend(settings.to, { width: 0, height: 0 })
          this.animate(settings);
        });
        this.addQueue(function(){
          if($.browser.msie && $.browser.version < 7)
            this.ptr.parents("." + this.options.classFixed).hide();

          this.underLayer.hide();
          //this.ptr.hide();
          this.ptr.css({ left: -9999, top: -9999 })
          this.o.content.children().hide();

          this.is.opened = false;
          this.is.closing = false;
          
          if(this.events && this.events.onclose && $.isFunction(this.events.onclose))
            this.events.onclose.call(this);
          
        });
      }
    },
    preload: function(settings){
      if(settings){
        this.addQueue(function(){
          this.is.loading = true;

          this.o.content.children().hide();
          this.ptr.addDependClass("loading");
          if(settings.success)
            this.ptr.addDependClass("loading-success");

          if(!settings.to) settings.to = {};
          $.extend(settings.to, this.options.preload);
          this.animate(settings);
        });
      } else {
        this.ptr.removeDependClass("loading");
        this.ptr.removeDependClass("loading-success");
        this.is.loading = false;
        this.o.content.children().hide();
      }
        
    },
    append: function(node, settings){
      var uid = (new Date()).getTime() + Math.round(Math.random()*9999);
      node = $(node);

      // if(node.parent().length)
      //   node = node.clone(true);

		  node.appendTo(this.o.content);
      this.dimension();

      if(settings && settings.oncreate && $.isFunction(settings.oncreate))
        settings.oncreate.call(this, node);

		  var w = node.outerWidth();
		  var h = node.outerHeight();
		  
		  node.hide();
      this.dimension(false);

		  this.cache.push({ uid: uid, node: node, w: w, h: h });
		  
		  return uid;
      
    },
    dimension: function(hide){
      if($.browser.msie && $.browser.version < 7)
        if(hide === false){
    		  this.ptr.parent().hide();
        } else {
          this.ptr.parent().show()
        }
    },


    
    // data types
    showImage: function(src, settings){
      var cache = this.inCache(src);
      if(cache){
        this.preload(false);

        this.o.content.html("<img src='" + cache.src + "' />");
        if(!settings.to) settings.to = new Object();
			  $.extend(settings.to, { width: cache.w, height: cache.h });
        this.animate(settings);

      } else {
        var $this = this;
        this.preload(settings);

        var image = $("<img src='" + src + "' />");
        if($.browser.msie && $.browser.version < 7)
          $("." + this.options.classFixed).append(image);
        else
          $(document.body).append(image);
				image.css({ position: "absolute", left: -9999, top: -9999, border: "none" });

				image.load(function() {
				  var w = image.width();
				  var h = image.height();
				  $this.cache.push({ src: src, w: w, h: h })

          $this.addQueue(function(){
            this.preload(false);
            this.o.content.html("<img src='" + src + "' />");
    			  $.extend(settings.to, { width: w, height: h })
    			  settings.from = null;
            this.animate(settings);
          })
					image.remove();
				});
      }
    },

    // data types
    showRemote: function(url, settings){
      var cache = this.inCache(url);

      if(cache && !settings.reload){
        this.preload(false);

        cache.node.show();
        if(!settings.to) settings.to = new Object();
			  $.extend(settings.to, { width: cache.w, height: cache.h });

        this.animate(settings);

      } else {
        var $this = this;
        
        if(cache && settings.reload)
          this.cleanCache(cache);
        
        var request = {
          url: url,
          dataType: "html",
          success: function(data){
            var node = $(data).css({ visibility: "hidden", position: "absolute", left: -9999, top: -9999 });
            
            var queue = $this.queueCount;

    			  node.appendTo($this.o.content);
  				  var w = node.outerWidth();
  				  var h = node.outerHeight();
  				  node.hide();

    			  if(w*h == 0){
    			    if($this.queueCount == queue){
      			    $this.close(settings);
    			    }
    			  } else {
              if(!settings.reload)
      			    $this.cache.push({ url: url, node: node, w: w, h: h });

              $this.addQueue(function(){
                this.preload(false);

                node.removeAttr("style");

        			  $.extend(settings.to, { width: w, height: h })
        			  settings.from = null;
                this.animate(settings);
              })
    			  }

            
          }
        }
        
        if(settings.serialize){
          var form = $("#" + settings.serialize);
          var data = { data: form.serialize(), type: form.attr("method") ? form.attr("method").toUpperCase() : "POST" };
          $.extend(request, data);
        }
        
        this.preload(settings);
        $.ajax(request)

      }
    },

    showNode: function(node, settings){
      var cache = this.inCache(node.get(0));
      if(cache){
        this.showByCacheId(cache.uid, settings);
      } else {
        var uid = this.append(node);
		    this.cache.push({ node: node.get(0), uid: uid });
        this.showByCacheId(uid, settings);
      }
    },

    
    showByCacheId: function(uid, settings){
      var cache = this.inCache(uid);
      if(cache){
        this.preload(false);
        
			  cache.node.show();

			  if(settings && settings.onshow && $.isFunction(settings.onshow))
			    settings.onshow.call(this, cache.node);

        if(!settings.to) settings.to = new Object();
			  $.extend(settings.to, { width: cache.w, height: cache.h });
        this.animate(settings);
      }
    }

  }
  
})(jQuery);

////////////////////////////////



var Bubble = {
	init:function(){
		var _my = this;
		this.wK = 52;
		this.hK = 16;
		this.outer = jQuery('div.outer');
		this.root = jQuery('div.bubble');
		this.bubbleWrap = jQuery('div.bubble-wrap');
		this.dark = jQuery('#DarkBlock');
		this.message = jQuery('#b_preload_message');
		this.ieFlag = false;
		if(jQuery.browser.msie){
			this.ieFlag = true;
		}
		this.isShow = false;
		this.content = this.root.find(".b_content");
		this.bc = this.root.find(".b_c");
		this.pt = parseInt(this.bc.css("padding-top"), 10);
		this.pl = parseInt(this.bc.css("padding-left"), 10);
		this.eh = this.root.find(".b_t div").height();
		this.ew = this.root.find(".b_l div").width();
		this.oh = this.root.find(".b_t, .b_b");
		this.ot = this.root.find(".b_r div div, .b_l");
		this.ob = this.root.find(".b_r div, .b_l div");
		this.ow = this.root.find(".b_r, .b_l");
		this.or = this.root.find(".b_t div, .b_b div");
		this.ol = this.root.find(".b_t div div, .b_b");
		
		this.checkWindowParam();
	},
	checkWindowParam:function(){
		this.windowParam = {
			ptr:jQuery(window),
			width:jQuery(window).width(),
			height:jQuery(window).height()
		};
	},
	abuse:function(abuse_type_id, resource_type, resource_id, suffix){
		jQuery('#b_abuse input#abuse_abuse_type_id').attr('value', abuse_type_id);
		jQuery('#b_abuse input#abuse_resource_type').attr('value', resource_type);
		jQuery('#b_abuse input#abuse_resource_id').attr('value', resource_id);
		jQuery('#b_abuse h3').html(suffix);
	},
	keypress:function(event) {
		if(event.keyCode == Event.KEY_ESC) {this.hide(this.id, event);}
	},
	show:function(id, evt, check){
		this.removeMessage();
		var _my = this;
		this.id = id;
				
		this.elem = jQuery(evt);
		if(check.altKey === true){
			this.aniTime = 2500;
		} else {
			this.aniTime = 250;
		}
		this.keypress_binding = this.keypress.bindAsEventListener(this);
		this.blur_binding = this.blur.bindAsEventListener(this);
		// We use keydown instead of keypress because in Safari, when you 
		// have focus on INPUT element and binded a keypress event <ESC> button doesn't fires an event
   		Event.observe(document, 'keydown', this.keypress_binding);
		this.dark.css({display:'block'});
		if(this.ieFlag === true){
			jQuery('select').css({visibility:'hidden'});
		}
		this.selectedBlock = {
			ptr:jQuery('#'+this.id),
			width:jQuery('#'+this.id).width(),
			height:jQuery('#'+this.id).height()
		};
		
		this.endParam = {
			ptr:this.elem,				  	
			left:this.windowParam.width/2,
			top:(this.selectedBlock.height < this.windowParam.height-160) ? this.windowParam.height/2 : 80,
			width:this.selectedBlock.width + this.wK,
			height:this.selectedBlock.height + this.hK,
			opacity:1
		};
				
		if(this.isShow === false){
			this.showFirst();
		} else {
			this.resize();
		}
		
		var formItems = jQuery("#" + id).find("input, textarea");
		jQuery(formItems[formItems.length - 1]).blur(function(){ jQuery(formItems[0]).focus();});
		jQuery(formItems).each(function(){ 
			jQuery(this).click(function(){ jQuery(this).focus();});
		});
		jQuery(formItems[0]).focus();
	},
	showFirst:function(){
		this.isShow = true;
		this.root.css({visibility:'visible'});
		this.selectedBlock.ptr.css({display:'block'});
		var y = jQuery.browser.msie ? document.documentElement.scrollTop : window.scrollY;
		this.startParam = {
			ptr:this.elem,
			left:this.elem.offset().left + this.elem.width()/2,
			top:this.elem.offset().top + this.elem.height()/2 - y,
			width:0,
			height:0,
			opacity:(this.ieFlag === true) ? 1 : 0
		};
		this.root.css({left:this.startParam.left, top:this.startParam.top});
		if(this.endParam.height > jQuery(window).height()-200){
			this.bubbleWrap.addClass('not-fixed');
			jQuery.scrollTo(0, 300);
		} else {
			this.bubbleWrap.removeClass('not-fixed');
		}
		this.ani('show');
	},
	resize:function(){
		var _my = this;
		this.root.find(".b_content").css('overflow', 'hidden');
		this.root.find(".b_content_out").css('overflow', 'hidden');
		this.isShow = true;
		this.allBlocks = this.content.find('div.b_inner');
		for(var i=0; i<this.allBlocks.length; i++){
			if(this.allBlocks.eq(i).css('display') == 'block'){
				this.prevBlock = jQuery('#'+this.allBlocks.eq(i).attr('id'));
				break;
			}
		}
		
		this.prevBlock.fadeOut(this.aniTime);
		
		this.startParam = {
			ptr:this.elem,
			left:this.windowParam.width/2,
			top:(this.selectedBlock.height < this.windowParam.height-160) ? this.windowParam.height/2 : 80,
			width:this.prevBlock.width() + this.wK,
			height:this.prevBlock.height() + 2*this.hK,
			opacity:1
		};
		setTimeout(function(){
			_my.endParam = {
				ptr:_my.elem,
				left:_my.windowParam.width/2,
				top:(_my.selectedBlock.height < _my.windowParam.height-160) ? _my.windowParam.height/2 : 80,
				width:_my.selectedBlock.width + _my.wK,
				height:_my.selectedBlock.height + _my.hK,
				opacity:1
			};
			if(_my.endParam.height > jQuery(window).height()-200){
				_my.bubbleWrap.addClass('not-fixed');
				jQuery.scrollTo(0, 300);
			} else {
				_my.bubbleWrap.removeClass('not-fixed');
			}
			_my.selectedBlock.ptr.fadeIn(_my.aniTime);
			_my.ani('show', 'resize');
		}, 300);
	},
	ani:function(dur, res){
		var oThis = this;

		this.calc = {
			w: this.startParam.width,
			h: this.startParam.height
		};

		this.content.css({
			width: Math.round(oThis.calc.w),
			height: Math.round(oThis.calc.h)
		});

		if(oThis.ieFlag === false){
			this.root.css({
				left: this.startParam.left - this.root.width()/2,
				top: this.startParam.top - this.root.height()/2,
				opacity: this.startParam.opacity
			});
		} else {
			this.root.css({
				left: this.startParam.left - this.root.width()/2,
				top: this.startParam.top - this.root.height()/2,
				visibility:'visible'
			});
		}
		
		this.setLimit();
		
		this.oAni = new Animate(function(now, prev){
			oThis.calc = {
               w: oThis.calc.w + ((oThis.endParam.width-oThis.startParam.width)*(now-prev)),
               h: oThis.calc.h + ((oThis.endParam.height-oThis.startParam.height)*(now-prev))
             };
             oThis.content.css({
               width: Math.round(oThis.calc.w),
               height: Math.round(oThis.calc.h)
             });
         
             oThis.setLimit();
         
             if(oThis.ieFlag === false){
               var new_top = oThis.startParam.top + (oThis.endParam.top-oThis.startParam.top)*now - oThis.root.height()/2;
               oThis.root.css({
                 left: oThis.startParam.left + (oThis.endParam.left-oThis.startParam.left)*now - oThis.root.width()/2,
                 top: new_top < 0 ? 0 : new_top
               });
               if(dur == 'hide'){
                 oThis.root.css({opacity:1-now});
               } else if(dur == 'show' && res != 'resize'){
                 oThis.root.css({opacity:oThis.endParam.opacity*now});
               }
             } else if(oThis.ieFlag === true){
               oThis.root.css({
                 left: oThis.startParam.left + (oThis.endParam.left-oThis.startParam.left)*now - oThis.root.width()/2,
                 top: oThis.startParam.top + (oThis.endParam.top-oThis.startParam.top)*now - oThis.root.height()/2
               });
             }
					
			}, this.aniTime, function(){
				if(oThis.ieFlag === true && dur == 'hide'){
					jQuery('select').css({visibility:'visible'});
				}
				if(dur == 'hide'){
					oThis.root.css({visibility:'hidden'});
					oThis.selectedBlock.ptr.css({display:'none'});
					oThis.dark.css({display:'none'});
					if(jQuery('#login-errors').length > 0){
						jQuery('#login-errors').remove();
					}
				} else {
					
					oThis.root.find(".b_content").css('overflow', 'visible');
					oThis.root.find(".b_content_out").css('overflow', 'visible');
				}
									
		
			});
	},
	hide:function(id, check, hideId){
		this.root.find(".b_content").css('overflow', 'hidden');
		this.root.find(".b_content_out").css('overflow', 'hidden');
		
		this.removeMessage();
		this.isShow = false;
		
		if (typeof(id) == "undefined"){
			jQuery('#bubble-contents').children().each(function(i, b){
				if(b.id != "bubble_close" && b.style.display == "block") {
					id = b.id;				
				}
			});
		}
		this.id = id;
		
		if(this.keypress_binding) 
			{Event.stopObserving(document, 'keydown', this.keypress_binding);}
		if(this.blur_binding)
			{Event.stopObserving(document, 'blur', this.keypress_binding);}
	
	 	if(check && check.altKey === true){
			this.aniTime = 2500;
		} else {
			this.aniTime = 250;
		}
		this.selectedBlock = {
			ptr:jQuery('#'+this.id),
			width:jQuery('#'+this.id).width(),
			height:jQuery('#'+this.id).height()
		};
		this.startParam = {
			ptr:this.elem,
			left:this.windowParam.width/2,
			top:(this.selectedBlock.height < this.windowParam.height-160) ? this.windowParam.height/2 : 80,
			width:this.selectedBlock.width + this.wK,
			height:this.selectedBlock.height + 2*this.hK,
			opacity:1
		};

		// If we have hideId (id of element to hide) calculate positions
		if(hideId){
			var o = jQuery("#" + hideId);
			_o = o.offset();
		}
		var y = jQuery.browser.msie ? document.documentElement.scrollTop : window.scrollY;
		this.endParam = {
			ptr:this.elem,
			left: (o && _o.left) ? _o.left+o.width()/2 : this.startParam.left, // use this positions
			top: (o && _o.top) ? _o.top+o.height()/2-y : this.startParam.top, // with window.scrollY
			width:1,
			height:1,
			opacity:(this.ieFlag === true) ? 1 : 0.01
		};
		this.root.css({left:this.startParam.left, top:this.startParam.top});
		this.ani('hide');
	},
	preload:function(message, evt, check){
		Bubble.show('b_preload', evt, check);
		Bubble.showMessage(message);
	},
	showMessage:function(message){
		this.message.html(message);
	},
	removeMessage:function(){
		this.message.html("");
	},
	setLimit:function(){
		var t = Math.round(this.pt + this.calc.h/2);
		if(t > this.eh) {t = this.eh;}
		
		var l = Math.round(this.pl + this.calc.w/2);
		if(l > this.ew) {l = this.ew;}
		
		this.oh.css({ height: t });
		this.ot.css({ top: t });
		this.ob.css({ bottom: t });

		this.ow.css({ width: l });
		this.or.css({ right: l });
		this.ol.css({ left: l });
	},
	blur: function(event) {
		setTimeout(function() {Bubble.visibleItems.first().focus();}, 1);
	}
};

var reInitSpan = {
	reInit:function(){
		jQuery('span.jsSpan').unbind().each(function(){
			var jsspan = new jsSpan(this);
		});
	}
};




function Cities(){
}

Cities.prototype = {
	init:function(){
		var _my = this;
		this.cityButton = jQuery('div.head h1 span');
		this.cityMenu = jQuery('div.head div.drop-menu');

		this.cityTopArr = this.cityMenu.find('div.top');
		this.cityBottomArr = this.cityMenu.find('div.bottom');

		this.cityStaticBlock = this.cityMenu.find('div.cities');
		this.cityMovedBlock = this.cityStaticBlock.find('ul');
		this.cityMovedBlockHeight = this.cityMovedBlock.height();
		this.cityElems = this.cityMovedBlock.find('li');
		this.cityStaticBlockPos = this.cityStaticBlock.offset().top;
		this.cityStaticBlockHeight = this.cityStaticBlock.height();
		for(var i=0; i<this.cityElems.length; i++){
			if(this.cityElems.eq(i).is('.active')){
				this.actCity = this.cityElems.eq(i);
				i = this.cityElems.length;
			}
		}
		this.slowSpeed = 7;
		this.fastSpeed = 12;
		this.intTime = 30;
		if(this.cityElems.length <= 10){
			this.cityStaticBlock.css({height:this.cityMovedBlockHeight});
			this.cityTopArr.find('div.button').addClass('disable');
			this.cityBottomArr.find('div.button').addClass('disable');
		} else {
			this.cityStaticBlock.css({height:350});
			if(this.cityMovedBlock.offset().top >= this.cityStaticBlockPos){
				this.cityTopArr.find('div.button').addClass('disable');
			} else if(this.cityMovedBlockPos + this.cityMovedBlockHeight <= this.cityStaticBlockPos + this.cityStaticBlock.height()){
				this.cityBottomArr.find('div.button').addClass('disable');
			}
		}
		this.cityButton.click(function(){
			_my.cityMenu.css({display:'none', visibility:'visible'});
			_my.cityMenu.show('normal');
		}).mouseover(function(){
			jQuery(this).css({textDecoration:'underline'});
		}).mouseout(function(){
			jQuery(this).css({textDecoration:'none'});
		});
		this.cityMenu.mouseout(function(){
			_my.showTimeout = setTimeout(function(){
				_my.cityMenu.hide('normal');
			}, 50);
		}).mouseover(function(){
			clearTimeout(_my.showTimeout);
		});


		this.cityBottomArr.hover(function(){
			if(!_my.cityBottomArr.find('div.button').is('.disable')){
				clearTimeout(_my.cityTimeout);
				_my.speedAni = _my.slowSpeed;
				_my.toggleScroll(-1);
			}
		}, function(){
			_my.cityTimeout = setTimeout(function(){
				_my.toggleScroll(0);
			}, 50);
		}).mousedown(function(){
			_my.speedAni = _my.fastSpeed;
		});

		this.cityTopArr.mouseover(function(){
			if(!_my.cityTopArr.find('div.button').is('.disable')){
				clearTimeout(_my.cityTimeout);
				_my.speedAni = _my.slowSpeed;
				_my.toggleScroll(1);
			}
		}).mouseout(function(){
			_my.cityTimeout = setTimeout(function(){
				_my.toggleScroll(0);
			}, 50);
		}).mousedown(function(){
			_my.speedAni = _my.fastSpeed;
		});

		this.movedStartPos = parseInt(this.cityMovedBlock.css('top'), 10);

		jQuery(document).mouseup(function(){
			_my.speedAni = _my.slowSpeed;
		});
	},
	toggleScroll:function(d){
		var _my = this;
		var dir = d;
		if(d === 0){
			clearInterval(this.moveInt);
			this.cityBottomArr.removeClass('b-act');
			this.cityTopArr.removeClass('t-act');
		} else {
			clearInterval(this.moveInt);
			if(d == -1){
				this.cityTopArr.find('div.button').removeClass('disable');
				if(!this.cityBottomArr.find('div.button').is('.disable')){
					this.cityBottomArr.addClass('b-act');
				}
			} else if(d== 1){
				this.cityBottomArr.find('div.button').removeClass('disable');
				if(!this.cityTopArr.find('div.button').is('.disable')){
					this.cityTopArr.addClass('t-act');
				}
			}
			this.moveInt = setInterval(function(){
				_my.scrollCities(dir);
			}, this.intTime);
		}
	},
	scrollCities:function(d){
		var dir = d;
		var startPos = parseInt(this.cityMovedBlock.css('top'), 10);
		var tempTop = startPos - this.movedStartPos;
		if(dir == 1 && startPos+this.speedAni <= 0){
			this.cityMovedBlock.css({top:tempTop + this.speedAni*dir});
		} else if(dir == -1 && startPos + this.cityMovedBlockHeight - this.speedAni > this.cityStaticBlockPos + this.cityStaticBlockHeight){
			this.cityMovedBlock.css({top:tempTop + this.speedAni*dir});
		} else if(dir == -1 && startPos + this.cityMovedBlockHeight - this.speedAni <= this.cityStaticBlockPos + this.cityStaticBlockHeight){
			clearInterval(this.moveInt);
			this.cityMovedBlock.css({top:-this.cityMovedBlockHeight+this.cityStaticBlockHeight});
			this.cityBottomArr.find('div.button').addClass('disable');
			this.cityBottomArr.removeClass('b-act');
		} else if(dir == 1 && startPos+this.speedAni >= 0){
			clearInterval(this.moveInt);
			this.cityMovedBlock.css({top:0});
			this.cityTopArr.find('div.button').addClass('disable');
			this.cityTopArr.removeClass('t-act');
		}
	}
};


function CodeForBlog(){
	this.init();
}

CodeForBlog.prototype = {
	init:function(){
		this.spans = jQuery('span.codeForBlog');
		this.spans.hover(function(){
			jQuery(this).css({borderBottom:0});
		}, function(){
			jQuery(this).css({borderBottom:'1px dashed #777'});
		});
	}
};

function PlaceHolders(){
	this.init();
}

PlaceHolders.prototype = {
	init:function(){
		var _my = this;
		if(jQuery.browser.safari){
			jQuery('div.search').removeClass('all-browsers');
		}
	}
};

function AddToBlog(){
	this.init();
}

AddToBlog.prototype = {
	init:function(){
		var _my = this;
		this.inputs = jQuery('input.addToBlog');
		this.inputs.click(function(){
			jQuery(this).select();
		});
	}
};

function Places(){
	
}

Places.prototype = {
	init:function(){
		var _my = this;
		this.places = jQuery('div.places');
		this.staticBlock = this.places.find('div.static');
		this.movedBlock = this.staticBlock.find('ul.moved');
		this.movedBlockHeight = this.movedBlock.height();
		this.staticBlockHeight = (this.movedBlock.find('li').eq(0).height()+1)*this.staticBlock.attr('num')-1;
		this.staticBlock.height(this.staticBlockHeight);
		this.topArr = this.places.find('div.top');
		this.topArr.addClass('t-act');
		this.bottomArr = this.places.find('div.bottom');
		this.slowSpeed = 7;
		this.fastSpeed = 12;
		this.intTime = 30;
		this.deltaHeight = this.staticBlockHeight - this.movedBlockHeight;
		this.bottomArrWrap = this.bottomArr.find('div.wrapper');
		this.bottomArrBlock = this.bottomArr.find('div.grey-block');
		this.topArrWrap = this.topArr.find('div.wrapper');
		this.topArrBlock = this.topArr.find('div.grey-block');
		this.topLeft = this.topArr.find('img.left');
		this.topRight = this.topArr.find('img.right');
		this.bottomLeft = this.bottomArr.find('img.left');
		this.bottomRight = this.bottomArr.find('img.right');
		this.bottomArr.hover(function(){
			_my.aniScroll(_my.slowSpeed, -1);
		}, function(){
			_my.aniScroll(0, 0);
		}).mousedown(function(){
			_my.aniScroll(_my.fastSpeed, -1);
		}).mouseup(function(){
			_my.aniScroll(_my.slowSpeed, -1);
		});
		this.topArr.hover(function(){
			_my.aniScroll(_my.slowSpeed, 1);
		}, function(){
			_my.aniScroll(0, 0);
		}).mousedown(function(){
			_my.aniScroll(_my.fastSpeed, 1);
		}).mouseup(function(){
			_my.aniScroll(_my.slowSpeed, 1);
		});
	},
	aniScroll:function(speed, dir){
		var _my = this;
		if(dir == -1){
			_my.bottomArrWrap.css({backgroundColor:'#cacaca'});
			_my.bottomArrBlock.css({backgroundColor:'#cacaca'});
			_my.bottomLeft.attr('src', '/images/embed-act-lb.gif');
			_my.bottomRight.attr('src', '/images/embed-act-rb.gif');
			_my.topLeft.attr('src', '/images/embed-lt.gif');
			_my.topRight.attr('src', '/images/embed-rt.gif');
			_my.topArr.removeClass('t-act');
		} else if(dir == 1){
			_my.topArrWrap.css({backgroundColor:'#cacaca'});
			_my.topArrBlock.css({backgroundColor:'#cacaca'});
			_my.topLeft.attr('src', '/images/embed-act-lt.gif');
			_my.topRight.attr('src', '/images/embed-act-rt.gif');
			_my.bottomLeft.attr('src', '/images/embed-lb.gif');
			_my.bottomRight.attr('src', '/images/embed-rb.gif');
			_my.bottomArr.removeClass('b-act');
		} else {
			_my.topArrWrap.css({backgroundColor:'#eaeaea'});
			_my.bottomArrWrap.css({backgroundColor:'#eaeaea'});
			_my.topArrBlock.css({backgroundColor:'#eaeaea'});
			_my.bottomArrBlock.css({backgroundColor:'#eaeaea'});
			_my.topLeft.attr('src', '/images/embed-lt.gif');
			_my.topRight.attr('src', '/images/embed-rt.gif');
			_my.bottomLeft.attr('src', '/images/embed-lb.gif');
			_my.bottomRight.attr('src', '/images/embed-rb.gif');
		}
		clearInterval(this.aniInt);
		if(dir !== 0){
			this.aniInt = setInterval(function(){
				var tempTop = parseInt(_my.movedBlock.css('top'), 10);
				if(tempTop <=0 && tempTop >= _my.deltaHeight){
					if(tempTop+speed*dir <= 0 && tempTop+speed*dir >= _my.deltaHeight){
						_my.movedBlock.css({top:tempTop+speed*dir});
					} else if(tempTop+speed*dir < 0 + speed*dir && dir == 1){
						clearInterval(_my.aniInt);
						_my.movedBlock.css({top:0});
						_my.topArr.addClass('t-act');
					} else if(tempTop+speed*dir > _my.deltaHeight+speed*dir && dir == -1){
						clearInterval(_my.aniInt);
						_my.movedBlock.css({top:_my.deltaHeight+1});
						_my.bottomArr.addClass('b-act');
					}
				}
			}, this.intTime);
		}
	}
};






function evtItem(sId){ this.sId = sId; }
evtItem.prototype = {
	init:function(){
		this.is = {
			flag: false,
			clickFlag: true,
			noClick: true,
			on: false
		};
		this.ptr = jQuery("#" + this.sId);
		this.data = {
			parent: this.ptr.find('div.rating'),
			h4: this.ptr.find('h4'),
			date: ""
		};
		this.data.date = this.data.h4.text().match(/(\d+) (.*)/)[0];
		this.data.wrap = this.data.parent.find('div.wrap');
	}
};

function addBlock(obj){
	this.span = jQuery(obj);
	this.init();
}

addBlock.prototype = {
	init:function(){
		var _my = this;
		this.name = this.span.attr('name');
		this.cloned = jQuery('#'+_my.name).clone().removeClass('no-top-pad no-border').removeAttr('id');
		this.blocks = jQuery('div.'+this.cloned.attr('class'));
		for(var i=0; i<this.blocks.length; i++){
			if(this.blocks.eq(i).attr('id') == this.name){
				this.firstI = i-1;
				break;
			}
		}
		this.blocks.eq(this.firstI+1).find('input.hideBlock').get(0).checked = false;
		this.blocks.eq(this.firstI+1).find('input.hideBlock').click(function(){
			_my.toggleChecks(this);
		});
		this.span.hover(function(){
			jQuery(this).css({borderBottom:0});
		}, function(){
			jQuery(this).css({borderBottom:'1px dashed #777'});
		}).click(function(){
			_my.cloneBlock();
		});
	},
	attachChecks:function(){
		var _my = this;
		this.blocks = jQuery('div.'+this.cloned.attr('class'));
		this.checks = this.blocks.eq(this.firstI+1).find('input.hideBlock');
		this.checks.click(function(){
			_my.toggleChecks(this);
		});
	},
	toggleChecks:function(elem){
		this.elem = jQuery(elem);
		this.parent = this.elem.parents('div.'+this.cloned.attr('class'));
		this.blockToHide = this.parent.find('div.hideBlock');
		if(this.elem.get(0).checked){
			this.blockToHide.fadeOut('fast');
		} else {
			this.blockToHide.fadeIn('fast');
		}
	},
	cloneBlock:function(){
		var _my = this;
		this.firstI = this.firstI + 1;
		this.blocks = jQuery('div.'+this.cloned.attr('class'));
		this.cloned.clone().css({display:'none'}).insertAfter(this.blocks.eq(this.firstI)).show('normal');
		this.attachChecks();
	}
};

function UsersAdd(obj){
	this.parent = jQuery(obj);
	this.init();
}

UsersAdd.prototype = {
	init:function(){
		var _my = this;
		this.left = this.parent.find('div.left-list');
		this.right = this.parent.find('div.right-list');
		this.items = this.parent.find('div.item-wrap');
		this.countFlag = false;
		if(this.parent.find('.l-right span').length > 0){
			this.counterBlock = this.parent.find('.l-right span');
			this.count = jQuery('div.item-wrap', this.right).length;
			this.counterBlock.html(this.count+' из 28');
			this.error = jQuery('div.users-list-info div.error');
			this.countFlag = true;
		}
		
		this.flag = true;
		this.items.click(function(){
			_my.checkMax(this);
		});
		if(jQuery('#r-random').length > 0 && jQuery('#r-random').length > 0){
			this.favor = jQuery('#r-favor');
			this.random = jQuery('#r-random');
			this.favor.click(function(){
				_my.radioBut();
			});
			this.random.click(function(){
				_my.radioBut();
			});
			this.radioBut();
		}
	},
	checkMax:function(obj){
		var _my = this;
		if(this.countFlag === true){
			if(jQuery(obj).parent().attr('class') == this.left.attr('class')){
				this.count = jQuery('div.item-wrap', this.right).length;
				if(this.count +1 <= 28){
					_my.checkPos(obj);
				} else {
					_my.error.slideDown('normal');
					_my.left.css({backgroundColor:'red'});
					_my.right.css({backgroundColor:'red'});
					_my.left.animate({backgroundColor:'white'}, 200);
					_my.right.animate({backgroundColor:'white'}, 200);
				}
			} else {
				_my.error.slideUp('normal');
				_my.checkPos(obj);
			}
		} else {
			this.checkPos(obj);
		}
	},
	countFunc:function(){
		if(this.countFlag === true){
			this.count = jQuery('div.item-wrap', this.right).length;
			this.counterBlock.html(this.count+' из 28');
		}
	},
	sortFunc:function(a, b){
		return parseInt(a.split('_')[1], 10)-parseInt(b.split('_')[1], 10);
	},
	radioBut:function(){
		var _my = this;
		if(_my.favor.get(0).checked === true){
			jQuery('div.users-list-freeze').css({display:'none'});
			jQuery('input', _my.right).each(function(){
				jQuery(this).get(0).disabled = false;
			});
		}
		if(_my.random.get(0).checked === true){
			jQuery('div.users-list-freeze').css({display:'block'});
			jQuery('input', _my.right).each(function(){
				jQuery(this).get(0).disabled = true;
			});
		}
	},
	checkPos:function(obj){
		var _my = this;
		if(this.flag === true){
			this.flag = false;
			this.leftArray = [];
			this.rightArray = [];
			jQuery('div.item-wrap', this.left).each(function(){
				var lId = jQuery(this).attr('id');
				_my.leftArray.push(lId);
			});
			jQuery('div.item-wrap', this.right).each(function(){
				var rId = jQuery(this).attr('id');
				_my.rightArray.push(rId);
			});
			this.elem = jQuery(obj);
			this.id = this.elem.attr('id');
			if(this.elem.parent().attr('class') == this.left.attr('class')){
				this.rightArray.push(this.id);
				this.elem.addClass('active');
				this.elem.find('input').get(0).disabled = false;
				this.temp = this.elem.clone();
				this.temp.css({display:'none'});
				setTimeout(function(){
					if(jQuery('div.item-wrap', _my.right).length === 0){
						if(jQuery.browser.msie){
							_my.elem.remove();
							_my.right.html(_my.temp);
							_my.temp.css({display:'block'});
							setTimeout(function(){
								_my.temp.removeClass('active');
								_my.flag = true;
								_my.countFunc();
							}, 250);
						} else {
							_my.elem.slideUp(150, function(){
								_my.elem.remove();
							});
							_my.right.html(_my.temp);
							_my.temp.slideDown(150, function(){
								setTimeout(function(){
									_my.temp.removeClass('active');
									_my.flag = true;
									_my.countFunc();
								}, 150);
							});
						}
					} else {
						_my.rightArray.sort(_my.sortFunc);
						for(var i=0; i<_my.rightArray.length; i++){
							if(_my.id == _my.rightArray[i]){
								if(jQuery.browser.msie){
									_my.elem.remove();
									if(jQuery('div.item-wrap', _my.right).eq(i).html() !== null){
										_my.temp.insertBefore(jQuery('div.item-wrap', _my.right).eq(i));
									} else {
										_my.temp.insertAfter(jQuery('div.item-wrap', _my.right).eq(i-1));
									}
									_my.temp.css({display:'block'});
									setTimeout(function(){
										_my.temp.removeClass('active');
										_my.flag = true;
										_my.countFunc();
									}, 250);
									break;
								} else {
									_my.elem.slideUp(150, function(){
										_my.elem.remove();
									});
									if(jQuery('div.item-wrap', _my.right).eq(i).html() !== null){
										_my.temp.insertBefore(jQuery('div.item-wrap', _my.right).eq(i));
									} else {
										_my.temp.insertAfter(jQuery('div.item-wrap', _my.right).eq(i-1));
									}
									_my.temp.slideDown(150, function(){
										setTimeout(function(){
											_my.temp.removeClass('active');
											_my.flag = true;
											_my.countFunc();
										}, 150);
									});
									break;
								}
							}
						}
					}
				}, 150);
				
				this.temp.click(function(){
					_my.checkMax(this);
				});
			} else {
				this.leftArray.push(this.id);
				this.elem.addClass('active');
				this.elem.find('input').get(0).disabled = true;
				this.temp = this.elem.clone();
				this.temp.css({display:'none'});
				setTimeout(function(){
					if(jQuery('div.item-wrap', _my.left).length === 0){
						if(jQuery.browser.msie){
							_my.elem.remove();
							_my.left.html(_my.temp);
							_my.temp.css({display:'block'});
							setTimeout(function(){
								_my.temp.removeClass('active');
								_my.flag = true;
								_my.countFunc();
							}, 250);
						} else {
							_my.elem.slideUp(150, function(){
								_my.elem.remove();
							});
							_my.left.html(_my.temp);
							_my.temp.slideDown(150, function(){
								setTimeout(function(){
									_my.temp.removeClass('active');
									_my.flag = true;
									_my.countFunc();
								}, 150);
							});
						}
					} else {
						_my.leftArray.sort(_my.sortFunc);
						for(var i=0; i<_my.leftArray.length; i++){
							if(_my.id == _my.leftArray[i]){
								if(jQuery.browser.msie){
									_my.elem.remove();
									if(jQuery('div.item-wrap', _my.left).eq(i).html() !== null){
										_my.temp.insertBefore(jQuery('div.item-wrap', _my.left).eq(i));
									} else {
										_my.temp.insertAfter(jQuery('div.item-wrap', _my.left).eq(i-1));
									}
									_my.temp.css({display:'block'});
									setTimeout(function(){
										_my.temp.removeClass('active');
										_my.flag = true;
										_my.countFunc();
									}, 250);
									break;
								} else {
									_my.elem.slideUp(150, function(){
										_my.elem.remove();
									});
									if(jQuery('div.item-wrap', _my.left).eq(i).html() !== null){
										_my.temp.insertBefore(jQuery('div.item-wrap', _my.left).eq(i));
									} else {
										_my.temp.insertAfter(jQuery('div.item-wrap', _my.left).eq(i-1));
									}
									_my.temp.slideDown(150, function(){
										setTimeout(function(){
											_my.temp.removeClass('active');
											_my.flag = true;
											_my.countFunc();
										}, 150);
									});
									break;
								}
							}
						}
					}
				}, 150);
				this.temp.click(function(){
					_my.checkMax(this);
				});
			}
		}
	}
};

function auto_complete_update_element_callback(element, value) {
 $A(value.getElementsByTagName('span')).each(function(element) {
   var classNames = element.className.split(' ');
   if(classNames.length == 1 && $(element.className)) {
     $(element.className).value = element.innerHTML;
   }
 });
}

var Flash = {
	// надо для поддержки старого флеша
	new_swfobject: function(url, width, height) {
		swfobject.embedSWF(url, url, width, height, "9.0.0");
	},
	super_new_swfobject: function(url, id, width, height) {
		swfobject.embedSWF(url, id, width, height, "9.0.0");
	}
};

var FlashBubbleFactory = {
	getBubble:function(bubble_id, message_block_id){
		var flash_bubble = {};
		Object.extend(flash_bubble, Bubble);
		Object.extend(flash_bubble, {
			init:function(){
				this.message_block_id = message_block_id;
				var _my = this;
				this.wK = 52;
				this.hK = 16;
				this.outer = jQuery('div.outer'); // xxx 
				this.root = jQuery('#' + bubble_id);
				this.dark = jQuery('#DarkBlock');
				this.message = this.root.find('b_preload_message'); // test
				this.ieFlag = false;
				if(jQuery.browser.msie){
					this.ieFlag = true;
				}
				this.isShow = false;
				this.content = this.root.find(".b_content");
				this.bc = this.root.find(".b_c");
				this.pt = parseInt(this.bc.css("padding-top"), 10);
				this.pl = parseInt(this.bc.css("padding-left"), 10);
				this.eh = this.root.find(".b_t div").height();
				this.ew = this.root.find(".b_l div").width();
				this.oh = this.root.find(".b_t, .b_b");
				this.ot = this.root.find(".b_r div div, .b_l");
				this.ob = this.root.find(".b_r div, .b_l div");
				this.ow = this.root.find(".b_r, .b_l");
				this.or = this.root.find(".b_t div, .b_b div");
				this.ol = this.root.find(".b_t div div, .b_b");
				this.checkWindowParam();
			},
			show_message:function(message, obj, evnt) {
				var message_box = this.root.find(".flash_message");
				message_box.html(message);
				this.show(this.message_block_id, obj, evnt);
			}	
		});
		return flash_bubble;
	}	
};

var GreenFlash = FlashBubbleFactory.getBubble("green_flash_bubble", "green_message");	
var RedFlash = FlashBubbleFactory.getBubble("red_flash_bubble", "red_message");

// мигание
var blinks = $A();
function blinker(element) {
  Effect.toggle(element, 'appear', { duration: 0.3, from: 1.0, to: 0.2, afterFinish: function() { if(blinks.include(element.id)) {blinker(element);} } });
}

function blink(element) {
  element = $(element);
  blinks.push(element.id);
  blinker(element);
}
 
 
function unblink(element) {
  element = $(element);
  blinks = blinks.without(element.id);
}

function colorize_error_checkbockses(){
	jQuery("input[type=checkbox].invalid").each(function(){
		var error_text = jQuery(this).attr("error_text"); 
		var checkbox_text = jQuery("label[for=" + this.id + "]");
		checkbox_text.css({"background-color": "#FFC7C7"});
		checkbox_text.html(checkbox_text.html() + "<div class='error-message'>" + error_text + "</div>");
	});
}

// Переключалка на странице лука
function change_look_image(id, path) {
  jQuery(".look-details .inner .item").removeClass("active");
  jQuery("#look_image_" + id).addClass("active");
  jQuery(".lc-big img").attr("src", path);
}

// Cчиталка-ограничитель
InputCounter = Class.create({
	initialize: function(elem) {
		this.inputElement = jQuery(elem);
		this.maxlength = parseInt(this.inputElement.attr("maxlength"), 10);
		if (isNaN(this.maxlength) || this.maxlength <= 0 || this.maxlength >= 100000) {
			return;
		}
		this.counter = jQuery(document.createElement('div'));
		this.counter.addClass('input-counter');
		this.counter.html(this.maxlength - this.inputElement.get(0).value.length);
		this.inputElement.after(this.counter);
		var _my = this;
		this.inputElement.keyup(function(){
			_my.counter.html(_my.maxlength - _my.inputElement.get(0).value.length);
		});
	}
});

// Карта для мест
PlaceMap = Class.create({
  initialize: function(map_id, lat, lng){
    this.pmap = new google.maps.Map2(document.getElementById(map_id));
    this.pmap.addControl(new GLargeMapControl());
    this.pmap.addControl(new GMapTypeControl());
    this.pmap.enableScrollWheelZoom()
    this.pmap.setCenter(new GLatLng(lat, lng), 15);
	this.place_mark = false;
  },
  handle_inputs: function(){
  	$(this.inputs_prefix + '_lat').value = this.place_mark.getLatLng().lat();
    $(this.inputs_prefix + '_lng').value = this.place_mark.getLatLng().lng();
  },
  geocode_address: function(text){
    var geocoder = new GClientGeocoder();
    var _me = this;
    geocoder.getLocations(text, function(response){
      if (!response || response.Status.code != 200) {alert("Ничего не найдено")}
      else{
        var place = response.Placemark[0];
        var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        _me.pmap.setCenter(point);
        _me.move_place_marker(point);
      }
    });
  },
  move_place_marker: function(latlng){
    this.place_mark.setLatLng(latlng);
    this.handle_inputs();
  },
  set_place_marker: function(lat, lng, text, handle_inputs_prefix){
    if(!!this.place_mark){this.pmap.removeOverlay(this.place_mark);}
    this.place_text = text;
    this.point = new GLatLng(lat, lng);

    this.pmap.setCenter(this.point, 16);
    this.place_mark = new GMarker(this.point, {draggable: !!handle_inputs_prefix});
   

    // Если карта предназначена для редактирования, а не просмотра, обновляем 
    // values у инпутов с именами _lat, _lng с префиксом handle_inputs_prefix
    if (!!handle_inputs_prefix){
      this.inputs_prefix = handle_inputs_prefix;
      this.pmap.addOverlay(this.place_mark);
      this.handle_inputs();
      var _me = this;
      GEvent.addListener(this.place_mark, "dragend", function() {
        _me.handle_inputs();});
    } else {
      this.pmap.addOverlay(this.place_mark);
      this.pmap.openInfoWindowHtml(this.point, "<p class='bubble-mapbubble'>" + this.place_text + "</p>");
    }
  }
});

BubblePlaceMap = {
  load_for_bubble : function(lat, lng, title){
    google.load('maps', '2.x', {'callback' : function(){
      window.GMAPS_LOADED = true;
        BubblePlaceMap.render_bubble_map(lat, lng, title);
      }
    });
  },
  render_bubble_map: function(lat, lng, title){
    if (window.GMAPS_LOADED && window.BUBBLE_LOADED && !BubblePlaceMap.rendered){
      place_map = new PlaceMap("PlaceMapID", lat, lng);
      place_map.set_place_marker(lat, lng, title, false);
      BubblePlaceMap.rendered = true;
      setTimeout("place_map.pmap.checkResize()", 300);
    }
  }
}


function Friend(user){
	this.init(user);
}

Friend.prototype = {
	init:function(user){
		var _my = this;
		this.bool = false;
		this.user = user;
		this.checkBlock = this.user.find('div.checkbox');
		this.check = this.user.find('input');
		this.check.removeAttr('checked');
		this.user.click(function(){
			_my.moveUser();
		});
	},
	moveUser:function(){
		var _my = this;
		this.check.attr('checked', 'checked');
		this.user.removeClass('visible');
		var id = this.user.attr('id');
		for(var i=0; i<_InviteFriends.usersHash.length; i++){
			if(_InviteFriends.usersHash[i].id == id){
				_InviteFriends.usersHash[i].on_left = false;
				break;
			}
		}
		this.copied = this.user.clone();
		this.copied.attr('id', this.copied.attr('id')+'_copy');
		selectedUsers.append(_my.copied.removeClass('hover')).addClass('selected-block');
		jQuery("#EmptyBlock").removeClass('empty-block');
		this.initSelected(this.copied);
	},
	initSelected:function(item){
		var _my = this;
		item.click(function(){
			var id = item.attr('id').replace('_copy', '');
			item.remove();
			jQuery("#"+id).addClass('visible');
			jQuery("#"+id+"_check").removeAttr("checked");
			for(var i=0; i<_InviteFriends.usersHash.length; i++){
				if(_InviteFriends.usersHash[i].id == id){
					_InviteFriends.usersHash[i].on_left = true;
					break;
				}
			}
			if(selectedUsers.find('div.item').length == 0){
				jQuery("#EmptyBlock").addClass('empty-block');
				selectedUsers.removeClass('selected-block');
			}
		});
	}
}

function InviteFriends(){
	this.init();
}

InviteFriends.prototype = {
	init:function(){
		var _my = this;
		selectedUsers = jQuery('#SelectedUsers');
		allUsers = jQuery('#AllUsers');
		this.usersHash = [];
		allUsers.find('div.item').each(function(){
			var user = jQuery(this);
			var id = user.attr('id');
			_my.usersHash.push({'id': id, "name":user.find('#'+id+'_nick').text()+user.find('#'+id+'_name').text(), on_left:true});
			new Friend(user);
		});
    if(window.location.hash == "#list_already_invited"){
      this.scrollToInvited();
    }
	},
	incremental:function(obj){
		var pattern = new RegExp(".*"+obj.value+".*", "i");
		for(var i=0; i< this.usersHash.length; i++){
			if(this.usersHash[i].name.match(pattern) && this.usersHash[i].on_left){
				jQuery('#'+this.usersHash[i].id).addClass('visible');
			} else {
				jQuery('#'+this.usersHash[i].id).removeClass('visible');
			}
		}
	},
	scrollToInvited:function(){
		this.invited = jQuery('#already_invited');
		allUsers.parent().scrollTo(this.invited, 500);
	}
}


function Lam(){}
Lam.prototype = {
	init:function(){
		var _my = this;
		
		if(jQuery('div.search').length > 0){
			var ph = new PlaceHolders();
		}
		
		jQuery('div.users-list').each(function(){
			var ua = new UsersAdd(jQuery(this));
		});
		
		
		if(jQuery('div.drop-menu').length > 0){
       		var cities = new Cities();
      		cities.init();
		}		
		if(jQuery('span.codeForBlog').length > 0){
       		var cfb = new CodeForBlog();
		}

		jQuery('span.jsSpan').each(function(){
	 		var js = new jsSpan(this);
		});

		jQuery('span.addBlock').each(function(){
       		var ab = new addBlock(jQuery(this));
		});

		if(jQuery('div.rating-inner').length !=0){
			this.getRatingInnerWidth();
			
			jQuery('div.rating-inner .minus').hover(function(){
				jQuery(this).addClass('minus-hover');
			}, function(){
				jQuery(this).removeClass('minus-hover');
			});
			
			jQuery('div.rating-inner .plus').hover(function(){
				jQuery(this).addClass('plus-hover');
			}, function(){
				jQuery(this).removeClass('plus-hover');
			});
		}

		if(jQuery('div.places').length > 0){
			var places = new Places();
			places.init();
		}
		
		jQuery('tr.external-user span.remove').each(function(){
			var jthis = jQuery(this);
			new SpanButton(jthis);
			jthis.click(function(){
				container = jthis.parents('tr.external-user');
				jthis.unbind();
				jQuery.ajax({
					type: "POST",
					url: "/external_users/" + container.attr('id').replace(/\D*/, ''),
					data: "_method=DELETE",
					success: function(data) {
						container.fadeOut('fast');
					}
			 	});
			});
		});
		
		if(jQuery('input.addToBlog').length > 0){
			var atb = new AddToBlog();
		}
		return this;
	},
	getRatingInnerWidth:function(){
		k = jQuery('div.rating-inner .points .value span').html().length
		if ( jQuery('div.rating-inner .minus').length > 0 ) {
			k = (k>1) ? (k-2)*30 : (k-1)*30
			k = '-' + k
			jQuery('div.rating-inner').css('left', eval(340+ k) +'px');
		} else {
			k = (k)*30
			k = '-' + k
			jQuery('div.rating-inner').css('left', eval(370+ k) +'px');
		}
	}
};

function SpanButton(elem){
	this.init(elem);
}

SpanButton.prototype = {
	init:function(elem){
		elem.hover(function(){
			elem.addClass('hover').parent().addClass('active');
		}, function(){
			elem.removeClass('hover').parent().removeClass('active');
		});
	}
}


function ExternalUser(){}
ExternalUser.prototype = {
	init:function(){
		var _my = this;
		jQuery("#external-users div.external-user").each(function(){
			_my.init_external_user(jQuery(this));
		})
	}, 
	init_external_user:function(block){
		var _my = this;
		new SpanButton(block.find("span.remove"));
		new SpanButton(block.find("span.add"));
		block.find("span.add").click(function(){
			jQuery.ajax({
				type: "POST",
				url: "/friendship",
				data: "id=" + block.find("li.add input[type=hidden]").val(),
				success: function() {
					jQuery.ajax({
						type: "POST",
						url: "/external_users/next",
						success: function(data) {
							_my.remover(block, data);
						}
					});
				}
			});
		});
		block.find("span.remove").click(function(){
			jQuery(this).unbind();
			jQuery.ajax({
				type: "POST",
				url: "/external_users/" + block.attr('id').replace(/\D*/, ''),
				data: "_method=DELETE&vomit=true",
				// может вернуться либо код нового юзера, на который надо навесить событие
				// либо пустая строка (больше пользователей нет)
				success: function(data) {
					_my.remover(block, data);
				}
		 	});
		});
	},
	remover:function(block, data) {
		var _my = this;
		block.fadeOut('fast', function(){
			if (data) {
				block.after(data);
				_my.init_external_user(jQuery("#" + data.match(/external_user_\d+/)));
			}
			block.remove();
			// если включенных блоков .external_user 0, то тогда убрать блок #external-users
			if (jQuery("#external-users div.external-user").length == 0){
				jQuery("#external-users").toggle();
			}
		});
	}
};

jQuery(document).ready(function(){
	var lam = new Lam();
	lam.init();
	if(jQuery('div.bubble').length > 0){
		Bubble.init();
		jQuery(window).resize(function(){
			Bubble.checkWindowParam();
		});
	}
	if(jQuery('div.micro').length > 0){
		Micro.init();
	}
  Micro.live_init();
	
	if(jQuery('div.invite-friends div.left-part-wrap').length !=0){
		_InviteFriends = new InviteFriends();
	}

	if(jQuery('.flash-outer .photo-player').length > 0){
	var hair = new Hairvideo();
	  setTimeout(function(){
	    hair.initPlayer();
	  }, 500);
	}

	$$(".input-wrapper").each(function(iw){
	  new InputCounter(jQuery(iw).children().get(0));
	});

	Preloader(preload);
 	colorize_error_checkbockses();

	var external_user = new ExternalUser();
	external_user.init();
});





/*global $, Launcher, document, jQuery, jsButton, jsSpan, lameditor */
// Сворачиваем форму в состояние ожидания, передаем сюда указатель DOM на форму
function commentAJAX(obj){
	var h = obj.find("div.cs-content").height();
	var _h = obj.removeClass("cs-form-full").addClass("cs-form-loading").find("div.cs-content").height();
	obj.addClass("cs-form-full").removeClass("cs-form-loading");
	
	obj.find("div.cs-content").css({ height: _h, overflow: "hidden" }).animate({ }, 500, function(){
		obj.removeClass("cs-form-full").addClass("cs-form-loading");
		jQuery(this).removeAttr("style");
	});
	obj.removeClass("cs-form-full").addClass("cs-form-loading");
	
	obj.find("div.cs-content-in").css({ opacity: 1 }).animate({ opacity: 0 }, 500, function(){
		jQuery(this).removeAttr("style");
	});

	// // //	Тест удаления формы			
	// setTimeout(function(){
	// 	removeForm(obj);
	// }, 600);
	
}


function commentFromInit(obj){
	obj
		.hover(function(){
			var $this = jQuery(this);
			if(!$this.is(".cs-form-full") && !$this.is(".cs-form-loading")){
				$this.addClass("cs-form-hover");
			}
		}, function(){
			var $this = jQuery(this);
			if(!$this.is(".cs-form-full") && !$this.is(".cs-form-loading")){
				$this.removeClass("cs-form-hover");
			}
		})
		.click(function(){
			var $this = jQuery(this);
			if(!$this.is(".cs-form-full") && !$this.is(".cs-form-loading")){
				$this.removeClass("cs-form-hover");
				var h = $this.find("div.cs-content").height();
				var _h = $this.addClass("cs-form-full").find("div.cs-content").height();
				$this.find("div.cs-content").css({ height: _h, overflow: "hidden" }); //.animate({ height: _h }, 500, function(){
					$this.find("div.cs-content").css({ height: "auto", overflow: "visible" });
				// });
				$this.find("div.cs-content-in").css({ opacity: 1 }); //.animate({ opacity: 1 }, 500, function(){
					//window.scrollTo(0, obj.offset().top);
				// });
			}
		})
		.find("span.cs-cancel")
			.unbind()
			.click(function(){
				var $this = jQuery(this).parents("div.cs-form").eq(0);
				$this.removeClass("cs-form-hover");
				var h = $this.find("div.cs-content").height();
				var _h = $this.removeClass("cs-form-full").find("div.cs-content").height();
				$this.addClass("cs-form-full");
				
				$this.find("div.cs-content-in").css({ opacity: 0 });
				//$this.find("div.cs-content-in").removeAttr("style");
				
				
				$this.find("div.cs-content").css({ height: _h, overflow: "hidden" }).removeClass("cs-form-full").animate({}, 500, function(){
					$this.removeClass("cs-form-full");
					jQuery(this).removeAttr("style");
				});

				if($this.parents("div.cs-group").length) {
					$this.css({ overflow: "hidden", height: 0, marginBottom: 0, paddingTop: 0 }).animate({}, 500, function(){
						$this.parent().prev().removeClass("cs-answered");
						$this.remove();
					});
					$this.parent().prev().removeClass("cs-answered");
					$this.remove();
				}
				
				$('form_comment_0').show();
				
			}).end()
		.find(".cs-leave")
			.click(function(){
				var div = jQuery(this).parents("div.cs-comment").eq(0);
				commentAJAX(div);
				// reaction here
			});

	var jsspan = new jsSpan(obj.find("span.cs-cancel"));
}

function commentInitText(obj){
	obj.find(".cs-content img").each(function(){
		var $this = jQuery(this);
		var w = $this.width();
		if(w > obj.width()){
			$this.wrap("<div>").parent().addClass("cs-image-big").css({ width: obj.width()-10 }).append('<div><a target="_blank" href="' + $this.attr("src") + '">открыть картинку в новом окне</a></div>');
		}
	});
}


// Инит и функции для работы с комментами.

// Инитим комменты, передаем сюда указатель DOM на созданный коммент
function commentInit(obj){
	var jobj = jQuery(obj);
	
	commentInitText(jobj);
	
	jobj.
		hover(function(){
			if(jQuery.browser.msie){
				// jQuery(this).addClass("cs-comment-hover");
			} else {
				// jQuery(this).find("div.cs-answer").css({display:'block'}).stop().animate({ opacity: 1 }, "fast")
				// jQuery(this).find("div.cs-answer").css({opacity:1})
				// console.log(jQuery(this).find("div.cs-answer"))
			}
		}, function(){
			if(jQuery.browser.msie){
				// jQuery(this).removeClass("cs-comment-hover");
			} else {
				// jQuery(this).find("div.cs-answer").stop().animate({ opacity: 0 }, "fast")
			}
		}).find("div.cs-answer")
			.click(function(){
				var $this = jQuery(this).parents("div.cs-comment").eq(0);
				if(!$this.is(".cs-answered")){
					if(!$this.next("div.cs-group").length) {
						$this.after("<div class='cs-group' id=\"answers_"+$this.attr("id")+"\"></div>");
					}
			
					$this.addClass("cs-answered");
					var answer = jQuery("div.comments div.cs-form").eq(0).clone().attr('id', 'form_'+$this.attr("id"));
					answer.find("input[type=hidden]").eq(0).val($this.attr("id").replace('comment_', ''));
					$this.next().prepend(answer.removeClass("cs-form-full").removeClass("cs-form-loading").css({'display': 'block'}));
					commentFromInit(answer);
					answer.find(".cs-cancel").css({visibility: 'visible'});
					answer.find("textarea").empty();
					b_button_init();
					$('form_comment_0').hide();
					answer.click();
					
					if (lameditor.length > 0) {
						var source = jQuery("div.comments div.cs-form").find("textarea.lameditor")[0];
						var destination = answer.find("form")[0];
						answer.find("div.editor").remove();
						Launcher.clone(source, destination);
					}
				} else {
					$this.next().find("div.cs-form span.cs-cancel").eq(0).click();
				}
			});
}


// Удаляем форму после аякса, передаем сюда указатель DOM на форму
function removeForm(obj){
	var jobj = jQuery(obj);

	if(!jobj.parent().is(".cs-group-main")){
		jobj.parent().prev().removeClass("cs-answered");
		jobj.remove();
	} else {
		jobj.find("div.cs-content").removeAttr("style");
		jobj.removeClass("cs-form-loading");
	}
	
}

jQuery(document).ready(function(){
	// Инициализация формы для оставления комментария
	commentFromInit(jQuery("div.comments div.cs-form"));

	// too slow
	// if(!jQuery.browser.msie){
	// 	jQuery("div.comments div.cs-comment div.cs-answer").css({ opacity: 0 }).show()
	// }
	
	jQuery("div.comments div.cs-comment").each(function(){
		commentInit(this);
	});

	jQuery("div.cs-comment-text").hover(
	  function(){
	    jQuery("div.ru-comment").css({ visibility:"hidden" });
	    jQuery(this).prev().css({ visibility:"visible" });
	  },
	  function(){
	    //jQuery(this).prev().css({ visibility:"hidden" });
	  }
	);

	jQuery("div.cs-group-main").hover(
	  function(){ },
	  function(){
	    jQuery("div.ru-comment").css({ visibility:"hidden" });
	  }
	);
  if (window.location.hash.slice(0, 9) == "#comment_"){
    jQuery(window.location.hash).find(".cs-answer").trigger('click');
  }
});


/**
 * SWFUpload: http://www.swfupload.org, http://swfupload.googlecode.com
 *
 * mmSWFUpload 1.0: Flash upload dialog - http://profandesign.se/swfupload/,  http://www.vinterwebb.se/
 *
 * SWFUpload is (c) 2006-2007 Lars Huring, Olov Nilz�n and Mammon Media and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * SWFUpload 2 is (c) 2007-2008 Jake Roberts and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */


/* ******************* */
/* Constructor & Init  */
/* ******************* */
var SWFUpload;

if (SWFUpload == undefined) {
	SWFUpload = function (settings) {
		this.initSWFUpload(settings);
	};
}

SWFUpload.prototype.initSWFUpload = function (settings) {
	try {
		this.customSettings = {};	// A container where developers can place their own settings associated with this instance.
		this.settings = settings;
		this.eventQueue = [];
		this.movieName = "SWFUpload_" + SWFUpload.movieCount++;
		this.movieElement = null;


		// Setup global control tracking
		SWFUpload.instances[this.movieName] = this;

		// Load the settings.  Load the Flash movie.
		this.initSettings();
		this.loadFlash();
		this.displayDebugInfo();
	} catch (ex) {
		delete SWFUpload.instances[this.movieName];
		throw ex;
	}
};

/* *************** */
/* Static Members  */
/* *************** */
SWFUpload.instances = {};
SWFUpload.movieCount = 1;
SWFUpload.version = "2.2.0 Beta 3";
SWFUpload.QUEUE_ERROR = {
	QUEUE_LIMIT_EXCEEDED	  		: -100,
	FILE_EXCEEDS_SIZE_LIMIT  		: -110,
	ZERO_BYTE_FILE			  		: -120,
	INVALID_FILETYPE		  		: -130
};
SWFUpload.UPLOAD_ERROR = {
	HTTP_ERROR				  		: -200,
	MISSING_UPLOAD_URL	      		: -210,
	IO_ERROR				  		: -220,
	SECURITY_ERROR			  		: -230,
	UPLOAD_LIMIT_EXCEEDED	  		: -240,
	UPLOAD_FAILED			  		: -250,
	SPECIFIED_FILE_ID_NOT_FOUND		: -260,
	FILE_VALIDATION_FAILED	  		: -270,
	FILE_CANCELLED			  		: -280,
	UPLOAD_STOPPED					: -290
};
SWFUpload.FILE_STATUS = {
	QUEUED		 : -1,
	IN_PROGRESS	 : -2,
	ERROR		 : -3,
	COMPLETE	 : -4,
	CANCELLED	 : -5
};
SWFUpload.BUTTON_ACTION = {
	SELECT_FILE  : -100,
	SELECT_FILES : -110,
	START_UPLOAD : -120
};
SWFUpload.CURSOR = {
	ARROW : -1,
	HAND : -2
};
SWFUpload.WINDOW_MODE = {
	WINDOW : "window",
	TRANSPARENT : "transparent",
	OPAQUE : "opaque"
};

/* ******************** */
/* Instance Members  */
/* ******************** */

// Private: initSettings ensures that all the
// settings are set, getting a default value if one was not assigned.
SWFUpload.prototype.initSettings = function () {
	this.ensureDefault = function (settingName, defaultValue) {
		this.settings[settingName] = (this.settings[settingName] == undefined) ? defaultValue : this.settings[settingName];
	};
	
	// Upload backend settings
	this.ensureDefault("upload_url", "");
	this.ensureDefault("file_post_name", "Filedata");
	this.ensureDefault("post_params", {});
	this.ensureDefault("use_query_string", false);
	this.ensureDefault("requeue_on_error", false);
	this.ensureDefault("http_success", []);
	
	// File Settings
	this.ensureDefault("file_types", "*.*");
	this.ensureDefault("file_types_description", "All Files");
	this.ensureDefault("file_size_limit", 0);	// Default zero means "unlimited"
	this.ensureDefault("file_upload_limit", 0);
	this.ensureDefault("file_queue_limit", 0);

	// Flash Settings
	this.ensureDefault("flash_url", "swfupload.swf");
	this.ensureDefault("prevent_swf_caching", true);
	
	// Button Settings
	this.ensureDefault("button_image_url", "");
	this.ensureDefault("button_width", 1);
	this.ensureDefault("button_height", 1);
	this.ensureDefault("button_text", "");
	this.ensureDefault("button_text_style", "color: #000000; font-size: 16pt;");
	this.ensureDefault("button_text_top_padding", 0);
	this.ensureDefault("button_text_left_padding", 0);
	this.ensureDefault("button_action", SWFUpload.BUTTON_ACTION.SELECT_FILES);
	this.ensureDefault("button_disabled", false);
	this.ensureDefault("button_placeholder_id", null);
	this.ensureDefault("button_cursor", SWFUpload.CURSOR.ARROW);
	this.ensureDefault("button_window_mode", SWFUpload.WINDOW_MODE.WINDOW);
	
	// Debug Settings
	this.ensureDefault("debug", false);
	this.settings.debug_enabled = this.settings.debug;	// Here to maintain v2 API
	
	// Event Handlers
	this.settings.return_upload_start_handler = this.returnUploadStart;
	this.ensureDefault("swfupload_loaded_handler", null);
	this.ensureDefault("file_dialog_start_handler", null);
	this.ensureDefault("file_queued_handler", null);
	this.ensureDefault("file_queue_error_handler", null);
	this.ensureDefault("file_dialog_complete_handler", null);
	
	this.ensureDefault("upload_start_handler", null);
	this.ensureDefault("upload_progress_handler", null);
	this.ensureDefault("upload_error_handler", null);
	this.ensureDefault("upload_success_handler", null);
	this.ensureDefault("upload_complete_handler", null);
	
	this.ensureDefault("debug_handler", this.debugMessage);

	this.ensureDefault("custom_settings", {});

	// Other settings
	this.customSettings = this.settings.custom_settings;
	
	// Update the flash url if needed
	if (this.settings.prevent_swf_caching) {
		this.settings.flash_url = this.settings.flash_url + "?swfuploadrnd=" + Math.floor(Math.random() * 999999999);
	}
	
	delete this.ensureDefault;
};

SWFUpload.prototype.loadFlash = function () {
	if (this.settings.button_placeholder_id !== "") {
		this.replaceWithFlash();
	} else {
		this.appendFlash();
	}
};

// Private: appendFlash gets the HTML tag for the Flash
// It then appends the flash to the body
SWFUpload.prototype.appendFlash = function () {
	var targetElement, container;

	// Make sure an element with the ID we are going to use doesn't already exist
	if (document.getElementById(this.movieName) !== null) {
		throw "ID " + this.movieName + " is already in use. The Flash Object could not be added";
	}

	// Get the body tag where we will be adding the flash movie
	targetElement = document.getElementsByTagName("body")[0];

	if (targetElement == undefined) {
		throw "Could not find the 'body' element.";
	}

	// Append the container and load the flash
	container = document.createElement("div");
	container.style.width = "1px";
	container.style.height = "1px";
	container.style.overflow = "hidden";

	targetElement.appendChild(container);
	container.innerHTML = this.getFlashHTML();	// Using innerHTML is non-standard but the only sensible way to dynamically add Flash in IE (and maybe other browsers)

	// Fix IE Flash/Form bug
	if (window[this.movieName] == undefined) {
		window[this.movieName] = this.getMovieElement();
	}
	
	
};

// Private: replaceWithFlash replaces the button_placeholder element with the flash movie.
SWFUpload.prototype.replaceWithFlash = function () {
	var targetElement, tempParent;

	// Make sure an element with the ID we are going to use doesn't already exist
	if (document.getElementById(this.movieName) !== null) {
		throw "ID " + this.movieName + " is already in use. The Flash Object could not be added";
	}

	// Get the element where we will be placing the flash movie
	targetElement = document.getElementById(this.settings.button_placeholder_id);

	if (targetElement == undefined) {
		throw "Could not find the placeholder element.";
	}

	// Append the container and load the flash
	tempParent = document.createElement("div");
	tempParent.innerHTML = this.getFlashHTML();	// Using innerHTML is non-standard but the only sensible way to dynamically add Flash in IE (and maybe other browsers)
	targetElement.parentNode.replaceChild(tempParent.firstChild, targetElement);

	// Fix IE Flash/Form bug
	if (window[this.movieName] == undefined) {
		window[this.movieName] = this.getMovieElement();
	}
	
};

// Private: getFlashHTML generates the object tag needed to embed the flash in to the document
SWFUpload.prototype.getFlashHTML = function () {
	// Flash Satay object syntax: http://www.alistapart.com/articles/flashsatay
	return ['<object id="', this.movieName, '" type="application/x-shockwave-flash" data="', this.settings.flash_url, '" width="', this.settings.button_width, '" height="', this.settings.button_height, '" class="swfupload">',
				'<param name="wmode" value="', this.settings.button_window_mode , '" />',
				'<param name="movie" value="', this.settings.flash_url, '" />',
				'<param name="quality" value="high" />',
				'<param name="menu" value="false" />',
				'<param name="allowScriptAccess" value="always" />',
				'<param name="flashvars" value="' + this.getFlashVars() + '" />',
				'</object>'].join("");
};

// Private: getFlashVars builds the parameter string that will be passed
// to flash in the flashvars param.
SWFUpload.prototype.getFlashVars = function () {
	// Build a string from the post param object
	var paramString = this.buildParamString();
	var httpSuccessString = this.settings.http_success.join(",");
	
	// Build the parameter string
	return ["movieName=", encodeURIComponent(this.movieName),
			"&amp;uploadURL=", encodeURIComponent(this.settings.upload_url),
			"&amp;useQueryString=", encodeURIComponent(this.settings.use_query_string),
			"&amp;requeueOnError=", encodeURIComponent(this.settings.requeue_on_error),
			"&amp;httpSuccess=", encodeURIComponent(httpSuccessString),
			"&amp;params=", encodeURIComponent(paramString),
			"&amp;filePostName=", encodeURIComponent(this.settings.file_post_name),
			"&amp;fileTypes=", encodeURIComponent(this.settings.file_types),
			"&amp;fileTypesDescription=", encodeURIComponent(this.settings.file_types_description),
			"&amp;fileSizeLimit=", encodeURIComponent(this.settings.file_size_limit),
			"&amp;fileUploadLimit=", encodeURIComponent(this.settings.file_upload_limit),
			"&amp;fileQueueLimit=", encodeURIComponent(this.settings.file_queue_limit),
			"&amp;debugEnabled=", encodeURIComponent(this.settings.debug_enabled),
			"&amp;buttonImageURL=", encodeURIComponent(this.settings.button_image_url),
			"&amp;buttonWidth=", encodeURIComponent(this.settings.button_width),
			"&amp;buttonHeight=", encodeURIComponent(this.settings.button_height),
			"&amp;buttonText=", encodeURIComponent(this.settings.button_text),
			"&amp;buttonTextTopPadding=", encodeURIComponent(this.settings.button_text_top_padding),
			"&amp;buttonTextLeftPadding=", encodeURIComponent(this.settings.button_text_left_padding),
			"&amp;buttonTextStyle=", encodeURIComponent(this.settings.button_text_style),
			"&amp;buttonAction=", encodeURIComponent(this.settings.button_action),
			"&amp;buttonDisabled=", encodeURIComponent(this.settings.button_disabled),
			"&amp;buttonCursor=", encodeURIComponent(this.settings.button_cursor)
		].join("");
};

// Public: getMovieElement retrieves the DOM reference to the Flash element added by SWFUpload
// The element is cached after the first lookup
SWFUpload.prototype.getMovieElement = function () {
	if (this.movieElement == undefined) {
		this.movieElement = document.getElementById(this.movieName);
	}

	if (this.movieElement === null) {
		throw "Could not find Flash element";
	}
	
	return this.movieElement;
};

// Private: buildParamString takes the name/value pairs in the post_params setting object
// and joins them up in to a string formatted "name=value&amp;name=value"
SWFUpload.prototype.buildParamString = function () {
	var postParams = this.settings.post_params; 
	var paramStringPairs = [];

	if (typeof(postParams) === "object") {
		for (var name in postParams) {
			if (postParams.hasOwnProperty(name)) {
				paramStringPairs.push(encodeURIComponent(name.toString()) + "=" + encodeURIComponent(postParams[name].toString()));
			}
		}
	}

	return paramStringPairs.join("&amp;");
};

// Public: Used to remove a SWFUpload instance from the page. This method strives to remove
// all references to the SWF, and other objects so memory is properly freed.
// Returns true if everything was destroyed. Returns a false if a failure occurs leaving SWFUpload in an inconsistant state.
// Credits: Major improvements provided by steffen
SWFUpload.prototype.destroy = function () {
	try {
		// Make sure Flash is done before we try to remove it
		this.cancelUpload(null, false);
		
		// Remove the SWFUpload DOM nodes
		var movieElement = null;
		movieElement = this.getMovieElement();
		
		if (movieElement) {
			// Loop through all the movie's properties and remove all function references (DOM/JS IE 6/7 memory leak workaround)
			for (var i in movieElement) {
				try {
					if (typeof(movieElement[i]) === "function") {
						movieElement[i] = null;
					}
				} catch (ex1) {}
			}

			// Remove the Movie Element from the page
			try {
				movieElement.parentNode.removeChild(movieElement);
			} catch (ex) {}
		}
		
		
		// Remove IE form fix reference
		window[this.movieName] = null;

		// Destroy other references
		SWFUpload.instances[this.movieName] = null;
		delete SWFUpload.instances[this.movieName];

		this.movieElement = null;
		this.settings = null;
		this.customSettings = null;
		this.eventQueue = null;
		this.movieName = null;
		
		
		return true;
	} catch (ex1) {
		return false;
	}
};

// Public: displayDebugInfo prints out settings and configuration
// information about this SWFUpload instance.
// This function (and any references to it) can be deleted when placing
// SWFUpload in production.
SWFUpload.prototype.displayDebugInfo = function () {
	this.debug(
		[
			"---SWFUpload Instance Info---\n",
			"Version: ", SWFUpload.version, "\n",
			"Movie Name: ", this.movieName, "\n",
			"Settings:\n",
			"\t", "upload_url:               ", this.settings.upload_url, "\n",
			"\t", "flash_url:                ", this.settings.flash_url, "\n",
			"\t", "use_query_string:         ", this.settings.use_query_string.toString(), "\n",
			"\t", "requeue_on_error:         ", this.settings.requeue_on_error.toString(), "\n",
			"\t", "http_success:             ", this.settings.http_success.join(", "), "\n",
			"\t", "file_post_name:           ", this.settings.file_post_name, "\n",
			"\t", "post_params:              ", this.settings.post_params.toString(), "\n",
			"\t", "file_types:               ", this.settings.file_types, "\n",
			"\t", "file_types_description:   ", this.settings.file_types_description, "\n",
			"\t", "file_size_limit:          ", this.settings.file_size_limit, "\n",
			"\t", "file_upload_limit:        ", this.settings.file_upload_limit, "\n",
			"\t", "file_queue_limit:         ", this.settings.file_queue_limit, "\n",
			"\t", "debug:                    ", this.settings.debug.toString(), "\n",

			"\t", "prevent_swf_caching:      ", this.settings.prevent_swf_caching.toString(), "\n",

			"\t", "button_placeholder_id:    ", this.settings.button_placeholder_id.toString(), "\n",
			"\t", "button_image_url:         ", this.settings.button_image_url.toString(), "\n",
			"\t", "button_width:             ", this.settings.button_width.toString(), "\n",
			"\t", "button_height:            ", this.settings.button_height.toString(), "\n",
			"\t", "button_text:              ", this.settings.button_text.toString(), "\n",
			"\t", "button_text_style:        ", this.settings.button_text_style.toString(), "\n",
			"\t", "button_text_top_padding:  ", this.settings.button_text_top_padding.toString(), "\n",
			"\t", "button_text_left_padding: ", this.settings.button_text_left_padding.toString(), "\n",
			"\t", "button_action:            ", this.settings.button_action.toString(), "\n",
			"\t", "button_disabled:          ", this.settings.button_disabled.toString(), "\n",

			"\t", "custom_settings:          ", this.settings.custom_settings.toString(), "\n",
			"Event Handlers:\n",
			"\t", "swfupload_loaded_handler assigned:  ", (typeof this.settings.swfupload_loaded_handler === "function").toString(), "\n",
			"\t", "file_dialog_start_handler assigned: ", (typeof this.settings.file_dialog_start_handler === "function").toString(), "\n",
			"\t", "file_queued_handler assigned:       ", (typeof this.settings.file_queued_handler === "function").toString(), "\n",
			"\t", "file_queue_error_handler assigned:  ", (typeof this.settings.file_queue_error_handler === "function").toString(), "\n",
			"\t", "upload_start_handler assigned:      ", (typeof this.settings.upload_start_handler === "function").toString(), "\n",
			"\t", "upload_progress_handler assigned:   ", (typeof this.settings.upload_progress_handler === "function").toString(), "\n",
			"\t", "upload_error_handler assigned:      ", (typeof this.settings.upload_error_handler === "function").toString(), "\n",
			"\t", "upload_success_handler assigned:    ", (typeof this.settings.upload_success_handler === "function").toString(), "\n",
			"\t", "upload_complete_handler assigned:   ", (typeof this.settings.upload_complete_handler === "function").toString(), "\n",
			"\t", "debug_handler assigned:             ", (typeof this.settings.debug_handler === "function").toString(), "\n"
		].join("")
	);
};

/* Note: addSetting and getSetting are no longer used by SWFUpload but are included
	the maintain v2 API compatibility
*/
// Public: (Deprecated) addSetting adds a setting value. If the value given is undefined or null then the default_value is used.
SWFUpload.prototype.addSetting = function (name, value, default_value) {
    if (value == undefined) {
        return (this.settings[name] = default_value);
    } else {
        return (this.settings[name] = value);
	}
};

// Public: (Deprecated) getSetting gets a setting. Returns an empty string if the setting was not found.
SWFUpload.prototype.getSetting = function (name) {
    if (this.settings[name] != undefined) {
        return this.settings[name];
	}

    return "";
};



// Private: callFlash handles function calls made to the Flash element.
// Calls are made with a setTimeout for some functions to work around
// bugs in the ExternalInterface library.
SWFUpload.prototype.callFlash = function (functionName, argumentArray) {
	argumentArray = argumentArray || [];
	
	var movieElement = this.getMovieElement();
	var returnValue, returnString;

	// Flash's method if calling ExternalInterface methods (code adapted from MooTools).
	try {
		returnString = movieElement.CallFunction('<invoke name="' + functionName + '" returntype="javascript">' + __flash__argumentsToXML(argumentArray, 0) + '</invoke>');
		returnValue = eval(returnString);
	} catch (ex) {
		throw "Call to " + functionName + " failed";
	}
	
	// Unescape file post param values
	if (returnValue != undefined && typeof returnValue.post === "object") {
		returnValue = this.unescapeFilePostParams(returnValue);
	}

	return returnValue;
};


/* *****************************
	-- Flash control methods --
	Your UI should use these
	to operate SWFUpload
   ***************************** */

// WARNING: this function does not work in Flash Player 10
// Public: selectFile causes a File Selection Dialog window to appear.  This
// dialog only allows 1 file to be selected.
SWFUpload.prototype.selectFile = function () {
	this.callFlash("SelectFile");
};

// WARNING: this function does not work in Flash Player 10
// Public: selectFiles causes a File Selection Dialog window to appear/ This
// dialog allows the user to select any number of files
// Flash Bug Warning: Flash limits the number of selectable files based on the combined length of the file names.
// If the selection name length is too long the dialog will fail in an unpredictable manner.  There is no work-around
// for this bug.
SWFUpload.prototype.selectFiles = function () {
	this.callFlash("SelectFiles");
};


// Public: startUpload starts uploading the first file in the queue unless
// the optional parameter 'fileID' specifies the ID 
SWFUpload.prototype.startUpload = function (fileID) {
	this.callFlash("StartUpload", [fileID]);
};

// Public: cancelUpload cancels any queued file.  The fileID parameter may be the file ID or index.
// If you do not specify a fileID the current uploading file or first file in the queue is cancelled.
// If you do not want the uploadError event to trigger you can specify false for the triggerErrorEvent parameter.
SWFUpload.prototype.cancelUpload = function (fileID, triggerErrorEvent) {
	if (triggerErrorEvent !== false) {
		triggerErrorEvent = true;
	}
	this.callFlash("CancelUpload", [fileID, triggerErrorEvent]);
};

// Public: stopUpload stops the current upload and requeues the file at the beginning of the queue.
// If nothing is currently uploading then nothing happens.
SWFUpload.prototype.stopUpload = function () {
	this.callFlash("StopUpload");
};

/* ************************
 * Settings methods
 *   These methods change the SWFUpload settings.
 *   SWFUpload settings should not be changed directly on the settings object
 *   since many of the settings need to be passed to Flash in order to take
 *   effect.
 * *********************** */

// Public: getStats gets the file statistics object.
SWFUpload.prototype.getStats = function () {
	return this.callFlash("GetStats");
};

// Public: setStats changes the SWFUpload statistics.  You shouldn't need to 
// change the statistics but you can.  Changing the statistics does not
// affect SWFUpload accept for the successful_uploads count which is used
// by the upload_limit setting to determine how many files the user may upload.
SWFUpload.prototype.setStats = function (statsObject) {
	this.callFlash("SetStats", [statsObject]);
};

// Public: getFile retrieves a File object by ID or Index.  If the file is
// not found then 'null' is returned.
SWFUpload.prototype.getFile = function (fileID) {
	if (typeof(fileID) === "number") {
		return this.callFlash("GetFileByIndex", [fileID]);
	} else {
		return this.callFlash("GetFile", [fileID]);
	}
};

// Public: addFileParam sets a name/value pair that will be posted with the
// file specified by the Files ID.  If the name already exists then the
// exiting value will be overwritten.
SWFUpload.prototype.addFileParam = function (fileID, name, value) {
	return this.callFlash("AddFileParam", [fileID, name, value]);
};

// Public: removeFileParam removes a previously set (by addFileParam) name/value
// pair from the specified file.
SWFUpload.prototype.removeFileParam = function (fileID, name) {
	this.callFlash("RemoveFileParam", [fileID, name]);
};

// Public: setUploadUrl changes the upload_url setting.
SWFUpload.prototype.setUploadURL = function (url) {
	this.settings.upload_url = url.toString();
	this.callFlash("SetUploadURL", [url]);
};

// Public: setPostParams changes the post_params setting
SWFUpload.prototype.setPostParams = function (paramsObject) {
	this.settings.post_params = paramsObject;
	this.callFlash("SetPostParams", [paramsObject]);
};

// Public: addPostParam adds post name/value pair.  Each name can have only one value.
SWFUpload.prototype.addPostParam = function (name, value) {
	this.settings.post_params[name] = value;
	this.callFlash("SetPostParams", [this.settings.post_params]);
};

// Public: removePostParam deletes post name/value pair.
SWFUpload.prototype.removePostParam = function (name) {
	delete this.settings.post_params[name];
	this.callFlash("SetPostParams", [this.settings.post_params]);
};

// Public: setFileTypes changes the file_types setting and the file_types_description setting
SWFUpload.prototype.setFileTypes = function (types, description) {
	this.settings.file_types = types;
	this.settings.file_types_description = description;
	this.callFlash("SetFileTypes", [types, description]);
};

// Public: setFileSizeLimit changes the file_size_limit setting
SWFUpload.prototype.setFileSizeLimit = function (fileSizeLimit) {
	this.settings.file_size_limit = fileSizeLimit;
	this.callFlash("SetFileSizeLimit", [fileSizeLimit]);
};

// Public: setFileUploadLimit changes the file_upload_limit setting
SWFUpload.prototype.setFileUploadLimit = function (fileUploadLimit) {
	this.settings.file_upload_limit = fileUploadLimit;
	this.callFlash("SetFileUploadLimit", [fileUploadLimit]);
};

// Public: setFileQueueLimit changes the file_queue_limit setting
SWFUpload.prototype.setFileQueueLimit = function (fileQueueLimit) {
	this.settings.file_queue_limit = fileQueueLimit;
	this.callFlash("SetFileQueueLimit", [fileQueueLimit]);
};

// Public: setFilePostName changes the file_post_name setting
SWFUpload.prototype.setFilePostName = function (filePostName) {
	this.settings.file_post_name = filePostName;
	this.callFlash("SetFilePostName", [filePostName]);
};

// Public: setUseQueryString changes the use_query_string setting
SWFUpload.prototype.setUseQueryString = function (useQueryString) {
	this.settings.use_query_string = useQueryString;
	this.callFlash("SetUseQueryString", [useQueryString]);
};

// Public: setRequeueOnError changes the requeue_on_error setting
SWFUpload.prototype.setRequeueOnError = function (requeueOnError) {
	this.settings.requeue_on_error = requeueOnError;
	this.callFlash("SetRequeueOnError", [requeueOnError]);
};

// Public: setHTTPSuccess changes the http_success setting
SWFUpload.prototype.setHTTPSuccess = function (http_status_codes) {
	if (typeof http_status_codes === "string") {
		http_status_codes = http_status_codes.replace(" ", "").split(",");
	}
	
	this.settings.http_success = http_status_codes;
	this.callFlash("SetHTTPSuccess", [http_status_codes]);
};


// Public: setDebugEnabled changes the debug_enabled setting
SWFUpload.prototype.setDebugEnabled = function (debugEnabled) {
	this.settings.debug_enabled = debugEnabled;
	this.callFlash("SetDebugEnabled", [debugEnabled]);
};

// Public: setButtonImageURL loads a button image sprite
SWFUpload.prototype.setButtonImageURL = function (buttonImageURL) {
	if (buttonImageURL == undefined) {
		buttonImageURL = "";
	}
	
	this.settings.button_image_url = buttonImageURL;
	this.callFlash("SetButtonImageURL", [buttonImageURL]);
};

// Public: setButtonDimensions resizes the Flash Movie and button
SWFUpload.prototype.setButtonDimensions = function (width, height) {
	this.settings.button_width = width;
	this.settings.button_height = height;
	
	var movie = this.getMovieElement();
	if (movie != undefined) {
		movie.style.width = width + "px";
		movie.style.height = height + "px";
	}
	
	this.callFlash("SetButtonDimensions", [width, height]);
};
// Public: setButtonText Changes the text overlaid on the button
SWFUpload.prototype.setButtonText = function (html) {
	this.settings.button_text = html;
	this.callFlash("SetButtonText", [html]);
};
// Public: setButtonTextPadding changes the top and left padding of the text overlay
SWFUpload.prototype.setButtonTextPadding = function (left, top) {
	this.settings.button_text_top_padding = top;
	this.settings.button_text_left_padding = left;
	this.callFlash("SetButtonTextPadding", [left, top]);
};

// Public: setButtonTextStyle changes the CSS used to style the HTML/Text overlaid on the button
SWFUpload.prototype.setButtonTextStyle = function (css) {
	this.settings.button_text_style = css;
	this.callFlash("SetButtonTextStyle", [css]);
};
// Public: setButtonDisabled disables/enables the button
SWFUpload.prototype.setButtonDisabled = function (isDisabled) {
	this.settings.button_disabled = isDisabled;
	this.callFlash("SetButtonDisabled", [isDisabled]);
};
// Public: setButtonAction sets the action that occurs when the button is clicked
SWFUpload.prototype.setButtonAction = function (buttonAction) {
	this.settings.button_action = buttonAction;
	this.callFlash("SetButtonAction", [buttonAction]);
};

// Public: setButtonCursor changes the mouse cursor displayed when hovering over the button
SWFUpload.prototype.setButtonCursor = function (cursor) {
	this.settings.button_cursor = cursor;
	this.callFlash("SetButtonCursor", [cursor]);
};

/* *******************************
	Flash Event Interfaces
	These functions are used by Flash to trigger the various
	events.
	
	All these functions a Private.
	
	Because the ExternalInterface library is buggy the event calls
	are added to a queue and the queue then executed by a setTimeout.
	This ensures that events are executed in a determinate order and that
	the ExternalInterface bugs are avoided.
******************************* */

SWFUpload.prototype.queueEvent = function (handlerName, argumentArray) {
	// Warning: Don't call this.debug inside here or you'll create an infinite loop
	
	if (argumentArray == undefined) {
		argumentArray = [];
	} else if (!(argumentArray instanceof Array)) {
		argumentArray = [argumentArray];
	}
	
	var self = this;
	if (typeof this.settings[handlerName] === "function") {
		// Queue the event
		this.eventQueue.push(function () {
			this.settings[handlerName].apply(this, argumentArray);
		});
		
		// Execute the next queued event
		setTimeout(function () {
			self.executeNextEvent();
		}, 0);
		
	} else if (this.settings[handlerName] !== null) {
		throw "Event handler " + handlerName + " is unknown or is not a function";
	}
};

// Private: Causes the next event in the queue to be executed.  Since events are queued using a setTimeout
// we must queue them in order to garentee that they are executed in order.
SWFUpload.prototype.executeNextEvent = function () {
	// Warning: Don't call this.debug inside here or you'll create an infinite loop

	var  f = this.eventQueue ? this.eventQueue.shift() : null;
	if (typeof(f) === "function") {
		f.apply(this);
	}
};

// Private: unescapeFileParams is part of a workaround for a flash bug where objects passed through ExternalInterface cannot have
// properties that contain characters that are not valid for JavaScript identifiers. To work around this
// the Flash Component escapes the parameter names and we must unescape again before passing them along.
SWFUpload.prototype.unescapeFilePostParams = function (file) {
	var reg = /[$]([0-9a-f]{4})/i;
	var unescapedPost = {};
	var uk;

	if (file != undefined) {
		for (var k in file.post) {
			if (file.post.hasOwnProperty(k)) {
				uk = k;
				var match;
				while ((match = reg.exec(uk)) !== null) {
					uk = uk.replace(match[0], String.fromCharCode(parseInt("0x" + match[1], 16)));
				}
				unescapedPost[uk] = file.post[k];
			}
		}

		file.post = unescapedPost;
	}

	return file;
};

SWFUpload.prototype.flashReady = function () {
	// Check that the movie element is loaded correctly with its ExternalInterface methods defined
	var movieElement = this.getMovieElement();

	// Pro-actively unhook all the Flash functions
	if (typeof(movieElement.CallFunction) === "unknown") { // We only want to do this in IE
		this.debug("Removing Flash functions hooks (this should only run in IE and should prevent memory leaks)");
		for (var key in movieElement) {
			try {
				if (typeof(movieElement[key]) === "function") {
					movieElement[key] = null;
				}
			} catch (ex) {
			}
		}
	}
	
	this.queueEvent("swfupload_loaded_handler");
};


/* This is a chance to do something before the browse window opens */
SWFUpload.prototype.fileDialogStart = function () {
	this.queueEvent("file_dialog_start_handler");
};


/* Called when a file is successfully added to the queue. */
SWFUpload.prototype.fileQueued = function (file) {
	file = this.unescapeFilePostParams(file);
	this.queueEvent("file_queued_handler", file);
};


/* Handle errors that occur when an attempt to queue a file fails. */
SWFUpload.prototype.fileQueueError = function (file, errorCode, message) {
	file = this.unescapeFilePostParams(file);
	this.queueEvent("file_queue_error_handler", [file, errorCode, message]);
};

/* Called after the file dialog has closed and the selected files have been queued.
	You could call startUpload here if you want the queued files to begin uploading immediately. */
SWFUpload.prototype.fileDialogComplete = function (numFilesSelected, numFilesQueued) {
	this.queueEvent("file_dialog_complete_handler", [numFilesSelected, numFilesQueued]);
};

SWFUpload.prototype.uploadStart = function (file) {
	file = this.unescapeFilePostParams(file);
	this.queueEvent("return_upload_start_handler", file);
};

SWFUpload.prototype.returnUploadStart = function (file) {
	var returnValue;
	if (typeof this.settings.upload_start_handler === "function") {
		file = this.unescapeFilePostParams(file);
		returnValue = this.settings.upload_start_handler.call(this, file);
	} else if (this.settings.upload_start_handler != undefined) {
		throw "upload_start_handler must be a function";
	}

	// Convert undefined to true so if nothing is returned from the upload_start_handler it is
	// interpretted as 'true'.
	if (returnValue === undefined) {
		returnValue = true;
	}
	
	returnValue = !!returnValue;
	
	this.callFlash("ReturnUploadStart", [returnValue]);
};



SWFUpload.prototype.uploadProgress = function (file, bytesComplete, bytesTotal) {
	file = this.unescapeFilePostParams(file);
	this.queueEvent("upload_progress_handler", [file, bytesComplete, bytesTotal]);
};

SWFUpload.prototype.uploadError = function (file, errorCode, message) {
	file = this.unescapeFilePostParams(file);
	this.queueEvent("upload_error_handler", [file, errorCode, message]);
};

SWFUpload.prototype.uploadSuccess = function (file, serverData) {
	file = this.unescapeFilePostParams(file);
	this.queueEvent("upload_success_handler", [file, serverData]);
};

SWFUpload.prototype.uploadComplete = function (file) {
	file = this.unescapeFilePostParams(file);
	this.queueEvent("upload_complete_handler", file);
};

/* Called by SWFUpload JavaScript and Flash functions when debug is enabled. By default it writes messages to the
   internal debug console.  You can override this event and have messages written where you want. */
SWFUpload.prototype.debug = function (message) {
	this.queueEvent("debug_handler", message);
};


/* **********************************
	Debug Console
	The debug console is a self contained, in page location
	for debug message to be sent.  The Debug Console adds
	itself to the body if necessary.

	The console is automatically scrolled as messages appear.
	
	If you are using your own debug handler or when you deploy to production and
	have debug disabled you can remove these functions to reduce the file size
	and complexity.
********************************** */
   
// Private: debugMessage is the default debug_handler.  If you want to print debug messages
// call the debug() function.  When overriding the function your own function should
// check to see if the debug setting is true before outputting debug information.
SWFUpload.prototype.debugMessage = function (message) {
	if (this.settings.debug) {
		var exceptionMessage, exceptionValues = [];

		// Check for an exception object and print it nicely
		if (typeof message === "object" && typeof message.name === "string" && typeof message.message === "string") {
			for (var key in message) {
				if (message.hasOwnProperty(key)) {
					exceptionValues.push(key + ": " + message[key]);
				}
			}
			exceptionMessage = exceptionValues.join("\n") || "";
			exceptionValues = exceptionMessage.split("\n");
			exceptionMessage = "EXCEPTION: " + exceptionValues.join("\nEXCEPTION: ");
			SWFUpload.Console.writeLine(exceptionMessage);
		} else {
			SWFUpload.Console.writeLine(message);
		}
	}
};

SWFUpload.Console = {};
SWFUpload.Console.writeLine = function (message) {
	var console, documentForm;

	try {
		console = document.getElementById("SWFUpload_Console");

		if (!console) {
			documentForm = document.createElement("form");
			document.getElementsByTagName("body")[0].appendChild(documentForm);

			console = document.createElement("textarea");
			console.id = "SWFUpload_Console";
			console.style.fontFamily = "monospace";
			console.setAttribute("wrap", "off");
			console.wrap = "off";
			console.style.overflow = "auto";
			console.style.width = "700px";
			console.style.height = "350px";
			console.style.margin = "5px";
			documentForm.appendChild(console);
		}

		console.value += message + "\n";

		console.scrollTop = console.scrollHeight - console.clientHeight;
	} catch (ex) {
		alert("Exception: " + ex.name + " Message: " + ex.message);
	}
};


/*global jQuery, document, setTimeout */

function Gallery(){

}

Gallery.prototype = {
	init:function(){
		this.parent = jQuery('div.right-column div.photo-previews');
		this.parentCenter = this.parent.width()/2;
		this.moved = this.parent.find('div.inner');
		this.leftArr = this.parent.find('div.left-arr');
		this.rightArr = this.parent.find('div.right-arr');
		
		this.items = this.moved.find('div.item');
		this.itemWidth = this.items.eq(0).width();
		this.itemCenter = this.itemWidth/2+8;
		this.aniFlag = true;
		this.moved.width(this.items.length*(this.itemWidth+16));
		for(var i=0; i<this.items.length; i++){
			if(this.items.eq(i).is('.active')){
				var act = i;
				this.checkLims(act);
				this.show(act);
				break;
			}
		}
		this.attachEvents();
	},
	checkLims:function(act){
	  var lLim, rLim;
		if(act>=4){
			lLim = act - 4;
		} else {
			lLim = 0;
		}
		if(act+4<=this.items.length){
			rLim = act + 4;
		} else {
			rLim = this.items.length;
		}
		for(var j=lLim; j<rLim; j++){
			// this.items.eq(j).find('img').css({display:'block'});
		}
	},
	attachEvents:function(){
		var _my = this;
		this.leftArr.mousedown(function(){
			_my.change(-1);
			return false;
		});
		this.rightArr.mousedown(function(){
			_my.change(1);
			return false;
		});
	},
	change:function(dur){
		var _my = this;
		if(this.aniFlag === true){
			for(var i=0; i<this.items.length; i++){
				if(this.items.eq(i).is('.current')){
					var j = i;
					break;
				}
			}

			if((j+dur) >=0 && (j+dur) < this.items.length){
				if(dur === -1){
					this.leftArr.addClass('arr-act');
					setTimeout(function(){
						_my.leftArr.removeClass('arr-act');
					}, 150);
				} else {
					this.rightArr.addClass('arr-act');
					setTimeout(function(){
						_my.rightArr.removeClass('arr-act');
					}, 150);
				}
				this.items.eq(j).removeClass('current');
				this.show(j+dur);
			}
		}
	},
	show:function(act){
		var _my = this;
		if(this.aniFlag === true){
			this.aniFlag = false;
			this.checkLims(act);
			var left = Math.round(-this.items.eq(act).get(0).offsetLeft-this.itemCenter+this.parentCenter);
			this.moved.animate({left:left}, 250, 'easeOutQuad', function(){
				_my.aniFlag = true;
				_my.items.eq(act).addClass('current');
			});
		}
	}
};


/* SWFObject v2.1 <http://code.google.com/p/swfobject/>
	Copyright (c) 2007-2008 Geoff Stearns, Michael Williams, and Bobby van der Sluis
	This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
var swfobject=function(){var b="undefined",Q="object",n="Shockwave Flash",p="ShockwaveFlash.ShockwaveFlash",P="application/x-shockwave-flash",m="SWFObjectExprInst",j=window,K=document,T=navigator,o=[],N=[],i=[],d=[],J,Z=null,M=null,l=null,e=false,A=false;var h=function(){var v=typeof K.getElementById!=b&&typeof K.getElementsByTagName!=b&&typeof K.createElement!=b,AC=[0,0,0],x=null;if(typeof T.plugins!=b&&typeof T.plugins[n]==Q){x=T.plugins[n].description;if(x&&!(typeof T.mimeTypes!=b&&T.mimeTypes[P]&&!T.mimeTypes[P].enabledPlugin)){x=x.replace(/^.*\s+(\S+\s+\S+$)/,"$1");AC[0]=parseInt(x.replace(/^(.*)\..*$/,"$1"),10);AC[1]=parseInt(x.replace(/^.*\.(.*)\s.*$/,"$1"),10);AC[2]=/r/.test(x)?parseInt(x.replace(/^.*r(.*)$/,"$1"),10):0}}else{if(typeof j.ActiveXObject!=b){var y=null,AB=false;try{y=new ActiveXObject(p+".7")}catch(t){try{y=new ActiveXObject(p+".6");AC=[6,0,21];y.AllowScriptAccess="always"}catch(t){if(AC[0]==6){AB=true}}if(!AB){try{y=new ActiveXObject(p)}catch(t){}}}if(!AB&&y){try{x=y.GetVariable("$version");if(x){x=x.split(" ")[1].split(",");AC=[parseInt(x[0],10),parseInt(x[1],10),parseInt(x[2],10)]}}catch(t){}}}}var AD=T.userAgent.toLowerCase(),r=T.platform.toLowerCase(),AA=/webkit/.test(AD)?parseFloat(AD.replace(/^.*webkit\/(\d+(\.\d+)?).*$/,"$1")):false,q=false,z=r?/win/.test(r):/win/.test(AD),w=r?/mac/.test(r):/mac/.test(AD);/*@cc_on q=true;@if(@_win32)z=true;@elif(@_mac)w=true;@end@*/return{w3cdom:v,pv:AC,webkit:AA,ie:q,win:z,mac:w}}();var L=function(){if(!h.w3cdom){return }f(H);if(h.ie&&h.win){try{K.write("<script id=__ie_ondomload defer=true src=//:><\/script>");J=C("__ie_ondomload");if(J){I(J,"onreadystatechange",S)}}catch(q){}}if(h.webkit&&typeof K.readyState!=b){Z=setInterval(function(){if(/loaded|complete/.test(K.readyState)){E()}},10)}if(typeof K.addEventListener!=b){K.addEventListener("DOMContentLoaded",E,null)}R(E)}();function S(){if(J.readyState=="complete"){J.parentNode.removeChild(J);E()}}function E(){if(e){return }if(h.ie&&h.win){var v=a("span");try{var u=K.getElementsByTagName("body")[0].appendChild(v);u.parentNode.removeChild(u)}catch(w){return }}e=true;if(Z){clearInterval(Z);Z=null}var q=o.length;for(var r=0;r<q;r++){o[r]()}}function f(q){if(e){q()}else{o[o.length]=q}}function R(r){if(typeof j.addEventListener!=b){j.addEventListener("load",r,false)}else{if(typeof K.addEventListener!=b){K.addEventListener("load",r,false)}else{if(typeof j.attachEvent!=b){I(j,"onload",r)}else{if(typeof j.onload=="function"){var q=j.onload;j.onload=function(){q();r()}}else{j.onload=r}}}}}function H(){var t=N.length;for(var q=0;q<t;q++){var u=N[q].id;if(h.pv[0]>0){var r=C(u);if(r){N[q].width=r.getAttribute("width")?r.getAttribute("width"):"0";N[q].height=r.getAttribute("height")?r.getAttribute("height"):"0";if(c(N[q].swfVersion)){if(h.webkit&&h.webkit<312){Y(r)}W(u,true)}else{if(N[q].expressInstall&&!A&&c("6.0.65")&&(h.win||h.mac)){k(N[q])}else{O(r)}}}}else{W(u,true)}}}function Y(t){var q=t.getElementsByTagName(Q)[0];if(q){var w=a("embed"),y=q.attributes;if(y){var v=y.length;for(var u=0;u<v;u++){if(y[u].nodeName=="DATA"){w.setAttribute("src",y[u].nodeValue)}else{w.setAttribute(y[u].nodeName,y[u].nodeValue)}}}var x=q.childNodes;if(x){var z=x.length;for(var r=0;r<z;r++){if(x[r].nodeType==1&&x[r].nodeName=="PARAM"){w.setAttribute(x[r].getAttribute("name"),x[r].getAttribute("value"))}}}t.parentNode.replaceChild(w,t)}}function k(w){A=true;var u=C(w.id);if(u){if(w.altContentId){var y=C(w.altContentId);if(y){M=y;l=w.altContentId}}else{M=G(u)}if(!(/%$/.test(w.width))&&parseInt(w.width,10)<310){w.width="310"}if(!(/%$/.test(w.height))&&parseInt(w.height,10)<137){w.height="137"}K.title=K.title.slice(0,47)+" - Flash Player Installation";var z=h.ie&&h.win?"ActiveX":"PlugIn",q=K.title,r="MMredirectURL="+j.location+"&MMplayerType="+z+"&MMdoctitle="+q,x=w.id;if(h.ie&&h.win&&u.readyState!=4){var t=a("div");x+="SWFObjectNew";t.setAttribute("id",x);u.parentNode.insertBefore(t,u);u.style.display="none";var v=function(){u.parentNode.removeChild(u)};I(j,"onload",v)}U({data:w.expressInstall,id:m,width:w.width,height:w.height},{flashvars:r},x)}}function O(t){if(h.ie&&h.win&&t.readyState!=4){var r=a("div");t.parentNode.insertBefore(r,t);r.parentNode.replaceChild(G(t),r);t.style.display="none";var q=function(){t.parentNode.removeChild(t)};I(j,"onload",q)}else{t.parentNode.replaceChild(G(t),t)}}function G(v){var u=a("div");if(h.win&&h.ie){u.innerHTML=v.innerHTML}else{var r=v.getElementsByTagName(Q)[0];if(r){var w=r.childNodes;if(w){var q=w.length;for(var t=0;t<q;t++){if(!(w[t].nodeType==1&&w[t].nodeName=="PARAM")&&!(w[t].nodeType==8)){u.appendChild(w[t].cloneNode(true))}}}}}return u}function U(AG,AE,t){var q,v=C(t);if(v){if(typeof AG.id==b){AG.id=t}if(h.ie&&h.win){var AF="";for(var AB in AG){if(AG[AB]!=Object.prototype[AB]){if(AB.toLowerCase()=="data"){AE.movie=AG[AB]}else{if(AB.toLowerCase()=="styleclass"){AF+=' class="'+AG[AB]+'"'}else{if(AB.toLowerCase()!="classid"){AF+=" "+AB+'="'+AG[AB]+'"'}}}}}var AD="";for(var AA in AE){if(AE[AA]!=Object.prototype[AA]){AD+='<param name="'+AA+'" value="'+AE[AA]+'" />'}}v.outerHTML='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+AF+">"+AD+"</object>";i[i.length]=AG.id;q=C(AG.id)}else{if(h.webkit&&h.webkit<312){var AC=a("embed");AC.setAttribute("type",P);for(var z in AG){if(AG[z]!=Object.prototype[z]){if(z.toLowerCase()=="data"){AC.setAttribute("src",AG[z])}else{if(z.toLowerCase()=="styleclass"){AC.setAttribute("class",AG[z])}else{if(z.toLowerCase()!="classid"){AC.setAttribute(z,AG[z])}}}}}for(var y in AE){if(AE[y]!=Object.prototype[y]){if(y.toLowerCase()!="movie"){AC.setAttribute(y,AE[y])}}}v.parentNode.replaceChild(AC,v);q=AC}else{var u=a(Q);u.setAttribute("type",P);for(var x in AG){if(AG[x]!=Object.prototype[x]){if(x.toLowerCase()=="styleclass"){u.setAttribute("class",AG[x])}else{if(x.toLowerCase()!="classid"){u.setAttribute(x,AG[x])}}}}for(var w in AE){if(AE[w]!=Object.prototype[w]&&w.toLowerCase()!="movie"){F(u,w,AE[w])}}v.parentNode.replaceChild(u,v);q=u}}}return q}function F(t,q,r){var u=a("param");u.setAttribute("name",q);u.setAttribute("value",r);t.appendChild(u)}function X(r){var q=C(r);if(q&&(q.nodeName=="OBJECT"||q.nodeName=="EMBED")){if(h.ie&&h.win){if(q.readyState==4){B(r)}else{j.attachEvent("onload",function(){B(r)})}}else{q.parentNode.removeChild(q)}}}function B(t){var r=C(t);if(r){for(var q in r){if(typeof r[q]=="function"){r[q]=null}}r.parentNode.removeChild(r)}}function C(t){var q=null;try{q=K.getElementById(t)}catch(r){}return q}function a(q){return K.createElement(q)}function I(t,q,r){t.attachEvent(q,r);d[d.length]=[t,q,r]}function c(t){var r=h.pv,q=t.split(".");q[0]=parseInt(q[0],10);q[1]=parseInt(q[1],10)||0;q[2]=parseInt(q[2],10)||0;return(r[0]>q[0]||(r[0]==q[0]&&r[1]>q[1])||(r[0]==q[0]&&r[1]==q[1]&&r[2]>=q[2]))?true:false}function V(v,r){if(h.ie&&h.mac){return }var u=K.getElementsByTagName("head")[0],t=a("style");t.setAttribute("type","text/css");t.setAttribute("media","screen");if(!(h.ie&&h.win)&&typeof K.createTextNode!=b){t.appendChild(K.createTextNode(v+" {"+r+"}"))}u.appendChild(t);if(h.ie&&h.win&&typeof K.styleSheets!=b&&K.styleSheets.length>0){var q=K.styleSheets[K.styleSheets.length-1];if(typeof q.addRule==Q){q.addRule(v,r)}}}function W(t,q){var r=q?"visible":"hidden";if(e&&C(t)){C(t).style.visibility=r}else{V("#"+t,"visibility:"+r)}}function g(s){var r=/[\\\"<>\.;]/;var q=r.exec(s)!=null;return q?encodeURIComponent(s):s}var D=function(){if(h.ie&&h.win){window.attachEvent("onunload",function(){var w=d.length;for(var v=0;v<w;v++){d[v][0].detachEvent(d[v][1],d[v][2])}var t=i.length;for(var u=0;u<t;u++){X(i[u])}for(var r in h){h[r]=null}h=null;for(var q in swfobject){swfobject[q]=null}swfobject=null})}}();return{registerObject:function(u,q,t){if(!h.w3cdom||!u||!q){return }var r={};r.id=u;r.swfVersion=q;r.expressInstall=t?t:false;N[N.length]=r;W(u,false)},getObjectById:function(v){var q=null;if(h.w3cdom){var t=C(v);if(t){var u=t.getElementsByTagName(Q)[0];if(!u||(u&&typeof t.SetVariable!=b)){q=t}else{if(typeof u.SetVariable!=b){q=u}}}}return q},embedSWF:function(x,AE,AB,AD,q,w,r,z,AC){if(!h.w3cdom||!x||!AE||!AB||!AD||!q){return }AB+="";AD+="";if(c(q)){W(AE,false);var AA={};if(AC&&typeof AC===Q){for(var v in AC){if(AC[v]!=Object.prototype[v]){AA[v]=AC[v]}}}AA.data=x;AA.width=AB;AA.height=AD;var y={};if(z&&typeof z===Q){for(var u in z){if(z[u]!=Object.prototype[u]){y[u]=z[u]}}}if(r&&typeof r===Q){for(var t in r){if(r[t]!=Object.prototype[t]){if(typeof y.flashvars!=b){y.flashvars+="&"+t+"="+r[t]}else{y.flashvars=t+"="+r[t]}}}}f(function(){U(AA,y,AE);if(AA.id==AE){W(AE,true)}})}else{if(w&&!A&&c("6.0.65")&&(h.win||h.mac)){A=true;W(AE,false);f(function(){var AF={};AF.id=AF.altContentId=AE;AF.width=AB;AF.height=AD;AF.expressInstall=w;k(AF)})}}},getFlashPlayerVersion:function(){return{major:h.pv[0],minor:h.pv[1],release:h.pv[2]}},hasFlashPlayerVersion:c,createSWF:function(t,r,q){if(h.w3cdom){return U(t,r,q)}else{return undefined}},removeSWF:function(q){if(h.w3cdom){X(q)}},createCSS:function(r,q){if(h.w3cdom){V(r,q)}},addDomLoadEvent:f,addLoadEvent:R,getQueryParamValue:function(v){var u=K.location.search||K.location.hash;if(v==null){return g(u)}if(u){var t=u.substring(1).split("&");for(var r=0;r<t.length;r++){if(t[r].substring(0,t[r].indexOf("="))==v){return g(t[r].substring((t[r].indexOf("=")+1)))}}}return""},expressInstallCallback:function(){if(A&&M){var q=C(m);if(q){q.parentNode.replaceChild(M,q);if(l){W(l,true);if(h.ie&&h.win){M.style.display="block"}}M=null;l=null;A=false}}}}}();

/*global document, jQuery, window, setTimeout */

jQuery(document).ready(function(){
	jQuery('.msg-link').hover(function(){
		jQuery(this).addClass('hover');
	}, function(){
		jQuery(this).removeClass('hover');
	});
});


var Messages = {
  
  validate : function(_this) {
    var body = jQuery(_this).find("textarea").val();
    if(!body || body.length <= 0) {
      alert("Напишите чего-нибудь в сообщении");
      return false;
    }
    if(body && body.length >= 1000) {
      alert("Слишком длинное сообщение: "+body.length+" символов вместо 1000");
      return false;
    }
    var user = jQuery(_this).find("input[name='message[user_login]']");
    if(user.length > 0 && (user.val() || user.val().length <= 0)) {
      //alert("Не указан получатель сообщения");
      //return false;
    }
    return true;
  },
	
	// Call this function when user read message first time
	onRead: function(obj){
		jQuery.post("/messages/"+obj.message_id+"/read", {}, function() {
			// obj.id — message id
			//if(!jQuery.browser.msie)
			//	console.log("Message '" + obj.id + "' was read");
			
		}, "script");
	},
	
	// Call this function when user send answer on message
	// you have to call "callback" function for future actions (animation)
	onSend: function(obj, callback){
    if(!Messages.validate(obj.answer)) {return false;}
		var reply = jQuery(obj.answer).find("textarea").val();
		jQuery(obj.answer).find("textarea").val("");
		jQuery('#message_'+obj.message_id).addClass('msg-replied');

    callback.call();

		jQuery.post("/messages", {"message[body]" : reply, "message[user_id]" : obj.sender_id, in_reply_to : obj.message_id}, function(reply) {
      var message = reply.message || reply;
		}, "json");
	},
	
	// User remove message
	onRemove: function(obj, callback){
		callback.call();
    this.increase_message_counters(-1, -1);
		jQuery.post("/messages/"+obj.message_id, {"_method" : "delete"}, function() {
      //if(!jQuery.browser.msie)
			//	console.log("Message '" + message_id + "' was removed");
		}, "json");
	},

  // User remove message
	onRemoveCorrespondence: function(obj, callback){
		callback.call();
		jQuery.post('/correspondences/remove/', {"_method" : "delete", 'ids': obj.message_id}, function() {
      //if(!jQuery.browser.msie)
			//	console.log("Message '" + message_id + "' was removed");
      window.location.href = "/messages";  
		}, "json");
	},

	// User restore message
	onRestore: function(obj, callback){
		callback.call();
		this.increase_message_counters(-1, -1);
		jQuery.post("/messages/"+obj.message_id+"/restore", {}, function() {
			//if(!jQuery.browser.msie)
			//	console.log("Message '" + message_id + "' was removed");
		}, "json");
	},

	// User put message to black list
	onBlack: function(obj, callback_fast, callback_slow){
		callback_fast.call();
		jQuery.post("/blacklists", {sender_id : obj.sender_id, type : obj.type, message_id : obj.message_id}, function(reply) {
      var blacklist = reply.blacklist || reply;
			obj.blacklist_id = blacklist.id;
			if(callback_slow) {callback_slow.call();}
		}, "json");
	},
	
	// User cancel action to put message to black list
	onCancel: function(obj, callback){
		callback.call();
		jQuery.post("/blacklists", {"_method" : "delete", type : obj.type, sender_id : obj.sender_id}, function() {
    }, "json");
	},

  onCommentUnsubscribe: function(obj, callback){
    callback.call();
    jQuery.post("/messages/settings", {'profile[send_comments_message]' : false, 'profile[send_comments_email]' : false}, function() {
    }, "json");
  },	

	//
	// Messages object
	//
	
	aMsg: [],
	s: {
		current: null,
		animate: false,
		prevent: true,
		navigate: true
	},
	
	filter: function(t){
	  var i, j;
		if(t && t.length){
			this.o.empty.stop().removeAttr("style").show();
			for (j=0; j < t.length; j++) {
				for (i=0; i < this.aMsg.length; i++) {
					if(this.aMsg[i].type == t[j] && !this.aMsg[i].removed){
						this.o.empty.stop().hide();
						this.aMsg[i].obj.show();
						this.aMsg[i].visible = true;
					}
				}
			}
			
		} else {
			this.o.empty.show();
			for (i=0; i < this.aMsg.length; i++) {
				this.aMsg[i].obj.stop().hide();
				this.aMsg[i].visible = false;
			}
		}
	},
	checkVisible: function(){
		var h = this.o.empty.show().height();
		this.o.empty.css({ height: 0, overflow: "hidden", opacity: 0 }).animate({ height: 40, opacity: 1 });
		for (var i=0; i < this.aMsg.length; i++) {
			if(this.aMsg[i].visible){
				this.o.empty.stop().hide();
				return false;
			}
		}
	},
	
	append: function(id){
		var o = jQuery("#" + id);
		var id_parts = id.split("_");
		
		this.aMsg.push({
			obj: o,
			id: id,
			message_id : id_parts[id_parts.length - 1],
			sender_id: o.attr('sender_id'),
			visible: true,
			removed: false,
			type: o.attr("messageType"),
			msgAnswer: (o.attr("msgAnswer") && o.attr("msgAnswer") == "false") ? false : true
		});

    this.aMsg[this.aMsg.length - 1].obj.find("*[msgAction]").click(Messages.action_click);
    this.aMsg[this.aMsg.length - 1].obj.find(".msg-removed ins").click(Messages.removed_click);
	},
	init: function(){
		this.events();
		this.o = {
			answer: jQuery(".messages .msg-hide .msg-answer"),
			empty: jQuery(".messages .msg-list-empty")
		};
    jQuery('#load_more_messages').click(function(){
      $this = jQuery(this);
      if($this.attr('loading') == 'true'){ return false; }
      $this.attr('loading', 'true');
      jQuery.get('?limit=10&offset='+(Messages.count()), {}, function(){jQuery('#messages_on_page_count').text(Messages.count());$this.attr('loading', 'false');}, 'script');
      return false;
    });
    jQuery('#load_all_messages').click(function(){
      $this = jQuery(this);
      if($this.attr('loading') == 'true'){ return false; }
      $this.attr('loading', 'true');
      jQuery.get('?load_all=true', {}, function(){$this.attr('loading', 'false');}, 'script');
      return false;
    });
	},
	events: function(){
		var $this = this;
		this.t = 0;
		jQuery(document).keydown(function(evt){

			$this.s.navigate = true;
			switch(evt.target.tagName){
				case 'INPUT':
					$this.s.navigate = false;
					break;
				case 'TEXTAREA':
					if(jQuery(evt.target).parents(".msg-list")) {
						if(evt.target.value.length > 0) {
							$this.s.navigate = false;
						} else {
							$this.s.navigate = true;
						}
					} else {
						$this.s.navigate = false;
					}
					
					break;
				case 'SELECT':
					$this.s.navigate = false;
					break;
			}

		});
		
		var msg_clicker = function(evt){
			$this._onclick(evt, jQuery(this));
		};
		
		var hover_in = function(){
			var jobj = jQuery(this);
			var k = $this.getById(jobj.attr("id"));
		
			if(!$this.aMsg[k].answer) {
				jobj.find("div.msg-answer-button").stop().css({ opacity: 0 }).show().animate({ opacity: 1 }, 200);
			}
		};
		
		var hover_out = function(){
			var jobj = jQuery(this);
			var k = $this.getById(jobj.attr("id"));

			if(!$this.aMsg[k].answer) {
				jobj.find("div.msg-answer-button").stop().animate({ opacity: 0 }, 200, function(){ jQuery(this).hide(); });
			}
		};
		
		var answer_click = function(){
			var jobj = jQuery(this);
			var k = $this.getById(jobj.parents(".msg-list-item").attr("id"));
		
			$this.open(k);
		};
		
		for (var i=0; i < this.aMsg.length; i++) {
			this.aMsg[i].obj.click(msg_clicker);
			
			if(this.aMsg[i].msgAnswer){
				this.aMsg[i].obj.hover(hover_in, hover_out);
				this.aMsg[i].obj.find("div.msg-answer-button").click(answer_click);
			}			
		}
	},

  // Binding events online
  removed_click: function(){
    var jobj = jQuery(this);
    $this = Messages;
    var k = $this.getById(jobj.parents(".msg-list-item").attr("id"));

    if(!jobj.is(".loading")){
      jobj.addClass("loading");
      $this.onCancel($this.aMsg[k], function(){
        $this.cancel(k);
      });
    }
  },

	action_click: function(){
    var jobj = jQuery(this);
    $this = Messages;
    var k = $this.getById(jobj.parents(".msg-list-item").attr("id"));

    if(!jobj.is(".loading")){
      jobj.addClass("loading");

      switch(jobj.attr("msgAction")){
        case 'reply':
          //TODO: for future
          break;

        case 'remove':
          $this.onRemove($this.aMsg[k], function(){
            $this.remove(k);
          });
          break;

        case 'remove_correspondence':
          $this.onRemoveCorrespondence($this.aMsg[k], function(){
            $this.remove(k);
            Messages.increase_message_counters(-1, -1);
          });
          break;
		
        case 'comment_unsubscribe':
          $this.onCommentUnsubscribe($this.aMsg[k], function(){
            $this.aMsg[k].obj.find(".comment_unsubscribe").hide();
            $this.aMsg[k].obj.find(".comment_unsubscribed").show();
          });
          break;
		
        case 'restore':
          $this.onRestore($this.aMsg[k], function(){
            $this.remove(k);
          });
          break;

        case 'cancel':
          $this.onCancel($this.aMsg[k], function(){
            $this.cancel(k);
          });
          break;

        case 'black':
          $this.onBlack($this.aMsg[k], function(){
            $this.black_fast(k);
          }, 	function(){
            $this.black_slow(k);
          });
          break;
      }
    }
  },
	
	_onclick: function(evt, obj){
		this.clean();
		this.s.current = this.getById(obj.attr('id'));
		if(this.s.current !== null) {
			this.light(this.s.current);
		}
	},
	
	next: function(){
		var s = this.s.current;
		this.s.current = this.getNextVisible( (this.s.current === null) ? 0 : (this.s.current + 1));
		if(this.s.current !== null && this.s.current != s){
			this.clean();
			this.light(this.s.current);
		}
	},
	
	prev: function(){
		var s = this.s.current;
		this.s.current = (this.s.current === null) ? this.getNextVisible(0) : this.getPrevVisible((this.s.current - 1));
		if(this.s.current !== null && this.s.current != s){
			this.clean();
			this.light(this.s.current);
		}
	},
	
	light: function(i){
		var o = this.aMsg[i].obj;

		if(!this.s.prevent){
			jQuery.scrollTo(this.aMsg[i].obj, 300, { offset: -10 });
			this.s.prevent = true;
		}

//  correspondence read once at load
//		o.addClass("msg-active");
//		if(o.is(".msg-not-read")){
//			o.removeClass("msg-not-read");
//			this.onRead(this.aMsg[i]);
//		}
	},
	
	clean: function(){
		for (var i=0; i < this.aMsg.length; i++) {
			this.aMsg[i].obj.removeClass("msg-active");
		}
	},
	
	open: function(i){
		var $this = this;
		if(i !== null && !this.aMsg[i].answer && !this.s.animate && this.aMsg[i].msgAnswer && this.aMsg[i].visible){
			var init = false;
			this.s.animate = true;
			this.aMsg[i].obj.find("div.msg-answer-button").hide();

			// copy answer form and animate
			if(this.aMsg[i].obj.find(".msg-answer").length){
				this.aMsg[i].answer = this.aMsg[i].obj.find(".msg-answer");
			} else {
				init = true;
				this.aMsg[i].answer = this.o.answer.clone(true).appendTo(this.aMsg[i].obj);
			}
				
			var h = this.aMsg[i].answer.removeAttr("style").height()+15;
			this.aMsg[i].answer.hide();

			if(jQuery.browser.msie) {
				h -= 15;
			}

			this.aMsg[i].answer.css({ height: 0, overflow: "hidden" }).animate({ height: h }, function(){
				$this.s.animate = false;
				$this.aMsg[i].answer.find("textarea").focus();
				jQuery(this).removeAttr("style");
			});
			this.aMsg[i].answer.find("div.msg-info, div.msg-ab-in").css({ opacity: 0 }).animate({ opacity: 1 });

			jQuery.scrollTo(this.aMsg[i].answer, 300, { offset: -jQuery(window).height()+h+10 });

			// init actions for answer form
			if(init) {
				this.initAnswerForm(this.aMsg[i].answer, i);
			}
		}
	},
	
	hide: function(i, remove){
		if(i !== null && this.aMsg[i].answer && !this.s.animate){
			var $this = this;
			this.aMsg[i].answer.animate({ height: 0 }, function(){
				if($this.aMsg[i].answer){
					$this.aMsg[i].answer.removeAttr("style").hide();
					if(remove) {
						$this.aMsg[i].answer.remove();
					}
					delete $this.aMsg[i].answer;
				}
			});
			this.aMsg[i].answer.find("div.msg-info, div.msg-ab-in").animate({ opacity: 0 });
			this.aMsg[i].answer.find("textarea").blur();
			jQuery.scrollTo(this.aMsg[i].obj, 300, { offset: -10 });
		}
	},
	
	initAnswerForm: function(obj, i){
		var $this = this;
		b_button_init();
		
		// init cancel action
		obj.find(".msg-ab-cancel a").click(function(){
			$this.hide(i);
			return false;
		});

		obj.find(".button-wrapper").click(function(){
			
			// move answer form to "loading" state
			obj.find(".msg-ab-in").css({ overflow: "hidden" }).animate({ height: 30 });
			obj.find(".msg-ab-in div.msg-ab-form").animate({ height: 0, opacity: 0 }, function(){
				jQuery(this).hide();
			});
			obj.find(".msg-ab-in div.msg-ab-loader").css({ opacity: 0 }).show().animate({ opacity: 1 });

			// call ajax function
			$this.onSend($this.aMsg[i], function(){
			  obj.find(".msg-ab-in").html('Сообщение отправлено');
			  setTimeout(function(){
			    $this.hide(i, true);
			  }, 2000);
				
			});
			
			return false;
		});

/*
		obj.find("textarea").focus(function(){
			if(this.value.length > 0)
				$this.s.navigate = false;
			else
				$this.s.navigate = true;
		}).blur(function(){
			$this.s.navigate = true;
		}).keyup(function(){
			if(this.value.length > 0)
				$this.s.navigate = false;
			else
				$this.s.navigate = true;
		})
*/
	},
	
	black_fast: function(i){
		var $this = this;
    this.aMsg[i].visible = false;
    this.aMsg[i].obj.find(".msg-blacklisted").parent().show();
    this.aMsg[i].obj.find(".msg-blacklisted").show();
    this.aMsg[i].obj.find(".msg-cont").parent().hide();
	},

	black_slow: function(i) {
    // there was some code
	},
	
	remove: function(i){
		var $this = this;
		this.aMsg[i].obj.css({ overflow: "hidden" }).animate({ height: 0, paddingBottom: 0, marginBottom: 0 }, function(){
			jQuery(this).hide();
		});
		
		if(!jQuery.browser.msie) {
			this.aMsg[i].obj.find(".msg-list-item-outer").animate({ opacity: 0 });
		}

		this.aMsg[i].visible = false;
		this.aMsg[i].removed = true;
		this.checkVisible();
	},
	
	cancel: function(i){
		this.aMsg[i].visible = true;
    this.aMsg[i].obj.find(".msg-blacklisted").parent().hide();
    this.aMsg[i].obj.find(".msg-blacklisted").hide();
    this.aMsg[i].obj.find(".msg-cont").parent().show();
	},
	
	getNextVisible: function(s){
	  var i;
		for (i=s; i < this.aMsg.length; i++) {
			if(this.aMsg[i].visible) {
				return i;
			}
		}

		for (i=this.aMsg.length-1; i >= 0; i--) {
			if(this.aMsg[i].visible) {
				return i;
			}
		}
		return null;

//		//Jump to first
//		for (var i=0; i < this.aMsg.length; i++) {
//			if(this.aMsg[i].visible)
//				return i;
//		};
//		return null;

	},

	getPrevVisible: function(s){
	  var i;
		if(s >=0 ){
			for (i=s; i >= 0; i--) {
				if(this.aMsg[i].visible) {
					return i;
				}
			}
		}

		for (i=0; i < this.aMsg.length; i++) {
			if(this.aMsg[i].visible) {
				return i;
			}
		}

//		//Jump to last
//		for (var i=this.aMsg.length-1; i >= 0; i--) {
//			if(this.aMsg[i].visible)
//				return i;
//		};
		return null;
	},

  empty_trash: function(){
    var f = document.createElement('FORM');
    f.action='/messages/empty_trash';
    f.method='post';
    document.body.appendChild(f);
    f.submit();
  },
	getById: function(id){
		for (var i=0; i < this.aMsg.length; i++) {
			if(this.aMsg[i].id == id) {
				return i;
			}
		}
		return null;
	},
  count: function(){
    return jQuery(".msg-list .msg-list-item").length - jQuery(".msg-list .correspondent-reply").length; // Пока поле для ответа считается сообщением
  },
  increase_message_counters: function(delta_showed, delta_total){
    var showed = parseInt(jQuery('#messages_on_page_count').text(), 10);
    var total  = parseInt(jQuery('#messages_total_count').text(), 10);
    jQuery('#messages_on_page_count').text(showed + delta_showed);
    jQuery('#messages_total_count').text(total + delta_total);
  },
  update_message_counters: function(showed, total){
    jQuery('#messages_on_page_count').text(showed);
    jQuery('#messages_total_count').text(total);
  },
  update_main_counters: function(unread_messages_count, unread_friendship_messages_count){
    jQuery('.unread_count').html(unread_messages_count);
    var unread_wrap = jQuery('.unread_count_wrap');
    unread_messages_count > 0 ? unread_wrap.show() : unread_wrap.hide();
    jQuery('.friend_requests_count').html(unread_friendship_messages_count);
    var unread_friendship_wrap = jQuery('.friend_requests_count_wrap');
    unread_friendship_messages_count > 0 ? unread_friendship_wrap.show() : unread_friendship_wrap.hide();
  },
  highlight: function(query){
    var messages = jQuery(".msg-body table tr td a, table.msg-item tr td p.msg-text");
    var pattern = new RegExp(query, "gi")
    messages.each(function(i){
      var $this = jQuery(this);
      $this.html($this.html().replace(pattern, "<i class='search-highlighted'>" + query + "</i>"));
    });
  }
};

var Correspondences = {
  init: function(){
    this.ms = jQuery(".all-msg tr");
	this.ms.hover(function(){
		jQuery(this).addClass('hover');
	}, function(){
		jQuery(this).removeClass('hover');
	});
    this.checkers = this.ms.find("input[type=checkbox]");
    this.all_toggler = jQuery("#select_all_correspondences").click(function(){Correspondences.toggle_select_all();});
    jQuery('#read_correspondences').click(function(){Correspondences.read_selected();});
    jQuery('#restore_correspondences').click(function(){Correspondences.restore(Correspondences.selected_ids());});
    jQuery('#remove_correspondences_to_trash').click(function(){Correspondences.remove_to_trash(Correspondences.selected_ids());});
    jQuery('#remove_correspondences_forever').click(function(){Correspondences.remove_forever(Correspondences.selected_ids());});
    jQuery('#empty_trash').click(function(){Messages.empty_trash();});
  },
  selected_ids: function(){
    var checked = this.checkers.filter(function(i){return Correspondences.checkers[i].checked;});
    var ids = [];
    checked.each(function(i, checker){ids.push(checker.id);});
    return ids;
  },
  showed_correspondences: function(){
    return jQuery(".corr-row").length - jQuery(".corr-hidden").length;
  },
  read_selected: function(){
    var ids = this.selected_ids();
    if (ids.length < 1) { alert('Выделите сообщения'); return false;}
    jQuery.post("/correspondences/read/", {'ids': ids.join(",")}, function(){}, 'script');
    ids.each(function(id){
      jQuery("#correspondence_"+id).removeClass("msg-new");
      jQuery("#correnpondence_unread_"+id).text("");
    });
  },
  action: function(url, ids){
    if (ids.length < 1) { alert('Выделите сообщения'); return false;}
    jQuery.post(url, {'ids': ids.join(",")}, function(){}, 'script');
    ids.each(function(id){
      jQuery("#correspondence_"+id).addClass('corr-hidden');
      jQuery("#correspondence_"+id).animate({ opacity : 0 }, function(){jQuery(this).hide();});
    });
    if(Correspondences.showed_correspondences() == 0){
      //window.location.reload();
    }
  },
  remove_to_trash: function(ids){
    this.action("/correspondences/remove/", ids);
  },
  restore: function(ids){
    this.action("/correspondences/restore/", ids);
  },
  remove_forever: function(ids){
    this.action("/correspondences/remove_forever/", ids);
  },
  toggle_select: function(id){
    var was_checked = jQuery('#correspondence_'+id).hasClass("msg-selected");
    if (was_checked){
      jQuery('#'+id).checked = false;
      jQuery('#correspondence_'+id).removeClass("msg-selected");
    }else{
      jQuery('#'+id).checked = true;
      jQuery('#correspondence_'+id).addClass("msg-selected");
    }

  },
  toggle_select_all: function(){
    var checked = this.all_toggler.attr('checked');
    if(!checked){this.deselect_all();}
    else{this.select_all();}

  },
  select_all: function(){
    this.checkers.each(function(i, c){c.checked = true});
    this.ms.addClass("msg-selected");
  },
  deselect_all: function(){
    this.checkers.each(function(i, c){c.checked = false});
    this.ms.removeClass("msg-selected");
  }
}

jQuery(document).ready(function(){
	jQuery(".clicked").
	  mousedown(function(){
			var $this = jQuery(this);
			$this.addClass($this.attr("class").match(/([^ ]+)/)[1] + "-down");
		}).
		mouseup(function(){
			var $this = jQuery(this);
			$this.removeClass($this.attr("class").match(/([^ ]+)/)[1] + "-down");
		});

	jQuery(".hovered")
		.hover(function(){
			var $this = jQuery(this);
			$this.addClass($this.attr("class").match(/([^ ]+)/)[1] + "-hover");
		}, function(){
			var $this = jQuery(this);
			$this.removeClass($this.attr("class").match(/([^ ]+)/)[1] + "-hover");
		});
		
		
	jQuery(document.body).mouseup(function(){
		jQuery(".clicked").each(function(){
			var $this = jQuery(this);
			$this.removeClass($this.attr("class").match(/([^ ]+)/)[1] + "-down");
		});
	});
	
	Messages.init();
  Correspondences.init();
/*
	jQuery("input").each(function(){
		if(!jQuery(this).parents("div.message").length)
			jQuery(this)
				.focus(function(){
					Messages.s.navigate = false;
				})
				.blur(function(){
					Messages.s.navigate = true;
				})
	})
*/	
	
});


/*global jQuery, clearInterval, setInterval */

function Animate(step, duration, easing, callback){
	this.opt = {
		step: step,
		complete: callback ? callback : function(){}
	};
	if(jQuery.isFunction(easing)) {this.opt.complete = easing;}

	this.now = 0;
	this.duration = duration;

	this.easing = !jQuery.isFunction(easing) ? easing : "swing";

	this.init();
	
}
Animate.prototype = {
	init: function(){
		var oThis = this;
		this.startTime = (new Date()).getTime();
		this.timerId = setInterval(function(){
			oThis.next();
		}, 13);
	},
	next: function(){
		this.prev = this.now;
		var t = (new Date()).getTime();
		this.step = t - this.startTime;

		if(this.step > this.duration){
			this.step = this.duration;
			this.exec();
			clearInterval(this.timerId);
			this.opt.complete.apply(this);
			return;
		}
		
		this.exec();
	},
	exec: function(){
		this.state = this.step / this.duration;
		this.now = jQuery.easing[this.easing](this.state, this.step, 0, 1, this.duration);
		this.opt.step.apply(this, [this.now, this.prev]);
	},
	stop: function(){
		clearInterval(this.timerId);
	}
};


/*global jQuery, Draggable, _back, _mark, d, document, jsButton, oThis, sClass, window*/
function Calendar(){
	this.baseConstructor.apply(this, arguments);
}

Calendar.inheritFrom(Draggable, {
	_default: { //current date
		year: (new Date()).getFullYear(),
		month: (new Date()).getMonth()+1,
		submit: "."
	},
	oninit: function(){
		var oThis = this;
		this.hSetting = {};
		jQuery.extend(this.hSetting, this._default, arguments[1] ? arguments[1] : {});

		jQuery.extend(this.is, {
			animate: false,
			controls: false,
			controlsState: false
		});

		function getDateText(date){
			return date.getDate() + ' ' + oThis.months.second[date.getMonth()] + ', ' + oThis.week_days[date.getDay()];
		}


		this.months = {
			first: ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'],
			second: ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря']
		};
		this.week_days = ['воскресенье', 'понедельник', 'вторник', 'среда', 'четверг', 'пятница', 'суббота'];
		this.aMonths = [];
		
		this.ptr.append("<div class='calendar-current'>" + getDateText(new Date()) + "</div>");
		this.ptr.append(
			"<div class='calendar-body'>" +
				"<div class='calendar-button calendar-button-left'><i></i></div>" +
				"<div class='calendar-button calendar-button-right'><i></i></div>" +
				"<div class='calendar-week'></div>" +
			"</div>");
		this.ptr.append(
			"<div class='calendar-controls'><div class='calendar-control-in'>" +
				"<span class='calendar-clear'>Отменить</span>" +
				'<div class="button-wrapper"><div class="button-block"><div class="left"><div class="in"></div></div><div class="center">' + 
					'<p><span>Показать</span></p>' +
				'</div><div class="right"><div class="in"></div></div></div></div>' +
			"</div></div>");

		this.ptr.append(
			'<form class="calendar-form" method="post" action="' + this.hSetting.submit + '">' +
				'<input type="hidden" name="dates" />' +
				'<input type="hidden" name="month" />' +
				'<input type="hidden" name="year" />' +
			'</form>');
		
		this.ptr = this.ptr.find("div.calendar-body");
		var curr = this.createMonth(this.hSetting);
		this.ptr.append(curr.ptr);

		if(curr.count > 35) {
			this.ptr.css({ height: 180 });
		}
			
		curr.current = true;
		this.aMonths.push(curr);
		this.redraw(curr);
		
		this.resize();
	},
	resize: function(){
		var _offset = this.ptr.offset();

		this.current = this.getCurrent();
		for(var key in this.current.items){
			var ptr = this.current.items[key].ptr;
			var _o = ptr.offset();
			this.current.items[key].left = _o.left - _offset.left;
			this.current.items[key].top = _o.top - _offset.top;
		}
		
		this.cx = _offset.left;
		this.cy = _offset.top;
	},
	events: function(){
		var oThis = this;
		this.ptr.find("div.calendar-button").mousedown(function(){
			jQuery(this).addClass("calendar-button-down");
		}).mouseup(function(evt){
			jQuery(this).removeClass("calendar-button-down");
			if(!oThis.is.animate) {
				oThis.onclick(this, evt);
			}
		});

		jQuery(document)
			.mousemove(function(evt){
				oThis._onmousemove(evt);
				return false;
			});

		var jsbutton = this.ptr.parents("div.calendar").find("div.button-wrapper").click(function(){
			var form = oThis.ptr.parents("div.calendar").find("form.calendar-form");
			
			var dates = '';
			for (var i=0; i < oThis.aMonths.length; i++) {
				for(var k in oThis.aMonths[i].items){
					if(oThis.aMonths[i].items[k].selected){
						var d = new Date(k);
						dates += oThis.dateString(d) + ',';
					}
				}
			}
			dates = dates.replace(/,$/, '');
			
			var curr = oThis.getCurrent();
			form.
				find("input[name='dates']").val(dates);
			form.
				find("input[name='month']").val(curr.date.getMonth()+1);
			form.
				find("input[name='year']").val(curr.date.getFullYear());
			
			form.submit();
		
		});
		
		this.ptr.parents("div.calendar").find("span.calendar-clear").click(function(){
			oThis.empty();
			oThis.redraw(oThis.getCurrent());
		});
		
		jQuery(window).resize(function(){
			oThis.resize();
		});
		
		this.oArrow = this.ptr.parents("div.calendar").find("div.calendar-week").click(function(evt){
			oThis.selectByWeek(evt);
		});
	},
	onmousedown: function(evt){
		this.resize();
		
		var _s = this.getDay(this.current, evt.pageX-this.cx, evt.pageY-this.cy);
		if(_s){
			if(!evt.shiftKey) {
				this.empty(this.current);
			}

			this.start = new Date(_s);

			var curr = this.current.items[_s];
			if(curr.selected){
				curr.ptr.removeClass("selected");
				curr._selected = false;
				this.remove = true;
			} else {
				curr.ptr.addClass("selected");
				curr._selected = true;
				this.remove = false;
			}
			this.redraw(this.current);
			this.is.controls = true;
		}
	},
	_onmousemove: function(evt){
		var week = this.getWeek(this.current, evt.pageY - this.cy);
		if(week) {
			this.oArrow.css({ left: week.left-20, top: week.top }).show();
		} else {
			this.oArrow.hide();
		}
	},
	onmousemove: function(evt, x, y){
		function _mark(curr, obj){
			if(obj.remove){
				curr.ptr.removeClass("selected");
				curr._selected = false;
			} else {
				curr.ptr.addClass("selected");
				curr._selected = true;
			}
		}

		function _back(curr){
			curr._selected = curr.selected;
			if(curr.selected) {
				curr.ptr.addClass("selected");
			} else {
				curr.ptr.removeClass("selected");
			}
		}

		if(this.start){
			this.end = new Date(this.getDay(this.current, x, y));
			for(var k in this.current.items){
				var d = new Date(k);
				var curr = this.current.items[k];
				if(this.start < this.end) {
					if(d >= this.start && d <= this.end) {_mark(curr, this);}
					else{ _back(curr);}
				} else {
					if(d <= this.start && d >= this.end) {_mark(curr, this);}
					else {_back(curr);}
				}
			}
			this.redraw(this.current);
		}
	},
	onmouseup: function(){
		for(var k in this.current.items){
			var curr = this.current.items[k];
			curr.selected = curr._selected ? curr._selected : false;
		}
		this.redraw(this.current);
		
		if(this.is.controls) {
			this.showControls();
		}
		
		this.start = null;
		this.end = null;
	},
	onclick: function(obj, evt){
		function setCurrent(month){
			for (var i=0; i < oThis.aMonths.length; i++) {
				oThis.aMonths[i].current = false;
			}
			month.current = true;
		}
		function exist(hDate){
			var date = new Date(hDate.year, hDate.month-1);
			for (var i=0; i < oThis.aMonths.length; i++) {
				if(oThis.aMonths[i].date.valueOf() == date.valueOf()) {
					return true;
				}
			}
			return false;
		}
		
		function getMonth(hDate){
			var date = new Date(hDate.year, hDate.month-1);
			for (var i=0; i < oThis.aMonths.length; i++) {
				if(oThis.aMonths[i].date.valueOf() == date.valueOf()) {
					return oThis.aMonths[i];
				}
			}
		}
		var oThis = this;
		this.is.animate = true;
		
		var direction = 1;
		if(jQuery(obj).is(".calendar-button-left")) {
			direction = -1;
		}
		
		this.current = this.getCurrent();
		this.current.ptr.css({ position: "absolute", left: 0, top: 0 });
		var date = new Date(this.current.date.getFullYear(), this.current.date.getMonth()+direction);
		var newMonth = { year: date.getFullYear(), month: date.getMonth()+1 };
		if(!exist(newMonth)){
			newMonth = this.createMonth(newMonth);
			newMonth.ptr.css({ position: "absolute", left: direction*240, top: 0 });
			this.ptr.append(newMonth.ptr);
		} else {
			newMonth = getMonth(newMonth);
		}

		this.redraw(newMonth);

		newMonth.ptr.show();
		this.current.ptr.animate({ left: -1*direction*240 }, function(){
			jQuery(this).hide();
		});
		newMonth.ptr.animate({ left: 0 }, function(){
			oThis.is.animate = false;
		});
		if(newMonth.count != this.current.count){
			if(this.current.count < newMonth.count) {
				this.ptr.animate({ height: 180 });
			} else {
				this.ptr.animate({ height: 160 });
			}
		}

		this.aMonths.push(newMonth);
		
		setCurrent(newMonth);

	},
	
	showControls: function(){
		if(!this.is.controlsState){
			this.is.controlsState = true;
			this.ptr.parents("div.calendar").find("div.calendar-controls").css({ height: 0, overflow: "hidden" }).show().
				animate({ height: 40 });
		}
	},
	selectByDay: function(day, evt){

		if(!evt.shiftKey) {
			this.empty(this.current);
		}

		var k;
		jQuery(day).parent().find("li").each(function(i){
			if(day == this) {
				k = i;
			}
		});
		k++;
		if(k == 7) {k = 0;}
		this.current = this.getCurrent();
		for(var key in this.current.items){
			var date = new Date(key);
			if(date.getDay() == k){
				this.current.items[key]._selected = true;
				this.current.items[key].ptr.addClass("selected");
			}
		}
		this.is.controls = true;

		this.redraw(this.current);
		this.onmouseup();
	},
	
	selectByWeek: function(evt){
		var week = this.getWeek(this.current, evt.pageY - this.cy);

		if(!evt.shiftKey) {
			this.empty(this.current);
		}

		for(var key in this.current.items){
			if(this.current.items[key].top == week.top){
				this.current.items[key]._selected = true;
				this.current.items[key].ptr.addClass("selected");
			}
		}

		this.is.controls = true;

		this.redraw(this.current);
		this.onmouseup();
		
	},
	redraw: function(month){
		function check(d){
			return month.items[d.toString()] && month.items[d.toString()].ptr.is('.selected');
		}

		function add(){
			if(arguments.length > 1){
				if(d.getDay() == arguments[0]) {sClass += 'o';}
				else {add(arguments[1]);}
			} else {
				var _d = new Date(d.getFullYear(), d.getMonth(), d.getDate()+arguments[0]);
				if(check(_d)) {sClass += 'f';}
				else {sClass += 'o';}
			}
		}
		
		function removeClasses(day){
			var s = day.ptr.is(".selected");
			day.ptr.attr("class", day.sClass);
			if(s) {day.ptr.addClass("selected");}
		}
		
		for(var k in month.items){
			var curr = month.items[k];
			removeClasses(curr);
			if(curr.ptr.is(".selected")){
				var d = new Date(k);
				
				var sClass = '';
				add(-7);
				add(0, 1);
				add(7);
				add(1,-1);
				add(1,-8);
				
				curr.ptr.addClass(sClass);
				
			}
		}
	},
	empty: function(month){
		this.hSetting.dates = [];
		for (var i=0; i < this.aMonths.length; i++) {
			for(var k in this.aMonths[i].items){
				var curr = this.aMonths[i].items[k];
				curr.selected = curr._selected = false;
				curr.ptr.removeClass("selected");
			}
		}
	},

	getCurrent: function(){
		for (var i=0; i < this.aMonths.length; i++) {
			if(this.aMonths[i].current){
				return this.aMonths[i];
			}
		}
		return this.aMonths[0];
	},
	getDay: function(month, x, y){
		for(var k in month.items){
			if(((x >= month.items[k].left && x < month.items[k].left+20) || (x === null)) && y >= month.items[k].top && y < month.items[k].top+20) {
				return k;
			}
		}
	},
	getWeek: function(month, y){
		var k = this.getDay(month, null, y);
		return k ? month.items[k] : null;
	},

	createMonth: function(oDate){
		var oThis = this;
		function getDate(sDate){
			if(oThis.hSetting.dates) {
				for (var i=0; i < oThis.hSetting.dates.length; i++) {
					if(oThis.hSetting.dates[i] == sDate) {return true;}
				}
			}
			return false;
		}
		
		function events(month){
			month.ptr.find("ul.calendar-week-days li").click(function(evt){
				oThis.selectByDay(this, evt);
			});
			return month;
		}
		
		var now = new Date();
		now = new Date(now.getFullYear(), now.getMonth(), now.getDate());
		var date = new Date(oDate.year, oDate.month-1, 1);
		var next = new Date(oDate.year, oDate.month, 0);
		var month = {
		 	ptr: jQuery("<div class='calendar-month'><div class='calendar-month-in'><h1>" + this.months.first[date.getMonth()] + ((date.getFullYear() != now.getFullYear()) ? (', ' + date.getFullYear()) : '') + "</h1><ul class='calendar-week-days'><li>ПН</li><li>ВТ</li><li>СР</li><li>ЧТ</li><li>ПТ</li><li class='calendar-free'>СБ</li><li class='calendar-free'>ВС</li></ul><ul class='calendar-days'></ul></div></div>"),
			date: new Date(date.getFullYear(), date.getMonth()),
			items: {},
			current: false
		};
		var days = month.ptr.find("ul.calendar-days");

		date.setDate(- (date.getDay() === 0 ? 7 : date.getDay()) + 1);
		
		var diff = next.valueOf() - date.valueOf();
		var to = Math.round(diff/1000/60/60/24 + (7-(next.getDay() === 0 ? 7 : next.getDay())));
		month.count = to;

		for (var i=0; i < to; i++) {
			var sId = '';
			var sClass = '';
			date.setDate(date.getDate()+1);
			if(date.valueOf() == now.valueOf()) {
				sId += " id='calendar-current'";
			}
				
			if(date.valueOf() < now.valueOf()) {
				sClass += " past";
			}
				
			if(date.getMonth() != oDate.month-1){
				sClass += " another";
			}
			
			var day = jQuery("<li" + sId + " class='" + sClass + "'>" + (sId.length ? '<b>' : '') + date.getDate() + (sId.length ? '</b>' : '') + "</li>");
			var sDate = this.dateString(date);
			var bCheck = getDate(sDate);
			if(bCheck) {day.addClass("selected");}
			month.items[date.toString()] = { ptr: day, sClass: sClass, selected: bCheck ? true : false, _selected: bCheck ? true : false };
			days.append(day);
		}
		return events(month);
		
	},
	
	dateString: function(date){
		return ((date.getDate() < 10) ? ('0' + date.getDate()) : date.getDate()) + "-" + (((date.getMonth()+1) < 10) ? ('0' + (date.getMonth()+1)) : (date.getMonth()+1)) + "-" + date.getFullYear();
	}

});


jQuery(document).ready(function(){
	if(jQuery('div.edit-looks-right').length !=0){
		var lc = new LookCloth();
	}
});


//Добавление Вещей

function LookCloth(){
	this.init();
};

LookCloth.prototype = {
	init:function(){
		var _my = this;
		this.meOrNot = jQuery('#me_or_not');
		this.inputsWrap = jQuery('#added_clothes');
		this.bookmarks = jQuery('div.edit-looks-right div.bookmarks li');
		this.bookmarksBlocks = jQuery('div.edit-looks-right div.elems div.elem');
		this.wrap = jQuery('div.item div.clothes');
		this.addedUl = this.wrap.find('ul.cloth-ul');
		this.addThing = this.wrap.find('p.add-thing');
		this.clothForm = new ClothForm(this);
		this.comment = jQuery('.new-thing .comment span')
		this.attachEvents();
	},
	attachEvents:function(){
		var _my = this;
		this.insertFormFlag = true;
		this.addThing.click(function(){
			if(_my.insertFormFlag == true){
				_my.insertFormFlag = false;
				jQuery(this).addClass('disable');
				_my.insertNewForm(-1);
			}
		});
		
		this.addedUl.find('li').each(function(i){
			_my.attachEditLinks(jQuery(this), i);
		});
		
		this.bookmarks.click(function(){
			_my.toggleBookmarks(jQuery(this));
		});
		
		this.comment.click(function(){
			title = jQuery(this).html()
			jQuery('.brand', this.parentNode.parentNode).attr('value', title)
		})
	},
	insertNewForm:function(ind){
		if(ind != -1){
			this.addedUl.find('li#cloth_'+ind).append(this.clothForm.form.clone(true));
		} else {
			this.addedUl.append('<li></li>').find('li:last').append(this.clothForm.form.clone(true));
		}
		this.newForm = this.addedUl.find('.new-thing');
		this.attachCancel(this.newForm.parents('li'));
    this.brandInp = this.newForm.find('input.brand');
		this.nameInp = this.newForm.find('input.type_name');

		this.attachSave(this);
	},
	attachSave:function(){
		var _my = this;
		jQuery('li.save').html('<button buttonType="simple" type="submit">Сохранить</button>');
		b_button_init();

		oSave = jQuery('button', 'li.save');
		oSave.click(function(){
			_my.checkForm();
			return false
		})
	},
	attachCancel:function(form){
		var _my = this;
		var __this = this;

		form.find('li.cancel span').click(function(){
			if(form.attr('id') != ""){
				var item = __this.inputsWrap.find('div.'+form.attr('id'));
				form.html('<p>'+item.find('input.c_type_name').val()+'</p>'+'<b>'+item.find('input.c_brand').val()+'</b><i style="background-color:#'+item.find('input.c_color').val()+'"></i> <span class="cloth-edit">редактировать</span> <span class="cloth-del">удалить</span>');
				_my.attachEditLinks(form, _my.addedUl.find('li').index(form));
			} else {
				form.remove();
			}
			
			// Сделать ссылку "Добавить вещь", после того как нажали "Отменить"
			__this.insertFormFlag = true;
			__this.addThing.removeClass('disable');
		});
	},
	checkForm:function(){
		if(this.nameInp.attr('value') != "" && this.brandInp.attr('value') != ""){
			this.saveThing();
		} else if(this.nameInp.attr('value') != "" ){
			this.brandInp.attr('value', '');
			this.brandInp.css({backgroundColor:'red'}).focus(function(){
				jQuery(this).css({backgroundColor:'white'});
			});
		} else if(this.brandInp.attr('value') != ""){
			this.nameInp.attr('value', '');
			this.nameInp.css({backgroundColor:'red'}).focus(function(){
				jQuery(this).css({backgroundColor:'white'});
			});
		} else if(this.nameInp.attr('value') == "" && this.brandInp.attr('value') == ""){
			this.brandInp.attr('value', '');
			this.nameInp.attr('value', '');
			this.nameInp.css({backgroundColor:'red'}).focus(function(){
				jQuery(this).css({backgroundColor:'white'});
			});
			this.brandInp.css({backgroundColor:'red'}).focus(function(){
				jQuery(this).css({backgroundColor:'white'});
			});
		}
	},
	saveThing:function(){
		var color = "";
		color = this.newForm.find('ul.colors li.active span').css('backgroundColor');
		this.li = this.newForm.parent();
		if(this.li.attr('id').replace('cloth_', '') != ''){
			this.ind = this.li.attr('id').replace('cloth_', '');
		} else {
			this.ind = this.addedUl.find('li').index(this.li);
			if(this.li.parent().find('li#cloth_'+this.ind).length !=0){
				this.ind = this.ind+1;
			}
		}
	    this.brandInp = this.li.find('input.brand');
		this.nameInp = this.li.find('input.type_name');

		this.li.attr('id', 'cloth_'+this.ind).attr('class', 'full').html('<p>'+this.nameInp.attr('value')+'</p>'+'<b>'+this.brandInp.attr('value')+'</b><i style="background-color:'+color+'" class="color"></i> <span class="cloth-edit">редактировать</span> <span class="cloth-del">удалить</span>');
		this.saveData(this.ind, this.nameInp.val(), this.brandInp.val(), color);
		this.insertFormFlag = true;
		this.addThing.removeClass('disable');
		this.attachEditLinks(this.li, this.ind);
	},
	saveData:function(num, name, brand, color){
		var divClass = 'cloth_'+num;
		if(color == undefined){
			color = "";
		}
		if(this.inputsWrap.find('div.'+divClass).length !=0){
			var wrap = this.inputsWrap.find('div.'+divClass);
			wrap.find('input.c_type_name').val(name);
			wrap.find('input.c_brand').val(brand);
			wrap.find('input.c_color').attr('value', color);
		} else {
			this.inputsWrap.append('<div class="'+divClass+'"><input type="hidden" class="c_id" name="clothes['+num+'][id]" value="" /><input type="hidden" class="c_type_name" name="clothes['+num+'][type_name]" value="'+name+'" /><input type="hidden" class="c_brand" name="clothes['+num+'][cached_cloth_brand_title]" value="'+brand+'" /><input type="hidden" class="c_color" name="clothes['+num+'][color_rgb]" value="'+color+'" /></div>');
		}
	},
	attachEditLinks:function(li, ind){
		var _my = this;
		li.find('span.cloth-edit').click(function(){
			_my.editThing(li);
		});
		li.find('span.cloth-del').click(function(){
			_my.deleteThing(li, ind);
		});
	},
	deleteThing:function(li, ind){
		li.remove();
		this.inputsWrap.find('div.cloth_'+ind).remove();
	},
	editThing:function(li){
		li.empty();
		var ind = li.attr('id').replace('cloth_', '');
		this.insertNewForm(ind);
		var editForm = li.find('div.new-thing');
		var inputs = this.inputsWrap.find('div.cloth_'+ind);
		editForm.find('input.type_name').val(inputs.find('input.c_type_name').val());
		editForm.find('input.brand').val(inputs.find('input.c_brand').val());
		var colors = editForm.find('ul.colors span');
		var active = inputs.find('input.c_color').val();
		if(active.search("rgb") == -1){active =  "#" + active;}
		this.inputsWrap.css('backgroundColor', active);
		for(var i=0; i<colors.length; i++){
			if(colors.eq(i).css('backgroundColor') == this.inputsWrap.css('backgroundColor')){
				colors.eq(i).css({ left:-5, top:-5, width:29, height:29}).parent().addClass('active');
			}
		}
	},
	toggleBookmarks:function(elem){
		if(!elem.is('.active')){
			if(this.bookmarksBlocks.parent().is('.that_is_me')){
				this.bookmarksBlocks.parent().removeClass('that_is_me');
				this.bookmarks.eq(0).removeClass('active');
				this.bookmarks.eq(1).addClass('active');
				this.meOrNot.val('b_second');
			} else {
				this.bookmarksBlocks.parent().addClass('that_is_me');
				this.bookmarks.eq(1).removeClass('active');
				this.bookmarks.eq(0).addClass('active');
				this.meOrNot.val('b_first');
			}
		}
	}
}

function ClothForm(parent){
	this.init(parent);
}

ClothForm.prototype = {
	init:function(parent){
		this.parent = parent;
		this.form = jQuery('#new-cloth-form div.new-thing');
		this.colors = this.form.find('ul.colors li');
		this.cancel = this.form.find('li.cancel span');
		this.save = this.form.find('.buttons li.save button');
		this.attachEvents();
	},
	attachEvents:function(){
		var _my = this;
		// this.cancel.click(function(){
		// 	_my.removeForm(jQuery(this));
		// });
		this.save.click(function(){
			_my.parent.checkForm();
			return false
		});
		this.colors.click(function(){
			_my.toggleColor(jQuery(this));
		});
	},
	// removeForm:function(span){
	// 	jQuery('div.clothes p.add-thing span').removeClass('disable');
	// 	span.parents('div.new-thing').parent().remove();
	// 	this.parent.insertFormFlag = true;
	// },
	toggleColor:function(li){
		var _my = this;
		this.colors = li.parent().find('li');
		if(li.is('.active')){
			li.find('span').animate({ left:0, top:0, width:21, height:21}, 150, function(){
				li.removeClass('active');
			});
		} else {
			for(var i=0; i<this.colors.length; i++){
				if(this.colors.eq(i).is('.active')){
					this.colors.eq(i).find('span').animate({ left:0, top:0, width:21, height:21}, 150, function(){
						_my.colors.eq(i).removeClass("active");
					});
					break
				}
			}
			li.addClass("active");
			li.find('span').animate({ left:-5, top:-5, width:29, height:29}, 150);
		}
	}
}


/*global Draggable, jQuery */

function Switcher(){
	this.baseConstructor.apply(this, arguments);
}

Switcher.inheritFrom(Draggable, {
	oninit: function(){

		this.sId = arguments[0];
		
		this.o = {
			ptr: this.ptr,
			root: this.ptr.find("div.switcher"),
			back: this.ptr.find("div.sw-back"),
			knob: this.ptr.find("div.sw-knob")
		};

		if(this.o.root.is(".switcher-left")) {this._state = 1;}
		else {this._state = 0;}
		
		this.state = this._state;

		if(this.o.root.is(".switcher-disabled")) {jQuery.extend(this.is, {
			disabled: true
		});}
		
		this.reinit();
		
		if(this.state) {
		   this._x = this.limits.right;
		}

		this.ptr = this.ptr.find("div.sw-knob div.swk-ctrl");
		
		this.left_value = jQuery(this.o.ptr).find(".swb-l").attr('value') || '0';
		this.right_value = jQuery(this.o.ptr).find(".swb-r").attr('value') || '1';
		if(this.o.root.attr("name")) {
			this.linked_input = jQuery(this.o.ptr).find("input[name=\"" + this.o.root.attr("name") + "\"]");
		}
		if(this.linked_input && this.linked_input.length > 0) {
		  this.state = this._state = (this.linked_input.val() == this.left_value) ? 1 : 0;
		  if(this.state) {
		    this.o.root.addClass("switcher-left");
		    this.correct(this.limits.right);
		  }
		}
	},
	
	reinit: function(){

		jQuery.extend(this.d, {
			backLeft: this.o.back.offset().left,
			rootLeft: this.o.root.offset().left,
			rootWidth: this.o.root.width()
		});

		this.limits = {
			left: -(this.d.rootWidth-34),
			right: 0
		};

		this.o.back.css({ left: this.d.backLeft-this.d.rootLeft });
		this.o.knob.css({ left: this.d.backLeft-this.d.rootLeft });

	},
	
	onmousedown: function(){
		if(!this.is.disabled){
			this.reinit();
			jQuery.extend(this.d, {
				backLeft: this.o.back.offset().left
			});
		}
	},
	
	onmousemove: function(evt, x, y){
		if(!this.is.disabled){
			var _x = x-this.d.rootLeft-(this.d.left-this.d.backLeft);
		
			if(_x > this.limits.right) {_x = this.limits.right;}
			if(_x < this.limits.left) {_x = this.limits.left;}
		
			this.o.back.css({ left: _x });
			this.o.knob.css({ left: _x });
		
			this._x = _x;
		}
	},
	
	onmouseup: function(evt){
		if(!this.is.disabled){
			if(this.is.toclick){
				this.onclick.apply(this, [evt]);
			} else {
				this.is.toclick = true;
				if(this._x != this.limits.left || this._x != this.limits.right){
					if(Math.abs(this._x) > Math.round((Math.abs(this.limits.left)-Math.abs(this.limits.right))/2)) {
						this.correct(this.limits.left);
					} else {
						this.correct(this.limits.right);
					}
				}
			}
		}
	},
	
	
	onclick: function(evt){
		switch(this.getState()){
			case 0:
				this.correct(this.limits.right);
				break;
			case 1:
				this.correct(this.limits.left);
				break;
		}
	},
	
	correct: function(x){
		this._x = x;
		this.o.back.animate({ left: this._x }, 200);
		this.o.knob.animate({ left: this._x }, 200);
		var _state = this.getState();
		if(this.state != _state){
			this.state = _state;
			this.handler();
		}
	},
	
	handler: function(){
		if(this.o.root.attr('handler')){
			this.is.disabled = true;
			var _p = this.o.ptr.find("p");
			if(!_p.find("img").length) {
  		  		_p.html("<img src='/images/ajax-loader-white.gif' alt='' />");
  		}
			
  		this.o.root.trigger('switched', [this]);
			eval(this.o.root.attr('handler'));
		}
		
		if(this.o.root.attr('handler_always')){
			eval(this.o.root.attr('handler_always'));
		}
    
    if(this.linked_input) {
  		this.linked_input.val(this.getState() == 1 ? this.left_value : this.right_value);
    }
	},
	
	getState: function(){
		if(this._x == this.limits.left) {
			return 0;
		} else if(this._x == this.limits.right) {
			return 1;
		} else {
			return this._state;
		}	
	}
});



/*global, jQuery, Switcher, current_user_id*/

var oRating = {
	ratingQueue: [],
	append:function(sId){
		sId = '#' + sId;
		for(var i=0; i<this.ratingQueue.length; i++){
			if(this.ratingQueue[i].sId == sId){
			  delete this.ratingQueue[i];
			  this.ratingQueue[i] = new Switcher(sId);
				return;
			}
		}
  	this.ratingQueue[this.ratingQueue.length] = new Switcher(sId);
	},
	each: function(callback){
		for (var i=0; i < this.ratingQueue.length; i++) {
			var b = callback.call(this.ratingQueue[i], [i]);
			if(b === false) {break;}
		}
	},
	resources : {}
};

(function(){
oRating.infos = {};
  
oRating.switched = function(switcher_object, info, rating) {
  jQuery.post("/votes", {klass : info.klass, id : info.id, operator : (info.voted ? '-' : '+')}, function(voted) {
    info.voted = voted;
    if(info.voted) {
      info.voters_count++;
    } else {
      info.voters_count--;
    }
    if(info.voters_count < 0) {info.voters_count = 0;}
    switcher_object.is.disabled = false;
    //switcher_object.state = info.voted ? 1 : 0;
    
    var value_html = "" + info.voters_count;
    if(info.friends_count) {
      value_html = value_html + "<span class='friends_count'>"+info.friends_count+"</span>";
    }
    jQuery(rating).find(".value").html(value_html);
  }, "json");  
}

oRating.appendAllRatings = function() {
  setTimeout(function() {
    for(dom_id in oRating.infos) {
      oRating.append(dom_id)
    }
  }, 50);
}

oRating.personalizeRating = function(dom_id) {
  var rating = jQuery("#"+dom_id);
  var info = oRating.infos[dom_id];
  var switcher = jQuery("#switcher_"+dom_id);
  if(current_user_id) {
    rating.removeClass('disable');
    switcher.removeClass("switcher-disabled");

    switcher.attr('handler', 'true;');
    switcher.bind('switched', function(event, switcher_object) {
      oRating.switched(switcher_object, info, rating)
    });

    if(info.voted) {
      switcher.addClass("switcher-left");
    }
    if(info.friends_count) {
      jQuery("#friends_count_"+dom_id).html(''+info.friends_count).show();
    }
  }
  switcher.css('visibility', 'visible');
//  oRating.append(dom_id);
};
})();

jQuery(oRating.appendAllRatings);

var scroller = function() {
	this.initialize.apply(this, arguments);
}

scroller.prototype = {
	initialize: function(selector, options) {
		options = options || new Object();
		this.visible_children = options.visible_children || 1;
		this.selector = selector;
		this.locked = false;
		this.loop = options.loop || false;
		this.data_selector = options.data_selector || ".data";
		this.scroll_window = jQuery(selector + ".body").get(0);
		this.content_width = options.content_width || (this.scroll_window && this.scroll_window.clientWidth);
		this.new_page_callback = options.new_page_callback;
		this.before_move_func = options.before_move_func;
		if(!this.content_width) return;
		
		/* Ajax loading !! */
		this.url = options.url;
		this.per_page = options.per_page;
		this.total_pages = options.total_pages;
		this.current_page = options.current_page;
		
		if(this.url)
			this.ajax = true;
		else
		{
			this.ajax = false;
		}
		if(this.ajax) {
			this.markFirstLast();
		} else {
			jQuery(selector+" "+this.data_selector+":first").addClass("first");
			jQuery(selector+" "+this.data_selector+":last").addClass("last");
		}
		this.duration = options.duration || 0.7;
		this.steps = options.steps || 100;
		var self = this;
		jQuery(selector+" .left").click(function() {self.scrollLeft(); return false; });
		jQuery(selector+" .right").click(function() {self.scrollRight(); return false; });
		jQuery(selector).bind('mousewheel', function(event){self.mouseSideScroll(event); return false;})
		//this.tryShowHideNav();
		
	},
	
	markFirstLast: function() {
		if(this.current_page == 1) {
			jQuery(this.selector+" "+this.data_selector+":first").addClass("first");
		}
		if(this.current_page == this.total_pages) {
			jQuery(this.selector+" "+this.data_selector+":last").addClass("last");
		}
	},

	tryShowHideNav: function() {
		if(this.hideNavigationControls()) {
			jQuery(this.selector).find(".left").css('visibility', 'hidden').end().find(".right").css('visibility', 'hidden');
		} else {
			jQuery(this.selector).find(".left").css('visibility', 'visible').end().find(".right").css('visibility', 'visible');
		}
	},
	
	hideNavigationControls: function() {
		if(this.ajax) {
			this.activateDeactivateNav();
			return this.total_pages <= 1;
		} else {
			var data = jQuery(this.selector).find(this.data_selector).get();
			return (data.length < this.visible_children+1);
		}
	},
	
	mouseSideScroll: function(event) {
		if(!event.wheelDeltaX) return;
		this.scroll(event.wheelDeltaX > 0);
	},
	
	activateDeactivateNav: function() {
		if(!this.ajax) return;
		jQuery(this.selector + " .left, "+this.selector + " .right").removeClass("not-act");
		if(this.current_page == 1) {
			jQuery(this.selector + " " + ".left").addClass("not-act");
		}
		if(this.current_page == this.total_pages) {
			jQuery(this.selector + " " + ".right").addClass("not-act");
		}
	},
	
	preloadPage: function(left) {
		var new_page = left ? this.current_page - 1 : this.current_page + 1;
		if(new_page < 1 || new_page > this.total_pages) { this.locked = false; return; }
		jQuery(this.selector + " .left, "+this.selector + " .right").addClass("not-act");
		jQuery(this.selector + " .scroll-overlay").show();
		var self = this;
		jQuery.ajax({
		url : this.url, 
		data : {page : new_page, per_page : this.per_page}, 
		success : function(reply) {
			jQuery(self.selector + " " + self.data_selector + ":last").after(reply);
			self.current_page = new_page;
			jQuery(self.selector + " .scroll-overlay").hide();
			self.locked = false;
			self.realScroll(left);
		},
		error : function() { 
			jQuery(self.selector + " .scroll-overlay").hide();
			self.locked = false; 
		},
		dataType : "html"});
	},

	scroll: function(left) {
		if(this.locked) return;
		if(this.ajax) {
			this.locked = true;
			this.preloadPage(left)
		} else {
			this.realScroll(left);
		}
	},

	realScroll: function(left) {
		var data = jQuery(this.selector+" "+this.data_selector).get();
		if(data.length < this.visible_children+1) return;
		if(!this.loop) {
			if(left && jQuery(data[0]).hasClass("first")) {return;}
			if(!left && data[this.visible_children] && jQuery(data[this.visible_children]).hasClass("first")) { return;}
		}
		
		this.locked = true;

		if(left) {
			data[data.length - 1].style.position = "absolute";
			data[data.length - 1].style.left = "-"+this.content_width+"px";
			data[0].parentNode.insertBefore(data[data.length - 1], data[0]);
			data = jQuery(this.selector+" "+this.data_selector).get();
			var shift = -1;
			var sign = 1;
		} else {
			var shift = 0;
			var sign = -1;
		}
		
		if (this.before_move_func) {
			this.before_move_func(data[this.current_page-sign]);
		}
		
		for(var i = 0; i < data.length; i++) {
			data[i].style.position = "absolute";
			data[i].style.left = this.content_width*(i+shift)+"px";
		}
		
		var self = this;
		this.mover(function(percent) {
			var offset = Math.round(self.content_width*(shift + sign * percent));
			for(var i = 0; i <= self.visible_children; i++) {
				data[i].style.left = "" + (self.content_width*i + offset)+"px";
			}
		}, function() {
				if(!left) {
				data[0].parentNode.appendChild(data[0]);
			}
			jQuery(self.selector+" "+self.data_selector).css('position', '').css('left', '');
			if(self.ajax) {
				for(var i = 0; i < self.visible_children; i++) {
					jQuery(self.selector + " " + self.data_selector + ":last").remove();
				}
				self.activateDeactivateNav();
			}
			if(self.new_page_callback) {
				self.new_page_callback(self.total_pages, self.current_page);
			}
			self.current_page -= sign;
			self.locked = false;
		});
	},
	
	scrollLeft: function() {
		this.scroll(true);
	},
	
	
	scrollRight: function() {
		this.scroll(false);
	},

	mover: function(offsetter, callback) {
		var self = this;
		var timeout = this.duration*970.0 / this.steps;
		var step_count = 0;
		var runner = function(){
			var steps_left = self.steps - step_count;
			offsetter(1.0 - steps_left*1.0/self.steps);
			step_count += 1;
			if(step_count <= self.steps) { window.setTimeout(runner, timeout);}
			else { callback(); }
		};
		window.setTimeout(runner, timeout);		
	}
};

scroller.prototype.constructor = scroller;

(function(b){function d(e,f){this.options=e;if(!e.cacheLength||e.cacheLength<1){this.options.cacheLength=1}this.persistent={};if(this.options.data){this.initPersistent(this.options.data)}this.flush()}d.prototype={flush:function(){this.data={};this.length=0},initPersistent:function(j){var e="",g={},l=[];for(var h=0;h<j.length;h++){l=j[h].strip();if(l.length>0){e=l.substring(0,1).toLowerCase();if(!g[e]){g[e]=[]}g[e].push(l)}}for(var f in g){this.addToPersistent(f,g[f])}},addToPersistent:function(f,e){if(!e||!f){return}this.persistent[f]=e},lookup:function(e){return this.merge(this.lookupPersistent(e),this.lookupTemp(e))},merge:function(g,f){var e=[];if(g){for(var h=0;h<g.length;h++){e[h]=g[h]}}if(f){for(h=0;h<f.length;h++){if(!e.include(f[h])){e[e.length]=f[h]}}}return e.length>0?e:null},lookupTemp:function(e){return this.lookupCache(e,this.data)},lookupPersistent:function(e){return this.lookupCache(e,this.persistent)},lookupCache:function(m,g){if(!m||!g){return null}if(g[m]){return g[m]}if(this.options.matchSubset){var l=[];for(var k=m.length-1;k>=this.options.minChars;k--){var f=m.substr(0,k);var n=g[f];if(n){for(var h=0;h<n.length;h++){var e=n[h];if(l.indexOf(e)==-1&&this.matchSubset(e,m)){l[l.length]=e}}}}l=l.uniq();if(l.length>0){g[m]=l;return l}}return null},matchSubset:function(g,f){if(!g){return false}if(!this.options.matchCase){g=g.toLowerCase()}var e=g.indexOf(f);if(e==-1){return false}return e===0||this.options.matchContains},store:function(f,e){if(!e||!f){return}if(!this.length){this.flush();this.length++}else{if(!this.data[f]){this.length++}}this.data[f]=e}};function a(e,g,f){var h=g.get(0);document.body.appendChild(h);this.results=b(h);this.timeout=null;this.input=e;this.results.hide();this.options=f;this.active=-1;if(typeof(this.options.width)=="string"){this.options.width=parseInt(this.options.width,10)}if(this.options.width>0){this.results.css("width",this.options.width)}}a.prototype={moveUp:function(){this.moveSelect(-1)},moveDown:function(){this.moveSelect(1)},moveSelect:function(g){var f=b("li",this.results);if(!f){return}this.active+=g;if(this.active<0){this.active=0}else{if(this.active>=f.size()){this.active=f.size()-1}}f.removeClass("selected");b(f[this.active]).addClass("selected");if(this.options.autoFill){var e=b(f[this.active]).get(0);this.input.autoFill(e.selectData||e.selectValue)}},hideNow:function(){if(this.timeout){clearTimeout(this.timeout)}if(this.results.is(":visible")){this.results.hide()}if(this.options.mustMatch){var e=this.input.value();if(e!=this.input.lastSelected){this.selectItem(null)}}},hide:function(){if(this.timeout){clearTimeout(this.timeout)}var e=this;this.timeout=setTimeout(function(){e.hideNow()},200)},selectCurrent:function(){var e=b("li.selected",this.results)[0];if(!e){var f=b("li",this.results);if(this.options.selectOnly){if(f.length==1){e=f[0]}}else{if(this.options.selectFirst){e=f[0]}}}if(e){this.selectItem(e);return true}else{return false}},show:function(){var f=(this.input.input.offset().top+this.input.input.get(0).offsetHeight);var e=this.input.input.offset().left;this.results.css({position:"absolute",width:(this.options.width+6)+"px",top:f+"px",left:e+"px",zIndex:999}).show().find("li:first").addClass("selected").end()},selectItem:function(e){if(!e){e=document.createElement("li");e.extra=[];e.selectValue=""}var f=b.trim(e.selectValue?e.selectValue:e.innerHTML);this.results.html("");this.input.trigger("valueselected",[f]);if(this.options.onItemSelect){setTimeout(function(){this.options.onItemSelect(e)},1)}},dataToDom:function(e){var h=document.createElement("ul");var g=e.length;var m=this;if((this.options.maxItemsToShow>0)&&(this.options.maxItemsToShow<g)){g=this.options.maxItemsToShow}for(var f=0;f<g;f++){var n=e[f];var k=document.createElement("li");if(this.options.formatItem){k.innerHTML=this.options.formatItem(n,f,g);k.selectValue=n}else{if(typeof(n)!="string"){k.innerHTML="";for(var j in n){if(n[j]){k.innerHTML+="<span>"+n[j]+"</span>"}}k.selectValue=n[this.options.select];k.selectData=n}else{k.innerHTML=n;k.selectValue=n}}h.appendChild(k)}var l=b(h).find("li");l.hover(function(){l.removeClass("selected");b(this).addClass("selected");m.active=l.indexOf(b(this).get(0))},function(){b(this).removeClass("selected")}).click(function(i){i.preventDefault();i.stopPropagation();m.selectItem(this)});return h},loadData:function(e){this.results.html("");if(b.browser.msie){}this.active=0;this.results.append(this.dataToDom(e))}};function c(f,h,g){this.input=b(f);this.input.get(0).autocompleter=h;this.input.attr("autocomplete","off");this.id="#"+this.input.attr("id");this.spinner=b(this.id+"_auto_complete_indicator");this.spinner.css("left",(parseInt(this.input.css("width"),10)-15)+"px");this.options=g;if(g.inputClass){this.input.addClass(g.inputClass)}this.hasFocus=false;this.lastKeyPressCode=null;var i=null;this.prev="";var e=this;this.input.keydown(function(j){this.lastKeyPressCode=j.keyCode;switch(j.keyCode){case 38:j.preventDefault();e.input.trigger("moveup");break;case 40:j.preventDefault();e.input.trigger("movedown");break;case 13:e.input.trigger("selectcurrent");j.preventDefault();return false;break;default:if(i){clearTimeout(i)}i=setTimeout(function(){e.change()},g.delay);break}}).focus(function(){e.hasFocus=true}).blur(function(){e.hasFocus=false;e.input.trigger("valuehide")})}c.prototype={change:function(){if(this.lastKeyPressCode==46||(this.lastKeyPressCode>8&&this.lastKeyPressCode<32)){return this.input.trigger("hide")}var e=this.value();if(e==this.prev){return}this.prev=e;if(e.length>=this.options.minChars){this.startLoad();this.input.trigger("valuechange",[e])}else{this.stopLoad();this.input.trigger("valuehide")}},startLoad:function(){this.input.addClass(this.options.loadingClass)},stopLoad:function(){this.input.removeClass(this.options.loadingClass);this.hideSpinner()},showSpinner:function(){this.spinner.show()},hideSpinner:function(){this.spinner.hide()},value:function(){var e=this.input.val().split(",").map(function(f){return f.strip()});return e.length>0?e[e.length-1]:""},setValue:function(e){var g=this.input.val().split(",").map(function(h){return h.strip()});var f=g.pop();g[g.length]=e;this.input.val(g.join(",")).trigger("valueSet",e)},currentStartPosition:function(){var e=this.input.val().lastIndexOf(",");return e>0?e+1:0},createSelection:function(i,f){var g=this.input.get(0);var h=this.currentStartPosition();i=h+i;f=h+f;if(g.createTextRange){var e=g.createTextRange();e.collapse(true);e.moveStart("character",i);e.moveEnd("character",f);e.select()}else{if(g.setSelectionRange){g.setSelectionRange(i,f)}else{if(g.selectionStart){g.selectionStart=i;g.selectionEnd=f}}}g.focus()},autoFill:function(e){var g=typeof(e)=="string"?e:e[this.options.select];if(this.lastKeyPressCode==8){return false}if(typeof(e)!="string"&&this.options.fieldPrefix){for(var f in e){b("#"+this.options.fieldPrefix+"_"+f).val(e[f])}}this.setValue(g);this.createSelection(this.prev.length,g.length)},receiveData:function(f,e){if(!this.hasFocus||!e.length){return false}if(this.options.autoFill&&(this.value().toLowerCase()==f.toLowerCase())){this.autoFill(e[0])}return true},trigger:function(e,f){this.input.trigger(e,f)}};b.autocomplete=function(j,o){var m=new c(j,this,o);if(!o.width>0){o.width=m.input.css("width")}var i=new a(m,b(m.id+"_auto_complete"),o);var e=new d(o,o.data);var n=false;var l=false;var h=null;function g(s){var p=o.url+"/"+encodeURI(s);for(var r in o.extraParams){p+="&"+r+"="+encodeURI(o.extraParams[r])}if(o.with_params&&typeof(o.with_params)=="function"){p+=o.with_params()}return p}function k(r,p){if(p){m.stopLoad();if(!m.receiveData(r,p)){return i.hideNow()}i.loadData(p);i.show()}else{i.hideNow()}}function f(p,r){if(!o.matchCase){r=r.toLowerCase()}var s=e.lookup(r);if(s){k(r,s)}if((!s||s.length<o.maxItemsToShow)&&(typeof o.url=="string")&&(o.url.length>0)){h=b.ajax({url:g(r),async:true,beforeSend:function(){m.showSpinner()},dataType:"json",complete:function(){m.hideSpinner()},success:function(q){e.store(r,q);k(r,e.merge(s,q))}})}else{m.stopLoad()}}m.input.bind("moveup",function(){i.moveUp()}).bind("movedown",function(){i.moveDown()}).bind("selectcurrent",function(){i.selectCurrent()}).bind("valuehide",function(){i.hide();m.stopLoad()}).bind("valuechange",f).bind("valueselected",function(q,p){m.lastSelected=p;this.prev=p;if(h){m.hideSpinner();h=null}m.setValue(p);e.flush()})};jQuery.fn.autocomplete=function(f,e,g){e=e||{};e.url=f;e.data=(g&&(typeof g=="object")&&(g.constructor==Array))?g:null;e.inputClass=e.inputClass||"ac_input";e.resultsClass=e.resultsClass||"ac_results";e.lineSeparator=e.lineSeparator||"\n";e.cellSeparator=e.cellSeparator||"|";e.minChars=e.minChars||2;e.delay=e.delay||400;e.matchCase=e.matchCase||0;e.matchSubset=e.matchSubset||true;e.matchContains=e.matchContains||false;e.cacheLength=e.cacheLength||1;e.mustMatch=e.mustMatch||0;e.extraParams=e.extraParams||{};e.loadingClass=e.loadingClass||"ac_loading";e.selectFirst=e.selectFirst||false;e.selectOnly=e.selectOnly||false;e.maxItemsToShow=e.maxItemsToShow||8;e.autoFill=e.autoFill||true;e.width=parseInt(e.width,10)||0;this.each(function(){var h=this;var i=new jQuery.autocomplete(h,e)});return this};jQuery.fn.autocompleteArray=function(f,e){return this.autocomplete(null,e,f)};jQuery.fn.indexOf=function(g){for(var f=0;f<this.length;f++){if(this[f]==g){return f}}return -1}})(jQuery);
(function(d){d.each(["backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","color","outlineColor"],function(f,e){d.fx.step[e]=function(g){if(g.state==0){g.start=c(g.elem,e);g.end=b(g.end)}g.elem.style[e]="rgb("+[Math.max(Math.min(parseInt((g.pos*(g.end[0]-g.start[0]))+g.start[0]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[1]-g.start[1]))+g.start[1]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[2]-g.start[2]))+g.start[2]),255),0)].join(",")+")"}});function b(f){var e;if(f&&f.constructor==Array&&f.length==3){return f}if(e=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(f)){return[parseInt(e[1]),parseInt(e[2]),parseInt(e[3])]}if(e=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(f)){return[parseFloat(e[1])*2.55,parseFloat(e[2])*2.55,parseFloat(e[3])*2.55]}if(e=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(f)){return[parseInt(e[1],16),parseInt(e[2],16),parseInt(e[3],16)]}if(e=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(f)){return[parseInt(e[1]+e[1],16),parseInt(e[2]+e[2],16),parseInt(e[3]+e[3],16)]}return a[d.trim(f).toLowerCase()]}function c(g,e){var f;do{f=d.curCSS(g,e);if(f!=""&&f!="transparent"||d.nodeName(g,"body")){break}e="backgroundColor"}while(g=g.parentNode);return b(f)}var a={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery);
(function(a){a.baseClass=function(b){b=a(b);var c=b.get(0).className.match(/([^ ]+)/);if(c&&c[1]){return c[1]}return false};a.fn.addDependClass=function(d,b){var c={delimiter:b?b:"-"};return this.each(function(){var e=a.baseClass(this);if(e){a(this).addClass(e+c.delimiter+d)}})};a.fn.removeDependClass=function(d,b){var c={delimiter:b?b:"-"};return this.each(function(){var e=a.baseClass(this);if(e){a(this).removeClass(e+c.delimiter+d)}})};a.fn.toggleDependClass=function(d,b){var c={delimiter:b?b:"-"};return this.each(function(){var e=a.baseClass(this);if(e){if(a(this).is("."+e+c.delimiter+d)){a(this).removeClass(e+c.delimiter+d)}else{a(this).addClass(e+c.delimiter+d)}}})}})(jQuery);
jQuery.cookie=function(b,j,m){if(typeof j!="undefined"){m=m||{};if(j===null){j="";m.expires=-1}var e="";if(m.expires&&(typeof m.expires=="number"||m.expires.toUTCString)){var f;if(typeof m.expires=="number"){f=new Date();f.setTime(f.getTime()+(m.expires*24*60*60*1000))}else{f=m.expires}e="; expires="+f.toUTCString()}var l=m.path?"; path="+(m.path):"";var g=m.domain?"; domain="+(m.domain):"";var a=m.secure?"; secure":"";document.cookie=[b,"=",encodeURIComponent(j),e,l,g,a].join("")}else{var d=null;if(document.cookie&&document.cookie!==""){var k=document.cookie.split(";");for(var h=0;h<k.length;h++){var c=jQuery.trim(k[h]);if(c.substring(0,b.length+1)==(b+"=")){d=decodeURIComponent(c.substring(b.length+1));break}}}return d}};
(function(d){d.fn.tabby=function(f){var g=d.extend({},d.fn.tabby.defaults,f);var h=d.fn.tabby.pressed;return this.each(function(){$this=d(this);var i=d.meta?d.extend({},g,$this.data()):g;$this.bind("keydown",function(k){var j=d.fn.tabby.catch_kc(k);if(16==j){h.shft=true}if(17==j){h.ctrl=true;setTimeout(function(){d.fn.tabby.pressed.ctrl=false},1000)}if(18==j){h.alt=true;setTimeout(function(){d.fn.tabby.pressed.alt=false},1000)}if(9==j&&!h.ctrl&&!h.alt){k.preventDefault;h.last=j;setTimeout(function(){d.fn.tabby.pressed.last=null},0);e(d(k.target).get(0),h.shft,i);return false}}).bind("keyup",function(j){if(16==d.fn.tabby.catch_kc(j)){h.shft=false}}).bind("blur",function(j){if(9==h.last){d(j.target).one("focus",function(k){h.last=null}).get(0).focus()}})})};d.fn.tabby.catch_kc=function(f){return f.keyCode?f.keyCode:f.charCode?f.charCode:f.which};d.fn.tabby.pressed={shft:false,ctrl:false,alt:false,last:null};function b(f){if(window.console&&window.console.log){window.console.log("textarea count: "+f.size())}}function e(i,h,g){var f=i.scrollTop;if(i.setSelectionRange){a(i,h,g)}else{if(document.selection){c(i,h,g)}}i.scrollTop=f}d.fn.tabby.defaults={tabString:String.fromCharCode(9)};function a(j,f,u){var t=j.selectionStart;var r=j.selectionEnd;if(t==r){if(f){if("\t"==j.value.substring(t-u.tabString.length,t)){j.value=j.value.substring(0,t-u.tabString.length)+j.value.substring(t);j.focus();j.setSelectionRange(t-u.tabString.length,t-u.tabString.length)}else{if("\t"==j.value.substring(t,t+u.tabString.length)){j.value=j.value.substring(0,t)+j.value.substring(t+u.tabString.length);j.focus();j.setSelectionRange(t,t)}}}else{j.value=j.value.substring(0,t)+u.tabString+j.value.substring(t);j.focus();j.setSelectionRange(t+u.tabString.length,t+u.tabString.length)}}else{var v=j.value.split("\n");var s=new Array();var l=0;var h=0;var g=false;for(var n in v){h=l+v[n].length;s.push({start:l,end:h,selected:(l<=t&&h>t)||(h>=r&&l<r)||(l>t&&h<r)});l=h+1}var k=0;for(var n in s){if(s[n].selected){var q=s[n].start+k;if(f&&u.tabString==j.value.substring(q,q+u.tabString.length)){j.value=j.value.substring(0,q)+j.value.substring(q+u.tabString.length);k-=u.tabString.length}else{if(!f){j.value=j.value.substring(0,q)+u.tabString+j.value.substring(q);k+=u.tabString.length}}}}j.focus();var p=t+((k>0)?u.tabString.length:(k<0)?-u.tabString.length:0);var m=r+k;j.setSelectionRange(p,m)}}function c(q,w,f){var p=document.selection.createRange();if(q==p.parentElement()){if(""==p.text){if(w){var l=p.getBookmark();p.moveStart("character",-f.tabString.length);if(f.tabString==p.text){p.text=""}else{p.moveToBookmark(l);p.moveEnd("character",f.tabString.length);if(f.tabString==p.text){p.text=""}}p.collapse(true);p.select()}else{p.text=f.tabString;p.collapse(false);p.select()}}else{var k=p.text;var n=k.length;var u=k.split("\r\n");var z=document.body.createTextRange();z.moveToElementText(q);z.setEndPoint("EndToStart",p);var m=z.text;var x=m.split("\r\n");var r=m.length;var y=document.body.createTextRange();y.moveToElementText(q);y.setEndPoint("StartToEnd",p);var v=y.text;var g=document.body.createTextRange();g.moveToElementText(q);g.setEndPoint("StartToEnd",z);var s=g.text;var h=d(q).html();d("#r3").text(r+" + "+n+" + "+v.length+" = "+h.length);if((r+s.length)<h.length){x.push("");r+=2;if(w&&f.tabString==u[0].substring(0,f.tabString.length)){u[0]=u[0].substring(f.tabString.length)}else{if(!w){u[0]=f.tabString+u[0]}}}else{if(w&&f.tabString==x[x.length-1].substring(0,f.tabString.length)){x[x.length-1]=x[x.length-1].substring(f.tabString.length)}else{if(!w){x[x.length-1]=f.tabString+x[x.length-1]}}}for(var t=1;t<u.length;t++){if(w&&f.tabString==u[t].substring(0,f.tabString.length)){u[t]=u[t].substring(f.tabString.length)}else{if(!w){u[t]=f.tabString+u[t]}}}if(1==x.length&&0==r){if(w&&f.tabString==u[0].substring(0,f.tabString.length)){u[0]=u[0].substring(f.tabString.length)}else{if(!w){u[0]=f.tabString+u[0]}}}if((r+n+v.length)<h.length){u.push("");n+=2}z.text=x.join("\r\n");p.text=u.join("\r\n");var j=document.body.createTextRange();j.moveToElementText(q);if(0<r){j.setEndPoint("StartToEnd",z)}else{j.setEndPoint("StartToStart",z)}j.setEndPoint("EndToEnd",p);j.select()}}}})(jQuery);
/*
 * XMLformat v0.2b : format one string XML or not good formatted XML to good tab-formatted XML
 * File: jquery.xmlformat.js
 * Copyright (c) 2009 Egor Hmelyoff, hmelyoff@gmail.com
 */


(function($) {
	// plugin definition
	
	var uid = 0;
	var getId = function(){
	  uid++;
	  return uid;
	};
	
	var OPTIONS = {
		isDebug: true,
		level: 0,
		
		ELEMENT_NODE   : 1,
		ATTRIBUTE_NODE : 2,
		TEXT_NODE      : 3,
		DOCUMENT_NODE  : 9,
		
		NODE_BLOCK     : 'block',
		NODE_INLINE    : 'inline',
		NODE_SELFCLOSED  : 'selfclosed',
		
		nodeTypes: {
			block      : ["DIV", "P", "UL", "OL", "LI"],
			inline     : ["STRONG", "B", "EM", "I", "SPAN", "A", "OBJECT", "DEL"],
			selfclosed   : ["BR", "HR", "IMG", "EMBED", "PARAM"]
		},
		
		attrFilter: [ // specific attrs for remove
		 "contentEditable",
		 /^jQuery(.*)?$/,
		 "start",
		 "loop"
		]
	};

	// Init global plugin method
	$.XMLformat = function(text, options){
		OPTIONS = $.extend(OPTIONS, options ? options : {});
    // save all links
		var links = save(text);
		text = links.text;
		OPTIONS.links = links.links;

		// clean up before actions
		text = cleanup(text);
		text = indent() + extract($(text));
		
		if($.browser.msie){
		  OPTIONS.objects = links.objects;
		  text = back(text);
		}

		return (text);
	};

	// Init plugin function
	$.fn.XMLformat = function(options){
		OPTIONS = $.extend(OPTIONS, options);
		
		return this.each(function(){
			XMLformat.call(this);
		});
	};

	var XMLformat = function(){
		var obj = $(this);
		switch(this.nodeName){
			case 'TEXTAREA':
				obj.val($.XMLformat(obj.val()));
				break;
			default:
				debug('unknown tag —> no actions');
				break;
		};
		
	};
	
	var cleanup = function(text){
		// remove all string modifiers
		text = text.replace(/[\n\t\0\v\f\r]/g, " ");
		
		// remove double[or more] spaces
		text = text.replace(/ {2,}/g, " ");

  	// remove spaces between block tags
  	var blocks = "div|p|hr|ul|ol|li|h1|h2|h3|h4|h5|h6|object|param|table|tbody|tr|td|th";
  	var rBlocks = new RegExp("<(\/)?(" + blocks + "){1}(\/)?> *<(" + blocks + "){1}>", "g")
  	text = text.replace(rBlocks, "<$1$2$3><$4>");

		// remove space at the beginning of the string
		// text = text.replace(/^ /g, "");
		
		// remove spaces between tags
		// text = text.replace(/<\/([^\/>]*)> *</g, "<\/$1><");
		// text = text.replace(/<([^ \>]*)? ?\/> *</g, "<$1 \/><");

		// remove spaces inside tag at the beginning/end of string
		// text = text.replace(/<(.*)?> *(.*?) *<\/ ?\1>/g, "<$1>$2</$1>");

		return (text);
	};
	
	var save = function(text){

  	var pattern_object = /<(object)([^>]*)>(.*?)(<\/ ?\1>)/ig;
	  
	  var result = [];
	  var objects = [];
	  text = text.replace(/img([^(:src=)]*)?src=(['"])([^'"]*)/gi, function(){
	    var stamp = "xmlformat-" + getId();//(new Date()).getTime() + Math.round(Math.random()*9999);
	    result.push({ url: arguments[3], stamp: stamp })
	    return "img" + arguments[1] + "src=" + arguments[2] + "/images/d.gif?" + stamp;
	  });
/*
	  text = text.replace(/href=(['"])([^'"]*)/gi, function(){
	    var stamp = "xmlformat-" + getId();//(new Date()).getTime() + Math.round(Math.random()*9999);
	    result.push({ url: arguments[2], stamp: stamp })
	    return "href=" + arguments[1] + stamp;
	  });
*/
		if($.browser.msie)
  	  text = text.replace(pattern_object, function(){
  	    var stamp = "xmlformat-" + getId();//(new Date()).getTime() + Math.round(Math.random()*9999);
  	    objects.push({ object: arguments[0], stamp: stamp })
  	    return '<img type="xmlformat_object" src="/images/d.gif?' + stamp + '" />';
      })

	  return { text: text, links: result, objects: objects };
	};
	
	var back = function(text){
  	var pattern_img = /<img([^>]*)\/?>/ig;

    return text.replace(pattern_img, function(){
      var attrs = getAttrs(arguments[1]);
      if(attrs.type == "xmlformat_object"){
        if(attrs.src){
          for (var i=0; i < OPTIONS.objects.length; i++) {
            if(attrs.src.split("?")[1] == OPTIONS.objects[i].stamp)
              return OPTIONS.objects[i].object;
            
          };
        }
        return "";
      } else {
        return arguments[0]
      }
    })

	};

  var getAttrs = function(string){
    var result;
    var attrs = {};
    var pattern = /(\w+)=(["']{1})([^\2]*?)(\2{1})/g;
    while((result = pattern.exec(string)) != null){
      attrs[result[1]] = result[3];
    }
    return attrs;
  };
	
	var extract = function(data, node){
		var result = new String();
		data.each(function(i){
			switch(this.nodeType){
				case OPTIONS.ELEMENT_NODE:
					result += createElement(this);
					break;

				case OPTIONS.TEXT_NODE:
				  if(data.length == 1 && node){
				    result += node.innerHTML;
				  } else {
  					result += this.nodeValue;
				  }
					break;
			};
		});
		return (result);
	};
	
	var createElement = function(node){
		var result = new String();

		// get next node if exist;
		var next = getNextNode(node);
		var prev = getPrevNode(node);

		switch(nodeType(node)){

			case OPTIONS.NODE_BLOCK:

				OPTIONS.level++;

				result += "<" + node.tagName.toLowerCase() + createAttrs(node) + ">";
					if(node.childNodes){
						if(nodeType(node.childNodes[0]) == OPTIONS.NODE_BLOCK){
							result += "\n" + indent();
						}
						result += extract($(node.childNodes), node);
					}
				result += "</" + node.tagName.toLowerCase() + ">";

				OPTIONS.level--;
				result += "\n";

				if(next)
					result += indent();
				else {
					OPTIONS.level--;
					result += indent();
					OPTIONS.level++;
				}
				
				break;


			case OPTIONS.NODE_INLINE:

				result += "<" + node.tagName.toLowerCase() + createAttrs(node) + ">";
					if(node.childNodes)
						result += extract($(node.childNodes), node);
				result += "</" + node.tagName.toLowerCase() + ">";

				break;


			case OPTIONS.NODE_SELFCLOSED:

				result += "<" + node.tagName.toLowerCase() + createAttrs(node) + " />";
				if(next && nodeType(next) == OPTIONS.NODE_BLOCK)
					result += "\n" + indent();

				break;
				
			default:
				break;
		};

		return (result);
	};
	
	var createAttrs = function(node){
		var result = new String();
			for (var i=0; i < node.attributes.length; i++) {
			  if(node.attributes[i].nodeValue && filterAttrs(node.attributes[i].nodeName)){
			    var name = node.attributes[i].nodeName.toLowerCase();
			    var value = node.attributes[i].nodeValue;
			    if(name == "src" || name == "href"){
			      for (var j=0; j < OPTIONS.links.length; j++) {
			        if(value.split("xmlformat")[1] && ('xmlformat' + value.split("xmlformat")[1]) == OPTIONS.links[j].stamp){
			        //if((value.split("?")[1] && value.split("?")[1] == OPTIONS.links[j].stamp) || value == OPTIONS.links[j].stamp){
		            value = OPTIONS.links[j].url;
		          }
			      };
			    }
				  result += ' ' + name + '="' + value + '"';
				}
			};
		return (result);
	}
	
	var filterAttrs = function(name){
	  for (var i=0; i < OPTIONS.attrFilter.length; i++) {
	    if(OPTIONS.attrFilter[i].test){
	      if(OPTIONS.attrFilter[i].test(name))
	        return false;
	    } else if(OPTIONS.attrFilter[i].toLowerCase() == name.toLowerCase())
	      return false;
	  };
	  return true;
	}
	
	var nodeType = function(node){
	  if(node){
  		switch(node.nodeType){
  			case OPTIONS.ELEMENT_NODE:
  				for(key in OPTIONS.nodeTypes){
  					for (var i=0; i < OPTIONS.nodeTypes[key].length; i++) {
  						if(OPTIONS.nodeTypes[key][i] == node.tagName)
  							return key;
  					};
  				}
  				break;

  			case OPTIONS.TEXT_NODE:
  				return OPTIONS.NODE_INLINE;
  				break;
  		}
  		// if undefined tag -> consider that is block node
  		return OPTIONS.NODE_BLOCK;
  	}
  	return false;
	};
	
	var indent = function(){
		var result = new String();

		for (var i=0; i < OPTIONS.level; i++) {
			result += "\t";
		};
		return (result);
	};

	var getPrevNode = function(node){
		_prev = $(node).prev();
		if(_prev.length) _prev = _prev.get(0);
		else _prev = null;
		return _prev;
	};
	
	var getNextNode = function(node){
		_next = $(node).next();
		if(_next.length) _next = _next.get(0);
		else _next = null;
		return _next;
	};
	
	// private function for debugging
	function debug(str) {
		if (window.console && window.console.log && OPTIONS.isDebug)
			window.console.log("[XMLformat: '" + str + "']");
	};

	// end of closure
})(jQuery);
(function(c){var a=c.scrollTo=function(f,e,d){c(window).scrollTo(f,e,d)};a.defaults={axis:"xy",duration:parseFloat(c.fn.jquery)>=1.3?0:1};a.window=function(d){return c(window)._scrollable()};c.fn._scrollable=function(){return this.map(function(){var e=this,d=!e.nodeName||c.inArray(e.nodeName.toLowerCase(),["iframe","#document","html","body"])!=-1;if(!d){return e}var f=(e.contentWindow||e).document||e.ownerDocument||e;return c.browser.safari||f.compatMode=="BackCompat"?f.body:f.documentElement})};c.fn.scrollTo=function(f,e,d){if(typeof e=="object"){d=e;e=0}if(typeof d=="function"){d={onAfter:d}}if(f=="max"){f=9000000000}d=c.extend({},a.defaults,d);e=e||d.speed||d.duration;d.queue=d.queue&&d.axis.length>1;if(d.queue){e/=2}d.offset=b(d.offset);d.over=b(d.over);return this._scrollable().each(function(){var l=this,j=c(l),k=f,i,g={},m=j.is("html,body");switch(typeof k){case"number":case"string":if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(k)){k=b(k);break}k=c(k,this);case"object":if(k.is||k.style){i=(k=c(k)).offset()}}c.each(d.axis.split(""),function(q,r){var s=r=="x"?"Left":"Top",u=s.toLowerCase(),p="scroll"+s,o=l[p],n=a.max(l,r);if(i){g[p]=i[u]+(m?0:o-j.offset()[u]);if(d.margin){g[p]-=parseInt(k.css("margin"+s))||0;g[p]-=parseInt(k.css("border"+s+"Width"))||0}g[p]+=d.offset[u]||0;if(d.over[u]){g[p]+=k[r=="x"?"width":"height"]()*d.over[u]}}else{var t=k[u];g[p]=t.slice&&t.slice(-1)=="%"?parseFloat(t)/100*n:t}if(/^\d+$/.test(g[p])){g[p]=g[p]<=0?0:Math.min(g[p],n)}if(!q&&d.queue){if(o!=g[p]){h(d.onAfterFirst)}delete g[p]}});h(d.onAfter);function h(n){j.animate(g,e,d.easing,n&&function(){n.call(this,f,d)})}}).end()};a.max=function(j,i){var h=i=="x"?"Width":"Height",e="scroll"+h;if(!c(j).is("html,body")){return j[e]-c(j)[h.toLowerCase()]()}var g="client"+h,f=j.ownerDocument.documentElement,d=j.ownerDocument.body;return Math.max(f[e],d[e])-Math.min(f[g],d[g])};function b(d){return typeof d=="object"?d:{top:d,left:d}}})(jQuery);
/* Global browsers definition */

(function($) {

	$(document.documentElement).addClass("b-loaded");

	if($.browser.safari)
		$(document.documentElement)
		  .addClass("b-khtml")
		  .addClass("b-safari");

	if($.browser.mozilla)
		$(document.documentElement).addClass("b-gecko");

	if($.browser.opera)
		$(document.documentElement).addClass("b-opera");


})(jQuery);


(function($) {
		jQuery("#days li a").click(function ()
		{
			jQuery('#afisha-loader').show();
			jQuery('#days li a').removeClass('active');
			jQuery(this).addClass('active');
			//jQuery('#events_title a').text(jQuery(this).text());
			new Ajax.Request(
				jQuery(this).attr('href')+'?ajax=1', 
				{
					asynchronous:true,
					evalScripts:false,
					onComplete:function(request){jQuery('#afisha-loader').hide();},
					onSuccess: function(transport) {Element.replace('afiash_content', transport.responseText);},
				}); return false;
		}
		);
})(jQuery);

(function( $ ){

  this.EventsViewtypeButton = {
    init: function(wrapper){
      this.events_wrapper = $(wrapper);
      var init_state = $.cookie("events_viewtype") || "mosaic";
      this.events_wrapper.addDependClass(init_state, "_");
    },
    onstatechange: function(old_state, new_state){
      $.cookie("events_viewtype", new_state);
      this.events_wrapper.removeDependClass(old_state, "_").addDependClass(new_state, "_");

      for (var i=0; i < oRating.ratingQueue.length; i++) {
        oRating.ratingQueue[i].reinit();
      };
    }
  };

  var Events = {
      hide_repeated_dates: function(){
      var prev_date = false;
      jQuery(".b-event").each(function(){
        var event = $(this);
        var date = event.find(".j-event-date").html();
        if(date == prev_date){
          event.addDependClass("dateless", "_");
        }
        prev_date = date;
      });
    }
  };

  jQuery(function() {
    EventsViewtypeButton.init(".b-events");

    // Скрываем повторяющиеся даты на странице топиков и на главной
    //if(document.location.pathname.match(/\/$|\/topic/)){
      Events.hide_repeated_dates();
    //}
  });
  
})( jQuery );

