// *** CARD CLASS ***
var Card = Class.create({
	initialize: function() {
		this.suits  = new Hash({ 0: 'spades', 1: 'hearts', 2: 'clubs', 3: 'diamonds'});
	},

	addSuitClassById: function(prefix, id) {
		$(prefix+id).addClassName(this.suits.get(this.modulo(id, 4)));
	},

	addSuitClassRand: function(prefix, id) {
		$(prefix+id).addClassName(this.suits.get(this.random(4)));
	},

	modulo: function(num, mod) {
		return parseInt(num,10) % mod;
	},

	random: function(num) {
		return Math.floor(Math.random()*num);
	}

});
// Initialize card class
var card = new Card();


// *** SEARCH CLASS ***
var Search = Class.create( {

	initialize: function(searchField, searchSpinner) { 
		this.url = blogurl+"/"; 
		this.searchField = searchField;
		this.searchSpinner = searchSpinner;
		this.searchString =''; 
		this.helpText = 'Enter search criteria here...';
		this.searchTimer = null;
		
		$(this.searchField).value = this.helpText;
		$(this.searchSpinner).hide();
		
		Event.observe(this.searchField, 'focus', this.typing.bindAsEventListener(this));
		Event.observe(this.searchField, 'blur', this.reset.bindAsEventListener(this));
		Event.observe(this.searchField, 'keyup', this.evaluate.bindAsEventListener(this));			
	},
	
	typing: function() {
		if ($F(this.searchField) == this.helpText) $(this.searchField).value = '';
	},
	reset: function() {
		if ($F(this.searchField) == '') $(this.searchField).value = this.helpText;
		$(this.searchSpinner).hide();
	},

	evaluate: function(event) {
		this.searchString = $F(this.searchField);
		var code = event.keyCode;
		if (code == Event.KEY_ESC || ((code == Event.KEY_DELETE || code == Event.KEY_BACKSPACE) && $F(this.searchField) == '') ) {
			this.reset.bind(this);
		} else if (code != Event.KEY_LEFT && code != Event.KEY_RIGHT && code != Event.KEY_DOWN && code != Event.KEY_UP && code != Event.KEY_RETURN) {
			if (this.searchTimer) { clearTimeout(this.searchTimer) };
			this.searchTimer = setTimeout(this.search.bind(this), 500);
		}
	},

	// Ajax search
	search: function() {
		$(this.searchSpinner).show();
		var pars = 's=' + this.searchString;	
		var xmlurl = this.url;
		if (this.searchString.strip() != '') {
			var wordpressSearch = new Ajax.Request(
				xmlurl, 
				{
					method: 'get', 
					parameters: pars,
					asynchronous: true,
					encoding: 'UTF-8', 
					onComplete: this.result.bind(this),				
					onFailure: this.reportError
				}
			);
		}
	},
	
	result: function(originalRequest) {			
		var response = originalRequest.responseText || "Failed to search, please try again...";
		$('container_posts').update(response);
		$(this.searchSpinner).hide();			
	},
	
	reportError: function(Request) {
		alert("Failed to search, please try again... (Error in Ajax Request)");
	}
});


//positioning of footer 
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
function setFooter() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var contentHeight = document.getElementById('container').offsetHeight + document.getElementById('header').offsetHeight;
			var footerElement = document.getElementById('footer');
			var footerHeight  = footerElement.offsetHeight;
			if (windowHeight - (contentHeight + footerHeight) >= 0) {
				footerElement.style.position = 'absolute';
				footerElement.style.top = (windowHeight - footerHeight) + 'px';
			}
			else {
				footerElement.style.position = 'static';
			}
		}
	}
}
window.onload = function() {
	setFooter();
}
window.onresize = function() {
	setFooter();
}
