// Social Tagging Javascript version 1.1
// May 3, 2007 - Hide link from PV

if (document.location.protocol == "https:") {
	var social_protocolDir = "https://secure.hp-ww.com/";
} else {
	var social_protocolDir = "http://welcome.hp-ww.com/";
}

// initial variables

if(!window.encodeURIComponent) {
	var social_page_url = location.href;
	var social_page_title = document.title;
	var social_page_description = social_getDescription();
} else {
	var social_page_url = encodeURIComponent(location.href);
	var social_page_title = encodeURIComponent(document.title);
	var social_page_description = encodeURIComponent(social_getDescription());
}

// JSON variables
var social_jsonLocal = 0;
var social_jsonList = 0;

var social_permalink = location.href;
var social_permalink_assigned = false;
var social_numMenus = 1;
var social_filled = false;
var social_linkID = 0;
var social_listLoaded = false;
var social_sitesEnd = true;
// array to store flags if layers are already filled
var social_filledFlags = new Array();
var social_titles = new Array();
var social_descriptions = new Array();
var social_permalinks = new Array();

// defaults
// text
var social_layerTitle = "Share or Tag with:";
var social_linkTitle = "View tagging links – opens a pop-up layer on this page";
var social_layerStart = "Start of Social Tagging links popup layer ";
var social_closeAction = "Close";
var social_closeActionScreen = " pop-up layer";
var social_openInNewWindow = "These links open in new window";
var social_layerEnd = "End of Social Tagging links popup layer";

// default URLs
var social_permalinkpopup = social_protocolDir + "country/us/en/soctag/hpweb_permalink.html";
var social_listURLdefault = social_protocolDir + "country/us/en/js/hpweb_soctag_list.js";
var social_listURL = "";


// Begin styles definitions	

	var social_css_file = social_protocolDir + "styles/hpweb_soctag.css";
	document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + social_css_file + "\">");
	var iecss_styles = '';
	iecss_styles += '<!--[if IE 5]>\n';
	iecss_styles += '<style>\n';
	iecss_styles += '.social_contentBlock { margin-top: 15px; padding-left: 10px; width: 100%; overflow: auto; padding-bottom:5px; }\n';
	iecss_styles += '.social_column { float:left; margin: 0px; padding-right:10px; width:120px; }\n';
	iecss_styles += '</style>\n';
	iecss_styles += '<![endif]-->\n';
	document.write(iecss_styles);

	// override rules
	var agt=navigator.userAgent.toLowerCase();
	var is_aol7  = (agt.indexOf('aol/7')!=-1);
	var is_moz = (agt.indexOf('mozilla/5')!=-1);
	var is_opera = (agt.indexOf('opera')!=-1);
	var is_ie5 = (agt.indexOf('msie 5')!=-1);
	//var is_ie = (agt.indexOf('msie')!=-1);
	//var is_ff = (agt.indexOf('firefox')!=-1);
	var is_safari = (agt.indexOf('safari')!=-1);
	var is_moz17 = (agt.indexOf('mozilla/5')!=-1) && (agt.indexOf('1.7')!=-1);
	
	var is_netscape71 = (agt.indexOf('netscape/7.1')!=-1);
	
	var safari_css_styles = '';
	
	if (is_safari) {
		safari_css_styles += '<style type="text/css">\n';
		safari_css_styles += '.social_screenReading { FONT-SIZE: 0px; OVERFLOW: hidden; WIDTH: 0px; POSITION: absolute; HEIGHT: 0px; display:none; }\n';
		safari_css_styles += '</style>\n';
		document.write(safari_css_styles);
	}
	
	var moz17_css_styles = '';
	if (is_moz17 || is_netscape71) {
		moz17_css_styles += '<style type="text/css">\n';
		moz17_css_styles += '.social_menu_style { width:404px; }\n';
		moz17_css_styles += '</style>\n';
		document.write(moz17_css_styles);
	}
	var aol_css_styles = '';
	if (is_aol7) {		
		aol_css_styles += '<style type="text/css">\n';
		aol_css_styles += '.social_menu_style { width:404px; }\n';
		aol_css_styles += '.social_contentBlock { margin-top: 15px; padding: 0; width: 100%; overflow: visible; padding-bottom:10px;\n';
		aol_css_styles += '</style>\n';
		document.write(aol_css_styles);
	}
	

