/************************************************************
*  Common Scripts
*  This script includes common functions used for this 
*  web site.  Do not include utility-specific functions.
*  Those scripts belong in their own script file(s).
************************************************************/
function InitPage()
{
	// Detect mobile browser
	if (IsMobile())
	{
		$("link[href='scripts/standard.css']").attr("href", "scripts/mobile.css");
	}
	
	// Rounded Corners
	$(".master_body").corner("bottom");
}
function InitOverlayVideos()
{
	// Popup Setup
	$("img[rel]").overlay({
		mask: 'Black',
		top: 20,
		onLoad: function(content) {
			LaunchVideo(this.getOverlay().find("div[video]"));
			TrackDemoRequestForm(this.getOverlay().find("div.form_container"));
		},
		onClose: function(content) {
			ResetVideo(this.getOverlay().find("div[video]"));
			ClearContactFormResults();
		},
        closeOnClick: false
	});
    
    // Rounded Corners
	$(".contentWrap").corner("10px");
}
function InitDemoRequestForm()
{
	form_subject = "Velma Corporate Demo Request";
	form_debugMode = false;
	
	// Load the form
	$("#popup5 .contentWrap").load("contact_form.htm", function() {	
		// Init the form
		InitContactForm();
		
		// Rounded Corners
		$(".contentWrap").corner("10px");
	});
}
function InitiSpeakForm()
{
	// Show/Hide Labels
	$("#iSpeak_Form input[type='text'], #iSpeak_Form textarea").each(function() {
		var showLabel = ($(this).val() == "");
		$("label[for='" + this.id + "']").toggle(showLabel);
	});
	
	// Configure DatePicker
	$("#demodate").datepicker({
		onClose: function(dateText, inst) {
			var showLabel = (dateText == "");
			$("label[for='demodate']").toggle(showLabel);
		}
	});
	
	// Label Events
	$("#iSpeak_Form label").click(function() {
		var fieldID = $(this).attr("for");
		$("#" + fieldID).focus();
		
		if (fieldID == "demodate")
		{
			$(".datepicker").datepicker("show");
		}
	});
	
	// Input Field Events
	$("#iSpeak_Form input[type='text'], #iSpeak_Form textarea").click(function() {
		$("label[for='" + this.id + "']").fadeOut(500);
	});
	$("#iSpeak_Form input[type='text'], #iSpeak_Form textarea").focus(function() {
		$("label[for='" + this.id + "']").fadeOut(500);
	});
	$("#iSpeak_Form input[type='text'], #iSpeak_Form textarea").blur(function() {
		if ($(this).val() == "")
		{
			$("label[for='" + this.id + "']").fadeIn(500);
		}
	});
}
function TrackDemoRequestForm(formContainer)
{
	try
	{
		// Do we have a valid form?
		if ($(formContainer).html() != null)
		{
			// Send to Google Analytics
			var pageTracker = _gat._getTracker("UA-1804118-1");
			pageTracker._setDomainName(".velma.com");
			pageTracker._trackPageview("/contact_form.htm");
		}
	}
	catch(e) { /*ignore*/ }
}

/*** VIDEO PLAYER ***/
function LaunchVideo(playerArea)
{
	try
	{
		var playerID = $(playerArea).attr("id");
		var videoFile = $(playerArea).attr("video");
		
		// Track video in Google Analytics
		TrackVideo(videoFile);
		
		// Play the video
		StartVideo(playerID, videoFile);
	}
	catch(e) { /*ignore*/ }
}
function ResetVideo(playerArea)
{
	try
	{
		var playerID = $(playerArea).attr("id");
		$("#"+playerID).html("<img id='preview_image' src='images/ajax-loader.gif' align='middle' />");
	}
	catch(e) { /*ignore*/ }
}
function StartVideo(playerID, filePath)
{
	// Hide the iSpeak video (if needed)
	try
	{
		hideTheVideo();
		showTheForm();
	}
	catch(e) { /*Ignore*/ }
	
	var params = "file=" + filePath;
	params += "&skin=scripts/snel.swf";
	params += "&controlbar=over";
	params += "&autostart=true";
	params += "&quality=true";
	params += "&abouttext=Velma Video";
	params += "&aboutlink=https://www.velma.com";
	params += "&resizing=true";
	params += "&stretching=fill";
	params += "&screencolor=#000000";

	var s1 = new SWFObject("scripts/player.swf", "ply", "640", "360", "1", "#cdcdcd");
	s1.addParam("allowfullscreen", "true");
	s1.addParam("allowscriptaccess", "always");
	s1.addParam("wmode", "opaque");
	s1.addParam("flashvars", params);
	s1.write(playerID);
}
function TrackVideo(filePath)
{
	try
	{
		// Create fake path for Google Analytics
		var virtualPath = filePath.replace("http://velmamortgage.s3.amazonaws.com", "/amazons3");
	
		// Send to Google Analytics
		var pageTracker = _gat._getTracker("UA-1804118-1");
		pageTracker._setDomainName(".velma.com");
		pageTracker._trackPageview(virtualPath);
	}
	catch(e) { /*ignore*/ }
}

/*** UTILITIES ***/
function GetQueryString()
{
    var assoc = new Array();
    var queryString = unescape(location.search.substring(1));
    var keyValues = queryString.split('&');
    for (var i in keyValues) {
        var key = keyValues[i].split('=');
        assoc[key[0]] = key[1];
    }
    return assoc;
}
var QueryString = GetQueryString();

function IsApple()
{
	return ((navigator.userAgent.match(/iPhone/i)) || 
			(navigator.userAgent.match(/iPod/i)));
}
function IsMobile()
{
	return ((IsApple()) || (navigator.userAgent.match(/Android/i)));
}
