/* 
 * Log an initial page load where the content search id needs to be created before
 * other logging can take place.
 * @argument type The log type
 * @argument data The log data object
 */
function vp_logContentLoad(type, data) {
	DWREngine.beginBatch();
	
	// Log content search if search id is not set and results or bpp page
	if ((_vpSearchId == 0) && ((_vpPage == "results") || (_vpPage == "bpp"))) {
		vp_logContentSearch();
	}

	// Log site visit
	vp_logSiteVisit();
	
	// Log site activities
	vp_logActivities(type, data);
	
	DWREngine.endBatch();
}

/* 
 * Log a site visit.
 */ 
function vp_logSiteVisit() {
    Log.logVisit();
}

/* 
 * Log a content search.
 */ 
function vp_logContentSearch() {
	Log.logContentSearch({callback:vp_processContentSearch});
}

/* 
 * Process log content search response (callback function)
 * @argument id The content search id
 */ 
function vp_processContentSearch(id) {
    if (id) {
		_vpSearchId = id;
    }
    else {
		_vpSearchId = 0;
	}
}

/*
 * Log content print.
 */
function vp_logContentPrint(printTypeId, pageName, zoneMapId, slugId) {
    var contentZoneId = vp_getContentZoneId(pageName);
    var logData = {zmId:zoneMapId, czId:contentZoneId, ptId:printTypeId};
    vp_logActivity(_LOG_CONTENT_PRINT, logData);
    
	// Omniture log print click
    if (printTypeId == "N") {
    	vp_omniLogClick("event17", "Print Now", "slug;" + slugId, contentZoneId);
    }
    else if (printTypeId == "P") {
    	vp_omniLogClick("event17", "Print Profile", "slug;" + slugId, contentZoneId);
    }
    else if (printTypeId == "E") {
    	// Envelope print click
    	vp_omniLogClick("event17", "Print Envelope", "slug;" + slugId, contentZoneId);
    }
    
    // Atlas log Print Now
    var imgObject = new Image();
    imgObject.src = "http://switch.atdmt.com/action/tgmval_IndivCoupButton_1";
}

/*
 * Log basket add.
 */
function vp_logBasketAdd(pageName, zoneMapId, slugId) {
    var contentZoneId = vp_getContentZoneId(pageName);
    var logData = {czId:contentZoneId, zmId:zoneMapId};
    vp_logActivity(_LOG_BASKET_ADD, logData);

	// Omniture log basket add click
	vp_omniLogClick("scAdd", "Add to Basket", "slug;" + slugId, contentZoneId, null, null);
	
}

/* 
 * Log user activity
 * @argument type The log type
 * @argument data The log data object
 */
function vp_logActivity(type, data) {
    // Log with search id if available
    if (_vpSearchId != 0) {
        Log.logActivity(type, _vpSearchId, data);
    }
    else {
        Log.logActivity(type, '0', data);
    }
}

/* 
 * Log user activity
 * @argument type The log type
 * @argument data The log data object
 */
function vp_logActivities(type, data) {
    // Only log if search id available
    if (_vpSearchId != 0) {
        Log.logActivities(type, _vpSearchId, data);
    }
    else {
        Log.logActivities(type, '0', data);
    }
}

/* 
 * Log link clicks
 * @argument featureType The log feature type
 * @argument zoneMapId The zone map id from the slug containing the link
 * @argument contentZoneId The content zone id of the link
 * @argument couponSlugId The id of the slug containing the link (optional)
 */
function vp_logLinkClick(featureType, zoneMapId, pageName, couponSlugId){
    // Activity Log website
    var contentZoneId = vp_getContentZoneId(pageName);
    
    var linkClickLog = {zmId:zoneMapId, czId:contentZoneId, ftId:featureType};
    
    vp_logActivity(_LOG_LINK_CLICK, linkClickLog);
}

/* 
 * Omniture clear variables
 * @argument type The type of Omniture call - page or click
 */ 
function vp_omniClear(type) {
    s.pageName="";
    s.channel="";
    s.pageType="";
    s.prop1="";
    s.prop2="";
    s.prop3="";
    s.prop4="";
    s.prop5="";
    s.prop6="";
    s.prop7="";
    s.prop8="";
    s.prop9="";
    s.prop11="";
    s.prop12="";
    s.prop14="";
    s.prop15="";
    s.prop16="";
    s.prop17="";
    s.prop24="";
    s.hier1="";
    
    s.events="";
    s.products="";
    s.eVar1="";
    s.eVar2="";
    s.eVar10="";
    s.eVar15="";
    s.eVar16="";
    s.eVar17="";
    s.eVar18="";
    s.eVar20="";
    s.eVar21="";
    s.eVar22="";
    s.eVar23="";
    s.eVar24="";
    
    // Clear certain values for clicks
    if (type == "click") {
	    s.prop14="";
	    s.prop15="";
	    s.prop16="";
	    s.prop19="";
	    s.referrer="";
	    s.campaign="";
	    s.eVar5="";
	    s.eVar11="";
    }
}

