// jQuery1.2.6対策.
(function($){
	$.each(['Height','Width'], function(i, name){
		var type = name.toLowerCase();
		var originalMethod = $.fn[ type ];
		$.fn[ type ] = function( size ) {
			return this[0] == window ? document.documentElement['client'+name] : originalMethod.call(this,size);
		};
	});
})($);

(function() {
	/**
	 * ObjectをWindowの特定の位置に固定します.
	 * @param {Object} options
	 * @author KAWAKITA Hirofumi.
	 */
	$.fn.fixed = function( options ){
		var w = $(window);
		var d = $(document);
		// 設定のデフォルト値指定.
		options = $.extend({
			"align" : "left", "valign" : "top", "offsetX" : 0, "offsetY" : 0
		}, options );
		var align  = options.align;
		var valign = options.valign;
		var type   = options.updateTrigger;
		var target = options.bindTarget;
		var oX = options.offsetX;
		var oY = options.offsetY;
        // 設定開始.
		this.each( function(){
			var t = $(this);
			// 既に設定済みの場合は解除する.
			if( $.data( t.get(0), "__fixed" ) != null ) $.data( t.get(0), "__fixed" )();
			// 横位置の設定.
			if( align == "center" ){
				var _update = function(){t.css("left",(w.width()-t.outerWidth(1))/2+oX);}
				w.resize( _update );
				if ( type && type != "") (target||t).bind(type,_update);
				_update();
			}else if( align == "right" ){
				t.css("right",oX);
			}else{
				t.css("left",oX);
			}
			// 縦位置の設定.
			// IE は fixed が使えないため処理をわける.
			var _updateV = null;
			if( $.browser.msie && $.browser.version == "6.0" ){
				t.css("position","absolute");
				if( valign == "bottom" ){
					_updateV = function(){t.css("top",w.height()-t.outerHeight(1)+d.scrollTop()+oY);}
				}else if( valign == "middle" ){
					_updateV = function(){t.css("top",(w.height()-t.outerHeight(1))/2+oY);}
				}else{
					_updateV = function(){t.css("top",d.scrollTop()+oY);}
				}
				w.scroll(_updateV);
			}else{
				t.css("position","fixed");
				if( valign == "bottom" ){
					_updateV = function(){t.css("top",w.height()-t.outerHeight(1)+oY);}
				}else if( valign == "middle" ){
					_updateV = function(){t.css("top",(w.height()-t.outerHeight(1))/2+oY);}
				}else{
					t.css("top", oY);
				}
			}
			if( _updateV ){
				w.resize(_updateV)
				if ( type && type != "") (target||t).bind(type,_updateV);
				_updateV();
			}
			// 解除用設定.
			$.data( t.get(0), "__fixed", function(){
				w.unbind("resize",_update).unbind("resize",_updateV).unbind("scroll",_updateV);
				t.css({"left": null,"top": null,"right": null});
				if ( type && type != ""){
					var _t = (target||t);
					_t.unbind(type,_update).unbind(type,_updateV);
				}
			});
		});
		return this;
	}
})($);