/*****************************************/
// Javascript for LemurMonitors.com
//
// Author:	Greg Browne
// Company:	Root Four Imagination Inc.
// Date:	October 2009
// Contact:	gbrowne@rootfour.com
/*****************************************/

var pBlue = '7=*=1';
var pEcono = '3=*=1';
var pSafe = '2=*=1';
var pAlert = '0=*=1';

function changePkg(imgFilename, imgID)
{	
	document.getElementById(imgID).src = 'images/' + imgFilename + '.png';
	document.getElementById(imgID).useMap = '#' + imgFilename + 'Map';
	document.getElementById('benefits').src = 'images/' + imgFilename + 'Benefits.jpg';

	document.getElementById('divUnderBenefits').innerHTML = "<p style='font-size: 150%; font-weight: bold'>Download User Manual</p>";

	document.getElementById('videoButtonHolder').innerHTML = "";
	document.getElementById('soundButtonsHolder').innerHTML = "";
	document.getElementById('soundContainer').innerHTML = "";

	if ('LemurMonitorsSafe' == imgFilename)
		document.getElementById('videoButtonHolder').innerHTML = "<a href='SafeDriverVideo.html'><img src='images/watchvideo.png' alt='Watch Video' /></a>";
	else if ('LemurMonitorsAlert' == imgFilename)
		document.getElementById('soundButtonsHolder').innerHTML = "<a href='javascript:void(0);'><img src='images/alertDriverWarning.png' alt='AlertDriver Warning' onclick=\"playSound('Warning', 'false')\" /></a><br /><a href='javascript:void(0);'><img src='images/alertDriverAlarm.png' alt='AlertDriver Alarm' onclick=\"playSound('Alarm', 'true')\" /></a>";

	var productName = imgFilename.replace("LemurMonitors","") + "Driver";

	//pageTracker._trackPageview(productName);	// Track product clicks in Google Analytics
	document.getElementById('tProduct').href = "http://twitter.com/home?status=" + encodeURIComponent(productName + ' - http://www.LemurMonitors.com'); // Change Twitter link for currently displayed product
	
	document.getElementById('fbProduct').innerHTML = "<iframe src='http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.lemurmonitors.com/index.html%23" + imgFilename.replace("LemurMonitors","") + "&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;font=lucida+grande&amp;colorscheme=light&amp;height=35' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:50px; height:22px; padding-left: 8px; position: relative; top: 8px' allowTransparency='true'></iframe>";	// Change Facebook tag for currently displayed product
	document.getElementById('shareProduct').innerHTML = productName;
}

function loadPkg()
{
	var pkg = location.hash.substring(1);
	
	if (pkg == 'Econo' || pkg == 'Safe' || pkg == 'Alert')
		changePkg('LemurMonitors' + pkg, 'frontPkg');	// Custom front package
	else
		changePkg('LemurMonitorsEcono', 'frontPkg');	// Default front package
}

//var pTimeout = "";
function popupInfo(imgID)
{
//	clearTimeout(pTimeout);
	var popupImg = document.getElementsByName('popupImg').length;

	for (i=0; i<popupImg; i++) {
		document.getElementsByName('popupImg')[i].style.display = 'none';
	}

	if(0 == imgID)
		return;

	document.getElementById(imgID).style.display = 'block';
//	pTimeout = setTimeout("popupInfo(0)", 5000);
		
}

function validateEmail(iconID, emailID, buttonID)
{
	var email = document.getElementById(emailID).value;	// Get email input from user
	document.getElementById(emailID).style.backgroundColor = 'white';
	document.getElementById(buttonID).disabled = "disabled";

	if (!email)	// If blank
	{
		document.getElementById(iconID).innerHTML = '';
		return;
	}

	var dot = email.lastIndexOf(".");	// Find last .
	var at 	= email.lastIndexOf("@", dot);	// Find @ before .
	var extLen = email.length - dot;	// Domain extension length

	if ( (at == -1) || (dot == -1) || extLen < 3 )	// No @ or . or extension has less than 2 chars
	{
		document.getElementById(emailID).style.backgroundColor = 'red';
		document.getElementById(iconID).style.color = 'red';
		document.getElementById(iconID).innerHTML = '&#10008;';
	}
	else	// Valid email detected
	{
		document.getElementById(buttonID).disabled = "";
		document.getElementById(iconID).style.color = 'green';
		document.getElementById(iconID).innerHTML = '&#10004;';
	}
}

function decipher(encrypted) {
	var key = 4; 
	var decrypted = "";

	for(i=0; i < encrypted.length; i++) { decrypted += String.fromCharCode( key ^ encrypted.charCodeAt(i) ); }

	return decrypted;
}

