/**
 * @author jleeder
 */

//LINK ALERTS ----------------------
function linkAlerts(){
	this.searchAlert = "This search field may not work, but our Boss Cam sure does. Turn it on now (it’s right under the log in section on the right) and we’ll watch your back while you explore.";
	
	this.randomAlerts = new Array(
		"That’s a fake link. Want something real? Click on the Webisodes tab on the top right of the screen and watch our videos.",
		
		"We wish that link worked too. But it doesn’t. Check out the Games tab on the top right of this page though. That totally works. We’ve got two games you can mess with all while looking completely productive at work.",
		
		"You guessed it: another fake link. Getting frustrated? Take your aggression out in our free games. Just click on the Games tab at the top of the page to play now.",
		
		"That link is just there to look pretty. If you want something that actually works, try drinking a Doubleshot® Energy+Coffee the next time you need help staying energized."
	);
	this.current = 0;
}

linkAlerts.prototype.getNextAlert = function(){
	tmp = this.randomAlerts[this.current];
	this.current++;
	if (this.current >= this.randomAlerts.length){
		this.current = 0;
	}
	return tmp;
};

linkAlerts.prototype.getSearchAlert = function(){
	return this.searchAlert;
}
links = new linkAlerts;

//----------------------------------------
function changeHeight(element, height){
	$(element).css({'height': height});
}

function hideCamInstructions(){
	$('.search p').text('If you get on a computer with a web cam, we can watch your back with the Boss Cam while you explore the site.');
	$('#BossCamHolder').hide();
}

//DATE NUMBERS TO WORDS
function dateConvert(){
	this.dateConverted = { 
		day: "",
		month: "",
		year: ""
	}
}
dateConvert.prototype.convertDate = function(){
	date = new Date();
	
	days = new Array( "0",
		"First","Second","Third","Fourth","Fifth","Sixth","Seventh","Eighth","Ninth",
		"Tenth","Eleventh","Twelfth","Thirteenth","Fourteenth","Fifteenth","Sixteenth",
		"Seventeenth","Eighteenth","Nineteenth","Twentieth","Twenty-First","Twenty-Second",
		"Twenty-Third","Twenty-Fourth","Twenty-Fifth","Twenty-Sixth","Twenty-Seventh",
		"Twenty-Eighth","Twenty-Ninth","Thirtieth","Thirty-First"
	);
	month = new Array(
		"January","February","March","April","May","June","July","August","September",
		"October","November","December"
	);
	
	this.dateConverted.day = days[date.getDate()];
	this.dateConverted.month = month[date.getMonth()];
	this.dateConverted.year = toWords(date.getFullYear());
}
dateWords = new dateConvert;
dateWords.convertDate();

function resetLoginOverlay(){
	$("form#pepsiLogin :input[type='text']").val(''); 
	$('#tabs .tabHandle').each(function(){ $(this).removeClass('active');});
	$('.tabHandle.leftTab').addClass('active');
	$('#tabs .content, #login .controls').each(function(){ $(this).hide();});
	$('#pepsi, .pepsiLogin.controls').show();
	$('#login').attr('style','');
	$('#login .outerBox').attr('style','');
	$('#login .innerBox').attr('style','');
	$('#facebook').attr('style','');
	$('#facebook').hide();
	$("#fb_login").show();
	$("#fb_registration").hide();
	$("#pepsi_tab").show();
	$("#facebook_tab").show();
	$(".fbCreate").hide();
	$('.pfcCreate').hide();
	$('#fbCreateBtn').attr('onClick', "doFbRegistration()");
	$('#fbCreateTxt').text('Create');
	$('#pepsi').attr('style','');
	$("#pfc_login").show();
	$("#pfc_registration").hide();
	$('#pfcCreateBtn').attr('onClick', "doPfcRegistration()");
	$('#pfcCreateTxt').text('Create');
	$('#header_copy').text("If you have created an account on another PepsiCo site, you log in with that info here. To send game challenges to your Facebook friends, sign in through Facebook Connect.");
}

