deepdyve = {
    divId: '',
    divIdList: [],
    docId: '',
    fieldName: 'journal_mjid',
    rentText: 'Rent at DeepDyve',
    affiliateId: 'test',
    host: 'www.deepdyve.com',
	rentalStatus: '',

    insertRentalLink: function() {
		deepdyve.getScript('http://' + deepdyve.host + '/rental-link?docId=' + deepdyve.docId + '&fieldName=' + deepdyve.fieldName + '&key=' + encodeURIComponent(deepdyve.affiliateId) + '&referrer=' + encodeURIComponent(location.href));
    },

    updateDiv: function(response) {
        if (deepdyve.divId) {
            deepdyve.divIdList.push(deepdyve.divId);
        }
        for (var i in deepdyve.divIdList) {
            if (!response) {
                document.getElementById(deepdyve.divIdList[i]).innerHTML = '';
            } else {
                document.getElementById(deepdyve.divIdList[i]).innerHTML = '<a href="' + response + '&affiliate_link=default&referrer=' + encodeURIComponent(location.href) + '">' + deepdyve.rentText + '</a>';
				deepdyve.rentalStatus = 'rentable';
				deepdyve.includejQuery(deepdyve.insertRentalOption, response);
            }
        }

		// Load jQuery for Highwire for tracking purpose
		if (deepdyve.affiliateId == 'highwire-ajpheart' ||
			deepdyve.affiliateId == 'highwire-fasebj' ||
			deepdyve.affiliateId == 'highwire-jleub' ||
			deepdyve.affiliateId == 'highwire-jn' ||
			deepdyve.affiliateId == 'demo' ||
			deepdyve.affiliateId == 'demo-jleub') {
				deepdyve.includejQuery(deepdyve.tracking);
		}
    },

	insertRentalOption: function(linkUrl) {
		if (deepdyve.affiliateId == 'highwire-ajpheart' ||
			deepdyve.affiliateId == 'highwire-fasebj' ||
			deepdyve.affiliateId == 'highwire-jleub' ||
			deepdyve.affiliateId == 'highwire-jn' ||
			deepdyve.affiliateId == 'demo' ||
			deepdyve.affiliateId == 'demo-jleub') {
				
			linkUrl = linkUrl + '&affiliate_link=fullmain&referrer=' + encodeURIComponent(location.href);
				
			if (deepdyve.affiliateId == 'highwire-jleub' ||	deepdyve.affiliateId == 'demo-jleub') {
				// Create the Rental Option <div>s for H20
				var rentalOptionBlock = $('<div style="width: 700px; margin-bottom: 10px; margin-left: 5px;"><div style="margin-bottom: 3px;"><img alt="*" src="http://ajpheart.physiology.org/icons/shared/misc/redtri.gif" style="width: 9px; height: 15px; margin: 0 5px;" /><a href="'+linkUrl+'" style="text-decoration: none;" onmouseover="this.style.textDecoration=\'underline\'" onmouseout="this.style.textDecoration=\'none\'"><font size="+1" color="c63227">Rent Article for <strong style="font-size: 18px">$0.99</strong></font><img src="http://deepdyve.com/assets/images/badge-new.gif" style="vertical-align: top; margin-left: 8px; border: 0;"></a></div><div style="padding-left: 22px;"><IMG alt="*" src="http://ajpheart.physiology.org/icons/shared/misc/redtinytri.gif" style="width: 6px; height: 5px; margin: 5px 5px 10px 2px; float: left;" /><FONT size="-1"><A href="'+linkUrl+'"  onmouseover="this.style.textDecoration=\'underline\'" onmouseout="this.style.textDecoration=\'none\'">Rent this article on DeepDyve</A> - Try out this new service! You may view, but not print, this article online for 1 day for just US$0.99.</FONT><div class="clear"></div></div></div>');
				var anchorElement = $('a[name=signup]');	// Locate the anchor element on the page				
			} else {
				// Create the Rental Option <tr>s
				var rentalOptionBlock = $('<tr><td width="20" align="center" valign="top"><img width="9" height="15" align="middle" src="http://ajpheart.physiology.org/icons/shared/misc/redtri.gif"></td><td width="425" colspan="2"><a href="'+linkUrl+'" style="text-decoration: none" onMouseOver="this.style.textDecoration=\'underline\'" onMouseOut="this.style.textDecoration=\'none\'"><font face="Arial, Helvetica, sans-serif" size="+1" color="003399">Rent Article for <strong style="font-size: 18px">$0.99</strong></font><img src="http://deepdyve.com/assets/images/badge-new.gif" style="vertical-align: top; margin-left: 8px; border: 0;"></a></td></tr> <tr><td width="20"></td><td width="10" align="center" valign="top"><img vspace="2" src="http://ajpheart.physiology.org/icons/shared/misc/redtinytri.gif"></td><td width="415"><font face="Arial, Helvetica, sans-serif" size="-1"><a href="'+linkUrl+'">Rent this article on DeepDyve</A> - Try out this new service! You may view, but not print, this article online for 1 day for just US$0.99.</font><div style="height:12px"></td></tr>');
				var anchorElement = $('font:contains("Purchase Short-Term Access")').parent('td').parent('tr');		// Locate the anchor element on the page
			}
			
			// Insert Rental Option
			rentalOptionBlock.insertBefore(anchorElement);
		}
	},
	
	tracking: function() {
		$('form[name=UserSignIn]').submit(function(e) {
			e.preventDefault();
			var currentTime = new Date().getTime();
			$('form[name=UserSignIn]').unbind();
			deepdyve.getScript('http://'+deepdyve.host+'/php-logger/log_click_get.php?affiliate_key='+deepdyve.affiliateId+'&event_name=highwire_signin&status='+deepdyve.rentalStatus+'&current_time_millis='+currentTime+'&callback=deepdyve.submitForm&callback_param=UserSignIn');			
		});
		$('a:contains("Pay per Article"), a:contains("Regain Access"), a:contains("Subscribe to the Journal")').click(function(e) {
			e.preventDefault();
			var currentTime = new Date().getTime();
			var linkText = $(this).text().toLowerCase().replace(/ /g, '_');
			var linkUrl = encodeURIComponent($(this).attr('href'));
			deepdyve.getScript('http://'+deepdyve.host+'/php-logger/log_click_get.php?affiliate_key='+deepdyve.affiliateId+'&event_name=highwire_'+linkText+'&status='+deepdyve.rentalStatus+'&current_time_millis='+currentTime+'&callback=deepdyve.goToUrl&callback_param='+linkUrl);
		});
	},
	
	goToUrl: function(url) {
		location.href = url;
	},
	
	submitForm: function(form) {
		$('form[name='+form+']').submit();
	},
	
	includejQuery: function(callback, params) {
		if (typeof $ == "undefined") deepdyve.getScript('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
		deepdyve.jQueryReady(0, callback, params);
	},
	
	jQueryReady: function(time_elapsed, callback, params) {
		// Continually polls to see if jQuery is loaded.
		if (typeof $ == "undefined") { // if jQuery isn't loaded yet...
			if (time_elapsed <= 5000) {
				setTimeout("deepdyve.jQueryReady("+(time_elapsed + 200)+", "+callback+", '"+params+"')", 200); // set a timer to check again in 200 ms.
			}
		} else {
			callback(params);
		}
	},	
	
	getScript: function(scriptFile) {
        var newScript = document.createElement('script');
        newScript.type = 'text/javascript';
        newScript.src = scriptFile;
		if (typeof scriptFile!="undefined")
			document.getElementsByTagName('head')[0].appendChild(newScript);
	},

    addEvent: function(obj, eventType, fn) {
        if (obj.addEventListener) {
            obj.addEventListener(eventType, fn, false);
            return true;
        } else if (obj.attachEvent) {
            var r = obj.attachEvent('on' + eventType, fn);
            return r;
        } else {
            return false;
        }
    }
}

deepdyve.addEvent(window, 'load', deepdyve.insertRentalLink);
