// This document Presupposes the existence and inclusion of the jQuery library prior to it's own inclusion.  Just like Schopenhauer's existence presupposes an essence... but more code'ish.  it also requires the canada.com scripts json2.js and jsoncookie-1.0.js



/*
jQuery Url Plugin
	* Version 1.0
	* 2009-03-22 19:30:05
	* URL: http://ajaxcssblog.com/jquery/url-read-get-variables/
	* Description: jQuery Url Plugin gives the ability to read GET parameters from the actual URL
	* Author: Matthias Jäggli
	* Copyright: Copyright (c) 2009 Matthias Jäggli under dual MIT/GPL license.
*/
(function ($) {
	$.url = {};
	$.extend($.url, {
		_params: {},
		init: function(){
			var paramsRaw = "";
			try{
				paramsRaw = 
					(document.location.href.split("?", 2)[1] || "").split("#")[0].split("&") || [];
				for(var i = 0; i< paramsRaw.length; i++){
					var single = paramsRaw[i].split("=");
					if(single[0])
						this._params[single[0]] = unescape(single[1]);
				}
			}
			catch(e){
				alert(e);
			}
		},
		param: function(name){
			return this._params[name] || "";
		},
		paramAll: function(){
			return this._params;
		}
	});
	$.url.init();
})(jQuery);


/*  
	End Plugins
*/


function allowEnter(speed){
	speed = typeof(speed) != 'undefined' ? speed : 2000;
	// remove remind me button and add enter button
	$("#remindBtn").fadeOut(speed);
	$("#enterBtn").fadeIn(speed);
	$(".enterLink").unbind("click");
}
function tempAlert(msg, callback){
	// remove remind me button and add enter button
	var theAlert = $("#activeMessage");
	theAlert.hide().text(msg).fadeIn(2000,  function(){
		$(this).fadeOut(5000, function(){
			if(callback){
				callback();	
			}
		});
	});
}

function checkAll(cars){
	for(var i in cars) {
		var gotAll = true;
		var car=cars[i];
		if(car.c1.activeState){
			$("#car_01 .activeState").show();
			$("#weekItem_1").addClass("found");
			$("#weekItem_1 a").text("Visit Page!");
		} else {
			gotAll=false;
		}
		if(car.c2.activeState){
			$("#car_02 .activeState").show();
			$("#weekItem_2").addClass("found");
			$("#weekItem_2 a").text("Visit Page!");
		} else {
			gotAll=false;
		}
		if(car.c3.activeState){
			$("#car_03 .activeState").show();
			$("#weekItem_3").addClass("found");
			$("#weekItem_3 a").text("Visit Page!");
		} else {
			gotAll=false;
		}
		if(car.c4.activeState){
			$("#car_04 .activeState").show();
			$("#weekItem_4").addClass("found");
			$("#weekItem_4 a").text("Visit Page!");
		} else {
			gotAll=false;
		}
		//repeat for all
	
		if (gotAll){
			return true;
		} else {
			return false;
		}
	}
}



