/////////////////////
// lifecycle_tables.js
// Description: This jQuery based code controls the display of the nested tables function, plus many other things that I didn't do...
//  Created by: Brent Leavitt/Susan Kim
// Created on: 19 May 2010
// Last Updated: 19 May 2010
////////////////////////
	
	var hasjQuery = jQuery || false;
	if(hasjQuery) {
		$(document).ready(function(){	
			var i = 0;
			var getRow = $('tr.row');
			
			$('table').each(function(i){
				i++;
				$(this).attr('id','table_'+i);
				//var clsName= 'table_'+i;

				$(this).children().each(function(){// to TBODY level
					//$(this).children().addClass(clsName); //To TR level, adds table class name to each TR in the table. 
					var tableRows = $(this).children();//creates a TR object 
					var mainRows = new Array(); //all .row class TR's
					var allRows = tableRows.length; //total number of all TR's, subs and mains. 
					var k = 0;
					$.each(tableRows,function(q,p){ //function to build the array of main TR rows. 
						if($(p).is('tr.row')){
							mainRows[k] = q; 
							k++;
							
						//determining row width for dynamic width setting of td.productrelease
						
							
						}					
					});
					allRows++;//zero offSet.	
					rowClick(allRows, mainRows, tableRows);
				});
			});
			
			$('tr.sub_row').hide(); //Set to Hide all sub rows for initial state. 

			function rowClick(leg, rowsArray, object){
				$(object).each(function(){
					if($(this).is('.row')){
						$(this).click(function(){
							var row1 = $(this).index();	
							var nextRowKey = "";
							var j = 0;
							for(j in rowsArray){ 
								if(rowsArray[j] == row1){
									j++;
									nextRowKey = j;
								}
							}
							var row2 = rowsArray[nextRowKey];
							 
							if(row2 == undefined){//last row has to be tweaked 
								row2 = leg;
							}
							row2--;
							
							
							
							if($(this).siblings().slice(row1, row2).is(':visible')){ //checks to see if row is currently visible
								$('*').find('td.note').css('background','none');
								$(this).siblings().slice(row1, row2).hide();
								$(".arrow", this).removeClass('arrowdown'); //if visible hide
								
							} else {
								$(this).siblings('.sub_row').hide();
								$('*').find('td.note').css('background','none');
								$(this).siblings('.row').find('td.arrow').removeClass('arrowdown'); //else close all other sub rows and
								$(this).prev().find('td.arrow').addClass('arrowdown');
								$(this).siblings().slice(row1, row2).fadeIn().find('td.note').css('background','#d5d5d5');//display cicked sub rows
								$(".arrow", this).addClass('arrowdown');
								
							}
						});
					}				
				});	
			}

			$(".note").corner("5px");	//adds curved edges to notes - probably don't need this here
			$(".search_box").corner("bottom 5px");
			$(".tabGrp .tab-head").corner("top 5px");
			$("#sub_q_basic_container").hide();

			//$("#adv_filters_list select.filtertype").change(showFilterOptions);

		});
	}
	// Advanced Search Filter Functions
	/*function addValueDropdown(dropdown) {
		var category = $(dropdown).val();
		switch(category) {
			case "date" :
				$(dropdown).parent().parent().children("td.fvalue").html('<select name="filter1_value" class="filtervalue"><option value="">Any<\/option><option value="">This Week<\/option><option value="">This Month<\/option><option value="">This Year<\/option><\/select>');
				break;
			case "category" :
				$(dropdown).parent().parent().children("td.fvalue").html('<select name="filter1_value" class="filtervalue"><option value="">Any<\/option><option value="">Open Source<\/option><option value="">Unix<\/option><option value="">Community Development<\/option><\/select>');
				break;
			case "kind" :
				$(dropdown).parent().parent().children("td.fvalue").html('<select name="filter1_value" class="filtervalue"><option value="">Any<\/option><option value="">PDF<\/option><option value="">HTML<\/option><\/select>');
				break;
		}
	}*/	
	/*function addOperatorDropdown(dropdown) {
		$(dropdown).parent().next("td.foperator").html('<select name="filter1_operator" class="filteroperator"><option value="#">is<\/option><option value="#">is not<\/option><\/select>');
	}	
	function expandAndOrBtn() {
		$(this).parent().addClass("expanded").animate({width:'74px'}, "fast", function(){
			$(this).find("strong").fadeIn();
		});
	}	
	function addAddFilter() {
		$(this).parent().fadeOut("fast", function() {
			$(this).parent().parent().animate({width:'21px'}, "fast", function() {
				$(this).removeClass("expanded");
				addFilterRow(this,"AND");
			});
		});
	}	
	function addOrFilter() {
		$(this).parent().fadeOut("fast", function() {
			$(this).parent().parent().animate({width:'21px'}, "fast", function() {
				$(this).removeClass("expanded");
				addFilterRow(this,"OR");
			});
		});
	}*/	
	/*var filterCount = 1;
	function addFilterRow(btn,andor) {
		filterCount++;
		$thisRow = $(btn).parent().parent().parent();
		$thisRow.after(''+
			'<tr class="andor"><td colspan="4">'+andor+'<\/td><\/tr>'+
			'<tr id="filterrow_'+filterCount+'" class="filterrow">'+
				'<td class="fparam">'+
					'<select name="filter'+filterCount+'_param" class="filtertype">'+
						'<option value="">Please Select<\/option>'+
						'<option value="date">Date<\/option>'+
						'<option value="kind">Kind<\/option>'+
						'<option value="category">Category<\/option>'+
					'<\/select>'+
				'<\/td>'+
				'<td class="foperator"><\/td>'+
				'<td class="fvalue"><\/td>'+
				'<td class="factions"><\/td>'+
			'<\/tr>');
		$("#filterrow_"+filterCount + " select").change(showFilterOptions);
	}	
	function removeFilterRow() {
		var row = $(this).parent().parent().parent().parent();
		if(row.parent().children("tr").size() == 1) {
			// this is the last row
			row.children("td.foperator,td.fvalue,td.factions").empty();
			row.find("select").attr("selectedIndex",0);
		}
		else {
			if(row.prev("tr").size() == 0) {
				var rowNext = row.next("tr");
				rowNext.remove();
			}
			else {
				var rowPrev = row.prev("tr");
				rowPrev.remove();
			}
			row.remove();
		}
	}*/
	/*function showFilterOptions() {
		var filter = this;
		var tr = $(this).parent().parent();
		tr.children("td.foperator").animate({opacity: 0.0}, 250, function() {
			addOperatorDropdown(filter);
			$(this).animate({ opacity: 1.0 }, 250);
		});
		tr.children("td.fvalue").animate({opacity: 0.0}, 250, function() {
			addValueDropdown(filter);
			$(this).animate({ opacity: 1.0 }, 250);
		});
		tr.children("td.factions").animate({opacity: 0.0}, 250, function() {
			$(filter).parent().parent().children("td.factions").html(''+
				'<div class="add_filter_expander">'+
					'<div class="removeRowBtn"><a class="remove_filter" href="#"><img src="\/common\/img\/lifecycle\/srch_filter_remove.png" alt="Remove Filter" \/><\/a><\/div>'+
					'<div class="addRowBtn">'+
						'<a class="add_filter" href="#"><img src="\/common\/img\/lifecycle\/srch_filter_add.png" alt="" \/><\/a>'+
						'<div class="add_filter_andor">'+
							'<strong><a class="add_filter_and" href="#">And<\/a> \/ <a class="add_filter_or" href="#">Or<\/a><\/strong>'+
						'<\/div>'+
					'<\/div>'+
				'<\/div>').find(".add_filter").click(expandAndOrBtn).end().find(".remove_filter").click(removeFilterRow).end().find(".add_filter_and").click(addAddFilter).end().find(".add_filter_or").click(addOrFilter);
			$(this).animate({ opacity: 1.0 }, 250);
		});
	}*/
	function onChange(obj) {
		$("#sub_q_basic_container").fadeIn();
	}

