/*
 *
 * SGF content engine, updated 2010-05-26 04:40 pm 
 *
 */

var TRACK_GENERAL = "Strategic";
var TRACK1 = "IPO and transaction readiness";
var TRACK2 = "What's next on the growth agenda?";
var SESSION_EXCLUDE = "SPEAKERS NOT ASSOCIATED WITH A SESSION";
var SPEAKER_COMPANY_HACK = "Inside Edition";
var SPEAKER_COMPANY_HACK_REPLACE = "<i>Inside Edition</i>";
var ICON_BASE = "http://a59.g.akamai.net/f/59/9312/1m/ernstyoung.download.akamai.com/9312/fy2010/markets/sgm/sgf/images/session_icons/";
var PHOTO_BASE_PATH2 = "http://a59.g.akamai.net/f/59/9312/1m/ernstyoung.download.akamai.com/9312/fy2010/markets/sgm/sgf/images/speaker_photos2010/list/";
var PHOTO_BASE_PATH3 = "http://a59.g.akamai.net/f/59/9312/1m/ernstyoung.download.akamai.com/9312/fy2010/markets/sgm/sgf/images/speaker_photos2010/singles/";
var SPONSOR_BASE = "http://a59.g.akamai.net/f/59/9312/1m/ernstyoung.download.akamai.com/9312/fy2010/markets/sgm/sgf/images/sponsors/";
var HOME_LINK = "/US/en/Services/Strategic-Growth-Markets/SGF_Article_Overview_Page_Main";
var SPEAKER_KEY = "speakerguid";
var SPEAKER_LINK_BASE = "/US/en/Services/Strategic-Growth-Markets/Strategic-Growth-Forum---Guest-speaker-line-up?"+SPEAKER_KEY+"=";
var SPEAKER_ALL_LINK = "/US/en/Services/Strategic-Growth-Markets/Strategic-Growth-Forum---Guest-speaker-line-up";
var SESSION_KEY = "sessionguid";
var SESSION_REFERRER_KEY = "from";
var SESSION_LINK_BASE = "/US/en/Services/Strategic-Growth-Markets/Strategic-Growth-Forum---Session-tracks?"+SESSION_KEY+"=";
var SESSION_ALL_LINK = "/US/en/Services/Strategic-Growth-Markets/Strategic-Growth-Forum---Session-tracks";

var sgfSessions = null, sgfUsers = null;

function sgfContentLoaded(json){
    sgfSessions = json;
    status = "content loaded";
}
function sgfUserContentLoaded(json){
    sgfUsers = json;
    status = "content loaded";
}


$(document).ready(function() {

    // test if agenda tables exist
    if ($('#agenda').length) {
        if (sgfSessions!=null) {
            // check for individual session request
            var sessionGuid = getQueryValue(SESSION_KEY);
            if (sessionGuid!=null && sessionGuid.length>0) {
                renderSessionDetail(sgfSessions, 'agenda', sessionGuid);
                $('.intro').hide();
                $('#sub_middle_1col h2').hide();
            } else {
                // show all sessions
                renderSessionList(sgfSessions, 'agenda');
                // agenda table configuration
                configureTabs('agenda', 'sessionSortNav', 0);
                configureTabs('byDay', 'byDayNav', 1);
                configureTabs('byTrack', 'byTrackNav', 0);
            }
        } else renderNoContentWarning('agenda', 'The agenda is temporarily unavailable. Please check here in a few minutes.');
    }

    // test if speaker section exists
    if ($('#speaker_section').length) {
        if (sgfUsers!=null) {
            // check for individual speaker request
            var speakerGuid = getQueryValue(SPEAKER_KEY);
            if (speakerGuid!=null && speakerGuid.length>0) {
                $('#sub_middle_1col .intro').hide();
                renderSpeakerDetail(sgfUsers, 'speaker_section', speakerGuid);
            } else {
                // show all speakers
                renderSpeakerList(sgfUsers, 'speaker_section');
            }
        } else renderNoContentWarning('speaker_section', 'The list of speakers is temporarily unavailable. Please check here in a few minutes.');
    }
    
    // display content section
    $('#contentSection').show();

    // accordion configuration
    try {
        $('.example').accordion({header: '.main', autoHeight: false, fillSpace: false,active: false, alwaysOpen: false});
    } catch(e) { }
    
});

