/*

Copyright (C) 2005 Mike Tyers, All Rights Reserved

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

For more information on this software, contact biogridadmin@gmail.com

*/

$(document).ready( function( ) {
	
	loadInitialView( 'interaction-body' );
	
	setupAdditionalDetailTooltips( );
	setupPublicationTooltips( );
	setupAssociationSummaryTooltips( );
	setupEvidenceCodeTooltips( );
	setupAnnotationDetailsTooltips( );
	
	$("#download").click( processDownloadClick );
	
	$( "#download" ).hover( 
		function( ) {
			$(this).css( {'background-color' : '#7B3B3B', 'color' : '#FFF'} );
		},
		function( ) {
			$(this).css( {'background-color' : '#EFF', 'color' : '#333'} );
		}
	);

	$( "#goProcessSummary" ).hover( 
		function( ) {
			$(this).css( {'background-color' : '#7B3B3B', 'color' : '#FFF'} );
		},
		function( ) {
			$(this).css( {'background-color' : '#DFF', 'color' : '#333'} );
		}
	);
	
	$( "#goFunctionSummary" ).hover( 
		function( ) {
			$(this).css( {'background-color' : '#7B3B3B', 'color' : '#FFF'} );
		},
		function( ) {
			$(this).css( {'background-color' : '#DFD', 'color' : '#333'} );
		}
	);
	
	$( "#goComponentSummary" ).hover( 
		function( ) {
			$(this).css( {'background-color' : '#7B3B3B', 'color' : '#FFF'} );
		},
		function( ) {
			$(this).css( {'background-color' : '#FFD', 'color' : '#333'} );
		}
	);
	
	$( "#goComponentSummary" ).click( function( ) {
		$("#goComponentTerms").slideToggle( "normal" );
		$("#goProcessTerms").hide( );
		$("#goFunctionTerms").hide( );
	});
	
	$( "#goProcessSummary" ).click( function( ) {
		$("#goProcessTerms").slideToggle( "normal" );
		$("#goComponentTerms").hide( );
		$("#goFunctionTerms").hide( );
	});
	
	$( "#goFunctionSummary" ).click( function( ) {
		$("#goFunctionTerms").slideToggle( "normal" );
		$("#goProcessTerms").hide( );
		$("#goComponentTerms").hide( );
	});
	
	$( ".viewTab" ).live( "click", function( ) {
											
		if( !$(this).hasClass( "activeTab" ) ) {
			var viewToShow = $(this).attr( "title" );
			refreshView( viewToShow, 'interaction-body', 0, '' );
	
			$( ".viewTab" ).each( function( ) {
				$(this).removeClass( 'activeTab' );
			});
			
			$(this).addClass( 'activeTab' );	
		}
		
	});
	
	$( ".viewTab" ).live( "mouseover mouseout", function( event ) {
		if( event.type == 'mouseover' ) {
			$(this).addClass( 'highlightTab' );
		} else {
			$(this).removeClass( 'highlightTab' );
		}
	});
	
});

function loadInitialView( target ) {
	
	var viewToShow = $("#displayView").val( );
	$("#" + viewToShow + "Tab").addClass( 'activeTab' );
	refreshView( viewToShow, target, 0, '' );

}

function processDownloadClick( ) {
	
	var geneID = $("#geneID").html( );
	
	window.location.href = "http://" + location.host + "/downloadgenerator.php?geneID=" + geneID;

}

function setupInitialStats( ) {
	
	var details = {
		pubCount: $("#publicationCount").val( ),
		physicalCount: $("#physicalCount").val( ),
		physicalHTPCount: $("#physicalHTPCount").val( ),
		geneticCount: $("#geneticCount").val( ),
		geneticHTPCount: $("#geneticHTPCount").val( ),
		filteredCount: $("#filteredCount").val( ),
		filteredHTPCount: $("#filteredHTPCount").val( ),
		proteinCount: $("#proteinCount").val( ) 
	};
	
	$.ajax({
		url: 'http://' + location.host + '/scripts/displayStatsAndFilters.php',
		data: details,
		type: 'POST',
		dataType: 'text',
		success: function( msg ) {
			$("#statsAndFilters").html( msg );
			setupFilterDetails( );
		}
	});
}

function setupFilterDetails( ) {
	
	$("#filterLink").fancybox({
		'hideOnOverlayClick': false,
		'hideOnContentClick': false,
		'overlayColor': '#333',
		'overlayOpacity': 0.8,
		'frameWidth': 550,
		'frameHeight': 325,
		'callbackOnShow': setupFilterButtons
	});
	
	$( ".noFilter" ).hover( 
		function( ) {
			$(this).css( {'background-color' : '#7B3B3B', 'color' : '#FFF'} );
		},
		function( ) {
			$(this).css( {'background-color' : '#EEE', 'color' : '#333'} );
		}
	);
	
	$( ".activeFilter" ).hover( 
		function( ) {
			$(this).css( {'background-color' : '#7B3B3B', 'color' : '#FFF'} );
		},
		function( ) {
			$(this).css( {'background-color' : '#EFE', 'color' : '#333'} );
		}
	);
}

function changeSort( script, target, colName ) {
	refreshView( script, target, 0, colName );
}