function IEFix(rowid){	
			
			$('#bundle'+rowid).children().find('table').each(function(){
				$(this).children().each(function(){// to TBODY level						
					//$(this).children().find('td').css('border-left','none'); 
					var tableRows = $(this).children();//creates a TR object 	
					$.each(tableRows,function(q,p){ //function to build the array of main TR rows. 
						var rowWidth = '';
						if(($(this).width)==0){
							rowWidth = 840; 
							//alert('this row has no width!');
						} else{
						
							rowWidth = $(this).width();
							var newRowWidth = (rowWidth - 502); //500 represent the sum of all other rows, plus padding on the  td.productrelease cell. 
							if($(p).is('tr.row')){
								//determining row width for dynamic width setting of td.productrelease
								//alert('This is a TR. The width is: '+ newRowWidth);
								$(this).find('td.arrow').css('width', '20px');
								$(this).find('td.productrelease').css('width', newRowWidth+'px');	
								$(this).find('td.general').css('width', '75px');					
								$(this).find('td.extended').css('width', '75px');					
								$(this).find('td.selfsupport').css('width', '79px');					
								$(this).find('td.currentversion').css('width', '103px');					
								$(this).find('td.replacement').css('width', '103px');	
								$(this).find('td.no-arrow').css('width', '20px');
							}
							
							if($(p).is('tr.table_header')){
								$(this).find('td.no-arrow').css('width', '20px');	
								$(this).find('td.productrelease').css('width', '340px');	
								$(this).find('td.general').css('width', '77px');					
								$(this).find('td.extended').css('width', '77px');					
								$(this).find('td.selfsupport').css('width', '81px');					
								$(this).find('td.currentversion').css('width', '105px');					
								$(this).find('td.replacement').css('width', '105px');		
							}
							
							if($(p).is('tr.sub_row')){
								var noPadWidth = (rowWidth - 20);
								
								$(this).find('td.no-arrow').css('width', '20px');
								$(this).find('td.no-pad').css('width', noPadWidth+'px');
								$(this).find('td.note').css('width', noPadWidth+'px'); // 20px padding on the 
							}
								
							//alert ($(this).find('td.productrelease').html() +' , its width is: '+ $(this).find('td.productrelease').width());

						}
						
					});
				});
			});				
	}