function resetRegisterOverlay(){
	$('#register').removeClass('reg').addClass('check');
	$('form#registerForm :input').val('');
	$('p.directions').text('Check the availability of your email address');
	$('.swappable').hide();
	$('#reg_control').hide();
	$('#check_control').show();
	$('#register.check').attr('style','');
	$('#register.check .outerBox').attr('style','');
	$('#register.check .innerBox').attr('style','');
}

function callExternalLogin(){
	if (loginOverlay.isOpened()) {
			nextAlert = "Login is Already Opened";
			if (alertOverlay.isOpened())
				alertOverlay.close();
			alertOverlay.load();
		}
		else{
			loginOverlay.load();
		}
		return false;
}

function loadNewGame(gameUrl){
	$('#games').html('<div id="gameHolder"></div>');
	var flashvars = {serviceURL: '/gateway.php'};
	var params = {
		wmode: 'transparent'
	};
	var attributes = {
		id: 'game',
		name: 'game'
	};
	swfobject.embedSWF(gameUrl, "gameHolder", "955", "600", "9.0.0", "/flash/expressinstall.swf", flashvars, params, attributes);
}




//DOM IS LOADED AND READY TO ATTACH EVENTS TO ELEMENTS
//Ran from script tag at bottom of body
var alertOverlay;
var loginOverlay;
var registerOverlay;
var nextAlert = "";  //holds value for alert
var storyOverlay;
var gameOverlay;
var gameSWF = ""; //holds href for game swf
var gameQuery = "";

function openGame(gurl){
	gameSWF = gurl;
	gameOverlay.load();
}

