Greenlight = function(p_nFbId) {

	this.l_aNew = new Array();
	
	this.l_nItemCount;
	this.l_nItemNewCount;
	
}



Greenlight.prototype.drawInitial = function() {
	
	var thisGreenlight = this;
	
	var l_nComplete = 0;
	
	var l_nGlCount = $('.gl').length;
	
	
	$('.gl').each(function() {
		
		var l_sId = $(this).attr('id');
		var l_aId = l_sId.split('_');
		var l_nId = parseInt(l_aId[1]);
		
		
		$.post("/lib/greenlightAjax.php?action=likeCount", { id: l_nId },
			function(data){

				if (data.answer!=0) {
					$('#' + l_sId + ' .count').html(data.count);
					l_nComplete++;
					if (l_nComplete==l_nGlCount ) {
						thisGreenlight.draw();
					}
					
				} else {
					alert('An error occurred. Please try again. [1]');
				}
			}, "json");
		
	});
		
}


Greenlight.prototype.draw = function() {
	
	var thisGreenlight = this;
	
	if (l_xController.l_sFBID != '') {
		$('.gl').each(function() {
		
			var l_sId = $(this).attr('id');
			var l_aId = l_sId.split('_');
			var l_nId = parseInt(l_aId[1]);
		
			$.post("/lib/greenlightAjax.php?action=isLiked", { userid: l_xController.l_nUserId, id: l_nId },
				function(data){
					if (data.answer!=0) {
						if (data.likes==1) {
							$('#' + l_sId + ' a').addClass('clicked');
						} 					
					} else {
						alert('An error occurred. Please try again. [1]');
					}
				}, "json");
		
		});
	}
		
}

Greenlight.prototype.reset = function() {
	
	var thisGreenlight = this;
	
	$('.gl a').removeClass('clicked');
}


Greenlight.prototype.triggerFb = function(p_nId) {
	
	var thisGreenlight = this;
	
	FB.login(function(response) {
	  	if (response.status==='connected') { 
	    	l_xController.checkUserExists(response.authResponse.userID,0);
		} else {
			alert('Please connect with Facebook first.');
			
		}

	},{});
	
}


Greenlight.prototype.toggleLike = function(p_nId) {
	
	var thisGreenlight = this;
	
	$.post("/lib/greenlightAjax.php?action=toggleLike", { userid: l_xController.l_nUserId, id: p_nId },
		function(data){
			if (data.answer!=0) {
				//alert(data.likes);
				
				var l_nCurrCount = parseInt($('#gl_' + p_nId + ' .count').html());
				if (data.likes==1) {
					$('#gl_' + p_nId + ' a').addClass('clicked');
					l_nNewCount = l_nCurrCount+1;
					$('#gl_' + p_nId + ' .count').html(l_nNewCount+'');
				} else {
					$('#gl_' + p_nId + ' a').removeClass('clicked');
					l_nNewCount = l_nCurrCount-1;
					$('#gl_' + p_nId + ' .count').html(l_nNewCount+'');
				}					
			} else {
				alert('An error occurred. Please try again. [1]');
			}
		}, "json");
		
}



Greenlight.prototype.getBadge = function() {
	
	var thisGreenlight = this;
		
	$.post("/lib/greenlightAjax.php?action=getBadge", { userid: l_xController.l_nUserId },
		function(data){
			if (data.answer!=0) {
				thisGreenlight.l_aNew = data.items;
				thisGreenlight.drawBadge();
			} else {
				alert('An error occurred. Please try again. [2]');
			}
		}, "json");		
}

Greenlight.prototype.drawBadge = function() {
	
	var thisGreenlight = this;
	var l_sHtml = '';	
	var l_nItemCount = thisGreenlight.l_aNew.length;
	var l_nItemNewCount = 0;
		
	for (i in thisGreenlight.l_aNew) {
		var l_xItem = thisGreenlight.l_aNew[i];
		
		if (l_xItem.type==1) {
			l_xItem.cat = 'Photo';
		} else if (l_xItem.type==2) {
			l_xItem.cat = 'Blog';
		} else {
			l_xItem.cat = 'Event';
		}
		
		l_sHtml += "<div class='badge_gl'>";
		l_sHtml += "<div class='badge_gl_left'>";
		l_sHtml += "<div class='badge_gl_thumb'>";
		l_sHtml += "<img src='http://graph.facebook.com/" + l_xItem.fbid + "/picture?type=square' />";
		l_sHtml += "</div>";
		l_sHtml += "<div class='badge_gl_name'>";
		l_sHtml += l_xItem.fbname;
		l_sHtml += "</div>";
		l_sHtml += "</div>";
		l_sHtml += "<div class='badge_gl_mid'>";
		l_sHtml += "<img src='/images/gl_click_small.png' />";
		
		if (l_xItem.is_new==1) {
			l_sHtml += '<span>New!</span>';
			l_nItemNewCount++;
		}
		
		l_sHtml += "</div>";
		l_sHtml += "<div class='badge_gl_right'>";
		l_sHtml += "<div class='badge_gl_title'>" + l_xItem.title + "</div>";
		l_sHtml += "<div class='badge_gl_linkwrap'>";
		l_sHtml += "<span class='badge_gl_cat'>" + l_xItem.cat + "</span>";
		l_sHtml += "<a href='" + l_xController.l_sDomain + l_xItem.slug + "/' target='_blank'>view</a>";
		l_sHtml += "</div><br class='clear' />";
		l_sHtml += "</div>";
		l_sHtml += "<br class='clear' />";
		l_sHtml += "</div>";
	}
	
	$('#badge_inner').html(l_sHtml);
	
	thisGreenlight.l_nItemCount = l_nItemCount;
	thisGreenlight.l_nItemNewCount = l_nItemNewCount;
	
	if (l_nItemNewCount > 0) {
		$('#hat_badge a').html(l_nItemNewCount + '');
		$('#hat_badge').removeClass('inactive');
	
	} else if (l_nItemCount > 0) {
		$('#hat_badge a').html(l_nItemCount + '');
		$('#hat_badge').addClass('no_new');
		$('#hat_badge').removeClass('inactive');
	} else {
		$('#hat_badge a').html('0');
	}
	
}

Greenlight.prototype.clearList = function() {
	var thisGreenlight = this;
		
	$.post("/lib/greenlightAjax.php?action=clearList", { userid: l_xController.l_nUserId },
		function(data){
			if (data.answer!=0) {
				$('#badge_inner').html('');
				
				$('#hat_badge a').html('0');
				$('#badge_contents').css('padding-bottom','0px');
				$('#badge_contents #clear_list').hide();
				$('#badge_contents #badge_loader').show();
				$('#badge_contents h4').html('Your List Has Been Reset!');
				setTimeout(function() {
					$('#hat_badge a').click();
					$('#hat_badge').addClass('inactive');
				},1200);
				
			} else {
				alert('An error occurred. Please try again. [3]');
			}
		}, "json");		
}

Greenlight.prototype.listChecked = function() {
	var thisGreenlight = this;
		
	$.post("/lib/greenlightAjax.php?action=listChecked", { userid: l_xController.l_nUserId },
		function(data){
			if (data.answer!=0) {
				$('#hat_badge').addClass('no_new');
				$('#hat_badge a').html(thisGreenlight.l_nItemCount + '');
				
			} else {
				alert('An error occurred. Please try again. [4]');
			}
		}, "json");		
}


Greenlight.prototype.initialize = function() {
	var thisGreenlight = this;
}

Greenlight.prototype.showLogin = function() {
	var thisGreenlight = this;
}
	