function IEFixAll(){	
			
			$('table').each(function(){
				$(this).children().each(function(){// to TBODY level						
					//$(this).children().find('td').css('border-left','none'); 
					var tableRows = $(this).children();//creates a TR object 	
					$.each(tableRows,function(q,p){ //function to build the array of main TR rows. 
						var rowWidth = '';
						if(($(this).width)==0){
							rowWidth = 840; 
							//alert('this row has no width!');
						} else{
						
							rowWidth = $(this).width();
							var newRowWidth = (rowWidth - 502); //500 represent the sum of all other rows, plus padding on the  td.productrelease cell. 
							if($(p).is('tr.row')){
								//determining row width for dynamic width setting of td.productrelease
								//alert('This is a TR. The width is: '+ newRowWidth);
								$(this).find('td.arrow').css('width', '20px');
								$(this).find('td.productrelease').css('width', newRowWidth+'px');	
								$(this).find('td.general').css('width', '75px');					
								$(this).find('td.extended').css('width', '75px');					
								$(this).find('td.selfsupport').css('width', '79px');					
								$(this).find('td.currentversion').css('width', '103px');					
								$(this).find('td.replacement').css('width', '103px');	
								$(this).find('td.no-arrow').css('width', '20px');
							}
							
							if($(p).is('tr.table_header')){
								$(this).find('td.no-arrow').css('width', '20px');	
								$(this).find('td.productrelease').css('width', '340px');	
								$(this).find('td.general').css('width', '77px');					
								$(this).find('td.extended').css('width', '77px');					
								$(this).find('td.selfsupport').css('width', '81px');					
								$(this).find('td.currentversion').css('width', '105px');					
								$(this).find('td.replacement').css('width', '105px');		
							}
							
							if($(p).is('tr.sub_row')){
								var noPadWidth = (rowWidth - 20);
								
								$(this).find('td.no-arrow').css('width', '20px');
								$(this).find('td.no-pad').css('width', noPadWidth+'px');
								$(this).find('td.note').css('width', noPadWidth+'px'); // 20px padding on the 
							}
								
							//alert ($(this).find('td.productrelease').html() +' , its width is: '+ $(this).find('td.productrelease').width());

						}
						
					});
				});
			});				
	}

