/* Url */
var block = 0;
function historyHandler() {
	var stateVar = "nothin'", displayDiv = document.getElementById("main");
	this.historyListener = function(historyHash) {
		stateVar = historyHash;
		if(block == 0){
			$("div#main").load(historyHash, {"ajax" : "yes"});
		}
	};
	unFocus.History.addEventListener('historyChange', this.historyListener);
	this.historyListener(unFocus.History.getCurrent());
};
var historyVar;

function setupNewLinks(){
	$('a').not("a[href^='/#']").not("a[target=_blank]").each(function(index) {
		var href = $(this).attr("href");
		if(href){
			$(this).attr({"href":"/#"+href});
		}
	});
	$("a").click(function(){
		var href = $(this).attr("href").slice(2);
		var rel = $(this).attr("rel");
		if(location.hash.slice(1) != href && href){
			if(rel == "block"){
				$(this).toggleClass('loadbar');
			}else if(rel == "win"){
				modal(href);
				return false;
			}else{
				$("div.top-menu .loadpage").css({"visibility":"visible"});
			}
			return true;
		}
	});
}
/* Window */
function overbg(type){
	if(type == "1"){
		$("body").append('<div id="overbg"></div>');
		
		$("#overbg").css("opacity", 0).fadeTo("fast","0.65");
		$('#overbg').css({ 'height':$(document).height(), 'width':$(window).width()});
		
		$('#overbg').css('height',$(document).height()).bind('click',function(){
			CloseSlot();
		});
	} else {
		$("#overbg").fadeOut("0.5",function(){ $(this).remove(); });
	}
}
function gScroll(){
	scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
	scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
	return {scrollTop:scrollTop,scrollLeft:scrollLeft};
}
function modal(patch){
	overbg(1);
	var scrollPos = gScroll();
	$("#slot").load(patch, {"slot":"yes"}).css({ 'top': scrollPos['scrollTop'] + 150 }).show();
	return false;
}
function CloseSlot(val, time){
	time = (time > 0) ? time : 2000;
	if(val == "auto"){
		setTimeout(function(){ CloseSlot(); }, time);
	}else{
		$("#slot").empty().hide(); overbg(2);
	}
	return false;
}
/* Form */
function form(name, result){
	$("form#"+name).ajaxForm({
	beforeSubmit: function(a,f,o) {
		$("form#"+name+" input, form#"+name+" textarea").attr({"disabled":"disabled"});
		o.dataType = "html";
	},
	success: function(data) {
		$("#"+result).show("slow").html(data);
		$("form#"+name+" input, form#"+name+" textarea").attr({"disabled":""});
	}
	});
}
$(document).ready(function() {
	historyVar = new historyHandler();
	setupNewLinks();
	/* Key function */
	$("body").keydown(function (e) {
		if(e.which == 27){
			CloseSlot();
		}
	});
	/* Key function */
	$("div.top-menu div#x2").addClass("active");
	$("div.top-menu div[id] a").click(function(){
		$("div.top-menu div").removeClass("active");
		$(this).parent().addClass("active");
	});
	/* Menu */
	$("#con6 ul#main_nav li").click(function(){
		$("#con6 ul#main_nav li").removeClass("active");
		$(this).addClass("active");
	});
});
// Function
function nl2br(str){
	return str.replace(/([^>])\n/g, '$1<br/>');
}
function setCookie(name, value, expiredays, path, domain, secure) {
   if (expiredays) {
      var exdate=new Date();
      exdate.setDate(exdate.getDate()+expiredays);
      var expires = exdate.toGMTString();
   }
   document.cookie = name + "=" + escape(value) +
   ((expiredays) ? "; expires=" + expires : "") +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "");
}
function getCookie(name) {
   var cookie = " " + document.cookie;
   var search = " " + name + "=";
   var setStr = null;
   var offset = 0;
   var end = 0;
   if (cookie.length > 0) {
      offset = cookie.indexOf(search);
      if (offset != -1) {
         offset += search.length;
         end = cookie.indexOf(";", offset)
         if (end == -1) {
            end = cookie.length;
         }
         setStr = unescape(cookie.substring(offset, end));
      }
   }
   return setStr;
}
if(!getCookie("js")){
	setCookie("js", 1, 365, "/");
}
var engine = {
	posts : [],
	target : null,
	busy : false,
	count : 10,
	patch: '',
	page : 0,
	
	init : function(posts, target){
		if (!target) return;
		this.target = $(target);
		
		this.append(posts);
		
		var that = this;
		$(window).scroll(function(){
			if ($(document).height() - $(window).height() <= $(window).scrollTop() + 200) {
				that.scrollPosition = $(window).scrollTop();
				that.get();
			}
		});
	},
	get : function() {
		if (!this.target || this.busy) return;
		
		if (this.posts && this.posts.length) {
			var lastId = this.posts[this.posts.length-1].id;
		}else{
			var lastId = 0;
		}
		
		this.setBusy(true);
		var that = this;
		
		$.post(this.patch, {"count": engine.count, "last": engine.page}, function(data){
			if (data.length > 0) {
				that.target.append(data);
				engine.page += 1;
			}
			that.setBusy(false);
		});
	},
	append : function(posts){
		posts = (posts instanceof Array) ? posts : [];
		this.posts = this.posts.concat(posts);
		if (this.scrollPosition !== undefined && this.scrollPosition !== null) {
			$(window).scrollTop(this.scrollPosition);
		}
	},
	showLoading : function(bState){
		//var loading = $('.loadpage');
		if (bState) {
			$(this.target).append(loading);
			//loading.show('slow');
		}else{
			//$('.loadpage').hide();
		}
	},
	setBusy : function(bState){
		this.showLoading(this.busy = bState);
	}
}  
