/*
--------------------------------------------------

Allstate Teen
Site-Wide Scripts [application.js]

Joe Morrow [joe.morrow@acquitygroup.com]
5/26/2010

Copyright (c) 2008-2010 Acquity Group LLC

--------------------------------------------------
*/


$(document).ready(function() {
	
	$("body select").msDropDown();

	// Paging Functions for Contact Gallery
	$('a.pageLink').click(function() {
		$('div.pageSet').addClass('hidden');
		$('div' + $(this).attr('href')).removeClass('hidden');
		$('a.pageLink').removeClass('activePageLink');  
		$(this).addClass('activePageLink');  
		return false;
	});
	
	// Gives each field a title tag with the text from the label
	// Custom titles set for month/year fields
	$('.inline-prompt').each(function() {
		$(this).attr("title", $("label[for='" + $(this).attr('id') + "']").html());
		if ($(this).attr('id') == 'approvalFeedback') {
			$(this).attr("title", 'Please use this space to provide any additional input you may have.');
		}
		if ($(this).attr('id') == 'birth-month') {
			$(this).attr("title", 'mm');
		}
		if ($(this).attr('id') == 'birth-day') {
			$(this).attr("title", 'dd');
		}
		if ($(this).attr('id') == 'birth-year') {
			$(this).attr("title", 'yyyy');
		}
		if ($(this).attr('id') == 'license-month') {
			$(this).attr("title", 'mm');
		}
		if ($(this).attr('id') == 'license-year') {
			$(this).attr("title", 'yy');
		}	
		if ($(this).attr('id') == 'graduation-month') {
			$(this).attr("title", 'mm');
		}
		if ($(this).attr('id') == 'graduation-year') {
			$(this).attr("title", 'yy');
		}
		if ($(this).attr('id') == 'phone-area') {
			$(this).attr("title", '123');
		}
		if ($(this).attr('id') == 'phone-exchange') {
			$(this).attr("title", '456');
		}
		if ($(this).attr('id') == 'phone-number') {
			$(this).attr("title", '7890');
		}
		if ($(this).attr('id') == 'comments') {
			$(this).attr("title", 'Please use this space to provide your comments');
		}
	}).focus(function() {
		if ($(this).val() == $(this).attr("title"))
			$(this).val("").removeClass("inline-prompt");
	}).blur(function() {
		if ($(this).val() == "")
			$(this).val($(this).attr("title")).addClass("inline-prompt");
	});
	

	// Remove italics from textarea on contract approval page on click
	//$('#approvalFeedback').click(function() {
	//	$(this).removeClass('unfocusedTextarea');
	//});

	// Home page carousel paging
	$('a.prev').click(function() {
	    var selected = $('div.carousel.selected');
	    if (selected.prev('div.carousel').length != 0)
	        selected.addClass('hidden').removeClass('selected').prev().addClass('selected').removeClass('hidden');
	});
	$('a.next').click(function() {
	    var selected = $('div.carousel.selected');
	    if (selected.next('div.carousel').length != 0)
	        selected.addClass('hidden').removeClass('selected').next().addClass('selected').removeClass('hidden');
	});

	// Module Page Navigation
	$("#prevModule a").click(function() {
		modulePrev("modulePlaceholder");
		return false;
	});
	$("#nextModule a").click(function() {
		moduleNext("modulePlaceholder");
		return false;
	});
	
});


// Hide login on pages that need it removed 
	$(document).ready(function() {
		$("body.contractApproval #utility-nav").addClass("hidden");
		$("body.Login.profile #utility-nav").addClass("hidden");
	});

//IE6 Layering with BGI Frame
	$(document).ready(function() {
		$("body.home #licenseContainer").bgiframe();
	});
	
// Z-index Layering in IE6
//$(document).ready(function() {	
//	if($('body').hasClass('browserIE7') || $('body').hasClass('browserIE6')) {
//		var zIndexNumber = 1000;
//		$('li').each(function() {
//			$(this).css('zIndex', zIndexNumber);
//			zIndexNumber -= 10;
//		});	
//	}
//});

//Show License Popup
	$(document).ready(function() {
		$("a#viewLicense").click(function() {
			$("#licenseContainer").removeClass("hidden");
			var modalBackground = document.createElement('div');
			modalBackground.className = 'modalbackground';
			document.body.appendChild(modalBackground);
			return false;			
		});
	});
	
//Hide License Popup
	$(document).ready(function() {
		$(".modalclose").click(function() {
			$("#licenseContainer").addClass("hidden");
			$(".modalbackground").removeClass("modalbackground");
		});
	});

	$(document).ready(function() {
		$(".modalbackground").click(function() {
			$("#licenseContainer").addClass("hidden");
			$(".modalbackground").removeClass("modalbackground");
		});
	});
	
	
// Show opt-out on edit page
	$(document).ready(function() {
		$("#body.registrationEdit fieldset.optOut").removeClass("hidden");
	});

// browser detectors ($.browser is deprecated in jQuery 1.3 and $.support isn't helpful when targeting style rules)
function isIE6() {
	if (typeof($.isIE6) == "undefined") {
		jQuery.extend({
			isIE6: ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined)) ? true : false
		});
	}
	return $.isIE6;
}