/* James' Functions*/

function loading()
{
	text = '<table cellpadding="0" cellspacing="0" width="100%" border="0" class="table_products">';
	text += '	<tr class="odd ">';
	text += '		<td class="" colspan="7" align="center">'+getLoadingText()+'</td>';
	text += '	</tr>';
	text += '</table>';
	
	$('#searchDiv').html(text);
}

function refineSearch()
{
	text = '<table cellpadding="0" cellspacing="0" width="100%" border="0" class="table_products">';
	text += '	<tr class="odd ">';
	text += '		<td class="" colspan="7" align="center">'+getRefineText()+'</td>';
	text += '	</tr>';
	text += '</table>';
	
	$('#searchDiv').html(text);
}

function search(term)
{
	loading();
	if(term.length > 1)
	{
		$.ajax({
			type: "GET",
			url: "lifecycle_list_search.jsp",
			data: "prod="+term, 
			success: showResults
		});
	}
	else
	{
		refineSearch();
	}
}

function listSearch(params)
{
	loading();
	$.ajax({
		type: "GET",
		url: "lifecycle_list_search.jsp",
		data: params, 
		success: showResults
	});
	$('#term').val('');
	$('#prod').val('');
	$('#info').val('');
	$('#maint').val('');
	$('#replace').val('');
	$('#to1').val('');
	$('#from1').val('');
	$('#to2').val('');
	$('#from2').val('');
	$('#to3').val('');
	$('#from3').val('');
}

function advSearch(params)
{
	loading();
	$.ajax({
		type: "GET",
		url: "lifecycle_list_search.jsp",
		data: params, 
		success: showResults
	});
	$('#term').val('');
}

