// global javascript file for press room

//declare variable for latest news year
var currentYearPress = '2010';
var d = new Date();
var thisYear = d.getFullYear();
var monthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function checkRelatedInfo() {
	var mytext = location.href;
	var indexpos = mytext.indexOf(".htm");
	if (indexpos>=0) {
		mytext = mytext;
	}
	var myresult = replace(mytext,'.htm','.txt');

	if (getFile(myresult).indexOf('RelatedInfo') !=-1) {
		relFlag = 'yes';
		document.write('<script language="javascript" src="' + myresult + '"></scr' + 'ipt>');
	} else {
		relFlag = 'no';
	}
	
	var IE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;
	
	if(IE6) {
		document.write('<link rel="stylesheet" type="text/css" href="/citi/press/css/print_ie6.css" media="print" />');
	}
}

relatedNews = function(showRelInfo, pressYear) {
	//global variable declarations
	rlInfo = new Object();
	rlInfo.showRelInfo = showRelInfo;
	rlInfo.currentSection = '';
	rlInfo.pressYear = pressYear;
	rlInfo.tabsNames = new Array('rl_hd', 'ln_hd');
	
	if (rlInfo.showRelInfo == 'yes') {
		rlInfo.jsonObject = RelatedInfo;
		this.buildRelInfo();
	}
	
	this.determineTab();
}

relatedNews.prototype.determineTab = function() {
	var tabHtml = '';
	
	if (rlInfo.showRelInfo == 'yes') {
		tabHtml += '<ul>'+
			'<li><a href="javascript:relNews.tabOverState(0);" id="rl_hd" title="Related Information"></a></li>'+
			'<li><a href="javascript:relNews.tabOverState(1);" id="ln_hd" title="Latest News"></a></li>'+
		'</ul>';
		
		hideDiv('latestNews');
	} else {
		tabHtml += '<img src="/citi/press/images/lt_news_red.gif" alt="Latest News" />';
		
		hideDiv('relInfo');
	}
	
	document.getElementById('rtHeaders').innerHTML = tabHtml;
}

relatedNews.prototype.tabOverState = function(tabNum) {
	if (rlInfo.showRelInfo == 'yes') {
		relImg = document.getElementById(rlInfo.tabsNames['0']);
		latImg = document.getElementById(rlInfo.tabsNames['1']);
	
		switch (tabNum) {
			case 0:
				relImg.style.cssText = "background: url('/citi/press/images/rel_info_btn0.gif') no-repeat 0px 0px;";
				latImg.style.cssText = "background: url('/citi/press/images/lt_news_btn1.gif') no-repeat 0px 0px;";
				
				hideDiv('latestNews');
				showDiv('relInfo');
			break;
			case 1:
				relImg.style.cssText = "background: url('/citi/press/images/rel_info_btn1.gif') no-repeat 0px 0px;";
				latImg.style.cssText = "background: url('/citi/press/images/lt_news_btn0.gif') no-repeat 0px 0px;";
				
				hideDiv('relInfo');
				showDiv('latestNews');
			break;
		}
	}
}

relatedNews.prototype.buildRelInfo = function() {
	var rsInfo = rlInfo.jsonObject;
	var relHtml = '';
	
	for (var i = 0; i < rsInfo.data.length; i++) {
		relHtml += '<div class="rtContainer">'+
			'<a href="javascript:relNews.showCategory(' + i + ')" class="rtHD" id="rlHD' + i + '">' + rsInfo.data[i].sectionTitle + '</a>';
			
			// checking to see if we have any items
			if (rsInfo.data[i].items != null) {
				relHtml += '<div class="rtContent" id="rlCont' + i + '" style="display: none;">'+
					'<ul>';
						for (var y = 0; y < rsInfo.data[i].items.length; y++) {
							relHtml += '<li><a href="' + rsInfo.data[i].items[y].link + '">' + rsInfo.data[i].items[y].label + '</a></li>';
						}
					relHtml += '</ul>'+
				'</div>';
			} else if (rsInfo.data[i].photos != null) {
				rlInfo.currentSection = i;
				relHtml += this.buildPhotos();
			}
			
		relHtml += '</div>';
	}
	
	document.getElementById('relInfo').innerHTML = relHtml;
	
	this.showCategory(0);
}