function playSound(soundName, loopState)
{
	document.getElementById('soundContainer').innerHTML = "<object data='AlertDriver_Sine_"+ soundName +".mp3' type='audio/mp3' height='0' width='0' loop='"+ loopState +"'><param name='filename' value='AlertDriver_Sine_"+ soundName +".mp3' /><param name='loop' value='"+ loopState +"' /></object>";
}

function loadYoutube(videoCode, divID, width, height)
{
	var videoHTML = "<object width='" + width + "' height='" + height + "'>";
	videoHTML += "<param name='movie' value='http://www.youtube.com/v/" + videoCode + "&hl=en&fs=1&autoplay=1'></param>";
	videoHTML += "<param name='allowFullScreen' value='true'></param>";
	videoHTML += "<param name='allowscriptaccess' value='always'></param>";
	videoHTML += "<embed src='http://www.youtube.com/v/" + videoCode + "&hl=en&fs=1&autoplay=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='" + width + "' height='" + height + "'></embed>";
	videoHTML += "</object>";

	document.getElementById(divID).innerHTML = videoHTML;
}

function loadSWF(videoCode, divID, width, height)
{
	var swfHTML = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='" + width + "' height='" + height + "' id='" + videoCode + "' align='middle'>";
	swfHTML += "<param name='allowScriptAccess' value='sameDomain' /><param name='allowFullScreen' value='false' /><param name='movie' value='" + videoCode + ".swf' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' />";
	swfHTML += "<embed src='" + videoCode + ".swf' quality='high' bgcolor='#ffffff' width='" + width + "' height='" + height + "' name='" + videoCode + "' align='middle' allowScriptAccess='sameDomain' allowFullScreen='false' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />";
	swfHTML += "</object>";

	document.getElementById(divID).innerHTML = swfHTML;
}

function enterContest()
{
	var submitURL = "http://www.lemurmonitors.com/cgi-bin/contest.pl?lvmAction=enterContest" + "&";
	submitURL += "FirstName=" + encodeURIComponent(document.getElementById('FirstName').value) + "&";
	submitURL += "Email=" + encodeURIComponent(document.getElementById('Email').value) + "&";

	OpenThought.CallUrl(submitURL);
}

function fadingRotator(msg) {

	var colors = new Array ('#000000', '#111111', '#222222', '#333333', '#444444', '#555555', '#666666', '#777777', '#888888', '#999999', '#AAAAAA', '#BBBBBB', '#CCCCCC', '#DDDDDD', '#EEEEEE', '#FFFFFF');	// Hex val of colors to fade between
	var colorMsg;
	var tms = 250;	// timeout (ms)

	var testimonial0 = "\"...I believe the SafeDriver is an ingenious device...\"";
	var testimonial1 = "\"...key fob monitor will soon pay for itself in fuel savings...\"";
	var testimonial2 = "\"...All in all a great product...\"";
	var testimonial3 = "\"...a cool device that makes me more aware...\"";
	var testimonial4 = "\"...One of the best plug n play devices avaialble for the car...\"";
	var testimonial5 = "\"...It is helping my daughter, who has a lead foot...\"";
	var testimonial6 = "\"...a great investment that gives me piece of mind...\"";
	var testimonial7 = "\"...Just by looking at the keys I know how fast he has been going...\"";
	var testimonial8 = "\"...she stays within the limits that we set for her...\"";

	var myMsgs = new Array (testimonial0, testimonial1, testimonial2, testimonial3, testimonial4, testimonial5, testimonial6, testimonial7, testimonial8);
	var readMore = " <a href=\"testimonials.html\">Read More</a>";
	var newMsg = "document.getElementById('testimonial').innerHTML = '" + myMsgs[msg] + readMore + "'";	// Display headline

	var j;
	for (j=1; j < colors.length; j++) {
		colorMsg = "document.getElementById('testimonial').style.color = '" + colors[j] + "'";	// Fade out
		setTimeout( colorMsg , tms*(j+1) );
	}

	setTimeout( newMsg, tms*(colors.length+1) );	// Load new headline

	for (j=colors.length-2; j >= 0; j--) {
		colorMsg = "document.getElementById('testimonial').style.color = '" + colors[j] + "'";	// Fade in
		setTimeout( colorMsg , tms*(2*colors.length-j) );
	}

	msg = (msg+1) % myMsgs.length;			// Ensure array index loops
	var nextMsg = "fadingRotator(" + msg + ")";	// load recursive function call
	setTimeout( nextMsg, tms*(2*colors.length+4));	// execute recursion after 1 second
}