/*
	prod_id: Clarity ID (ex. 5000418)
	parent_ids: List of NPP IDs (ex. 7507,7052)
*/
function getSubRow(prod_id, parent_ids) 
{
	if($('#detail'+parent_ids).is(':visible')||$('#bundle'+parent_ids).is(':visible')){ //checks to see if row is currently visible
		$('*').find('td.note').css('background','none');
		$('#sub_row'+parent_ids).hide();
		$('#bundle'+parent_ids).hide();
		$('#detail'+parent_ids).hide();
		$(".arrow", '#row'+parent_ids).removeClass('arrowdown');
	} else {
		
		text = '<table cellpadding="0" cellspacing="0" width="100%" border="0">';
		text += '<tr class="odd row">';
		text += '<td class="tableCell" colspan="7" align="center">'+getLoadingText()+'</td>';
		text += '</tr>';
		text += '</table>';
		$('#sub_row'+parent_ids).html(text);
		
		if(prod_id != 0)
		{
			var params = 'prod_id='+prod_id+'&parent_ids='+parent_ids;
			$.ajax({
				type: "GET",
				url: "lifecycle_subrows.jsp",
				data: params, 
				success: showSubRows
			});
		}
		$('#row'+parent_ids).siblings('.sub_row').hide();
		$('*').find('td.note').css('background','none');
		$('#sub_row'+parent_ids).fadeIn();
		$('#bundle'+parent_ids).fadeIn();
		$('#detail'+parent_ids).fadeIn().find('td.note').css('background','#d5d5d5');
		$('#detail'+parent_ids).fadeIn().find('td.note').corner("5px");
		$('#row'+parent_ids).siblings('.row').find('td.arrow').removeClass('arrowdown'); //else close all other sub rows and
		$(".arrow", '#row'+parent_ids).addClass('arrowdown');
	}
		/*	$('tr.sub_row').show(); //Set to Hide all sub rows for initial state. */
							/*
							if($('#'+div_id).is(':visible')){ //checks to see if row is currently visible
								$('*').find('td.note').css('background','none');
								$(this).siblings().slice(row1, row2).hide();
								$(".arrow", this).removeClass('arrowdown'); //if visible hide
								
							} else {
								$(this).siblings('.sub_row').hide();
								$('*').find('td.note').css('background','none');
								$(this).siblings('.row').find('td.arrow').removeClass('arrowdown'); //else close all other sub rows and
								$(this).prev().find('td.arrow').addClass('arrowdown');
								$(this).siblings().slice(row1, row2).fadeIn().find('td.note').css('background','#d5d5d5');//display cicked sub rows
								$(".arrow", this).addClass('arrowdown');
								
							}
						});
					}				
			}*/
	
}

function showResults(req)
{
	var cnt = 0;
	var text = '';
	text = '<table cellpadding="0" cellspacing="0" border="0" class="table_products">';
	
	$(req).find('product').each(function(){
			
			cnt = cnt + 1;
			evenOdd = cnt%2==1?'odd':'even';
			text += '\n<tr onclick="getSubRow('+$(this).attr('bundle')+','+$(this).attr('prod_id')+')" id="row'+$(this).attr('prod_id')+'" class="row ' + evenOdd + '">';
			if($(this).attr('arrow') == 0)
				text += '	<td class="no-arrow">&nbsp;</td>';
			else
				text += '	<td class="arrow">&nbsp;</td>';
			text += '	<td class="productrelease">'+$(this).attr('name')+'</td>';
			text += '	<td class="general">'+$(this).attr('egs')+'</td>';
			text += '	<td class="extended">'+$(this).attr('ees')+'</td>';
			text += '	<td class="selfsupport">'+$(this).attr('ess')+'</td>';
			if($(this).attr('cur_version_id') != "0")
				text+= '<td class="currentversion download_icon"><a href="http://download.novell.com/index.jsp?product_id=&search=Search&version='+$(this).attr('cur_version_id')+'">' + $(this).attr('cur_version') + '</a></td>';
			else 
				text += '<td class="currentversion download_icon"><a href="http://download.novell.com/index.jsp?product_id=&search=Search&version='+$(this).attr('prod_id')+'">' + $(this).attr('cur_version') + '</a></td>';
			if($(this).attr('replace_prod') != "n/a" && $(this).attr('mktg_url') != "NO_URL")
				text += '<td class="replacement replace_link"><a href="'+$(this).attr('mktg_url')+'">' + $(this).attr('replace_prod') + '</a></td>';
			else
				text += '	<td class="replacement">'+$(this).attr('replace_prod')+'</td>';

			text += '</tr>';
			if($(this).attr('details') != 0 || $(this).attr('url') != 0)
			{
				text += '<tr style="display:none;" class="sub_row" id="detail'+$(this).attr('prod_id')+'"><td class="note" colspan="7">';
				if($(this).attr('details') != 0)
					text +=	'	<p>'+$(this).attr('details')+'</p>';
				if($(this).attr('url') != 0)
					text +=	'	<p class="readmore"><a href="'+$(this).attr('url')+'">Read more</a></p>';
				text += '</td></tr>';
			}
			if($(this).attr('bundle') != 0)
			{
				text += '<tr class="sub_row" style="display:none;" id="bundle'+$(this).attr('prod_id')+'"><td class="no-arrow">&nbsp;</td><td colspan="6" class="no-pad"><div id="sub_row'+$(this).attr('prod_id')+'"></div></td></tr>';
			}

	})
		
	text += '</table>';
	
	if(cnt == "0")
	{
		text = '<table cellpadding="0" cellspacing="0" width="100%" border="0">';
		text += '<tr class="odd">';
		text += '<td class="tableCell" colspan="7" align="center">'+getNoResultsText()+'</td>';
		text += '</tr>';
		text += '</table>';
	}
	
	$('#searchDiv').html(text);
	$('tr.sub_row').hide(); //Set to Hide all sub rows for initial state. 
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		IEFixAll();
	}

}

