/**
 * Enriching Communications JavaScript library
 */

// Global Document Setup

	$(document).ready(function() {
		if ($.browser.msie || $.browser.safari || $.browser.mozilla || $.browser.opera) {
			$("#dropArchives a").attr('href', '#');
			$("#dropPodcasts a").attr('href', '#');
			$("#menu_dropArchives").html('<table cellpadding="0" cellspacing="0" border="0"><tr><td class="dropMenu_chooseText" valign="top">Choose An Edition >></td><td class="dropMenu_volumeText" valign="top"><div id="dropArchives_v3">Volume 3</div><div id="dropArchives_v2">Volume 2</div><div id="dropArchives_v1">Volume 1</div></td><td class="dropMenu_issueText" valign="top"><div id="dropArchives_v3_issues"><div><a href="../v3i12009/home.html">Issue 1: The Dynamic Enteprise</a></div></div><div id="dropArchives_v2_issues"><div><a href="../v2i32008/home.html">Issue 3: Navigating the Transformation</a></div><div><a href="../v2i22008/home.html">Issue 2: New Rules in the Communications Economy</a></div><div><a href="../v2i12008/home.html">Issue 1: Anywhere, Anytime Communications</a></div></div><div id="dropArchives_v1_issues"><div><a href="../v1i22007/home.html">Issue 2: Business Critical Communication</a></div><div><a href="../v1i12007/home.html">Issue 1: Competitive Transformation</a></div></div></td></tr></table>');
			$('#menu_dropPodcasts').html('<table cellpadding="0" cellspacing="0" border="0"><tr><td class="dropMenu_chooseText" valign="top">Choose An Edition >></td><td class="dropMenu_volumeText" valign="top"><div id="dropPodcasts_v3">Volume 3</div><div id="dropPodcasts_v2">Volume 2</div><div id="dropPodcasts_v1">Volume 1</div></td><td class="dropMenu_issueText" valign="top"><div id="dropPodcasts_v3_issues"><div><a href="podcast.html">Issue 1 - The Dynamic Enterprise</a></div></div><div id="dropPodcasts_v2_issues"><div><a href="podcast.html#v2i3">Issue 3 - Navigating the Transformation</a></div><div><a href="podcast.html#v2i2">Issue 2 - New Rules in the Communications Economy</a></div><div><a href="podcast.html#v2i1">Issue 1 - Anywhere, Anytime Communications</a></div></div><div id="dropPodcasts_v1_issues"><div><a href="podcast.html#v1i2">Issue 2 - Business Critical Communication</a></div><div><a href="podcast.html#v1i1">Issue 1 - Competitive Transformation</a></div></div></td></tr></table>');
			$("#dropArchives").bind("click", function() { dropGlobalMenu($(this).attr("id")); });
			$("#dropPodcasts").bind("click", function() { dropGlobalMenu($(this).attr("id")); });
			$(".dropMenu_volumeText div").bind("click", function() { showRelatedIssues($(this).attr("id")); });
		}
	});

	/**
	 * APPEND A STRING TO ALL A TAGS FOR CACHING PURPOSES
	 * This is to stop browser from caching a page in the wrong language
	 *
	 * @param {String} value to append
	 */	
	function cacheAppend(langAppend){		
		$('a').each(function(){
			var currentHREF = $(this).attr("href");							
			if (currentHREF) {			
				var regexHTML = '.html';
				var regexHTTP = 'http:';
				var regexPOUND = '#';
				if 	((currentHREF.search(regexHTML) != '-1') &&
				 	(currentHREF.search(regexHTTP) == '-1')  &&
					(currentHREF.search(regexPOUND) == '-1')) {
					$(this).attr("href", currentHREF + "?l=" + langAppend);
				}
			}
		});
	}

