
var Site = 'International';
//var serverAddress = 'dev-am.iweb.ey.com'

function replaceStrings(inString,searchString,replacementString) {
	var newString = inString;
	var p = newString.indexOf(searchString);
	while (p>=0) {
		newString = newString.substring(0,p) + replacementString + newString.substring(p+searchString.length,newString.length);
		p = newString.indexOf(searchString);
	}
	return(newString);
}

function loadTabs() {
	$("#TabbedPanels1 > ul").tabs({ 
		fx: { 
			opacity: "toggle", duration: 200 
		} 
	});
}

function initialLoad( filepath, sector) {
	
	loadMenu();
	
	if(filepath != undefined) {
		loadXMLData(filepath,sector);	       //loadXMLData('/Global/ccr.nsf/Images/A79425F6E3C44609852574EC006294B7/$FILE/xml_content_automotive.xml','automotive');
	}
	
	$(".eyRightWhiteBox h3").click (function() {
	$(".eyRightWhiteBox").toggleClass("expandthis");
	} );
}


function loadMenu() {
	
	//alert('in Load Menu');
	
$.ajax({
			type:"GET",
			url: '/global/ccr.nsf/Images/57CB4EE81D7D59E985257592007755ED/$FILE/xml_menu.xml',
			dataType: ($.browser.msie) ? 'text' : 'xml',
			success: function(data) {
			
			var menuxml;
     		if (typeof data == "string") {
       			menuxml = new ActiveXObject("Microsoft.XMLDOM");
       			menuxml.async = true;
       			menuxml.loadXML(data);
     		} else {
       			menuxml = data;
     		}
     		// Returned data available in object "menuxml"
			
			$(menuxml).find('item').each(function(){
					var menu_title_text = $(this).find('title').text()
					var menu_identifier_text = $(this).find('identifier').text()
					var menu_address_text = $(this).find('address').text()
					
					if( menu_identifier_text == 'menu-separator' ) {
						
					$('<span id="' + menu_identifier_text + '" style="font-size:1.16em; font-weight:bold; color:#000000; font-family: Helvetica,Arial,Verdana,sans-serif;"></span>')
					.html(menu_title_text).appendTo('#i360nav');
					} else {
					$('<li id="' + menu_identifier_text + '"></li>')
					.html('<a href="' + menu_address_text + '">' + menu_title_text + '</a>').appendTo('#i360nav');}
				}); //close each(
			//loadMenuClicks();
			//$('Industry 360° content').appendTo('#i360navtitle');
						},
			error: function(){
        		alert('Error loading menu items.');
				//alert(this.showerr)
    		}
		});//close $.ajax(		

}