var bgHandle = "";
function backgroundRotator(bgNum, doRotate)
{
	var tms = 7000;	// timeout (ms)

	var imageFile0 = "images/Lemur-Monitors-BlueDriver-Index.jpg";
	var imageFile1 = "images/Lemur-Monitors-EconoDriver-Index.jpg";
	var imageFile2 = "images/Lemur-Monitors-SafeDriver-Index.jpg";
	var imageFile3 = "images/Lemur-Monitors-AlertDriver-Index.jpg";

	var vid0 = "";
	var vid1 = "<a href='http://www.youtube.com/watch?v=aj-TWZK4Kck' target='_blank'><img src='images/Econo-Driver-News-Screenshot.png' alt='Independent New Review' width='200px' /></a>";
	var vid2 = "<a href='http://www.youtube.com/watch?v=lc3imccVDvI' target='_blank'><img src='images/Safe-Driver-News-Screenshot.png' alt='Independent New Review' width='200px' /></a>";
	var vid3 = "";

	var imgFiles 	= new Array(imageFile0, imageFile1, imageFile2, imageFile3);
	var vids 	= new Array(vid0, vid1, vid2, vid3);

	document.getElementById('circle0').innerHTML = "<img src='images/icon-0-off.png' class='seaButtons' />";
	document.getElementById('circle1').innerHTML = "<img src='images/icon-1-off.png' class='seaButtons' />";
	document.getElementById('circle2').innerHTML = "<img src='images/icon-2-off.png' class='seaButtons' />";
	document.getElementById('circle3').innerHTML = "<img src='images/icon-3-off.png' class='seaButtons' />";

	document.getElementById('circle' + bgNum).innerHTML 	= "<img src='images/icon-" + bgNum + "-on.png' class='seaButtons' />";
	document.getElementById('mainVid').innerHTML 		= vids[bgNum];
	document.getElementById('mainImg').src	 		= imgFiles[bgNum];

	bgNum = (bgNum+1) % imgFiles.length;			// Ensure array index loops
	var nextImg = "backgroundRotator(" + bgNum + ", true)";	// load recursive function call
	
	if(doRotate)
		bgHandle = setTimeout( nextImg, tms);		// execute recursion
	else
		clearTimeout(bgHandle);				// Stop rotation
}

var iDeviceImgNum = 0;
function iDeviceRotator()
{
	var tms = 5000;		// timeout (ms)
	var numiPhoneImgs = 7;	// Total # of iPhone images to rotate through
	var numiPadImgs = 5;	// Total # of iPad images to rotate through
	var numiDeviceImgs = numiPhoneImgs;
	var iDevice = "iPhone";

	if (navigator.platform == "iPad") {
		numiDeviceImgs = numiPadImgs;
		iDevice = "iPad";
	}
	document.getElementById('iDevice').innerHTML = "<img src='images/" + iDevice + "inHands" + iDeviceImgNum + ".jpg' alt='BlueDriver on " + iDevice + "' style='position: absolute; left: 1px; top: 337px; z-index: 2' />";

	iDeviceImgNum = (iDeviceImgNum + 1) % numiDeviceImgs;
	var nextImg = "iDeviceRotator()";	// load recursive function call

	iDeviceHandle = setTimeout(nextImg, tms);	// execute recursion
}

function iOSRotator(offset, width)
{
	offset = Math.floor(offset);
	width = Math.floor(width);

	var tms = 5000;		// timeout (ms)
	var numiPhoneImgs = 7;	// Total # of iPhone images to rotate through
	var numiPadImgs = 5;	// Total # of iPad images to rotate through
	var numiDeviceImgs = numiPhoneImgs;
	var iDevice = "iPhone";

	if (navigator.platform == "iPad") {
		numiDeviceImgs = numiPadImgs;
		iDevice = "iPad";
	}

	document.getElementById('inHandsContainer').innerHTML = "<img src='images/" + iDevice + "inHands" + iDeviceImgNum + ".jpg' id='inHandsImg' alt='BlueDriver App' width='" + width + "px' style='position: relative; z-index: 2; top: " + offset + "px; margin-bottom: " + offset + "px' />";

	iDeviceImgNum = (iDeviceImgNum + 1) % numiDeviceImgs;
	var nextImg = "iOSRotator(" + offset + "," + width + ")";	// load recursive function call

	iDeviceHandle = setTimeout(nextImg, tms);	// execute recursion
}

// showFAQ() -	Display FAQ answers based on clicked FAQ
function showFAQ(faqNum) {
	var curFAQ = document.getElementById('currentFAQ').value;	// Get current FAQ displayed
	document.getElementById(curFAQ).style.display = 'none';		// Hide it
	
	var nextFAQ = faqNum + 'Div';					// Set FAQ DIV to be displayed
	document.getElementById(nextFAQ).style.display = 'block';	// Display it
	document.getElementById('currentFAQ').value = nextFAQ;		// Store it
}

/* gup() -	Get URL parameters
 		Source:	http://www.netlobo.com/url_query_string_javascript.html	*/
function gup(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  	var regexS = "[\\?&]"+name+"=([^&#]*)";
  	var regex = new RegExp( regexS );
  	var results = regex.exec( window.location.href );
  	
	if( results == null )
    		return "";
  	else
    		return results[1];
	// Use like: var frank_param = gup( 'frank' );
}