function writeFacebookPlayer(vurl){
	return '<object width="330" height="224">' +
    '<param name="allowfullscreen" value="true" />' +
	'<param name="wmode" value="transparent" />' +
    '<param name="allowscriptaccess" value="always" />' +
    '<param name="movie" value="' + vurl + '" />' +
    '<embed src="' + vurl + '" wmode="transparent" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="330" height="224"></embed>' +
'</object>';
}
$(document).ready(function(){
	
	//IE6 Hover Issue
	$('#shareIE6 .hoverable').hover(
		function(){
			$(this).addClass('hovered');
		},
		function(){
			$(this).removeClass('hovered');
		}
	);
	$('#gameIE6 .hoverable').hover(
		function(){
			$(this).addClass('hovered');
		},
		function(){
			$(this).removeClass('hovered');
		}
	);
	$('#date').html("<span>" + dateWords.dateConverted.month + "</span> <span>" + dateWords.dateConverted.day + "</span>, <span>" + dateWords.dateConverted.year + "</span>");
	
	var camPos = $('#BossCamHolder').offset();
	var storyHref = "";
	var storyTitle = "";
	
	if (!($.browser.msie && parseInt($.browser.version) <= 6)){
		$('#BossCamHolder').css({'position': 'fixed', 'top': camPos.top, 'left': camPos.left}); 
	}

	$(function(){
    	$('#alert').overlay({
			speed: 'fast',
			closeOnClick: false,
			preload: false,
			oneInstance: false,
			zIndex: 850,
			onBeforeLoad: function(){
				$('#alert p').html(nextAlert);
			},
			onClose: function(){
				$('#alert p').html("&nbsp;");
			}
		}).draggable({
			handle: 'a.header',
			cursor: 'move',
			containment: 'parent'
		});
		 
		$('#games').overlay({
			speed: 'fast',
			closeOnClick: false,
			preload: false,
			oneInstance: false,
			zIndex: 500,
			onBeforeLoad: function(){
				loadNewGame(gameSWF);
				$('html').css('overflow','hidden');
			},
			onClose: function(){
				$('#games').html("&nbsp;");
				$('html').css('overflow','scroll');
			}
		}); 
		
		$('#fullStory').overlay({
			speed: 'fast',
			closeOnClick: false,
			preload: false,
			oneInstance: false,
			zIndex: 250,
			onBeforeLoad: function(){
				$('#storyTitle').html(storyTitle);
				$('#story').load(storyHref);	
			}
		}).draggable({
			handle: 'a.header',
			cursor: 'move',
			containment: 'parent'
		}); 
		
		$('#login').overlay({
			speed: 'fast',
			closeOnClick: false,
			preload: false,
			oneInstance: false,
			zIndex: 750,
			onClose: function(){
				resetLoginOverlay();
			}
		}).draggable({
			handle: 'a.header',
			cursor: 'move',
			containment: 'parent'
		});
		
		$('#register').overlay({
			speed: 'fast',
			closeOnClick: false,
			preload: false,
			oneInstance: false,
			zIndex: 500,
			onClose: function(){
				resetRegisterOverlay();
			}
		}).draggable({
			handle: 'a.header',
			cursor: 'move',
			containment: 'parent'
		});       
	});
	//get reference to the overlay
	alertOverlay = $('#alert').overlay();
	loginOverlay = $('#login').overlay();
	registerOverlay = $('#register').overlay();
	storyOverlay = $('#fullStory').overlay();
	gameOverlay = $('#games').overlay();
	
	$('a.openGame').bind("click", function(){
		if (!(gameOverlay.isOpened()))
			gameSWF = $(this).attr('href');
			gameOverlay.load();
			return false;
	});
	
	$('a.openWebisode').bind("click", function(){
		var flashObject;
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		if(isIE) 
		 flashObject = window['pulldown'];
		else 
		 flashObject = document['pulldown'];
		
		flashObject.showTheVideo();			
	});
	$('a.fake').bind("click", function(){
		if (alertOverlay.isOpened())
			alertOverlay.close();
			
		nextAlert = links.getNextAlert();
		alertOverlay.load();
		return false;		
	});
	$('.search a').bind("click", function(){
		if (alertOverlay.isOpened())
			alertOverlay.close();
		nextAlert = links.getSearchAlert();
		alertOverlay.load();
		return false;
	});
	
	$('a.login').bind("click", function(){
		if (loginOverlay.isOpened()) {
			nextAlert = "Login is Already Opened";
			if (alertOverlay.isOpened())
				alertOverlay.close();
			alertOverlay.load();
		}
		else{
			loginOverlay.load();
		}
		return false;
	});
	
	$('a.read').bind("click", function(){
		storyTitle = $(this).attr('title');
		storyHref = $(this).attr('href');
		storyOverlay.close();
		storyOverlay.load();
		return false;
	});
	$('a.register').bind("click", function(){
		if (registerOverlay.isOpened()) {
			nextAlert = "Registration is Already Opened";
			if (alertOverlay.isOpened())
				alertOverlay.close();
			alertOverlay.load();
		}
		else{
			registerOverlay.load();
		}
		return false;
	});
	$('a.videoSelector').bind('click', function(){
		$link = $(this);
		video = $link.attr('href');
		vid = $link.attr('rel');
		$('#facebookVideo').contents().remove();	
		$('#facebookVideo').append(writeFacebookPlayer(video));
		$('.videoSelection .facebook').attr('href', 'http://www.facebook.com/sharer.php?u=http://www.facebook.com/video/video.php?v=' + vid);
		return false;
	});
	
	$('#tabs .tabHandle').bind("click", function(){
		$this = $(this);
		$('#tabs .tabHandle').each(function(){ $(this).removeClass('active');});
		$this.toggleClass('active');
		$('#tabs .content, #login .controls').each(function(){ $(this).hide();});
		$($this.attr('href')).show();
		return false;
	});
	
	$('#surveyForm').submit(function(){
		processPoll('/request/ProcessSurvey.php');
		return false;	
	});
	
	$('#seeResults').click(function(){
		processPoll('/request/ProcessSurvey.php?seeResults');	
		return false;
	});
	
	//HANDLE QUERY FOR GAMES
	if (gameQuery == "meetingblocks"){
		openGame('/sop/de-vite/meetris.swf');
	}
	
});
	

