/*
Scrollable Layer
Works for IE7, FF3, Chrome, Safari, Opera
By: Chonla
Create Date: 13 July 2009
#1 Modify Date: 6 August 2009
+ Add smooth option to smoothen scrolling.
URL: http://blog.chonla.com
*/

(function($)
{
	$.fn.scrollads=function(options)
	{
		var defaults=
		{
			closer:null,
			hsnap:"none",
			vsnap:"none",
			left:0,
			top:0,
			smooth:"false"
		};
		var pat=/^(\d+)px$/;
		options=$.extend(defaults,options);
		if(typeof(scrollables)=="undefined")
		{
			scrollables=new Array();
		}
		this.each(function(){
			var o=$(this);
			var id=o.attr("scrollees_id");
			if(typeof(id)=="undefined")
			{
				scrollables.push(this);
				o.attr("scrollees_id",scrollables.length);
			}
			switch(options.hsnap)
			{
				case"left":o.css("left","0px");
				break;
				case"right":o.css("right","0px").css("margin-right","0px");
				break;
				case"center":o.css("left","-"+parseInt(o.width()/2)+"px").css("margin-left","50%");
				break;
				case"custom":o.css("left",options.left);
				break;
			}
			switch(options.vsnap)
			{
				case"top":o.attr("offsettop",0);
				break;
				case"bottom":border=parseInt(pat.exec(o.css("border-top-width")))+parseInt(pat.exec(o.css("border-bottom-width")));o.attr("offsettop",$(window).height()-o.height()-border);
				break;
				case"middle":border=parseInt(pat.exec(o.css("border-top-width")))+parseInt(pat.exec(o.css("border-bottom-width")));o.attr("offsettop",parseInt(($(window).height()-o.height()-border)/2));
				break;
				case"custom":o.attr("offsettop",options.top);break;
				default:o.attr("offsettop",0);
			}
			if(options.closer)
			{
				o.find(options.closer).click(function(){
					o.html("").hide();
				});
			}
			o.attr("smooth",options.smooth);
			$(this).css("position","absolute");
		});
		$(window).scroll(function(){
			$(scrollables).each(function(){
				var o=$(this);
				if(o.css("display")!="none"){
					if(o.attr("smooth")=="false"){
						o.css("top",$(window).scrollTop()+parseInt(o.attr("offsettop")));
					}
					else
					{
						o.animate({top:$(window).scrollTop()+parseInt(o.attr("offsettop"))},{queue:false,duration:500});
					}
				}
			});
		}).scroll();
	};
})(jQuery);