// End styles definitions


// BEGIN social tagging popup code
function social_getObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId);
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId);
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
}

function social_changeObjectVisibility(objectId, newVisibility, social_linkID) {
    var styleObject = social_getObject(objectId).style;
    if(styleObject) {
		styleObject.visibility = newVisibility;
		if(newVisibility == 'visible') {
			// This code hiddes any other social tagging links when the layer is open / fixes a IE overlapping bug (1 of 2)
			/*if(is_ie){
    			for(j = 1; j <= social_numMenus; j++) {
					if(j != social_linkID) document.getElementById('soc_layerTriggerLink'+j).style.visibility = 'hidden';
    			}
			}*/
			document.getElementById("social_closeButton"+social_linkID).focus();
		}
		return true;
    } else {
		return false;
    }
}

function social_reposition(social_linkID) {
	var menuObject = social_getObject('social_menu'+social_linkID);
	var columnObject = social_getObject('social_column'+social_linkID); // IE5 needs to see dynamic internal content to calculate height
	var share = social_getObject('social_img_block'+social_linkID);
	var socialContent = social_getObject('social_contentAll'+social_linkID);
	
	menuObject.style.left = '0px';
	
	var menu_height = menuObject.offsetHeight;
	var column_height = columnObject.offsetHeight;
	var link_height = share.offsetHeight;
	var social_height = socialContent.offsetHeight;
	//menuObject.style.top = share.offsetHeight-menuObject.offsetHeight+'px';
	//if(is_ie5) { menuObject.style.top = -1*social_height+10+'px'; }
	
	social_changeObjectVisibility('social_menu'+social_linkID, 'visible', social_linkID);
	if(is_safari || is_opera) { window.resizeBy(-1,0); window.resizeBy(1,0); social_refreshDelay(); }
	
}

function social_reposition_resize(social_linkID) {
	var menuObject = social_getObject('social_menu'+social_linkID);
	var columnObject = social_getObject('social_column'+social_linkID); // IE5 needs to see dynamic internal content to calculate height
	var share = social_getObject('social_img_block'+social_linkID);
	var socialContent = social_getObject('social_contentAll'+social_linkID);
	
	menuObject.style.left = '0px';
	
	var menu_height = menuObject.offsetHeight;
	var column_height = columnObject.offsetHeight;
	var link_height = share.offsetHeight;
	var social_height = socialContent.offsetHeight;
	//menuObject.style.top = share.offsetHeight-menuObject.offsetHeight+'px';
	//if(is_ie5) { menuObject.style.top = -1*social_height+10+'px'; }	
}

function social_blurCloseLight(social_linkID) {
	social_hideMenus();
	var sclose = document.getElementById("social_returnIndex"+social_linkID);
	sclose.focus();	
	if(is_safari || is_opera) { window.resizeBy(-1,0); window.resizeBy(1,0); }
	return false;
}

function social_blurClose(opt_pars) {
	flagPopup = false;
	
	if(opt_pars) {
		var opt_parsArray = opt_pars.split(/,\s*/);
		for(i = 0; i<opt_parsArray.length; i++){
			var params = opt_parsArray[i].split("=");
			if(params[0] == 'flagPopup'){
				if(params[1] == 'true') flagPopup = true;
			}
			if(params[0] == 'social_linkID') social_linkID = params[1];
		}
	}
	
	social_hideMenus();
	var sclose = document.getElementById("social_returnIndex"+social_linkID);
	sclose.focus();	
	if(is_safari || is_opera) { window.resizeBy(-1,0); window.resizeBy(1,0); }
	if(flagPopup) {
		social_popUp(social_linkID);
		return false;
	}
}

function social_refresh() {
	window.resizeBy(-1,0); window.resizeBy(1,0);
}

var social_refreshtimerID=null;
function social_refreshDelay() {
	social_refreshtimerID = setTimeout("social_refresh()",2000);
}

function social_hideDelay() {
	social_blurClose();
}

function social_loadContent(file,opt_id){
	var script_id = 'loadScript';
	if(opt_id){
		script_id = opt_id;
	}
	var head = document.getElementsByTagName('head').item(0);
	var scriptTag = document.getElementById(script_id);
	if(scriptTag) head.removeChild(scriptTag);
	script = document.createElement('script');
	script.src = file;
	script.type = 'text/javascript';
	script.id = script_id;
	head.appendChild(script);
}