function removeContestForm(ineligibleMsg){
	//The person is on the entry page without collecting everything.  bad form, nobody likes a cheater.
	$("#theForm").html("<h2 style=\"margin:5em; text-align:center;\">" + ineligibleMsg + "</h2>")
}
$(document).ready(function(){
// this is where stuff happens when the browser can handle it.
	$("#ContestWrapper").append('<a href="default.aspx" title="Return Home" id="homeLinkLarge">Return Home</a>');

	var cName="carsCookie";
	var cKey="cars";
	var cOptions={
		daysToLive:45	
	};
	var cUniq = "carsUniq";
	var fadeSpeed=2000;
	var refer=document.referrer;
	var referrers = new Array();
	//$(".weeksList li").addClass("active").removeClass("inactive");
	referrers[1] = $("#weekItem_1.active a").attr("href");
	referrers[2] = $("#weekItem_2.active a").attr("href");
	referrers[3] = $("#weekItem_3.active a").attr("href");
	referrers[4] = $("#weekItem_4.active a").attr("href");
	referrers[41] = "http://www.showcase.ca/blog/";
	referrers[42] = "http://www.showcase.ca/blog/Default.aspx";
	
	
	var cars = {};
	var gotAll=true;
	var gotAllMsg = "You have collected all the cars and may now enter below!";
	var ineligibleMsg = "You must collect all the Nissan Vehicles before you may enter the Nissan Find and Fill Contest.";
	var inactiveMsg = "Please come back at a later date for this clue in the Nissan Find and Fill Contest.";
	$(".weeksList li.inactive a").attr("href", "#inactive").click(function(){
		tempAlert(inactiveMsg);
	});
	$(".enterLink").click(function(){
		tempAlert(ineligibleMsg);
		return false;
	});
	$("#remindBtn").toggle(function(){
		$("#remindMeForm").show("slow")
		return false;
	}, function(){
		$("#remindMeForm").hide("slow")
		return false;
	});
	
	
	/* JSON Cookie */
	if($.url.param("delete")){
		JSONCookie.Delete(cName);
	}
	JSONCookie.Init(cName,cOptions);
	cars=JSONCookie.GetValue(cName,cKey);
	if(!cars){
		cars = {};
		cars.c1= {};
		cars.c1.activeState= false;
		cars.c2= {};
		cars.c2.activeState= false;
		cars.c3= {};
		cars.c3.activeState= false;
		cars.c4= {};
		cars.c4.activeState= false;
		JSONCookie.AddValue(cName,cKey,cars, 1, cUniq);
		cars=JSONCookie.GetValue(cName,cKey);
	}
	if($.url.param("getAll")){
		cars[0].c1.activeState = true;
		cars[0].c2.activeState = true;
		cars[0].c3.activeState = true;
		cars[0].c4.activeState = true;
		JSONCookie.AddValue(cName,cKey,cars, 1, cUniq);
		cars=JSONCookie.GetValue(cName,cKey);
	}
	gotAll = checkAll(cars);
	if(gotAll){allowEnter(); }
	else if ($("body.enter #nissanForm")[0]){ removeContestForm(ineligibleMsg);}
	//alert(cars[0].c1.activeState);
	if(refer == referrers[1]){
		cars[0].c1.activeState = true;
		JSONCookie.AddValue(cName,cKey,cars, 1, cUniq);
		cars=JSONCookie.GetValue(cName,cKey);
		tempAlert("Congratulations, You Found the "+$("#car_01 .activeState").attr("title"), function(){
			gotAll = checkAll(cars);
			if(gotAll){tempAlert(gotAllMsg); allowEnter();}
		});
		$("#car_01 .activeState").fadeIn(2000);
    }
	if(refer == referrers[2]){
		cars[0].c2.activeState = true;
		JSONCookie.AddValue(cName,cKey,cars, 1, cUniq);
		cars=JSONCookie.GetValue(cName,cKey);
		tempAlert("Congratulations, You Found the "+$("#car_02 .activeState").attr("title"), function(){
			gotAll = checkAll(cars);
			if(gotAll){tempAlert(gotAllMsg); allowEnter();}
		});
		$("#car_02 .activeState").fadeIn(2000);
    }
	if(refer == referrers[3] || refer.match(/showcase\.ca\/video/)){
		cars[0].c3.activeState = true;
		JSONCookie.AddValue(cName,cKey,cars, 1, cUniq);
		cars=JSONCookie.GetValue(cName,cKey);
		tempAlert("Congratulations, You Found the "+$("#car_03 .activeState").attr("title"), function(){
			gotAll = checkAll(cars);
			if(gotAll){tempAlert(gotAllMsg); allowEnter();}
		});
		$("#car_03 .activeState").fadeIn(2000);
    }
	if(refer == referrers[4] || refer == referrers[41] || refer == referrers[42]){
		cars[0].c4.activeState = true;
		JSONCookie.AddValue(cName,cKey,cars, 1, cUniq);
		cars=JSONCookie.GetValue(cName,cKey);
		tempAlert("Congratulations, You Found the "+$("#car_04 .activeState").attr("title"), function(){
			gotAll = checkAll(cars);
			if(gotAll){tempAlert(gotAllMsg); allowEnter();}
		});
		$("#car_04 .activeState").fadeIn(2000);
    }
});
    
    