// Global Navigation Functions

	/**
	 * Drop down the contextual menu
	 *
	 * @param {String} id is the ID of the triggering DIV
	 * NOTE: variable 'oldMenu' stores state for switching
	 */
	var oldMenu = 'NULL';
	function dropGlobalMenu(id) {
		if (oldMenu != id && oldMenu != 'NULL') {
			$('#' + oldMenu).removeClass('dropSelectTitle_' + oldMenu + '_selected').addClass('dropSelectTitle_' + oldMenu);
			$('#selectNeck_' + oldMenu).hide();
			$('#menu_' + oldMenu).hide();

			$('#' + id).addClass('dropSelectTitle_' + id + '_selected').removeClass('dropSelectTitle_' + id);
			$('#selectNeck_' + id).show();
			$('#menu_' + id).show();
			oldMenu = id;
		}
		else if (oldMenu == id) {
			$('#' + id).removeClass('dropSelectTitle_' + id + '_selected').addClass('dropSelectTitle_' + id);
			$('#selectNeck_' + id).fadeOut("slow");
			$('#menu_' + id).slideUp("slow");
			oldMenu = 'NULL';
		}
		else if (oldMenu == 'NULL') {
			$('#' + id).addClass('dropSelectTitle_' + id + '_selected').removeClass('dropSelectTitle_' + id);
			$('#selectNeck_' + id).fadeIn("fast");
			$('#menu_' + id).slideDown("slow");
			oldMenu = id;
		}
	}

	/**
	 * Reveal the related Issues options
	 *
	 * @param {String} id is the ID of the triggering DIV
	 * NOTE: variable 'oldIssue' stores state for switching
	 */
	var oldIssue = 'NULL';
	function showRelatedIssues(id) {
		if (oldIssue != id && oldIssue != 'NULL') {
			$('#' + oldIssue).removeClass('selected');
			$('#' + oldIssue + "_issues").hide();
			$('#' + oldIssue + "_issues div").hide();
			$('#' + id).addClass('selected');
			$('#' + id + "_issues").show();
			$('#' + id + "_issues div").show();
			oldIssue = id;
		}
		else if (oldIssue == 'NULL') {
			$('#' + id).addClass('selected');
			$('#' + id + "_issues").show();
			$('#' + id + "_issues div").show();
			oldIssue = id;
		}
	}

// Windows & Page Manipulation Functions

	/**
	 * Change the current language.
	 *
	 * @param {Number} param_lang_id
	 */
	function changeLanguage(param_lang_id) {
	    if (param_lang_id == 1 ) {
			setCookie("lang_code", "en", 365, getContextPath());
			setCookie("lang_code_upper", "EN", 365, getContextPath());
		}
		if (param_lang_id == 2) {
			setCookie("lang_code", "fr", 365, getContextPath());
			setCookie("lang_code_upper", "FR", 365, getContextPath());
		}
		location.reload(true);
	    return true;
	}

	/**
	 * Change the window location.
	 *
	 * @param {String} newLocation
	 */
	function changeLocation(newLocation) {
	    window.location = newLocation;
	}

	/**
	 * Open a window for downloading PDFs.
	 *
	 * @param {Object} mypage
	 * @param {Object} myname
	 * @param {Number} w the width
	 * @param {Number} h the height
	 * @param {Boolean} scroll
	 */
	function DLWindow(mypage, myname, w, h, scroll) {
	    var winl = (screen.width - w) / 2;
	    var wint = ((screen.height - h) / 2) - 50;
	    winprops = 'toolbar=0,location=1,directories=0,status=0,menubar=0,scrollbars='+scroll+',resizable=1,width='+w+',height='+h+',top='+wint+',left='+winl+''
	    win = window.open(mypage, myname, winprops)
	    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	}

	/**
	 * Open a Window for the e-mail form.
	 */
	function emailPage() {
		emailWindow=window.open('http://www.alcatel-lucent.com/wps/portal/emailPage?LMSGreset=start','emailWindow','width=575,height=580,left=100,top=100,resizable,scrollbars');
	}

	/**
	 * Open a Window for the podcast player.
	 *
	 * @param {Object} mypage
	 * @param {Object} myname
	 * @param {Number} w the width
	 * @param {Number} h the height
	 * @param {Object} trackingId
	 */
	function PODWindow(mypage, myname, w, h, trackingId) {
	    setPageTracking(trackingId, myname);
	    var winl = (screen.width - w) / 2;
	    var wint = ((screen.height - h) / 2) - 50;
	    winprops = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width='+w+',height='+h+',top='+wint+',left='+winl+''
	    win = window.open(mypage, "podcast", winprops);
	    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	}

	/**
	 * Present the survey offer on the page.
	 */
	function presentSurveyOffer() {
	    var visits = getCookie("visits");
	    if ( visits == null ) {
	        visits = '0';
	    }
	    if ( visits.length < 4 ) {
	        setCookie("visits", visits + 1, 365, getContextPath());
			$("#survey").fadeIn("slow");
	    }
	}

	/**
	 * Open the link to the full survey forms in a new Window.
	 */
	function doSurvey() {
	    setCookie("visits", '1111', 365, getContextPath());
		$("#survey").fadeOut("slow");
	    window.open('http://vovici.com/wsb.dll/s/7136g321a9', 'survey');
	}

	/**
	 *  Get current URL in plain ASCII
	 *
	 *  @returns the URL
	 */
	function getCurrentFilename() {
		var url = unescape(location.href);
		var xstart = url.lastIndexOf("/") + 1;
		var xend = url.length;
		var hereName = url.substring(xstart, xend);
		var herePath = url.substring(0, xstart);
		var curPath = herePath;
		return hereName;
	}

