// F R O N T E N D
//  "use strict";

// init setup
$(function(evt){
	bogus.log('engine started.');
	
	bogus.poll.setup();
	
	/*
	bogus.apicall('/echo')
		 .success(function(data) {
		 	bogus.log(data);
		 })
		 .error(function(xhr, status){
		 	bogus.log(status);
		 });
	*/

	// menu re/deem
	var $head = $('#head');
	bogus.effects.deem($head, 10000);
	$head.hover(
		function(evt) {
			bogus.effects.redeem($(this));
		},
		function(evt) {
			bogus.effects.deem($(this));
		}
 	);
		
});



var bogus = {};

	// params
	bogus.params = {
		_debug : false,
		_apiurl: 'http://www.bogus.cz/api'
	};
	
	// param getter
	bogus.get = function(key, def) {
		def = def || null;
		return this.has(key) ? this.params['_'+key] : def;
	};
	
	// param setter
	bogus.set = function(key, val) {
		if ((typeof key).match('object')) {
			for(param in key) {
				this.params['_'+param] = key[param];
			}
			return true;
		}
		else if (typeof(key).match('string')) {
			this.params['_'+key] = val;
			return true;
		}
		return false;
	};
	
	// param index verifier
	bogus.has = function(key) {
		return this.params['_'+key] ? true : false;
	};
	
	// ---------------------------------
	
	bogus.effects = {};	// fx factory
	
	bogus.effects.deem = function($item, time) {
		time = time || 250;
		$item.animate({
			opacity: 0.5
		}).trigger('bogus.effects.deem');
	};
	
	bogus.effects.redeem = function($item, time) {
		time = time || 250;
		$item.animate({
			opacity: 1
		}).trigger('bogus.effects.redeem');
	};
	
	// ---------------------------------
	
	
	// simple logger
	bogus.log = function(text, code) {
		code = code || null;
		if (this.get('debug')) {
			mess = (code?'#'+code+' ':'') + text 
			if ((typeof console).match('object') && (typeof console.log).match('function')) {
				console.log(mess);
			}
			else {
				alert(mess);
			}
		}
	};
	
	// api stub
	bogus.apicall = function(method, params, type) {
		type   = type || 'GET';
		params = params || {};
		method = method || '/'; // LOL
		return $.ajax({
			url: this.get('apiurl') + method,
			type: type,
			//cache: false,
			data: params
		});	
	};
	
	bogus.poll = {};
	
	bogus.poll.setup = function (id) {
		if (cookie.read('bogus.poll.vote.'+id)) {
			$('#'+id).hide();
			return;
		}
		$('.poll-answers input[type=radio]').click(bogus.poll.vote);
	};
	
	bogus.poll.vote = function (evt) {
		var vote = $(this);
		var id   = vote.parents('.poll').attr('id');
		var val  = vote.val();
		bogus.log(vote);
		bogus.apicall('/poll/vote', { 'poll':id, 'vote':val }, 'POST')
			 .success(function(data){
			 	if (data.match('OK')) {
			 		vote.parents('.poll')
			 			.html('<p>thx 4 your vote!</p>');
			 		cookie.create('bogus.poll.vote.'+id, 'voted', 365);
			 	}
			 })
			 .error(function(xhr, status){
			 	bogus.log(status);
			 });
	};
	
	
var cookie = {};

	cookie.create = function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	};

	cookie.read = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	};

	cookie.erase = function(name) {
		createCookie(name,"",-1);
	};

	
