/* DEPENDICIES
 * pfc/js/pfc.js
 * jquery
 * gla_vote.css - in this case the style is in the default_style.css


function trackclick1() {
var axel = Math.random() + "";
var a = axel * 1000000000000000000;
var spotpix = new Image();
spotpix.src ="http://ad.doubleclick.net/activity;src=2155040;type=2010g826;cat=green243;ord=" + a + '?"';
}

var gla_vote = function()
{
	this.overlay;
	this.container;
	this.holder;
	this.voteInProgress;
}

gla_vote.prototype.openBox = function(page,width, height)
{
	this.overlay = $('#gla_overlay');
	var scope = this;
	this.holder = $('#gla_holder');
	if (this.holder.length == 0)
	{
		this.holder = $('<div id="gla_holder"></div>').appendTo('body');
		
		this.holder.css(
		{
			"width" : $(window).width(),
			"height" : $(window).height(),
			"position" : "absolute",
			"top" : "0px",
			"left" : "0px",
			"z-index" : 9000
		});
		
		scope.bindDelegatesToHolder();
	}
	
	
	if (this.overlay.length == 0){
		this.overlay = $('<div id="gla_overlay"></div>').appendTo(this.holder);
	
		this.overlay.css(
		{
			"width" : $(window).width(),
			"height" : $(window).height(),
			"display" : "none",
			"top" : "0px",
			"left" : "0px",
			"background-color" : "black",
			"z-index" : 9500
		});
		this.overlay.click(function()
		{
			scope.closeBox();
		});
	}
	
	this.container = $('#gla_container');
	if (this.container.length == 0)
	{
		this.container = $('<div id="gla_container"></div>').appendTo(this.holder);
		this.container.css(
		{
			"display" : "none",
			"position" : "relative",
			"z-index" : 9999,
			"margin" : "0px auto",
			"overflow" : 'auto'
		});
	}
	this.container.css('width', width);
	this.holder.show();
	this.overlay.fadeTo('slow', 0.6, function()
	{
		scope.container.animate({'height': height, 'top': $(document).scrollTop() + 50});
		scope.container.load(page);
	});
}

gla_vote.prototype.vote = function(shopKey, sweepsBypass)
{
	if (this.voteInProgress)
	{
		return false;
	}
	
	var scope = this;
	
	scope.voteInProgress = true;
	
	$.ajax(
		      {url: '/request/vote.php', 
		       type: 'POST', 
		       dataType: 'json', 
		       processData: 'application/json',
		       data: {'shopKey': shopKey, 'sweepsBypass': sweepsBypass}, 
		       success: function(voteResponse)
		       {
		    	 $('#cBlocker').hide();
		    	 
		    	 if (voteResponse.vote == "failure")
		    	 {
		    		 scope.openBox("/voteError.php", 397, 341);
		    	 }
		    	 
		    	 if(voteResponse.vote == "maxVotes" || voteResponse.contest == "maxVotes")
				 {
					 scope.openBox("/thanks_maxVotes.php", 397, 341);
				 }
				 else if (voteResponse.vote = "success" && voteResponse.contest == "success")
				 {
				 	 trackclick1();
					 scope.openBox("/thanks.php", 397, 341);
				 }
				 else if (voteResponse.vote = "success" && voteResponse.contest == "bypass")
				 {	
				 	trackclick1();
					scope.openBox("/thanks_noSweeps.php", 397, 341); 
				 }
				 scope.voteInProgress = false;
		       }
		    });	
}

gla_vote.prototype.checkAndVote = function(shopKey)
{
	if (this.voteInProgress)
	{
		return false;
	}	
	var scope = this;
	//check if logged in
	$.ajax({ url: '/request/checkLoggedIn.php', dataType: 'json', success: function(response)
	{
		$('#cBlocker').hide();
		
		if (response.isLoggedIn)
		{
			if(response.sweepsEligible)
			{
				scope.vote(shopKey, false);
			}
			else if(response.isIneligible)
			{
				scope.openBox("/ineligible.php", 397, 341);
			}
			else
			{
				scope.openBox("/interstitial.php?shop="+shopKey, 397, 341);
			}
		}
		else
		{
			pfc.setSuccessCallback( function()
			{
				scope.pfcCallback(shopKey);
			});
			pfc.showPrefCenter();
		}
	}});
}

gla_vote.prototype.bindDelegatesToHolder = function()
{
	var scope = this;
	
	this.holder.delegate('#voteUpdateProfile', 'click', function()
	{
		scope.closeBox();
		pfc.showPrefCenter();
		return false;
	});
	
	this.holder.delegate('#voteNoSweeps', 'click', function()
	{
		var shopKey = $('#hdnShopKey').val();
		scope.closeBox();
		scope.vote(shopKey, true);
		return false;
	});
	
	this.holder.delegate('#close', 'click', function()
	{
		scope.closeBox(); 
		return false;
	});
}

gla_vote.prototype.pfcCallback = function(shopKey)
{
	var scope = this;
	pfc.closeMessenger(true, function(){
		scope.checkAndVote(shopKey);
		pfc.setSuccessCallback(function(){});
	});
};

gla_vote.prototype.closeBox = function()
{
	$('#cBlocker').hide();
	
	this.container.hide();
	this.overlay.hide();
	this.holder.hide();
}

var glaVote = new gla_vote();

$(document).ready(function()
{
	$('#finalVoteButtons').delegate('.glaVote','click',function()
	{
			$('#cBlocker').css({'height': $(document).height()}).show();
			//var hrefArr = $(this).attr('href').split('_'); 
			//var shopKey = hrefArr[hrefArr.length-1];  
			var shopKey = $(this).attr('id').replace("shop_", "");
			glaVote.checkAndVote(shopKey);			
			return false;
	});
	
	$(document).undelegate("#pfcLogin", "click").delegate("#pfcLogin", "click", function(){
		pfc.setSuccessCallback(function(){});
		pfc.showPrefCenter();
	});
	
});*/