relatedNews.prototype.showCategory = function(id) {
	var currentID = id;
	//alert(currentID);
	var thisSection = document.getElementById('rlCont' + currentID);
	
	//check display status
	if (thisSection.style.display == '') {
		//thisSection.style.display = 'none';
		new Effect.SlideUp('rlCont' + currentID, { queue: 'front'});
		
		// changing class for section header to show selected/unselected state
		changeClass('rlHD' + currentID, 'rtHD');
	} else {
		//thisSection.style.display = '';
		new Effect.SlideDown('rlCont' + currentID, { queue: 'front'});
		
		// changing class for section header to show selected/unselected state
		changeClass('rlHD' + currentID, 'rtHDSelected');
	}
}

relatedNews.prototype.buildPhotos = function() {
	var rsPhotoSection = rlInfo.jsonObject.data[rlInfo.currentSection].photos;
	
	var photoHtml = '';
	photoHtml += '<div class="rtContent" id="rlCont' + rlInfo.currentSection + '" style="display: none;">';
	
	var mainImgCt = 0;
	var innerCt = 0;
	
	for (var p = 0; p < rsPhotoSection.length; p++) {
		if (rsPhotoSection[p].smallImg != '') {
			mainImgCt = mainImgCt + 1;
		}
	}
	
	for (var p = 0; p < rsPhotoSection.length; p++) {
		if (rsPhotoSection[p].smallImg == '') {
			photoHtml += '<a href="' + rsPhotoSection[p].largeImg + '" rel="lightbox[press]" title="' + rsPhotoSection[p].Caption + '"></a>';
		} else {
			photoHtml += '<div id="photo' + innerCt + '"';
				if(innerCt != 0) {
					photoHtml += ' style="display: none;"';
				}
				photoHtml += '>';
				if ((rsPhotoSection[p].largeImg == '') && (rsPhotoSection[p].link != '')) {
					photoHtml += '<a href="' + rsPhotoSection[p].link + '" title="' + rsPhotoSection[p].Caption + '"><img src="' + rsPhotoSection[p].smallImg + '" width="200" height="150" /></a>';
				} else if ((rsPhotoSection[p].largeImg == '') && (rsPhotoSection[p].link == '')) {
					photoHtml += '<img src="' + rsPhotoSection[p].smallImg + '" width="200" height="150" />';
				} else {
					photoHtml += '<a href="' + rsPhotoSection[p].largeImg + '" rel="lightbox[press]" title="' + rsPhotoSection[p].Caption + '"><img src="' + rsPhotoSection[p].smallImg + '" width="200" height="150" /></a>';
				}
				photoHtml += '<div class="caption2">';
				if (rsPhotoSection[p].Caption.length > 66) {
					photoHtml += rsPhotoSection[p].Caption.substring(0, 66) + '...';
				} else {
					photoHtml += rsPhotoSection[p].Caption;
				}
				photoHtml += '</div>';

				if (mainImgCt > 1) {
					photoHtml += '<div id="pager">';
						if(innerCt == 0) {
							photoHtml += '<img src="../images/lt_arrow_red.jpg" /> ';
						} else {
							photoHtml += '<a href="javascript:showDiv(\'photo' + (innerCt-1) + '\');hideDiv(\'photo' + innerCt + '\')"><img src="../images/lt_arrow_red.jpg" /></a> ';
						}
						
						photoHtml += (innerCt + 1) + ' of ' + (mainImgCt);
						
						if(innerCt == (mainImgCt - 1)) {
							photoHtml += ' <img src="../images/rt_arrow_red.jpg" />';
						} else {
							photoHtml += ' <a href="javascript:showDiv(\'photo' + (innerCt+1) + '\');hideDiv(\'photo' + innerCt + '\')"><img src="../images/rt_arrow_red.jpg" /></a>';
						}
					photoHtml += '</div>';
				}
			photoHtml += '</div>';
			innerCt = innerCt + 1;
		}
	}
	
	photoHtml += '</div>';
	
	return photoHtml;
}