// Load sites from default location / if a localized file has its own site list the object will be overwritten
// social_loadContent(social_listURLdefault,'loadList');

function createSocialTagging(social_title, social_style, social_permalink_par, opt_pars){
	
	if (typeof (is_printable) == 'undefined') {		// hide from printable version
	
	social_linkID++;
	social_numMenus = social_linkID;
	// Create new flag and set to false, this layer is empty
	social_filledFlags.push(false);
		
	social_permalink = social_permalink_par;
	social_permalink_assigned=true;
	
	// Store desired page title, description and permalink for multiple links
	social_titles.push(social_page_title);
	social_descriptions.push(social_page_description);
	social_permalinks.push(social_permalink);
	
	if(opt_pars) {
		//var opt_parsArray = opt_pars.split(/,\s*/);
		var opt_parsArray = opt_pars.split("|");
		for(i = 0; i<opt_parsArray.length; i++){
			var params = opt_parsArray[i].split("=");
			if(params[0].toLowerCase() == 'list') social_listURL = params[1];
			if(params[0].toLowerCase() == 'title'){
				window.encodeURIComponent ? social_titles[social_linkID-1] = encodeURIComponent(params[1]) : social_titles[social_linkID-1] = params[1];
			}
			if(params[0].toLowerCase() == 'desc'){
				window.encodeURIComponent ? social_descriptions[social_linkID-1] = encodeURIComponent(params[1]) : social_descriptions[social_linkID-1] = params[1];
			}
		}
	}
	// list is stored as global var, only one list is allowed (for multiple service links)
	if(!social_listLoaded) {
		social_listLoaded = true;
		if(social_listURL=="") social_listURL = social_listURLdefault;
		social_loadContent(social_listURL);
	}
	
	var social_closeLinkChar = social_closeAction.substring(0,1);
	var social_closeLinkKey = social_closeLinkChar.toLowerCase();
	var social_closeLinkRest = social_closeAction.substring(1,social_closeAction.length);
	
	
	var social_placeholder = '<div id="social_menuBlock'+social_linkID+'" class="social_menuBlock"><div id="social_menuShare'+social_linkID+'" class="social_menuShare_style"><span id="soc_layerTriggerLink'+social_linkID+'"><a href="javascript: social_showMenu(\''+social_linkID+'\');" title="'+social_linkTitle+'" class="social_noline '+social_style+'" id="social_returnIndex'+social_linkID+'"><img src="'+social_protocolDir+'img/soctag/social.gif" alt="" width="16" height="16" border="0" class="social_img" id="social_img_block'+social_linkID+'"><span>'+social_title+'</span></a></span></div>';
	social_placeholder += '         <div id="social_menu'+social_linkID+'" class="social_menu_style" onmouseup="event.cancelBubble = true;">';
	social_placeholder += '			<div class="social_content" id="social_contentAll'+social_linkID+'" style="float:left">';
	social_placeholder += '				<div class="theme" style="padding:5px 0 5px 0; height:1.45em;"><div class="social_bandedHeader">';
	social_placeholder += '					<span id="social_layerStart'+social_linkID+'" class="social_screenReading">'+social_layerStart+'</span><span id="soc_layerCloseHeader'+social_linkID+'"><div class="themeheader" style="display:inline; float:right">&raquo;&nbsp;<a href="javascript: social_blurClose(\'social_linkID='+social_linkID+'\');" class="themelink" id="social_closeButton'+social_linkID+'" title="'+social_closeAction+social_closeActionScreen+'" accesskey="'+social_closeLinkKey+'"><span class="social_underline">'+social_closeLinkChar+'</span>'+social_closeLinkRest+'<span class="social_screenReading" id="social_closeActionScreen'+social_linkID+'">'+social_closeActionScreen+'</span></a></div></span>';
	social_placeholder += '					<div style="display:inline; float:left"><h2 class="themeheader" style="display:inline;" id="soc_layerTitle'+social_linkID+'">'+social_layerTitle+'</h2><div class="social_screenReading" id="soc_openNewWindow'+social_linkID+'">'+social_openInNewWindow+'</div></div>';

	social_placeholder += '				</div></div>';
	social_placeholder += '				<div class="social_contentBlock">';
	social_placeholder += '				<div class="social_column">';
	social_placeholder += '					<div id="social_column1_'+social_linkID+'" class="social_linksUnit">';
	social_placeholder += '					</div>';
	social_placeholder += '				</div>';
	social_placeholder += '				<div class="social_column">';
	social_placeholder += '					<div id="social_column2_'+social_linkID+'" class="social_linksUnit">';
	social_placeholder += '					</div>';
	social_placeholder += '				</div>';
	social_placeholder += '				<div class="social_column" style="padding-right:0px">';
	social_placeholder += '					<div id="social_column3_'+social_linkID+'" class="social_linksUnit">';
	social_placeholder += '					</div>';
	social_placeholder += '				</div>';
	social_placeholder += '				</div>';
	social_placeholder += '				<div class="theme social_bottomColor" style="float:left"></div>';
	social_placeholder += '			</div>';
	social_placeholder += '		</div>';
	social_placeholder += '	</div>';
	
	document.write(social_placeholder);
	
	} // end if hide from printable version
	
}

