// JavaScript Document
/* CONFIGURATION */
document.domain = "hirevue.com";
var qip_css_url = "http://now.eloqua.com/e/er.aspx?s=2050&lid=105&elq=0";
var qip_js_url = "http://now.eloqua.com/e/er.aspx?s=2050&lid=106&elq=0";
var elq_cfg_path = "http://now.eloqua.com/e/er.aspx?s=2050&lid=102&elq=0";
var elq_img_path = "http://now.eloqua.com/e/er.aspx?s=2050&lid=103&elq=0";
var elq_cpers_path = "http://now.eloqua.com/e/er.aspx?s=2050&lid=104&elq=0";
var	elqPPS = '50';
var gated_asset_settings_lookup_key = escape('098139721c604a378d304fb2e09a51a3');
var gated_asset_lookup_key = escape('820a75e374244b8984390b7e0dd76456');
var gated_visitor_lookup = escape('39fe31d569a048b282553c9c0d52e0b3');
var gated_group_lookup = escape('03303c5d840141948ee9c9046428707b');
var gated_contact_lookup = escape('5fd26e05befe41e89aab5aac64d0d075');
var email_script = "<eloqua" + " type='emailfield'" + " syntax='EmailAddress' />";
var elq_email = "<eloqua type='emailfield' syntax='EmailAddress' />";

/* APP VARIABLES */
var  APP_CONSTANTS = {	
	FLAG_QTIP_CSS_LOADED: 0x00000001,
	FLAG_QTIP_JS_LOADED: 0x00000002,
	FLAG_ELQCFG_JS_LOADED: 0x00000004,
	FLAG_GATE_SETTINGS_LOADED: 0x00000008,
	FLAG_GATE_VISITOR_LOADED: 0x000000010,
	FLAG_GATE_CONTACT_GROUPS_LOADED: 0x00000020,
	FLAG_GATE_CONTACT_LOADED: 0x00000040,
	FLAG_ALL_LOADED : 127,
	LOOKUP_KEY_GATED_ASSET_SETTINGS : "KEY_GATED_ASSET"
};