function configureTabs(containerID, tabNavigatorID, tabIndexToShow)
{
    var tabContainers = $('#'+containerID+' > div');
    tabContainers.hide().filter(function(index) { return(index==tabIndexToShow); }).show();
    $('#'+tabNavigatorID+' a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('#'+tabNavigatorID+' a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(function(index) { return(index==tabIndexToShow); }).click();
}

/* move navigation menu - called from Flash */
function displayNav()
{
    $('#feature div:first').animate( { width:"705px"}, 400 );
    $('#sub_rightcolumn').animate({marginTop:"38px"}, 400);
    $('#featurenav h3').animate({marginTop:"10px"}, 400);
}

function showSessionsBy(selectEl)
{
}

function expand(param)
{
    param.style.display=(param.style.display=="none")?"":"none";
}

function getQueryValue(queryID,win) {
	if (!win) win=window;
	var queryString = win.location.search;
	queryID = queryID+"=";
	var idPos = queryString.indexOf(queryID);
	var queryValue = "";
	if (idPos>=0) {
		var ampPos = queryString.indexOf("&",idPos+queryID.length);
		if (ampPos<0) ampPos = queryString.length;
		queryValue = queryString.substring(idPos+queryID.length,ampPos);
    	return(unescape(queryValue));
	} else return(null);
}

function renderSpeakerList(content, sectionID) {
    var speakerHTML = '<div id="'+sectionID+'"><table>';
    try {
        $.each(content.users, function(i, user) {
            speakerHTML += '<tr>';
            speakerHTML += '<td>';
            if (user.hasbio=="true") speakerHTML += '<a border="0" href="'+SPEAKER_LINK_BASE+user.guid+'">';
            if (user.hasphoto=="true") speakerHTML += '<img width="70" height="70" alt="'+user.first+' '+user.last+' photo" src="'+PHOTO_BASE_PATH2+user.photo+'">';
            else speakerHTML += '&nbsp;';
            if (user.hasbio=="true") speakerHTML += '</a>';
            speakerHTML += '</td>';
            speakerHTML += '<td>';
            if (user.hasbio=="true") speakerHTML += '<a border="0" href="'+SPEAKER_LINK_BASE+user.guid+'">';
            speakerHTML += '<p class="speaker">'+user.first+' '+user.last;
            if (user.hasbio=="true") speakerHTML += '</a>';
            speakerHTML += '<br/>';
            if (user.title!=null && user.title.length>1) speakerHTML += getSpeakerTitleHtml(user)+'<br/>';
            speakerHTML += getSpeakerCompanyHtml(user)+'</p></td>';
            speakerHTML += '</tr>';
        });
    } catch(e) {
    }
    speakerHTML += '</table></div>';
	$("#"+sectionID).replaceWith(speakerHTML);
}

function renderSpeakerDetail(content, sectionID, guid) {
    var speakerHTML = '<div id="'+sectionID+'"><table margin="0" padding="0">';
    try {
	    $.each(content.users, function(i, user) {
	        if (user.guid==guid) {
                speakerHTML += '<tr>';
                speakerHTML += '<td class="tdspeakerphoto">';
                if (user.hasphoto=="true") speakerHTML += '<img width="70" height="70" alt="'+user.first+' '+user.last+' photo" src="'+PHOTO_BASE_PATH3+user.photo+'">';
                else speakerHTML += '&nbsp;';
                speakerHTML += '</td>';
                speakerHTML += '<td style="vertical-align:top">';
                speakerHTML += '<p class="speaker">'+user.first+' '+user.last+'<br/>';
                if (user.title!=null && user.title.length>1) speakerHTML += getSpeakerTitleHtml(user)+'<br/>';
                speakerHTML += getSpeakerCompanyHtml(user)+'</p></td>';
                speakerHTML += '</tr>';
                speakerHTML += '<tr><td colspan="2" class="speakerbio">'+user.bio+'</td></tr>';
	            speakerHTML += '<p style="margin-top: 20px;"><a href="'+SPEAKER_ALL_LINK+'">View a list of all speakers at the Forum</a></p>';
		    }
	    });
    } catch(e) {
    }
    speakerHTML += '</table></div>';
	$("#"+sectionID).replaceWith(speakerHTML);
}

function renderSessionList(content, sectionID) {
    renderSessionByDay(content, 'agenda_table_day0', 'Tuesday, November 09, 2010');
    renderSessionByDay(content, 'agenda_table_day1', 'Wednesday, November 10, 2010');
    renderSessionByDay(content, 'agenda_table_day2', 'Thursday, November 11, 2010');
    renderSessionByDay(content, 'agenda_table_day3', 'Friday, November 12, 2010');
    renderSessionByDay(content, 'agenda_table_day4', 'Saturday, November 13, 2010');
    renderSessionByDay(content, 'agenda_table_day5', 'Sunday, November 14, 2010');
    renderSessionByTrack(content, 'agenda_table_track_generalsessions', TRACK_GENERAL);
    renderSessionByTrack(content, 'agenda_table_track_track1', TRACK1);
    renderSessionByTrack(content, 'agenda_table_track_track2', TRACK2);
}
function renderSessionDetail(content, sectionID, guid) {
    var sessionHTML = '<div id="'+sectionID+'">';
    try {
	    $.each(content.items, function(i, session) {
	        if (session.guid==guid) {
	            sessionHTML += '<h3>'+session.title+'</h3>';
	            sessionHTML += '<h4>'+session.subtitle+'</h4>';
	            if (session.moderator!=null) {
	                sessionHTML += '<h4>Moderator:</h4>';
	                sessionHTML += '<p class="moderator">';
	                if (session.moderator.hasbio=="true") sessionHTML += '<a href="'+SPEAKER_LINK_BASE+session.moderator.guid+'">';
	                sessionHTML += '<b>'+session.moderator.first+' '+session.moderator.last+'</b>';
	                if (session.moderator.hasbio=="true") sessionHTML += '</a>';
	                sessionHTML += '<br/>'+getSpeakerTitleHtml(session.moderator);
	                if (session.moderator.title.length>1 && session.moderator.company.length>1) sessionHTML += ', ';
	                sessionHTML += getSpeakerCompanyHtml(session.moderator);
	                sessionHTML += '</p>';
	            }
	            if (session.panelists && session.panelists.length>0) {
	                sessionHTML += '<h4>Panelists:</h4>';
	                $.each(session.panelists, function(p, panelist) {
	                    sessionHTML += '<p class="panelist">';
    	                if (panelist.hasbio=="true") sessionHTML += '<a href="'+SPEAKER_LINK_BASE+panelist.guid+'">';
	                    sessionHTML += '<b>'+panelist.first+' '+panelist.last+'</b>';
	                    if (panelist.hasbio=="true") sessionHTML += '</a>';
	                    sessionHTML += '<br/>'+getSpeakerTitleHtml(panelist);
	                    if (panelist.title.length>1 && panelist.company.length>1) sessionHTML += ', ';
	                    sessionHTML += getSpeakerCompanyHtml(panelist);
	                    sessionHTML += '</p>';
    	            });
	            }
	            if (session.description) sessionHTML += '<div class="description">'+session.description+'</div>';
	            if (session.sponsor) sessionHTML += '<div class="sponsorlogo" style="margin-top: 20px;"><p>This session is sponsored by:</p><p><img src="'+SPONSOR_BASE+session.sponsor+'"></p></div>';
	            var link = (location.search!=null && location.search.indexOf(SESSION_REFERRER_KEY+"=home")>=0 ? HOME_LINK : SESSION_ALL_LINK);
	            sessionHTML += '<p style="margin-top: 20px;"><a href="'+link+'">Return to all sessions and tracks</a></p>';
		    }
	    });
    } catch(e) {
    }
    sessionHTML += '</div>';
	$("#"+sectionID).replaceWith(sessionHTML);
}

function renderSessionByDay(content, tableID, dayString) {
	var agendaHtml = '<table cellspacing="0" cellpadding="0" class="featuretable" id="'+tableID+'"><tbody>';
	try {
	    // if instances of both track 1 and 2 appear on this day, use side-by-side method
	    var useSideBySideRendering = false, hasShownTracks = false;
	    $.each(content.items, function(i, session) {
	        if (session.date==dayString && session.series.indexOf(TRACK2)==0) {
	            useSideBySideRendering = true;
	        }
	    });
	    $.each(content.items, function(i, session) {
	        if (session.date==dayString) {
	            var shortTime = session.time.substring(0, session.time.indexOf(" Eastern time"));
		        if (session.series.indexOf(TRACK1)==0 && useSideBySideRendering && session.title.indexOf(SESSION_EXCLUDE)==-1) {
		            if (hasShownTracks==false) {
		                agendaHtml += '<tr><td>&nbsp;</td><td><p style="font-weight: bold">'+TRACK1+'</p></td><td><p style="font-weight: bold">'+TRACK2+'</p></td></tr>';
		                hasShownTracks = true;
		            }
		            agendaHtml += '<tr>';
		            agendaHtml += '<td class="time"><p class="emphasize">'
		                +(session.suppresstime=="true" ? "" : shortTime)+'</p></td>';
		            //agendaHtml += '<td class="thumbnail">'+(session.icon && session.icon.length>0 ? '<img src="'+ICON_BASE+session.icon+'">' : '&nbsp;')+'</td>';
		            agendaHtml += '<td class="session"><p class="concurrentsession-a">';
		            if (session.hasdescription=="true")
		                agendaHtml += '<a href="'+SESSION_LINK_BASE+session.guid+'">'+session.title+'</a>';
		            else agendaHtml += session.title;
		            agendaHtml += '<BR />'+session.subtitle+'</p></td>';
		        } else if (session.series.indexOf(TRACK2)==0 && useSideBySideRendering && session.title.indexOf(SESSION_EXCLUDE)==-1) {
		            //agendaHtml += '<td class="thumbnail">'+(session.icon && session.icon.length>0 ? '<img src="'+ICON_BASE+session.icon+'">' : '&nbsp;')+'</td>';
		            agendaHtml += '<td class="session"><p class="concurrentsession-b">';
		            if (session.hasdescription=="true")
		                agendaHtml += '<a href="'+SESSION_LINK_BASE+session.guid+'">'+session.title+'</a>';
		            else agendaHtml += session.title;
		            agendaHtml += '<BR />'+session.subtitle+'</p>';
		            agendaHtml += '</td></tr>';
		        } else if (session.title.indexOf(SESSION_EXCLUDE)==-1) {
		            agendaHtml += '<tr>';
		            agendaHtml += '<td class="time"><p class="emphasize">'
		                +(session.suppresstime=="true" ? "" : shortTime)+'</p></td>';
		            //agendaHtml += '<td class="thumbnail">'+(session.icon && session.icon.length>0 ? '<img src="'+ICON_BASE+session.icon+'">' : '&nbsp;')+'</td>';
		            agendaHtml += '<td class="session" colspan="2"><p>'+getSessionTitleHtml(session)+'</p></td></tr>';
		        }
		    }
	    });
	} catch(e) {
	    //alert("Error "+e+" for "+item.title+", "+item.subtitle);
	}
	agendaHtml += '</tbody></table>';
	
	$("#"+tableID).replaceWith(agendaHtml);
}

function renderSessionByTrack(content, tableID, seriesString) {
	var agendaHtml = '<table cellspacing="0" cellpadding="0" class="featuretable" id="'+tableID+'"><tbody>';
	try {
	    var currentDay = null;
	    $.each(content.items, function(i, session) {
	        if (session.series.indexOf(seriesString)==0 && session.title.indexOf(SESSION_EXCLUDE)==-1) {
	            var shortTime = session.time.substring(0, session.time.indexOf(" Eastern time"));
	            var day = session.date.substring(0, session.date.indexOf(", 2010"));
	            if (day!=currentDay) {
	                agendaHtml += '<tr>';
	                agendaHtml += '<td colspan="3"><h3>'+day.replace(' 09', ' 9')+'</h3></td>';
	                agendaHtml += '</tr>';
	                currentDay = day;
	            }
	            agendaHtml += '<tr>';
	            agendaHtml += '<td class="time"><p class="emphasize">'
	                +(session.suppresstime=="true" ? "" : shortTime)+'</p></td>';
	            //agendaHtml += '<td class="thumbnail">'+(session.icon && session.icon.length>0 ? '<img src="'+ICON_BASE+session.icon+'">' : '&nbsp;')+'</td>';
	            agendaHtml += '<td class="session"><p>'+getSessionTitleHtml(session)+'</p></td></tr>';
		    }
	    });
	} catch(e) {
	    //alert("Error "+e+" for "+item.title+", "+item.subtitle);
	}
	agendaHtml += '</tbody></table>';
	
	$("#"+tableID).replaceWith(agendaHtml);
}

function getSessionTitleHtml(session) {
    var html = "";
    
    if (session.hasdescription=="true") {
        var link = SESSION_LINK_BASE+session.guid;
        if (location.href.indexOf(SESSION_ALL_LINK)>=0) link += '&'+SESSION_REFERRER_KEY+'=sessions';
        else link += '&'+SESSION_REFERRER_KEY+'=home';
        html += '<a href="'+link+'">'+session.title+'</a>';
    } else html += session.title;
    
    html += '<BR />'+session.subtitle;
    
    return(html);
}

function getSpeakerTitleHtml(speaker) {
    return(speaker.title);
}

function getSpeakerCompanyHtml(speaker) {
    if (speaker.company.indexOf(SPEAKER_COMPANY_HACK)>=0) {
        return(SPEAKER_COMPANY_HACK_REPLACE);
    } else return(speaker.company);
}

function renderNoContentWarning(sectionID, message) {
    $("#"+sectionID).replaceWith('<div id="'+sectionID+'"><p>'+message+'</p></div>');
}

