var script = new function()
{
	
	var $this = this;
	var timer;
	
	this.init = function()
	{
		$('#content').customScroller();
		
		$this.events();
		$this.resizer();
		$this.fadein_animation();
	}
	
	this.events = function()
	{
		$("a").live("mouseover",                    $this.link_mouseover    );
		$("a").live("mouseleave click",             $this.link_mouseleave   );
		
		$("input,textarea").live("mouseover",       $this.formelem_mouseover    );
		$("input,textarea").live("mouseleave click",$this.formelem_mouseleave   );
		
		$("#content").watch('height',               $this.resizer			);
		$(window).resize(							$this.resizer			);
	}
	
	this.resizer = function() {
		$('#content').height($(window).height() - 100);
		
		clearTimeout(timer);
		timer = setTimeout(function() {$('#content').setCustomScroller();},100);
	}
	
	this.link_mouseover = function()
	{
		$(this).animate({opacity:1},100,'easeInOutQuad',function()
		{
			$(this).animate({opacity:0.6},250,'easeInOutQuad');
		});
	}
	
	this.link_mouseleave = function()
	{
		$(this).stop();
		$(this).css({opacity:1});
	}
	
	this.formelem_mouseover = function()
	{
		var opacity = $(this).hasClass("button") ? 0.6 : 0.8;
		$(this).animate({opacity:1},100,'easeInOutQuad',function()
		{
			$(this).animate({opacity:opacity},250,'easeInOutQuad');
		});
	}
	
	this.formelem_mouseleave = function()
	{
		$(this).stop();
		$(this).animate({opacity:1.0},250);
		//$(this).css({opacity:1});
	}
	
	this.preloader = function()
	{
		var scripts = [];
		$("link[rel='script/deferred']").each( function() {
			scripts.push($(this));
		});
		
		if (scripts.length == 0)
		{
			return;
		}
		
		var num_dots = 5;
		
		$("body").append(
			$("<div>")
			.attr("id","loader")
			.width("100%")
			.height($(window).height())
			.css({position: "absolute", top: 0, left: 0, "z-index": 99999})
		);
		
		$("#loader").fadeIn().append(
			$("<span>")
			.css({position: "absolute", top: ($(window).height()/2)-50, left: 0, "z-index": 999999})
		);
		
		for (var i=0;i<num_dots;i++)
		{
			$("#loader > span").append($("<span>").addClass('normal').text('.'));
		}
		
		var pos = 1;
		var iv = setInterval(function()
		{
			if (pos==num_dots) pos = 1;
			$("#loader > span > span.bright").removeClass('bright');
			$("#loader > span > span:nth-child("+pos+")").addClass('bright');
			pos++;
		},100);
		
		var loaded = 0;
		for (var i=0; i < scripts.length; i++) {
			
			$.getScript(scripts[i].attr("href"),function() {
				loaded++;
				if (loaded == scripts.length)
				{
					setTimeout(function() {
						$this.preloader_finished(iv);
					},500);
				}
			});
			
		}
	}
	
	this.preloader_finished = function(iv) {
		$this.init();
		$("#loader").fadeOut(function() {
			clearInterval(iv);
			$("#loader").remove();
		});
	}
	
	this.fadein_animation = function() {
		var elems = [];
		$("#sidebar > .wrapper").hide();
		$("#content").find("div:contains(text), li, p, blockquote, a").each(function() {
			if (!$(this).is(":visible"))
				return;
			
			elems.push(this);
		})
		
		for (var i=0;i<elems.length;i++)
			$(elems[i]).css({visibility: "hidden", opacity: 0});
		
		var rand = function(){ return (Math.round(Math.random())-0.5); }
		elems.sort(rand);
		
		var per = Math.ceil((elems.length / 60) * 10);
		var iv = setInterval(function() {
			for (var i=0;i<per;i++) {
				if (elems.length==0) {
					clearInterval(iv);
					$("#sidebar > .wrapper").show('slide', {direction: 'right', easing: 'easeOutBounce'}, 500);
					break;
				}
					
				var elem = elems.splice(0,1);
				$(elem).css("visibility","visible")
				$(elem).animate({opacity:1},500);
			}
		},100);
	}
	
	this.preloader();
	
};
