var actionItemTemplate = '<div class=\"wildfire-widget-b-action-item\" onclick=\"doAction(\'BASE_URLan/dashboard\');\">\n' + 
    '<div class=\"wildfire-widget-b-action-checkbox\"><a class=\"wildfire-widget-b-unchecked\" href=\"#\"></a></div>\n' + 
    '<div class=\"wildfire-widget-b-action-details\"><strong>ACTION_ITEM_TITLE</strong><span class=\"wildfire-widget-b-potential-impact\">+</span></div>\n' + 
    '<div class=\"wildfire-widget-clear\"></div>\n' + '</div>\n';

var activityTemplate = '<li>- <a href=\"BASE_URLan/dashboard\">ACTIVITY_LINK_TEXT</a></li>\n';

var newUserGreeting = '<h3><span id=\"wildfire-widget-b-user-fname\"></span>, thanks for joining <span id="wildfire-widget-b-network-name0"></span>!</h3>\n' +
    '<h2>Start making an impact...</h2>';

var newUserActivityDetails = '<h3 id="wildfire-widget-b-personal-impact-header">What is your Personal Impact?</h3>\n'+
    '<span id=\"wildfire-widget-b-personal-impact\">5</span>' +
    '<p>Our amazing Impact Engine tracks the involvement of every supporter. Do an Action an the left and watch your Impact score begin to climb. <a href=\"BASE_URLan/help#ImpactPoints\"><strong>Learn more ></strong></a></p>' +
    '<a href=\"BASE_URLan/tour\"><strong>Take the <span id="wildfire-widget-b-network-name3"></span> Tour ></strong></a>';

var returningUserGreeting = '<h3><span id=\"wildfire-widget-b-user-fname\"></span>, welcome back!</h3>\n' +
    '<h2>You have <span id=\"wildfire-widget-b-user-num-actions\"></span> incomplete actions...</h2>\n';

var returningUserActivityDetails = '<h3>Your Friends\' Activity</h3><ul id=\"wildfire-widget-b-activity-list\"><li>- No recent activity</li></ul><a href=\"BASE_URLan/dashboard\">View All</a>\n';

function doWidgetA(recognizedContainerId, unrecognizedContainerId, options) {
    var rEl = document.getElementById(recognizedContainerId);
    var uEl = document.getElementById(unrecognizedContainerId);    
    if (typeof(window['wildfire']) != 'undefined' && wildfire.user && wildfire.user.id && wildfire.user.id.length > 0) {
	if (options) {
	    insertProps(options);
	}
	showWidget(rEl, uEl);
    } else {
	showWidget(uEl, rEl);
    }
}

function doWidgetB(recognizedContainerId, unrecognizedContainerId, greetingContainerId, actionItemContainerId, activityDetailsContainerId, baseUrl, options) {
    var rEl = document.getElementById(recognizedContainerId);
    var uEl = document.getElementById(unrecognizedContainerId);
    if (typeof(window['wildfire']) != 'undefined') {
	if (wildfire.user && wildfire.user.id && wildfire.user.id.length > 0) {
	    insertHeader(greetingContainerId);
	    insertActivityDetails(activityDetailsContainerId, baseUrl);
	    if (wildfire.user.actionItems) {
		insertActionItems(actionItemContainerId, baseUrl);
	    }
	    showWidget(rEl, uEl);
	} else {
	    showWidget(uEl, rEl);
	}

	if (options) {
	    insertProps(options);
	}
    }
}

function doAction(actionUrl) {
    window.location = actionUrl;
}

function insertHeader(containerId) {
    var el = document.getElementById(containerId);
    if (el) {
	if (wildfire.user.newUser) {
	    el.innerHTML = newUserGreeting;
	} else {
	    el.innerHTML = returningUserGreeting;
	}
    }
}

function insertActionItems(containerId, baseUrl) {
    var el = document.getElementById(containerId);
    var actionItems = wildfire.user.actionItems;
    var actionItemsStr = '';
    if (el) {
    	var i = 0;
    	for (var actionItem in actionItems) {
    		actionItemsStr = actionItemsStr + actionItemTemplate.replace(/ACTION_ITEM_TITLE/, actionItems[actionItem].title).replace(/BASE_URL/g, baseUrl);
    		if (++i == 2) break;
    	}
	el.innerHTML = actionItemsStr;
    }
}

function insertActivityDetails(containerId, baseUrl) {
    var el = document.getElementById(containerId);
    if (wildfire.user.newUser) {
	el.innerHTML = newUserActivityDetails.replace(/BASE_URL/g, baseUrl);
    } else {
	el.innerHTML = returningUserActivityDetails.replace(/BASE_URL/, baseUrl);
	var listEl = document.getElementById('wildfire-widget-b-activity-list');
	var activities = wildfire.user.activities;
	var activitiesStr = '';
	if (listEl) {
	    for (var activity in activities) {
		    activitiesStr = activitiesStr + activityTemplate.replace(/BASE_URL/g, baseUrl).replace(/ACTIVITY_LINK_TEXT/, activities[activity]);
	    }
	    if (activitiesStr.length > 0) {
		    listEl.innerHTML = activitiesStr;
	    }
	}
    }
}

function insertProps(options) {
    if (options.orgProps && wildfire.org) {
	insertPropsFromObject(options.orgProps, wildfire.org);
    }
    if (options.userProps && wildfire.user) {
	insertPropsFromObject(options.userProps, wildfire.user);
    }
    if (typeof wildfire.myActionItems != 'undefined' && options.actionItemProps) {
	insertPropsFromObject(options.actionItemProps, wildfire.user.actionItems);
    }
}


/**
 * Inserts the values of properties of the supplied object as the innerHTML of elements.
 * This is accomplished by supplying a hash (props) of element ids to the path to the property
 * on the supplied object (obj) whose value you want inserted. An example hash that would
 * insert the value of obj.address.city as the innerHTML of the element with the id
 * 'city-element' would be:
 * var props = { 'city-element': 'address.city' }
 */
function insertPropsFromObject(props, obj) {
    for (var prop in props) {
	var el = document.getElementById(prop);
	var propPathArr = props[prop].split('.');

	if (el) {
	    el.innerHTML = getPropertyValue(propPathArr, obj);
	}
    }
}

function getPropertyValue(propPathArr, obj) {
    if (propPathArr.length < 1) {
	return undefined;
    }

    if (propPathArr.length == 1) {
	return obj[propPathArr[0]];
    }

    obj = obj[propPathArr.shift()];
    return getPropertyValue(propPathArr, obj);
}

function showWidget(toShow, toHide) {
    if (toHide) {
        toHide.style.display = 'none';
	toHide.style.visibility = 'hidden';
    }
    if (toShow) {
        toShow.style.display = 'block';
        toShow.style.visibility = 'visible';
    }
}

function unhint(el, actualType, hintText) {
    if (el.value == hintText) {
	el.type = actualType;
	el.value = '';
    }
}

function hint(el, hintText) {
    if (el.value.length < 1) {
	el.type = 'text';
	el.value = hintText;
    }
}

function logout(logoutUrl, redirectUrl) {
	var loc = logoutUrl;
	if (redirectUrl && redirectUrl.length > 0) {
		loc += loc.indexOf("?") == -1 ? "?r=" : "&r=";
	    loc += redirectUrl;
	}
	window.location = loc;
}