/* 
 * Log a standard Omniture link click
 * @argument event The click event
 * @argument name The name of the link or null
 * @argument products The complete products string variable contents
 * @argument zone The content zone id of the link, the page name or null
 * @argument feature The feature type of the link or null
 * @argument vars Object containing props and eVars to modify (optional)
 */
function vp_omniLogClick(event, name, products, zone, feature, vars) {
    vp_omniClear("click");
    s.events = (event) ? event : "link";

    if (products) {
        s.products = products;
    }
    
    if (zone) {
        s.eVar10 = vp_getContentZoneId(zone);
    }
    
    if (feature) {
        s.eVar16 = feature;
    }
    
    // Add/modify the s object with passed in values
    if (vars) {
        $.extend(s, vars);
    }
    
    name = (name) ? name : "";
    s.tl(this, "o", name);
}

/* 
 * Log an Omniture page view
 * @argument name The name of the page or null
 * @argument channel The channel - consumer, advertiser or general or null
 * @argument vars Object containing props and eVars to modify (optional)
 */
function vp_omniLogPage(name, channel, vars) {
    vp_omniClear("page");
    
    if (name) {
        s.pageName = name;
    }
    
    if (channel) {
        s.channel = channel;
    }
    
    // Add/modify the s object with passed in name:value pairs
    if (vars) {
        $.extend(s, vars);
    }

    s.t();
}

function vp_getContentZoneId(pageName) {
    var contentZoneId;
    if (pageName == "results") {contentZoneId = 28;}
    else if (pageName == "bpp" || pageName == "bppPagePrint") {contentZoneId = 31;}
    else if (pageName == "basket") {contentZoneId = 33;}
    else if (pageName == "mpo") {contentZoneId = 34;}
    else if (pageName == "printConf") {contentZoneId = 34;}
    else {contentZoneId = pageName;}

    return contentZoneId;
}

/** Log video states for Martha Stewart (288)
 *  This function is called by the Martha Flash program to log
 *  how the user play the instant win game.
 *  
 * @param status - The video status.  Either 'play' or 'stop'
 * @param value - The video number.  For example, 1 = video #1
 * and 2 = video #2.
 * @return nothing.
 */
function vp_videoPlay(status, value) {
	// Convert the video status to all lower case.
	if (status != null){
		// Convert activity name to lower case.
		status = status.toLowerCase();
		
		// Convert the video number to match with the activity types
		// defined in the VPLogConstants.java
		var activity = 0;
		if (value == 1) {
			activity = 4; // Video 1.
		} else if (value == 2){
			activity = 5; // Video 2.
		}

		if (activity != 0){
			// Log video activity.
			if (status == 'play'){
				vp_logContestActivity(activity, 1); // Play video
			} else if (status == 'stop'){
				vp_logContestActivity(activity, 0); // Stop video
			}
		}
	}
}

/** Log contest actions
 *  This function is called by the Martha Flash program to log
 *  how the user play the instant win game.
 *
 *  @param activity - The user's activity such as webcam, print symbol, etc.
 *  The contest activities are defined in the VPLogConstants.java
 *  These constants are defined here for convenience but should refer
 *  the VPLogConstants.java for the latest values.
 *  
 *   // Contest Activity Types
 *   public static final int CONTEST_ACTIVITY_WEBCAM   				= 1;
 *   public static final int CONTEST_ACTIVITY_PRINT_SYMBOL			= 2;
 *   public static final int CONTEST_ACTIVITY_INSTANT_GAME			= 3;
 *   public static final int CONTEST_ACTIVITY_VIDEO_1				= 4;
 *   public static final int CONTEST_ACTIVITY_VIDEO_2				= 5;
 *   
 *   
 *  @param value - The value input parameter is either 1 or 0.  1 = true/yes/start 
 *  and 0 = false/no/stop.
 *  @return nothing
 *  
 */
function vp_logContestActivity(activity, value) {
	if (activity > 0){
		// Log user's activities for playing the contest.
		Log.logContestActivity(288, activity, value);
	}
}

