var candy_titles = new Array("Apples", "Toothbrush", "Raisins", "Dum Dums", "Assorted Hard Candy", "Pennies", "Candy Corn", "Wax Lips", "Smarties", "Pencils");
var candy_descriptions = new Array();
candy_descriptions[0] = "Seasonal, local products teach a good lesson. Still, Halloween fruit will end up on a roof.";
candy_descriptions[1] = "The act of trick or treating overrides any rights of cavity prevention. ";
candy_descriptions[2] = "No friends are made over dried fruit. A great way to make the kids skip your house.";
candy_descriptions[3] = "A sucker with nothing in the middle? We want some gum or chocolate to look forward to. C&rsquo;mon!";
candy_descriptions[4] = "In a society with a shortened attention span, candy needs to have an immediate return.";
candy_descriptions[5] = "Teaching kids the value of money, good. Giving them ammunition for your windows, bad.";
candy_descriptions[6] = "Does anybody actually enjoy this? For real.";
candy_descriptions[7] = "Calorie free fun for everyone! For 7 seconds. A taste only more candy can cover up.";
candy_descriptions[8] = "A mix of sugar and dust create an illusion of candy.";
candy_descriptions[9] = "No kid has ever said, 'I can&rsquo;t wait for Ms. Johnson&rsquo;s house, she has the best pencils.'";
var candy_hints = new Array();
candy_hints[0] = "";
candy_hints[1] = "";
candy_hints[2] = "";
candy_hints[3] = "";
candy_hints[4] = "Our <a href='http://www.infinitydirect.com/team.html'>team</a> is here to help you look real intelligent.";
candy_hints[5] = "Our <a href='http://www.infinitydirect.com/clients.html'>clients</a> appreciate our smarts, but they don&rsquo;t usually mention our looks. So, thanks!";
candy_hints[6] = "Disguised or <a href='http://www.infinitydirect.com/direct-social-web-marketing.html'>direct, social marketing</a> can be an effective part of your marketing.";
candy_hints[7] = "A multi-neighborhood <a href='http://www.infinitydirect.com/strategy.html'>strategy</a> will lead to sweeter results.";
candy_hints[8] = "<a href='http://www.infinitydirect.com/customer-profiling-analytics.html'>Profiling</a> your customers and prospects will help to lower your risk.";
candy_hints[9] = ""
var hint_links = new Array();
hint_links[0] = "";
hint_links[1] = "";
hint_links[2] = "";
hint_links[3] = "";
hint_links[4] = "http://www.infinitydirect.com/team.html";
hint_links[5] = "http://www.infinitydirect.com/clients.html";
hint_links[6] = "http://www.infinitydirect.com/direct-social-web-marketing.html";
hint_links[7] = "http://www.infinitydirect.com/strategy.html";
hint_links[8] = "http://www.infinitydirect.com/customer-profiling-analytics.html";
hint_links[9] = "";

var hint_out = false;
var list_out = false;
var candy_cookie_name = "ID20_Oct_Status";
var email_cookie_name = "ID20_Oct_Email";

var o_prefix = "/id20/";

$(document).ready(function(){
	$('#modal_candy').jqm({modal: true});

	$('a.candy_pop').live('click',function(){
		var candy_idx = $(this).attr('id').substring(10);
		$(this).addClass('found');
		if(pc_idx == 0) { 
			ReorderCandy(candy_idx);
		}
		FindCandy(candy_idx, this);
		return false;
	});
	
	$('.jqmOverlay, .jqmCloseButton').live('click',function(){
		$('#modal_candy').jqmHide();
	});
	
	$('#ofin_submit').click(function(){
		$('#ofin_email, #ofin_name').removeClass("input_err");
		var sub_email = $.trim($('#ofin_email').val());
		var sub_name = $.trim($('#ofin_name').val());
		if(sub_email.length == 0 || sub_name.length == 0)
		{
			if(sub_email.length == 0) { $('#ofin_email').addClass("input_err"); }
			if(sub_name.length == 0) { $('#ofin_name').addClass("input_err"); }
		}
		else
		{
			SubmitOctoberEmail(sub_email, sub_name, js_getCookie(candy_cookie_name));
			$('#jqmc_cf_form').hide();
			$('#jqmc_cf_thanks').show();
		}
	});
	
	if(js_getCookie(candy_cookie_name) != ""){
		//CheckBucketValue();
		//ShowBucket();
	}
		
});

function ReorderCandy(clicked_idx)
{
	var hidden_count = parseInt($('#cc_count').html());
	if(clicked_idx != (hidden_count + 1))
	{
		var swap_idx = hidden_count + 1;
		var targ = $('#pop_candy_'+clicked_idx);
		$('#pop_candy_'+swap_idx).attr('id', 'pop_candy_'+clicked_idx);
		targ.attr('id', 'pop_candy_'+swap_idx);
	}
}

