// Start function when DOM has completely loaded 
$(document).ready(function(){
   

   
   //tabs
	$('#tabs ul').tabs();
	
	
	//tabLinks
	$('#tabProductLink').click(function() { // bind click event to link
    	$('#tabs ul').tabs('select', 1); // switch to third tab
    	return false;
	});
	
	$('#tabMarketLink').click(function() { // bind click event to link
    	$('#tabs ul').tabs('select', 2); // switch to third tab
    	return false;
	});
	
	
	
	
	//accordion
	$('#product-accordion').accordion({
		header: '.ui-accordion-header',
		clearStyle: true,		
		active: true, 
		alwaysOpen: false 
	});
	$('#market-accordion').accordion({
		header: '.ui-accordion-header',
		clearStyle: true,		
		active: true, 
		alwaysOpen: false 
	});
	
	//applies table sorting only when needed - saves cpu time
	$(".ui-accordion-header").click(function () { 
      //var headerName = this.class;
	  $(this).parent().children('.ui-accordion-content').children(0).children(0).initTable( {perPages:[10]} ).controlTable();
    });
	
	//ready url query
   
	

   if (getQueryValue('market') != "")
   {
   		//open markets tab
		$('#tabs ul').tabs('select', 2);
		//open market accordian
		var marketToOpen = "#" + getQueryValue('market');
		$('#market-accordion').accordion( "activate", marketToOpen) ;
		//this doesn't work the best but will have to do for now, needs testing on different machines
		setTimeout(function (){
        	$(marketToOpen).parent().children('.ui-accordion-content').children(0).children(0).initTable( {perPages:[10]} ).controlTable();
		}, 300); 
		//
		//
		
		
   } else if (getQueryValue('product') != "")
   {
	   
	   $('#tabs ul').tabs('select', 1);
	   var productToOpen = "#" + getQueryValue('product');
		$('#product-accordion').accordion( "activate", productToOpen) ;
		//this doesn't work the best but will have to do for now, needs testing on different machines
		setTimeout(function (){
        	$(productToOpen).parent().children('.ui-accordion-content').children(0).children(0).initTable( {perPages:[10]} ).controlTable();
		}, 300); 
		//
		//
	   
   }

	
	
	$.get("http://www.strongwell.com/Literature/application_profiles/application_profiles_library.xml",{},function(xml){
																												   
		//listTableHTML html
		//new
		newListHTML = '<table id="newListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left" style="background:#fff;"><tbody>';
		//products
		customListHTML = '<table id="customListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		extrenListHTML = '<table id="extrenListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		composoliteListHTML = '<table id="composoliteListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		utilityPolesListHTML = '<table id="utilityPolesListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		duragrateDuratreadListHTML = '<table id="duragrateDuratreadListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		duradekDuragridSafrailListHTML = '<table id="duradekDuragridSafrailListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		durashieldListHTML = '<table id="durashieldListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		safplateListHTML = '<table id="safplateListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		safplankListHTML = '<table id="safplankListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		strongrailListHTML = '<table id="strongrailListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		//markets
		aerospaceMilitaryListHTML = '<table id="aerospaceMilitaryListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		architecturalListHTML = '<table id="architecturalListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		buildingConstructionListHTML = '<table id="buildingConstructionListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		chemicalListHTML = '<table id="chemicalListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		cellularListHTML = '<table id="cellularListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		coolingTowerListHTML = '<table id="coolingTowerListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		electricalListHTML = '<table id="electricalListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		emiRfiTestingListHTML = '<table id="emiRfiTestingListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		foodProcessingListHTML = '<table id="foodProcessingListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		hotelMotelListHTML = '<table id="hotelMotelListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		infrastructureListHTML = '<table id="infrastructureListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		marineListHTML = '<table id="marineListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		miningListHTML = '<table id="miningListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		offshoreListHTML = '<table id="offshoreListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		parksAndRecreationListHTML = '<table id="parksAndRecreationListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		petrochemicalListHTML = '<table id="petrochemicalListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		platingListHTML = '<table id="platingListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		pollutionControlListHTML = '<table id="pollutionControlListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		powerListHTML = '<table id="powerListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		specialtyListHTML = '<table id="specialtyListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		transportationListHTML = '<table id="transportationListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		waterWastewaterListHTML = '<table id="waterWastewaterListings" width="100%" border="0" cellspacing="0" cellpadding="5" align="left"><tbody>';
		

		//productCheck Variable
		var productCheck = -1;
		var pdfLocation = 'http://www.strongwell.com/pdffiles/Application%20Profiles/'
		
		// Run the function for each document tag in the XML file
		
		$('ap',xml).each( function(i) {
			
			documentID = $(this).find("id").text();
			documentProduct = $(this).find("product").text();
			documentTitle = $(this).find("title").text();
			documentCustomer = $(this).find("customer").text();
			documentLink = $(this).find("link").text();
			documentNew = $(this).find("id").attr("new"); 				
			documentMarkets = $(this).find("markets").text();
			documentThumbnail = "images/popups/" + documentID + ".jpg";						
			
			//is new?
			if ((documentNew) != undefined){
				documentNewHTML = '<img src="../../images/newAP.gif" alt="New" width="36" height="16" align="texttop" border="0" style="margin:0px 5px 0px 0px;">';
				newListHTML += '<tr><td align="left" valign="top" style="width:30px;">' + documentID +'</td><td align="left" valign="top"><a href="' + pdfLocation +documentLink + '" class="highlight"  target="_blank">'  + documentNewHTML + documentTitle + '&nbsp;&nbsp;&#8212;&nbsp;&nbsp;' + documentCustomer + '</a></td></tr>';
				
				
			} else	{
				documentNewHTML = "";
			}
			
			var tableRowCode= '<tr><td align="left" valign="top" style="width:30px;">' + documentID +'</td><td align="left" valign="top"><a href="' + pdfLocation + documentLink + '" class="highlight" target="_blank">'  + documentNewHTML + documentTitle + '&nbsp;&nbsp;&#8212;&nbsp;&nbsp;' + documentCustomer + '</a></td></tr>';
				
			//sort by products
			productCheck = documentProduct.indexOf('CUSTOM');
			if (productCheck > -1){ 
				customListHTML += tableRowCode;
			}
			productCheck = documentProduct.indexOf('EXTREN/FABRICATION');
			if (productCheck > -1){ 
				extrenListHTML += tableRowCode;
			}
			productCheck = documentProduct.indexOf('COMPOSOLITE');
			if (productCheck > -1){ 
				composoliteListHTML += tableRowCode;
			}
			productCheck = documentProduct.indexOf('UTILITY POLES');
			if (productCheck > -1){ 
				utilityPolesListHTML += tableRowCode;
			}
			productCheck = documentProduct.indexOf('DURAGRATE/DURATREAD');
			if (productCheck > -1){ 
				duragrateDuratreadListHTML += tableRowCode;
			}
			productCheck = documentProduct.indexOf('DURADEK/DURAGRID/SAFRAIL');
			if (productCheck > -1){ 
				duradekDuragridSafrailListHTML += tableRowCode;
			}
			productCheck = documentProduct.indexOf('DURASHIELD');
			if (productCheck > -1){ 
				durashieldListHTML += tableRowCode;
			}
			productCheck = documentProduct.indexOf('SAFPLATE');
			if (productCheck > -1){ 
				safplateListHTML += tableRowCode;
			}
			productCheck = documentProduct.indexOf('SAFPLANK');
			if (productCheck > -1){ 
				safplankListHTML += tableRowCode;
			}
			productCheck = documentProduct.indexOf('STRONGRAIL');
			if (productCheck > -1){ 
				strongrailListHTML += tableRowCode;
			}
			
			//sort by markets
			productCheck = documentMarkets.indexOf('AEROSPACE/MILITARY');
			if (productCheck > -1){ 
				aerospaceMilitaryListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('ARCHITECTURAL');
			if (productCheck > -1){ 
				architecturalListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('BUILDING/CONSTRUCTION');
			if (productCheck > -1){ 
				buildingConstructionListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('CHEMICAL');
			if (productCheck > -1){ 
				chemicalListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('CELLULAR');
			if (productCheck > -1){ 
				cellularListHTML += tableRowCode;
            }
            productCheck = documentMarkets.indexOf('COOLING TOWER');
            if (productCheck > -1) {
                coolingTowerListHTML += tableRowCode;
            }
			productCheck = documentMarkets.indexOf('ELECTRICAL');
			if (productCheck > -1){ 
				electricalListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('EMI/RFI TESTING');
			if (productCheck > -1){ 
				emiRfiTestingListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('FOOD PROCESSING');
			if (productCheck > -1){ 
				foodProcessingListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('HOTEL/MOTEL');
			if (productCheck > -1){ 
				hotelMotelListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('INFRASTRUCTURE');
			if (productCheck > -1){ 
				infrastructureListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('MARINE');
			if (productCheck > -1){ 
				marineListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('MINING');
			if (productCheck > -1){ 
				miningListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('OFFSHORE');
			if (productCheck > -1){ 
				offshoreListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('PARKS AND RECREATION');
			if (productCheck > -1){ 
				parksAndRecreationListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('PETROCHEMICAL');
			if (productCheck > -1){ 
				petrochemicalListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('PLATING');
			if (productCheck > -1){ 
				platingListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('POLLUTION CONTROL');
			if (productCheck > -1){ 
				pollutionControlListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('POWER');
			if (productCheck > -1){ 
				powerListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('SPECIALTY');
			if (productCheck > -1){ 
				specialtyListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('TRANSPORTATION');
			if (productCheck > -1){ 
				transportationListHTML += tableRowCode;
			}
			productCheck = documentMarkets.indexOf('WATER/WASTEWATER');
			if (productCheck > -1){ 
				waterWastewaterListHTML += tableRowCode;
			}
		});
	
			//ending html
			var endingHTML = '</tbody><tfoot><tr><td colspan="2"><div class="pagination"></div><div class="paginationTitle">Page</div><div class="selectPerPage"></div><div class="status"></div></td></tr></tfoot></table>';
			//new
			newListHTML += endingHTML;
			//products
			customListHTML += endingHTML;
			extrenListHTML += endingHTML;
			composoliteListHTML += endingHTML;
			utilityPolesListHTML += endingHTML;
			duragrateDuratreadListHTML += endingHTML;
			duradekDuragridSafrailListHTML += endingHTML;
			durashieldListHTML += endingHTML;
			safplateListHTML += endingHTML;
			safplankListHTML += endingHTML;
			strongrailListHTML += endingHTML;
			//markets
			aerospaceMilitaryListHTML += endingHTML;
			architecturalListHTML += endingHTML;
			buildingConstructionListHTML += endingHTML;
			chemicalListHTML += endingHTML;
			cellularListHTML += endingHTML;
			coolingTowerListHTML += endingHTML;
			electricalListHTML += endingHTML;
			emiRfiTestingListHTML += endingHTML;
			foodProcessingListHTML += endingHTML;
			hotelMotelListHTML += endingHTML;
			infrastructureListHTML += endingHTML;
			marineListHTML += endingHTML;
			miningListHTML += endingHTML;
			offshoreListHTML += endingHTML;
			parksAndRecreationListHTML += endingHTML;
			petrochemicalListHTML += endingHTML;
			platingListHTML += endingHTML;
			pollutionControlListHTML += endingHTML;
			powerListHTML += endingHTML;
			specialtyListHTML += endingHTML;
			transportationListHTML += endingHTML;
			waterWastewaterListHTML += endingHTML;
			
			// Update the DIVs
			//new
			$("#newList").append(newListHTML);
			//products
			$("#customList").append(customListHTML);
			$("#extrenList").append(extrenListHTML);
			$("#composoliteList").append(composoliteListHTML);
			$("#utilityPolesList").append(utilityPolesListHTML);
			$("#duragrateDuratreadList").append(duragrateDuratreadListHTML);
			$("#duradekDuragridSafrailList").append(duradekDuragridSafrailListHTML);
			$("#durashieldList").append(durashieldListHTML);
			$("#safplateList").append(safplateListHTML);
			$("#safplankList").append(safplankListHTML);
			$("#strongrailList").append(strongrailListHTML);
			//markets
			$("#aerospaceMilitaryList").append(aerospaceMilitaryListHTML);
			$("#architecturalList").append(architecturalListHTML);
			$("#buildingConstructionList").append(buildingConstructionListHTML);
			$("#chemicalList").append(chemicalListHTML);
			$("#cellularList").append(cellularListHTML);
			$("#coolingTowerList").append(coolingTowerListHTML);
			$("#electricalList").append(electricalListHTML);
			$("#emiRfiTestingList").append(emiRfiTestingListHTML);
			$("#foodProcessingList").append(foodProcessingListHTML);
			$("#hotelMotelList").append(hotelMotelListHTML);
			$("#infrastructureList").append(infrastructureListHTML);
			$("#marineList").append(marineListHTML);
			$("#miningList").append(miningListHTML);
			$("#offshoreList").append(offshoreListHTML);
			$("#parksAndRecreationList").append(parksAndRecreationListHTML);
			$("#petrochemicalList").append(petrochemicalListHTML);
			$("#platingList").append(platingListHTML);
			$("#pollutionControlList").append(pollutionControlListHTML);
			$("#powerList").append(powerListHTML);
			$("#specialtyList").append(specialtyListHTML);
			$("#transportationList").append(transportationListHTML);
			$("#waterWastewaterList").append(waterWastewaterListHTML);
			
			
			//add paging
			//new
			$("#newListings").initTable( {perPages:[10]} ).controlTable();
			
			
			

	}); //end getting of xml file
});

 

function BuilddocumentHTML(documentID,documentTitle,documentLink){
	
	// Check to see if their is a "post" attribute in the name field
	if ((documentNew) != undefined){
		documentNewHTML = '<img src="../../images/newAP123.gif" alt="New" width="36" height="16" align="texttop">';
	}
	else
	{
		documentNewHTML = "";
	}
	
	// Build HTML string and return
	output = '';
	output += '<tr>';
	output += '<td align="left" valign="top" width="70">'+ documentID + documentNewHTML + '</td>';
	output += '<td align="left" valign="top"><a href="' + documentLink + '" class="highlight">' + documentTitle +'</a></td>';
	output += '</tr>';
	return output;
}
	


function getQueryValue( 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];
	}













