$(function(){

	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname) {
			//var $target = $(this.hash);
			var h=this.hash.slice(1);
			var target=$('a[name='+h+']');
			if(target.length)
			{
				//var targetOffset = target.offset().top-99;
				var targetOffset = target.offset().top-0;
				$('body, html').animate({scrollTop: targetOffset}, 1000,  function(){
					var save=target.attr("name");
					target.attr("name","ololoolo");
					window.location.hash=h;
					target.attr("name",save);
				});
				return false;
			}
		}
	});
	if(window.location.hash!="")
	{
		var h=window.location.hash.slice(1);
		var clicker=$('a[href*=#'+h+']');
		var target=$('a[name='+h+']');
		if(clicker.length && target.length)
		{
			window.location.hash="";
			$(window).load(function(){
				clicker.click();
			});
		}

	}
});

var globalAcOffset;
var globalAcBlock;

$(document).ready(function(){
	acReinit();
	bindScroll();
});

function acReinit()
{
	if($("#autoClick").length)
	{

		globalAcOffset=$("#autoClick").offset();
		globalAcBlock=false;
	}
	else
	{
		globalAcOffset=false;
	}
	//bindScroll();
}
function bindScroll()
{
	//$(window).unbind("scroll");
	if(globalAcOffset)
	{
		$(window).scroll(function()
		{
			//alert(($(window).scrollTop()+$(window).height()+800)+"  ["+globalAcOffset.top+"]");
			if(!globalAcBlock && (($(window).scrollTop()+$(window).height()+10)>globalAcOffset.top))
			{
				//alert("click!");
				globalAcBlock=true;
				$("#autoClick").click();
			}
		});
	}
}

function anchorControl()
{
	this.curAnchor=false;
	this.delay=50;
	this.cb=[];
	this.counter=0;
	this.inited=false;

	var self = this;


	this.anchorChange = function(str)
	{
		this.curAnchor=str;
		for(var i in this.cb)
		{
			this.cb[i](str)
		}
	};

	this.getAnchor = function()
	{
		return window.location.hash.substr(1);
	};
	this.pushHandler = function(ob)
	{
		this.counter++;
		this.cb[this.counter]=ob;
	};
	this.init = function()
	{
		this.inited=true;
		setInterval(function(){
			var g=self.getAnchor();
			if(self.curAnchor != g)
			{
				self.anchorChange(g);
			}
		},this.delay);
	}
}

function makeStyle(item,style,over)
{
	if(!item.is("disabled") || over)
	{
		if(over) item.removeClass("disabled");
		item.removeClass("pressed").removeClass("hover").removeClass("ok").addClass(style);
	}
}
$(function(){
	$(".elsButton").mouseenter(function(){
		makeStyle($(this),"hover");
	});
	$(".elsButton").mouseleave(function(){
		makeStyle($(this),"ok");
	});
	$(".elsButton").mousedown(function(){
		makeStyle($(this),"pressed");
	});
	$(".elsButton").mouseup(function(){
		makeStyle($(this),"ok");
	});
});


function validate(jq,subm)
{
	this.jq=jq;
	this.subm=subm;
	this.checkers=[];
	this.sEnabler=function(){};

	this.jq.submit(function(){
		if(self.validateForm())
			self.subm();
		return false;
	});

	var self=this;

	this.addChecker = function(name,func)
	{
		var ret=this.checkers.push({ name:name, func:func});
		var ob=this.getob(this.checkers[ret-1].name);
		ob.focus(function(){
			self.notChecked(ob);
		});
		ob.keyup(function(){
			self.sEnabler(self.jq);
		});
	};
	this.addSendEnabler=function(func)
	{
		this.sEnabler=func;
	};
	this.valid=function(ob)
	{
		this.notChecked(ob);
		ob.addClass("valid");
	};
	this.invalid=function(ob)
	{
		this.notChecked(ob);
		ob.addClass("invalid");
	};
	this.notChecked=function(ob)
	{
		ob.removeClass("valid").removeClass("invalid");
	};
	this.getob = function(name)
	{
		return this.jq.find("[name="+name+"]");
	}
	this.validateForm = function(onlyGood)
	{
		var ret=true;
		for(var i in this.checkers)
		{
			var ob=this.getob(this.checkers[i].name);
			//this.jq.find("[name="+this.checkers[i].name+"]");
			if(!this.checkers[i].func(ob))
			{
				if(!onlyGood) this.invalid(ob);
				ret=false;
			} else this.valid(ob);
		}
		return ret;
	}
}