// State Mgmt Functions

 	/**
	 * Get the website context path.
	 *
	 * @return {String} the context path, e.g. "/enrich"
	 */
	function getContextPath() {
	    var path = unescape(location.pathname).substring(1, location.pathname.length);
	    return path.substring(0, path.indexOf('/'));
	}

	/**
	 * Get the value of a cookie.
	 *
	 * @param {String} name the name of the cookie
	 * @return {Object} the value of the cookie
	 */
	function getCookie(name) {
	  var dc = document.cookie;
	  var prefix = name + "=";
	  var begin = dc.indexOf("; " + prefix);
	  if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	  }
	  else {
	    begin += 2;
	  }
	  var end = document.cookie.indexOf(";", begin);
	  if (end == -1)
	    end = dc.length;
	  return unescape(dc.substring(begin + prefix.length, end));
	}

	/**
	 * Get a query parameter from the current URL.
	 *
	 * @param {String} name the name of the query string parameter
	 * @return {String} the value of the parameter
	 */
	function getParam(name) {
	    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	    var regexS = "[\\?&]" + name + "=([^&#]*)";
	    var regex = new RegExp(regexS);
	    var results = regex.exec(window.location.href);
	    if (results == null)
	        return "";
	    else
	        return results[1];
	}

	/**
	 * Set a cookie.
	 *
	 * @param {String} c_name the cookie name
	 * @param {Object} value the cookie value
	 * @param {Number} expiredays
	 * @param {String} path the path or scope of the cookie, e.g. / or /some_context
	 */
	function setCookie(c_name,value,expiredays, path) {
	    var exdate=new Date();
	    exdate.setDate(exdate.getDate() + expiredays);
	    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires=" + exdate.toGMTString() + ";path=/" + path + '/');
	}

// Site Catalyst Integration Functions

	/**
	 * Send a tracking event message to Site Catalyst.
	 *
	 * @param {String} trackingKey the SiteCatalyst key
	 * @param {String} pageTitle the title of the page
	 * @deprecated
	 */
	function setFlashPlayerTracking(trackingKey, pageTitle){
	    s.pageName = trackingKey;
		s.prop31 = s.pageName;
	    if ( pageTitle.length > 90 ) {
	        pageTitle = pageTitle.substr(0, 89);
	    }
	    s.prop30 = pageTitle;
		s.prop2 = "Static Magazine Page";
		s.prop10 = "Anonymous";
		var s_code=s.t();
	    if(s_code)document.write(s_code);
	}

	/**
	 * Send a tracking event message to Site Catalyst.
	 *
	 * @param {String} trackingKey the SiteCatalyst key
	 * @param {String} pageTitle the title of the page
	 */
	function setPageTracking(trackingKey, pageTitle){
		s.pageName = trackingKey;
	    s.prop31 = s.pageName;
	    if ( pageTitle.length > 90 ) {
	        pageTitle = pageTitle.substr(0, 89);
	    }
	    s.prop30 = pageTitle;
	    s.prop2 = "Static Magazine Page";
	    s.prop10 = "Anonymous";
	    var s_code=s.t();
	    if (s_code) {
	        document.write(s_code);
	    }
	}