var lookup_email = "";
var first_name = "";
var last_name = "";
var url_cid = gup('cid');
var url_lead_source = gup('ls');
var loaded_libs = 0;
var gated_asset_settings = {};
var gated_assets = {};
// Load dependancies
/* JQUERY LIB */
function initJQuery() {    
	IsGroupMember = function(){ return false; }
	//if the jQuery object isn't available
	if (typeof(jQuery) == 'undefined') {
		if (typeof initJQuery.jQueryScriptOutputted == 'undefined') {
			//only output the script once..
			initJQuery.jQueryScriptOutputted = true;
	
			//output the script (load it from google api)
			document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js\"></scr" + "ipt>");
			}
			setTimeout("initJQuery()", 500);
	} else {
		/* QTIP LIBS */
		
		$("head").append("<link rel='stylesheet' type='text/css' href='"+qip_css_url+"' />");
		loaded_libs |= APP_CONSTANTS.FLAG_QTIP_CSS_LOADED;
		jQuery.getScript( qip_js_url, function(){
					loaded_libs |= APP_CONSTANTS.FLAG_QTIP_JS_LOADED;
				});
		/* ELOQUA LIBS */
		if ( typeof ( elqCurESite ) == 'undefined' )
		{
			// Eloqua scripts are not loaded, load them.
			jQuery.getScript( elq_cfg_path, function(){
				loaded_libs |= APP_CONSTANTS.FLAG_ELQCFG_JS_LOADED;
				loadGateSettings();
			});			
		}
		else
		{
			loaded_libs |= APP_CONSTANTS.FLAG_ELQCFG_JS_LOADED;	
			loadGateSettings();
		}
	}
}
initJQuery();	
/* GATE FUNCTIONS */
function loadGateSettings()
{
	// do lookup - load gate settings
	elqDLKey = gated_asset_settings_lookup_key;
	elqDLLookup = '<KEY1>'+APP_CONSTANTS.LOOKUP_KEY_GATED_ASSET_SETTINGS+'</KEY1>';
	var script_url = getElqScriptUrl( elqDLKey, elqDLLookup );
	jQuery.getScript( script_url, function(){
	   loaded_libs |= APP_CONSTANTS.FLAG_GATE_SETTINGS_LOADED;	
		if ( typeof ( GetElqContentPersonalizationValue ) != 'undefined' )
		{
			gated_asset_settings[ 'group_id' ] = GetElqContentPersonalizationValue('Contact_Group_ID1');
			gated_asset_settings[ 'form_url' ] = GetElqContentPersonalizationValue('Gate_URL1');
			gated_asset_settings[ 'width' ] = GetElqContentPersonalizationValue('Form_Width1');
			gated_asset_settings[ 'height' ] = GetElqContentPersonalizationValue('Form_Height1');
			gated_asset_settings[ 'campaign_id' ] = GetElqContentPersonalizationValue('CampaignID1');
			gated_asset_settings[ 'lead_source' ] = GetElqContentPersonalizationValue('LeadSource1');
		}
		// do lookup - Visitor info
		elqDLKey = gated_visitor_lookup;
		elqDLLookup = '';
		var script_url = getElqScriptUrl( elqDLKey, elqDLLookup );
		jQuery.getScript( script_url, function(){
			loaded_libs |= APP_CONSTANTS.FLAG_GATE_VISITOR_LOADED;										   
			if ( typeof( GetElqContentPersonalizationValue ) != 'undefined' )
			{
				lookup_email = GetElqContentPersonalizationValue('V_ElqEmailAddress');
				if ( elq_email != email_script && elq_email.length > 0)
					lookup_email = elq_email;
				// do lookup - contact group
				elqDLKey = gated_group_lookup;
				elqDLLookup = '<C_EmailAddress>'+lookup_email+'</C_EmailAddress>';
				var script_url = getElqScriptUrl( elqDLKey, elqDLLookup );
				
				jQuery.getScript( script_url, function(){
					loaded_libs |= APP_CONSTANTS.FLAG_GATE_CONTACT_GROUPS_LOADED;
					if ( typeof ( GetElqGroupMembershipStatus ) != 'undefined' )
						IsGroupMember = GetElqGroupMembershipStatus;
					// do lookup - contact
					elqDLKey = gated_contact_lookup;
					elqDLLookup = '<C_EmailAddress>'+lookup_email+'</C_EmailAddress>';
					var script_url = getElqScriptUrl( elqDLKey, elqDLLookup );
					jQuery.getScript( script_url, function(){
						loaded_libs |= APP_CONSTANTS.FLAG_GATE_CONTACT_LOADED;
						if ( typeof ( GetElqContentPersonalizationValue ) != 'undefined' )
						{
							first_name = GetElqContentPersonalizationValue('C_FirstName');
							last_name = GetElqContentPersonalizationValue('C_LastName');
						}						
						if ( typeof ( pageinfo ) != 'undefined' )
							gatePage();
						if ( typeof ( onGateInfoLoaded ) != 'undefined' )
							onGateInfoLoaded();									   
					});					
				});	
			}
			else
			{
				loaded_libs |= APP_CONSTANTS.FLAG_GATE_CONTACT_GROUPS_LOADED;	
				if ( typeof ( pageinfo ) != 'undefined' )
						gatePage();
				if ( typeof ( onGateInfoLoaded ) != 'undefined' )
						onGateInfoLoaded();
			}
			
		});	
	});
	
		
}
function getAsset( asset_id )
{
	var url = "";
	if ( gated_assets[ asset_id ] )
	{
		url = gated_assets[ asset_id ];
		window.open(url,"_blank");
		return;
	}
	if ( loaded_libs != APP_CONSTANTS.FLAG_ALL_LOADED )
		setTimeout("getAsset("+ asset_id+")", 50);
	// do lookup
	elqDLKey = gated_asset_lookup_key
	elqDLLookup = '<ID1>'+asset_id+'</ID1>';
	var script_url = getElqScriptUrl( elqDLKey, elqDLLookup );
	jQuery.getScript( script_url, function(){
		url = GetElqContentPersonalizationValue('Asset_URL1');
		gateLink( url, 
				 {  "a_id" : asset_id,
				 	"getAssetInfo" : GetElqContentPersonalizationValue
				 });		
	});
	
}

function gatePage()
{
	gateLink( null, pageinfo );
}