// Modal Handlers
var setVideoName = function(name, link) {
	$("#videoModal .infoText a").attr("href", link).html(name);
};	


// Module Page Handlers
var moduleFadeOut = function(callback) {
	$("#module .moduleFlag, #prevModule a, #nextModule a, #moduleDescription").fadeOut((isIE6()) ? 0 : 800, callback);
	return false;
};

var moduleFadeIn = function(callback) {
	$("#prevModule a, #nextModule a").fadeIn((isIE6()) ? 0 : 800, callback);
	return false;
};

var moduleDestroy = function(moduleID) {
	// Destroy the module
	swfobject.removeSWF(moduleID);
	// Replace the module container, if necessary
	$("#moduleDescription").show(0, function() {
		if ($("div#" + moduleID).length == 0)
			$("#moduleDescription").after('<div id="' + moduleID + '" class="hidden"></div>');
	});
	return false;
};	

var modulePrev = function(moduleID) {
/* Developer Note 6/22/2010:
1.  Todo:  Need to add code here to enable the previous module, once more than one module is available.
*/
	if (typeof(console) != "undefined")
		console.log("previous module");

	// Destroy the current module
	moduleDestroy(moduleID);
	return false;
};

var moduleNext = function(moduleID) {
/* Developer Note 6/22/2010:
1.  Todo:  Need to add code here to enable the next module, once more than one module is available.
*/
	if (typeof(console) != "undefined")
		console.log("next module");

	// Destroy the current module
	moduleDestroy(moduleID);
	return false;
};

var loadModule = function(swfPath, moduleID, flashvars, params, attrs, callback) {

	if (flashvars == null)
		flashvars = {};
	if (params == null)
		params = {};
	if (attrs == null) 
		attrs = {};

	// Init default params
	params.width = (params.width != null) ? params.width : "660";
	params.height = (params.height != null) ? params.height : "440";
	params.quality = (params.quality != null) ? params.quality : "high";
	params.align = (params.align != null) ? params.align : "middle";
	params.play = (params.play != null) ? params.play : "play";
	params.loop = (params.loop != null) ? params.loop : "true";
	params.scale = (params.scale != null) ? params.scale : "showall";
	params.wmode = (params.wmode != null) ? params.wmode : "transparent";
	params.devicefont = (params.devicefont != null) ? params.devicefont : "false";
	params.bgcolor = (params.bgcolor != null) ? params.bgcolor : "#ffffff";
	params.menu = (params.menu != null) ? params.menu : "true";
	params.allowFullScreen = (params.allowFullScreen != null) ? params.allowFullScreen : "false";
	params.allowScriptAccess = (params.allowScriptAccess != null) ? params.allowScriptAccess : "always";
	params.salign = (params.salign != null) ? params.salign : "";
	params.type = (params.type != null) ? params.type : "application/x-shockwave-flash";
	params.pluginspage = (params.pluginspage != null) ? params.pluginspage : "http://www.adobe.com/go/getflashplayer";

	// Load the SWF
	swfobject.switchOffAutoHideShow();
	swfobject.embedSWF(swfPath, moduleID, params.width, params.height, '9.0.0', 'media/flash/expressInstall.swf', flashvars, params, attrs, callback);
	return false;
};

var moduleBegin = function(swfPath, moduleID, flashvars, params, attrs, callback) {
	// Load the SWF
	loadModule(swfPath, moduleID, flashvars, params, attrs, callback);
	// Turn off the navigation
	moduleFadeOut();
	return false;
};

// Module Flash Communication
var moduleEnd = function(moduleID, returnData) {
	postNiaUpdate();

	// Turn on the navigation
	moduleFadeIn();

/* Developer Note 6/22/2010:
1.  Todo:  Need to add code here to pass the JSON-formatted value object to the backend system.

	if (typeof(console) != "undefined")
		console.log(returnData);
	return false;
*/
	
	/* Displays received var/value pairs for clarity. Delete after backend knows what they're getting. */
	//alert("ActionScript says: moduleID = " + moduleID);
	//var arr = new Array();
	//arr = ['userID', 'completed'];
	//for (var item in returnData) {
    	//alert("ActionScript says: " + arr[item] + " = " + returnData[item]);
	//}
	/* This is a hard-coded response back to Flash. Replace values with backend data after backend response. */
	//thisMovie("drive-in").sendToActionScript({'claimReward': 'true', 'earnedLessons': '07/24'});
};


// Module Flash Communication


function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

// Contract Flash Communication
function sendFromRecorder(_obj){
	var arr = new Array();
	var msg = '';
	arr = ['mediaType', 'mediaContent', 'mediaPath', 'userThumb','firstName', 'lastName', 'userEmail', 'parentEmail', 'userMessage', 'termsPost', 'termsEULA', 'timeStamp']
	for (var item in _obj) {
		//alert(item + '] ' + arr[item] + " : " + _obj[item] + " ");
		msg = msg + arr[item] + "=" + _obj[item] + "&";
	}
	createContract(msg);
}