function refreshView( script, target, pageID, sortColumn ) {
	
	$("#statsAndFilters").html( "<img src='http://" + location.host + "/images/ajax-loader.gif' alt='BioGRID Service Loading, Please Stand By...' />" );
	$("#" + target).html( "<img src='http://" + location.host + "/images/ajax-loader.gif' alt='BioGRID Service Loading, Please Stand By...' />" );
	$("#displayView").val( script );
	
	$.ajax({
		url: 'http://' + location.host + '/views/' + script + "-view.php",
		data: {geneID: $("#geneID").html( ), page: pageID, sortcol: sortColumn },
		type: 'GET',
		dataType: 'text',
		success: function( msg ) {
			$("#" + target).html( msg );
	
			var dateVal = new Date( );
			dateVal.setTime( dateVal.getTime( ) + (3600*24*365*1000) );
			document.cookie = 'biogrid-view=' + script + '; expires=' + dateVal.toGMTString( ) + '; path=/'; 

			setupDetailsLinks( );
			setupInitialStats( );
		}
	});
	
}

function setupFilterButtons( ) {
	$(".filterButton").click( function( ) { 
		var id = $(this).attr( "id" );
		var idInfo = id.split( "-" );
		
		$.ajax({
			url: 'http://' + location.host + '/scripts/setActiveFilter.php',
			data: {filterID: idInfo[1]},
			type: 'POST',
			dataType: 'text',
			success: function( msg ) {
				
				var viewToShow = $("#displayView").val( );
				var sortColumn = "";
				if( $("#sortColumn").length > 0 ) {
					sortColumn = $("#sortColumn").val( );
				}
				
				$.fn.fancybox.close( );
				refreshView( viewToShow, "interaction-body", "0", sortColumn );
				
			}
		});
		
	});
}

function setupDetailsLinks( ) {
	$( ".associationSummary" ).click( function( ) {
		$(this).parent( ).find( ".resultTable" ).slideToggle( );
	});
}

function setupPublicationTooltips( ) {

	$(".pubLink img").live( "click", function( ) {
										  
		var idInfo = $(this).parent( ).parent( ).find( ".pubID" ).html( );
		var toTip = $(this);
		
		$.ajax({
			url: 'http://' + location.host + '/scripts/displayPublicationDetails.php',
			data: ({ pubID: idInfo }),
			type: 'POST',
			dataType: 'text',
			success: function( msg ) {
				toTip.qtip({
					content: msg,
					position: {
						corner: {
							target: 'topMiddle',
							tooltip: 'bottomMiddle'
						}
					},
					style: {
						width: 650,
						name: 'light',
						tip: true,
						color: '#333',
						border: {
							width: 3,
							radius: 8
						}
					},
					show: {
						ready: true,
						when: {
							event: "click"
						}
					}
				})
			}
		});
										  
	});
}


function setupAdditionalDetailTooltips( ) {

	$(".additionalDetails").live( "click", function( ) {
		
		var idInfo = $(this).find( ".additionalDetailInfo" ).html( );
		var toTip = $(this);
		
		$.ajax({
			url: 'http://' + location.host + '/scripts/displayAdditionalDetails.php',
			data: { ids: idInfo },
			type: 'POST',
			dataType: 'text',
			success: function( msg ) {
				toTip.qtip({
					content: msg,
					position: {
						corner: {
							target: 'topMiddle',
							tooltip: 'bottomRight'
						}
					},
					style: {
							width: 350,
							name: 'light',
							tip: true,
							color: '#333',
							border: {
									width: 3,
									radius: 8
							}
					},
					show: {
						ready: true,
						when: {
							event: "click"
						}
					}
				})
			}
		});
	});
	
}

function setupAnnotationDetailsTooltips( ) {

	$(".annotationDetails").live( "mouseover", function( ) {
		$(this).qtip({
			content: $(this).parent( ).parent( ).find( ".hiddenDefinition" ).html( ),
			position: {
				corner: {
					target: 'topRight',
					tooltip: 'bottomLeft'
				}
			},
			style: {
					width: 500,
					name: 'light',
					tip: true,
					color: '#333',
					border: {
							width: 3,
							radius: 8
					}
			},
			show: {
				ready: true
			}
		})
	});

}

function setupAssociationSummaryTooltips( ) {

	$(".associationSummaryBox").live( "mouseover", function( ) {
		$(this).qtip({
			content: $(this).find( ".associationSummaryDetails" ).html( ),
			position: {
					corner: {
							target: 'topMiddle',
							tooltip: 'bottomRight'
					}
			},
			style: {
					width: 500,
					name: 'light',
					tip: true,
					color: '#333',
					border: {
							width: 3,
							radius: 8
					}
			},
			show: {
				ready: true
			}
		})
	});
}

function setupEvidenceCodeTooltips( ) {

	$(".systemLink img").live( "click", function( ) {
		$(this).qtip({
			content: $(this).parent( ).parent( ).find(".systemDesc").html( ),
			position: {
					corner: {
							target: 'topRight',
							tooltip: 'bottomLeft'
					}
			},
			style: {
					width: 500,
					name: 'light',
					tip: true,
					color: '#333',
					border: {
							width: 3,
							radius: 8
					}
			},
			show: {
				ready: true,
				when: {
					event: "click"
				}
			}
		});
	});

}