function getDropNews() {
	document.getElementById('topLatestNews').innerHTML = ' ';
	
	var dropVal1 = document.getElementById("presstype");
	passVarValue = new Array();
	passVarLabel = new Array('ntype');
	
	//passVarValue[0] = dropVal1.value;
	passVarValue[0] = dropVal1.options[dropVal1.selectedIndex].text;
	
	switch (passVarValue[0]) {
		case 'Citi-Wide':
			var latestNewsContent = new loadData("../xml/press" + thisYear + ".xml", "../xsl/latenews_drop.xsl", "topLatestNews", passVarLabel, passVarValue, false);
		break;
		case 'Business-Specific':
			var latestNewsContent = new loadData("../xml/press" + thisYear + ".xml", "../xsl/latenews_drop.xsl", "topLatestNews", passVarLabel, passVarValue, false);
		break;
		case 'Community Affairs':
			var latestNewsContent = new loadData("../xml/press" + thisYear + ".xml", "../xsl/latenews_drop.xsl", "topLatestNews", passVarLabel, passVarValue, false);
		break;
		case 'Features':
			var latestNewsContent = new loadData("../xml/press" + thisYear + ".xml", "../xsl/latenews_drop.xsl", "topLatestNews", passVarLabel, passVarValue, false);
		break;
	}
}

function topNews() {	
	document.write('<div id="pageContainerBorder">');
	
	document.write('<div id="SiteTitle"><img src="/citi/press/images/pr_sitename.gif" alt="Press Room" /></div>');
	
	document.write('<div class="lgCont_TopBg"></div>');
		
	document.write('<div class="lgCont_CenterBg">');
			
		document.write('<table cellpadding="0" cellspacing="0" width="100%" border="0" class="lgCont_DividerBg">');
			document.write('<tr>');
				document.write('<td id="col3_left">');
					document.write('<div id="leftNavContainer"></div>');
				document.write('</td>');
				document.write('<td id="col3_center">');
					document.write('<img src="/citi/template/images/header/citi_corp_logo.gif" alt="Citi" id="logoPrint" />');
}

function bottomNews() {
	document.write('</td>');
				document.write('<td id="col3_right">');
					document.write('<div id="rtHeaders"></div>');
	            	
	                document.write('<div id="relInfo"></div>');
	                
	                document.write('<div id="latestNews">');
	                
	                	document.write('<select name="presstype" id="presstype">');
                        	document.write('<option>- Select -</option>');
                            document.write('<option name="Citi-Wide">Citi-Wide</option>');
                            document.write('<option name="Business-Specific">Business-Specific</option>');
                            document.write('<option name="Community Affairs">Community Affairs</option>');
                            document.write('<option name="Features">Features</option>');
                        document.write('</select>');
                        document.write('<a href="javascript:getDropNews();"><img src="/citi/press/images/red_submit.gif" width="15" height="15" alt="submit" /></a>');
                        
                        document.write('<div id="topLatestNews"></div>');
	                    
	                document.write('</div>');
				document.write('</td>');
			document.write('</tr>');
		document.write('</table>');
			
	document.write('</div>');
	
	document.write('<div class="lgCont_BtmBg"></div>');
	
	document.write('</div>');
}

function getFile(filename) {
	oxmlhttp = null;
    try {
		oxmlhttp = new XMLHttpRequest();
        oxmlhttp.overrideMimeType("text/xml");
	}
    catch(e) {
		try {
			oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			return null;
		}
	}
	
	if(!oxmlhttp) return null;
	
	try {
		oxmlhttp.open("GET",filename,false);
        oxmlhttp.send(null);
	}
	catch(e) {
		return null;
	}
	
	return oxmlhttp.responseText;
}
  
function replace(string,text,by) {
	// Replaces text with by in string
	var strLength = string.length, txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0)) return string;
	
	var i = string.indexOf(text);
	if ((!i) && (text != string.substring(0,txtLength))) return string;
	if (i == -1) return string;
	
	var newstr = string.substring(0,i) + by;
	
	if (i+txtLength < strLength) newstr += replace(string.substring(i+txtLength,strLength),text,by);
	
	return newstr;
}