function gateLink( url, info )
{
	if ( info == null ) info = {
	cid: url_cid,
	ls: url_lead_source
	};
	
	// Check we have defined lead source / campaign id.
	if ( typeof ( info.cid ) == 'undefined' ) info.cid = url_cid;
	if ( typeof ( info.ls ) == 'undefined' ) info.ls = url_lead_source;
	
	var getAssetInfo = info.getAssetInfo;
	// If no asset info callback was passed, assume we are getting directly from
	// the user. 
	if ( getAssetInfo == null ){
		getAssetInfo = function(param){
			if ( param == 'CampaignID1' )
				return info.cid;
			if ( param == 'LeadSource1' )
				return info.ls;
			return "";
		};
	}
	
	// Check we have defined lead source / campaign id.
	if ( typeof ( info.cid ) == 'undefined' ) info.cid = url_cid;
	if ( typeof ( info.ls ) == 'undefined' ) info.ls = url_lead_source;
	
	var asset_id = info.a_id;
	if ( asset_id && gated_assets[ asset_id ] )
	{
		url = gated_assets[ asset_id ];
		window.location = url;
		return;
	}
	if ( loaded_libs != APP_CONSTANTS.FLAG_ALL_LOADED )
		setTimeout("gateLink("+url+")", 50);
		
	var guid = getAssetInfo('Contact_Group_GUID1');
	var campaign_id = getAssetInfo('CampaignID1');
	var lead_source = getAssetInfo('LeadSource1');

	if ( guid.length == 0 )	guid = gated_asset_settings.group_id;
	if ( campaign_id.length == 0 ) campaign_id = gated_asset_settings.campaign_id;
	if ( lead_source.length == 0 ) lead_source = gated_asset_settings.lead_source;
	
	var height = getAssetInfo('Form_Height1');
	var width = getAssetInfo('Form_Width1');
	var form_url = getAssetInfo('Form_Url1');		
	if ( form_url.length == 0 )
	{
		height = gated_asset_settings.height;
		width = gated_asset_settings.width;
		form_url = gated_asset_settings.form_url;
	}
	
	var div_width = width - 20;
	var div_height = height;
	var iFrame = $("<iFrame id='iGatedForm' src='"+form_url+"' scrolling='no' frameborder='0' style='width:"+width+"px; height:"+height+"px; overflow:hidden'></iframe>" );    
	var hDiv = $("<div id='dvGatedContent' style='width:"+div_width+"px; height:"+div_height+"; display:none; overflow:hidden'></div>").append(iFrame);
		
	if ( IsGroupMember( guid ) )
	{
		window.location = url;
		/*iFrame.load(function(){
			// pass form hidden information to form
			try
			{
				var iForm = 	$(iFrame).contents().find("form");
				if ( iForm.length > 0 )
				{
					if (lead_source.length > 0 )
						iForm.find("[name=leadSource]").val( lead_source );
					if ( campaign_id.length > 0 )
						iForm.find("[name=hiddenCampaignID]").val( campaign_id );
					iForm.submit();
					iFrame.load(function(){
						gated_assets[ asset_id ] = url;
						window.location = url;
					});
				}
				
			}
			catch(e){}			
			
		});
		$('body').append(hDiv);*/
		
	}
	else
	{
					

		hDiv.qtip({
					id: 'modal', // Since we're only creating one modal, give it an ID so we can style it
					content: {
						text: hDiv,				
						title: {
							text: '',
							button: false
						 }
					},
					style: {
						classes: 'ui-tooltip-light ui-tooltip-rounded'
					},
					position: {
						at: "center",
						my: "center",
						target: $(window) // my target
					},
					show: {
						event: 'click', // Show it on click...
						solo: true, // ...and hide all other tooltips...
						modal: {
							blur: true,
							keyboard: true
						}, // ...and make it modal,
					  },
					hide: false
				});
		var loaded = false;
		iFrame.load(function(){
			// pass form hidden information to form
			if ( loaded == false )
			{
				loaded = true;
				var iForm = 	$(iFrame).contents().find("form");
				$( iForm ).find("[name=leadSource]").val( lead_source );
				$( iForm ).find("[name=hiddenCampaignID]").val( campaign_id );			
				$( iForm ).find("[name=email]").val( lookup_email );
				$( iForm ).find("[name=firstName]").val( first_name );
				$( iForm ).find("[name=lastName]").val( last_name );
				iFrame.load(function(){
					window.location = url;
				});
				oldOnSubmit = iForm[0].onsubmit || function(){};
				iForm[0].onsubmit = function(e){
					var ret = oldOnSubmit.call(this, e || window.event) !== false;
					if ( ret )
					{
						hDiv.qtip('hide');
						gated_assets[ asset_id ] = url;
					}				
					return ret;
				}
			}
			
			
		});
		$('body').append(hDiv);
		hDiv.qtip('show');
	}
		
}

function getContactGroups( email, callback )
{	
	elqDLKey = gated_group_lookup;
	elqDLLookup = '<C_EmailAddress>'+email+'</C_EmailAddress>';
	var script_url = getElqScriptUrl( elqDLKey, elqDLLookup );
	jQuery.getScript( script_url, function(){
	   var ret = new Array();
		if ( typeof ( GetElqGroupMembershipStatus ) != 'undefined' )
		{
				var str = GetElqGroupMembershipStatus.toString();
				var token_start = "toLowerCase() == '";
				var token_end = "'";
				var idx = str.indexOf( token_start );
				while ( idx > -1 )
				{
					idx += token_start.length;
					idx_end = str.indexOf( token_end, idx );
					ret.push( str.substring( idx, idx_end ) );
					idx = str.indexOf( token_start,idx_end ); 
				}
				
		}
		callback( ret );
	});	
}

function getElqScriptUrl( argElqDLKey, argElqDLLookup )
{
	var elqDt = new Date();
	var elqMs = elqDt.getMilliseconds();
	return elqCurE + '?pps=' + elqPPS + '&siteid=' + elqSiteID + '&DLKey=' + argElqDLKey + '&DLLookup=' + argElqDLLookup + '&ms=' + elqMs;
}

function gup( name, url )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	if ( url == null )
		url = window.location.href;
	var results = regex.exec( url );
	if( results == null )
	  return "";
	else
	  return unescape(results[1]);
}
