/*
 * Author: Abid Omar Date: 4 sep. 2010 (raining in Sfax, Tunisia :D :D )
 */
function Contest() {
	var that = this;
	/**
	 * @type Entries Cells
	 */
	this.jQueryentries = jQuery('ul li', '#entries');
	/**
	 * @type Choices Table
	 */
	this.jQuerytable = jQuery('ul li', '#table');
	/**
	 * @type Add team button
	 */
	this.jQuerybuttons = jQuery('.add', this.jQuerytable.get());
	this.jQuerybuttons.unbind();
	/**
	 * @type Submit button
	 */
	this.jQuerysubmit = jQuery('#submitcontest');
}

Contest.prototype.init = function() {
	var that = this;
	/*
	 * Step 1 : Add some text to the Entries
	 */
	this.jQueryentries.text('Select a Team');
	/*
	 * Step 2 : bind add buttons
	 */
	this.jQuerybuttons.click(function() {
				teamname = jQuery(this).parent().text().substring(2);
				that.addTeam(teamname);
				jQuery(this).remove();
			return false;
			});
	/*
	 * Step 3: Free all entries (class=empty)
	 */
	this.jQueryentries.addClass('empty');

	/*
	 * Step 4: Submit button
	 */
	this.jQuerysubmit.click(function() {
				/*
				 * Check user input
				 */
				var pass = true;
				var msg = "unkown error";
				var username = jQuery('input[name=username]').val();
				if (username === '') {
					pass = false;
					msg = "Please Type a Username";
				}
				if (that.emptyEntry() !== false) {
					pass = false;
					msg = "Select 5 teams"
				}
				if (pass === true) {
					that.submitEntry(username);
				} else {
					that.msg(msg,'Message');
				}
				return false;
			});
	/*
	 * Step 5: Init Message Boxes
	 */
	jQuery('.notification').live('mouseover mouseout',function() {
			jQuery(this).css('cursor','pointer');
		}, function() {
				jQuery(this).css('cursor','auto');
		});

	jQuery('.notification span').live('click',function() {
        jQuery(this).parents('.notification').fadeOut(800);
    });
	
	jQuery('.notification').live('click',function() {
        jQuery(this).fadeOut(800).remove();
    });

}

Contest.prototype.addTeam = function(teamname) {
	var that = this;
	var jQueryemptyEntry = that.emptyEntry();
	if (jQueryemptyEntry === false) {
		that.msg('Table is Full!','Message');
	} else {
		jQueryemptyEntry.text(teamname).removeClass('empty');
	}
}

Contest.prototype.emptyEntry = function() {
	var returnvalue = false;
	this.jQueryentries.each(function() {
				var that = this;
				if (jQuery(that).hasClass('empty')) {
					returnvalue = jQuery(that);
					return false;
				}
			});
	return returnvalue;
}

Contest.prototype.submitEntry = function(username) {
	var that = this;
	/*
	 * Step 1: Gather the data
	 */
	var data = {
		username : username,
		choices : [5]
	};
	that.jQueryentries.each(function(i,v) {
		data.choices[i] = jQuery(v).text();
	});
	
	/*
	 * Step 2: Replace Submit with a loading
	 * 
	 */
	that.jQuerysubmit.remove();
	jQuery('#form').append('<img  id="iloading" src="contest/files/loading.gif">');
	/*
	 * Step 3: Send the data
	 */
	jQuery.ajax({
		url : 'admin/submit.php',
		type : 'POST',
		data : data,
		success : function (data) {
		msg = "Submission Sent";
		if (data  === "okay") {
			msg = 'Your submission was added';
			type = 'Success';
			
		} else if (data === "duplicate") {
			msg = 'You can only submit one time';
			type = 'Warning';
			
		} else if (data === "error") {
			msg = "DataBase error";
			type = 'Error';
		}
		callback(msg,type);
		},
		error : function () {
			msg = 'An Error Occured';
			type = 'Error';
			callback(msg,type);
		}
	});
	/*
	 * Step 4: Tell the user
	 */
	function callback(msg,type) {
	jQuery('#iloading').remove();
	that.msg(msg,type);
	}
}

Contest.prototype.msg = function (msg,type) {
	jQuery('body').append('<div class="notification '+type.toLowerCase()+'"><span></span><div class="text"><p><strong>'+type+'!</strong> '+msg+'.</p></div></div> ');
	/*
	 * Position the notification in the middle
	 */
	$a = jQuery('.notification');
	center = this.centerScreen(parseInt($a.height(),10),parseInt($a.width(),10));
	var itop = (parseInt(center.y,10));
	var ileft = (parseInt((center.x/2),10));
	if (isNaN(itop)) { itop = '900px';} else { itop = (itop + 100) + 'px';}
	if (isNaN(ileft)) { ileft = '50%';} else { ileft = ileft + 'px';}
	jQuery('.notification').css({
		position:'absolute',
		left:ileft,
		top:itop
	});
}

/**
 * @description Return the coordinates of the center of the screen.
 * @param {integer}
 *            X Height of the element to position
 * @param {integer}
 *            Y Width of the element to position
 * @return {object}
 *         <p>
 *         <strong>x</strong>: Left<br>
 *         <strong>y</strong>: Top<br>
 *         </p>
 */
Contest.prototype.centerScreen = function(X, Y) {

	/*
	 * Get Scroll Coordinates
	 */
	var scrolledX, scrolledY;
	if (self.pageYoffset) {
		scrolledX = self.pageXoffset;
		scrolledY = self.pageYoffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop;
	} else if (document.body) {
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
	}

	/*
	 * Get the center of browser coordinates
	 */
	var centerX, centerY;
	if (self.innerHeight) {
		centerX = self.innerWidth;
		centerY = self.innerHeight;
	} else if (document.documentElement
			&& document.documentElement.clientheight) {
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientheight;
	} else if (document.body) {
		centerX = document.body.clientWidth;
		centerY = document.body.clientheight;
	}

	/*
	 * Return center of the screen
	 */
	return {
		x : scrolledX + (centerX - X) / 2,
		y : scrolledY + (centerY - Y) / 2
	}
}