function showSubRows(req)
{
	var cnt = 0;
	var text = '';
	var parent_ids = '';
	text = '<table cellpadding="0" cellspacing="0" border="0">';
	
	$(req).find('product').each(function(){
	
			parent_ids = $(this).attr('parent_ids');
			cnt = cnt + 1;
			evenOdd = cnt%2==1?'odd':'even';
			text += '\n<tr onclick="getSubRow('+$(this).attr('bundle')+',\''+$(this).attr('row_id')+'\')" id="row'+$(this).attr('row_id')+'" class="row ' + evenOdd + '">';
			if($(this).attr('arrow') == 0)
				text += '	<td class="no-arrow">&nbsp;</td>';
			else
				text += '	<td class="arrow">&nbsp;</td>';
			text += '	<td class="productrelease">'+$(this).attr('name')+'</td>';
			text += '	<td class="general">'+$(this).attr('egs')+'</td>';
			text += '	<td class="extended">'+$(this).attr('ees')+'</td>';
			text += '	<td class="selfsupport">'+$(this).attr('ess')+'</td>';
			if($(this).attr('cur_version_id') != "0")
				text+= '<td class="currentversion download_icon"><a href="http://download.novell.com/index.jsp?product_id=&search=Search&version='+$(this).attr('cur_version_id')+'">' + $(this).attr('cur_version') + '</a></td>';
			else 
				text+= '<td class="currentversion download_icon"><a href="http://download.novell.com/index.jsp?product_id=&search=Search&version='+$(this).attr('prod_id')+'">' + $(this).attr('cur_version') + '</a></td>';
			if($(this).attr('replace_prod') != "n/a" && $(this).attr('mktg_url') != "NO_URL")
				text += '<td class="replacement replace_link"><a href="'+$(this).attr('mktg_url')+'">' + $(this).attr('replace_prod') + '</a></td>';
			else
				text += '	<td class="replacement">'+$(this).attr('replace_prod')+'</td>';

			text += '</tr>';
			if($(this).attr('details') != 0 || $(this).attr('url') != 0)
			{
				text += '<tr style="display:none;" class="sub_row" id="detail'+$(this).attr('row_id')+'"><td class="note" colspan="7">';
				if($(this).attr('details') != 0)
					text +=	'	<p>'+$(this).attr('details')+'</p>';
				if($(this).attr('url') != 0)
					text +=	'	<p class="readmore"><a href="'+$(this).attr('url')+'">Read more</a></p>';
				text += '</td></tr>';
			}
			if($(this).attr('bundle') != 0)
			{
				text += '<tr class="sub_row" style="display:none;" id="bundle'+$(this).attr('row_id')+'"><td class="no-arrow">&nbsp;</td><td colspan="6" class="no-pad"><div id="sub_row'+$(this).attr('row_id')+'"></div></td></tr>'
			}

	})
		
	text += '</table>';
	
	$('#sub_row'+parent_ids).html(text);
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		IEFix(parent_ids);
	}
			
}