function CheckBucketValue(){
	var cval = js_getCookie(candy_cookie_name);
	$.ajax({type : 'POST', url : o_prefix + 'services/october.php', dataType : 'json',
		data: { request_type : "bucket", cookie_val : cval },
		success : function(data){
			if(data.msg == "success"){
				PopulateBucket(data.total);
				if(data.total > 4 && data.total < 10)
				{
					ShowHint(data.total);
					$('#cur_hint').html(candy_hints[data.total-1]);
					$('#om_start').hide();
					$('#om_hint').show();
				}
			}
			else {
				
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
}

function PopulateBucket(total){
	$('#cc_count').html(total)
	var c_list = "";
	for (i=0;i<total;i++)
	{
		c_list += "<li>" + candy_titles[i] + "</li>"
	}
	if(total == 0){ c_list = "<li>Go Find Some Candy!</li>" }
	$('#cc_list').html(c_list)
}

function ShowBucket(){
	$('.candy_contain .inner .tab').animate({left:0},500);
		
	$('.candy_contain .inner .tab a').live('click',function(){
		if(list_out) { 
			$('.candy_contain .inner .list').animate({left:-162},500); 
			list_out = false;
		}
		else { 
			$('.candy_contain .inner .list').animate({left:-10},500); 
			list_out = true;	
		}
	});
	$('.candy_contain .inner .hint_tab a').live('click',function(){
		if(hint_out) { 
			$('.candy_contain .inner .hint').animate({left:-187},500); 
			hint_out = false;
		}
		else { 
			$('.candy_contain .inner .hint').animate({left:-8},500); 
			hint_out = true;	
		}
	});
}

function ShowHint(num){
	$('.candy_contain .inner .hint_tab').animate({left:-8},500);
	
	$('.candy_contain .inner #cc_hint').html(candy_hints[num-1]);
}

function FindCandy(cidx,tag){
	var cval = js_getCookie(candy_cookie_name);
	$.ajax({type : 'POST', url : o_prefix + 'services/october.php', dataType : 'json',
		data: { request_type : "find", candy_idx : cidx, page_idx : pc_idx, cookie_val : cval },
		success : function(data){
			if(data.fire){
				$('#jqmc_unwrapped').hide();
				$('#jqmc_wrapped').hide();
				$('#modal_candy').jqmShow();
				ShowCandy(data.candy_num);
				js_setCookie(candy_cookie_name, data.new_cookie_val, 168);
				$(tag).hide();
				//if(cidx == 10){ AddSecondEntry(); }
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$(tag).hide();
		}
	});
}

function ShowCandy(num){
	var c_title = '#'+num+' '+candy_titles[num-1];
	var c_image_src = o_prefix + "images/october/candy/" + num + ".jpg";
	$('#jqmc_candy_hero').html('');
	
	var img = new Image();
	$(img).attr('src', c_image_src);
    $('#jqmc_candy_hero').append(img);
	
	$('#jqmc_title').html(c_title);
	$('#jqmc_desc').html(candy_descriptions[num-1]);
	
	if(num == 10)
	{
		$('#jqmc_hint').html('')
		$('#jqmc_final').show();
	}
	else
	{
		if(candy_hints[num-1] != "")
		{
			var h_html = "<div class='hint_heading'>Hint to find your next treat:</div>";
			h_html += candy_hints[num-1];
			$('#jqmc_hint').html(h_html);
		}
	}
	
	$('#jqmc_unwrapped').fadeIn(400, function(){
		CheckBucketValue();
		CheckForCandy(pc_idx)
	});
}

function CheckForCandy(page_candy_idx){
	var cval = ""; //js_getCookie(candy_cookie_name);
	if(cval != "")
	{
		$.ajax({type : 'POST', url : o_prefix + 'services/october.php', dataType : 'json',
			data: { request_type : "check", page_idx : page_candy_idx, cookie_val : cval },
			success : function(data){
				if(data.reveal){
					var targ = data.target;
					for (c=1;c<=data.show_num;c++)
					{
						$('#pop_candy_'+targ).show();
						targ--;
					}
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				//alert(textStatus);
			}
		});
	}
}

function SubmitOctoberEmail(email, name, c_cookie_val){
	$.ajax({type : 'POST', url : o_prefix + 'services/october.php', dataType : 'json',
		data: { request_type : "email", email_val : email, name_val : name, cookie_val: c_cookie_val },
		success : function(data){
			$('.oe_finish').hide();
			$('.oe_finish_email').show();
			js_setCookie(email_cookie_name, data.msg, 720);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			//alert(textStatus);
		}
	});
}

function AddSecondEntry(){
	var e_val = js_getCookie(email_cookie_name);
	$.ajax({type : 'POST', url : o_prefix + 'services/october.php', dataType : 'json',
		data: { request_type : "finish", email_val : e_val },
		success : function(){
			//alert("finish updated");
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			//alert("error finishing");
		}
	});
}
	