function loadXMLData(source, selection) {

		$('#results').empty();
		
		var xmlSource = '';
		var contentSelection = '';
		
		var statusLast = '';
		var statusCount = 0;
		var link_text = '';
		var href_text = '';
		var target_text = '';
		var target_text_dsp = '';
		var hottopic_text = '';
		var filesizedsp_text = '';
		var firstPrintReport = 0;
		var firstPodcast = 0;
		var firstWebcast = 0;
		xmlSource = source;
		contentSelection = selection;
			
		$.ajax({
			type:"GET",
			url: xmlSource,
			dataType: ($.browser.msie) ? 'text' : 'xml',
			success: function(data) {
			
			var xml;
     		if (typeof data == "string") {
       			xml = new ActiveXObject("Microsoft.XMLDOM");
       			xml.async = false;
       			xml.loadXML(data);
     		} else {
       			xml = data;
     		}
     		// Returned data available in object "xml"
	
			$(xml).find('item').each(function(){
				var status = $(this).find('status').text()
				var statusChar = replaceStrings(status, "\/", "-")
				statusChar = replaceStrings(statusChar, "(", "-")
				statusChar = replaceStrings(statusChar, ")", "-")
				var status_text = contentSelection+'_'+replaceStrings(statusChar, " ", "_")
				var title_text = $(this).find('title').text()
				var description_text = $(this).find('description').text()
				var type_text = $(this).find('type').text()
				var location_text = $(this).find('location').text()
				var target_text = $(this).find('target').text()
				var image_location_text = $(this).find('image').text()
				var filesize_text = $(this).find('filesize').text()
				var date_text = $(this).find('date').text()
				var hottopic = $(this).find('hottopic').text()
				
				if(hottopic == 'Yes' && (contentSelection != 'CCS' & contentSelection != 'IFRS')) {
					hottopic_text = '<li class="hottopic">Hot topic</li>'
				} else {
					hottopic_text = ''
				}
				
				target_text_dsp = '';
				if (location_text.indexOf('.pdf') > 0 || target_text == '_blank' ) { target_text_dsp = ' target="_blank"'}
				
				if(type_text == 'Print report') {
					if(firstPrintReport == 0) {
						$('<li class="TabbedPanelsTab "><a href="#tabOneContent" title="Print reports" id="tab1">Print reports</a></li>').appendTo('#TabbedPanelsTabGroup');
						$('<div id="tabOneContent" class="TabbedPanelsContent"></div>').appendTo('#results');
						firstPrintReport = 1;
					}
					if(location_text == '') {
						href_text = '';
						image_text = '<img src="' + image_location_text +'" width="85" height="110" alt="' + title_text +'">'
					} else {
						href_text = '<a href="' + location_text + '" title="' + title_text + '"' + target_text_dsp + '>View</a>';
						image_text = '<a href="' + location_text + '" title="' + title_text + '"' + target_text_dsp + '><img src="' + image_location_text +'" width="85" height="110" alt="' + title_text + '"></a>'
					}
					if(filesize_text != '') {
					filesizedsp_text = '<span> (' + filesize_text + ')</span>'	
					} else {
						filesizedsp_text = ''
					}
					link_text = href_text + filesizedsp_text;
					if(status == 'Featured item') {spotlight_text = 'spotlight '} else {spotlight_text = ''}
					
					$('<ul class=\"items ' + spotlight_text + status_text + ' ' + type_text + '\" style=\"display:block\"></ul>').html('<li class="doctype">' + status + '</li><li class="img">' + image_text + '</li><li class="title">' +  title_text + '</li><li class="date">' + date_text + '</li>' + hottopic_text + '<li class="type">' + link_text + '</li>').appendTo('#tabOneContent');
				
				} else if (type_text == 'Podcast') {
					if(firstPodcast == 0) {
						$('<li class="TabbedPanelsTab" ><a href="#tabTwoContent" title="Podcasts" id="tab2">Podcasts</a></li>').appendTo('#TabbedPanelsTabGroup');
						$('<div id="tabTwoContent" class="TabbedPanelsContent"></div>').appendTo('#results');
						firstPodcast = 1;
					}
					if(location_text == '') {
						href_text = '';
						image_text = '<img src="' + image_location_text +'" width="85" height="110" alt="' + title_text +'">'
					} else {
						href_text = '<a href="' + location_text + '" title="' + title_text + '"' + target_text_dsp + '>View</a>';
						image_text = '<a href="' + location_text + '" title="' + title_text + '"' + target_text_dsp + '><img src="' + image_location_text +'" width="85" height="110" alt="' + title_text +'"></a>'
					}
					link_text = href_text;
					if(status == 'Featured item') {spotlight_text = 'spotlight '} else {spotlight_text = ''}
					
					$('<ul class=\"items ' + spotlight_text + status_text + ' ' + type_text + '\" style=\"display:block\"></ul>').html('<li class="doctype">' + status + '</li><li class="img">' + image_text + '</li><li class="title">' +  title_text + '</li><li class="date">' + date_text + '</li>' + hottopic_text + '<li class="type">' + link_text + '</li>').appendTo('#tabTwoContent');
					
				} else if (type_text == 'Webcast') {
					if(firstWebcast == 0) {
						$('<li class="TabbedPanelsTab" ><a href="#tabThreeContent" title="Webcasts" id="tab3">Webcasts</a></li>').appendTo('#TabbedPanelsTabGroup');
						$('<div id="tabThreeContent" class="TabbedPanelsContent"></div>').appendTo('#results');
						firstWebcast = 1;
					}
					
					if(location_text == '') {
						href_text = '';
						image_text = '<img src="' + image_location_text +'" width="85" height="110" alt="' + title_text +'">'
					} else {
						href_text = '<a href="' + location_text + '" title="' + title_text + '"' + target_text_dsp + '>View</a>';
						image_text = '<a href="' + location_text + '" title="' + title_text + '"' + target_text_dsp + '><img src="' + image_location_text +'" width="85" height="110" alt="' + title_text +'"></a>'
					}
					link_text = href_text;
					if(status == 'Featured item') {spotlight_text = 'spotlight '} else {spotlight_text = ''}
					
					$('<ul class=\"items ' + spotlight_text + status_text + ' ' + type_text + '\" style=\"display:block\"></ul>').html('<li class="doctype">' + status + '</li><li class="img">' + image_text + '</li><li class="title">' +  title_text + '</li><li class="date">' + date_text + '</li>' + hottopic_text + '<li class="type">' + link_text + '</li>').appendTo('#tabThreeContent');
				}
				
				}); //close each(
			$('<div style="clear:both;"></div>').appendTo('#results');
			loadTabs();
			},
			error: function(){
        		alert('Error loading XML document');
				//alert(this.showerr)
    		}
		});//close $.ajax(
}