function social_popUp(social_linkID){
	var popw = 400;
	var poph = 250;
	var winl = (screen.width-popw)/2;
	var wint = (screen.height-poph)/2;
	var params = 'width='+popw+',height='+poph+',scrollbars,status,top='+wint+',left='+winl;
	var NewWindow = window.open(social_permalinkpopup+"?pl="+social_permalinks[social_linkID-1], 'HP_permalink', params);
}

function social_fillLinksJSON(social_linkID){
	//social_filled = true;

	// If localized texts are present replace original texts
	// These only are texts inside tags - accessible with innerHTML
	if(social_jsonLocal.text) {
		if(social_jsonLocal.text.text_layerTitle) document.getElementById('soc_layerTitle'+social_linkID).innerHTML = social_jsonLocal.text.text_layerTitle;
		if(social_jsonLocal.text.text_layerStart) document.getElementById('social_layerStart'+social_linkID).innerHTML = social_jsonLocal.text.text_layerStart;
		if(social_jsonLocal.text.text_openInNewWindow) document.getElementById('soc_openNewWindow'+social_linkID).innerHTML = social_jsonLocal.text.text_openInNewWindow;
		if(social_jsonLocal.text.text_layerEnd) social_layerEnd = social_jsonLocal.text.text_layerEnd;
		
		// These are texts in attributes, rewritting the whole span content
		if(social_jsonLocal.text.text_closeAction && social_jsonLocal.text.text_closeActionScreen) {
			var social_closeLinkChar = social_jsonLocal.text.text_closeAction.substring(0,1);
			var social_closeLinkKey = social_closeLinkChar.toLowerCase();
			var social_closeLinkRest = social_jsonLocal.text.text_closeAction.substring(1,social_jsonLocal.text.text_closeAction.length);
			var newHtml = '<div class="themeheader" style="display:inline; float:right">&raquo;&nbsp;<a href="javascript: social_blurClose(\'social_linkID='+social_linkID+'\');" class="themelink" id="social_closeButton'+social_linkID+'" title="'+social_jsonLocal.text.text_closeAction+social_jsonLocal.text.text_closeActionScreen+'" accesskey="'+social_closeLinkKey+'"><span class="social_underline">'+social_closeLinkChar+'</span>'+social_closeLinkRest+'<span class="social_screenReading" id="social_closeActionScreen'+social_linkID+'">'+social_jsonLocal.text.text_closeActionScreen+'</span></a></div>';
			document.getElementById('soc_layerCloseHeader'+social_linkID).innerHTML = newHtml;
		}
		if(social_jsonLocal.text.text_linkTitle) {
			var oldHtml = document.getElementById('soc_layerTriggerLink'+social_linkID).innerHTML;
			var newTitle = 'title="'+social_jsonLocal.text.text_linkTitle+'"';
			newHtml = oldHtml.replace(/title=\".*?\"/g,newTitle);
			document.getElementById('soc_layerTriggerLink'+social_linkID).innerHTML = newHtml;
		}
	}
	
	social_filledFlags[social_linkID-1] = true;
	// Get JSON object with site list
	var x = social_jsonList.sites;
	
	var social_pathname = window.location.pathname.toLowerCase();
	
	// Find which link will be the last to be tabbed and assign the onBlur action
	var social_lastTabIndex = 0;
	var social_lastTabbed = x.length/3;
	social_lastTabbed -= parseInt(social_lastTabbed);
	if(social_lastTabbed > .3 && social_lastTabbed < .4 && x.length>=3) social_lastTabIndex = x.length-2;
	else if(social_lastTabbed > .6 && social_lastTabbed < .7 && x.length>=3) social_lastTabIndex = x.length-3;
	else if(social_lastTabbed == 0) social_lastTabIndex = x.length-1;
	else if(x.length == 2) social_lastTabIndex = x.length-1;
	else if(x.length == 1) social_lastTabIndex = x.length-1;
	
	var k = 1;
	var onblurcall = "";
	var onclickcall = "";
	for (i=0;i<x.length;i++) {		
		var urlString = x[i].url;
		var wintarget = " target=\"_blank\"";
		urlString = urlString.replace(/SOCIAL_PAGE_URL/,social_permalinks[social_linkID-1]);
		urlString = urlString.replace(/SOCIAL_PAGE_TITLE/,social_titles[social_linkID-1]);
		urlString = urlString.replace(/SOCIAL_PAGE_DESCRIPTION/,social_descriptions[social_linkID-1]);
				
		var newdiv = document.createElement('div');
				
		var innerHTML_string = '<img alt="" width="16" height="16" border="0" class="social_img" src="'+social_protocolDir+x[i].image_path+'">';
		
		var social_page_permalink_string = false;
		if(!urlString.indexOf('SOCIAL_PERMALINK')) {
			wintarget = "";
			social_page_permalink_string = true;
			var pop_location = urlString.split("=");
			if(pop_location[1]) social_permalinkpopup = pop_location[1];
			urlString = "";
		}
		
		// Prepare metrics call
		var social_service_name = x[i].name;
		social_service_name = social_service_name.toLowerCase();
		social_service_name = social_service_name.replace(/\W+|\s+/g,"");
		var social_metricsEvent = ' onmousedown=s_sendCustomLinkEvent("","'+social_service_name+':'+social_pathname+'");';
		
		// Build link
		onclickcall = "";
		onblurcall = "";
		if(i == social_lastTabIndex) onblurcall = ' onBlur="social_blurCloseLight('+social_linkID+');"';
		onclickcall = ' onclick="return social_blurClose(\'flagPopup='+social_page_permalink_string+', social_linkID='+social_linkID+'\');"';
		
		innerHTML_string += '<a class="udrline"';
		innerHTML_string += wintarget+social_metricsEvent+onclickcall+onblurcall+' href="'+urlString+'">'+x[i].name+'</a>';
		
		if(i == social_lastTabIndex) innerHTML_string += '<span class="social_screenReading">'+social_layerEnd+'</span>';
		
		newdiv.innerHTML = innerHTML_string;
		
		var column = document.getElementById('social_column'+k+'_'+social_linkID);
		column.appendChild(newdiv);
		if(k < 3) k++;
		else k=1;
		
	}
	// finished drawing
	social_reposition(social_linkID);	
}

function social_showMenu(mOrder) {
    var mId = 'social_menu' + mOrder;
	// Check flags, if is the layer is empty fill it with links else just display it
	if(social_filledFlags[mOrder-1]) { 
		social_reposition_resize(mOrder);
		social_changeObjectVisibility(mId, 'visible', mOrder);
		if(is_safari || is_opera) social_refresh();
	}
	else social_fillLinksJSON(mOrder);
}

function social_hideMenus() {
    for(j = 1; j <= social_numMenus; j++) {
		social_changeObjectVisibility('social_menu' + j, 'hidden');
		// This code shows any other social tagging links when the layer is closed / fixes a IE overlapping bug (1 of 2)
		//if(is_ie) document.getElementById('soc_layerTriggerLink'+j).style.visibility = 'visible';
    }
}
function social_hideMenu(menuNumber) {
	social_changeObjectVisibility('social_menu' + menuNumber, 'hidden');
}

function social_getDescription() {
	var metaElements = document.all ? document.all.tags('META') : document.getElementsByTagName ? document.getElementsByTagName ('META') : new Array();
	var metaDesc = "";
	var i = 0;
	for (var m = 0; m < metaElements.length; m++) {
		if (metaElements[m].name.toLowerCase() == 'description') metaDesc = metaElements[m].content;
	}
	return metaDesc;
}


document.onmouseup = social_hideMenus;
// END social tagging popup code

// 20070410