var INVSEARCH; 
if( !INVSEARCH ){ INVSEARCH = {}; }
if( !INVSEARCH.Application ){ INVSEARCH.Application = {}; }

if( typeof( jQuery ) == 'undefined' )
{
	
	alert('WARNING - JQuery core library missing!');
	
}
else
{
	
	jQuery(document).ready(function(){

		INVSEARCH.Application = {
			WEB_SERVICE: '/index.php/downloads/webservice',
			
			searchLang: "1",
			
			searchRegion: "",
			
			searchSolution: "",
			
			searchSection: "",
			
			searchOrderBy: "Date",
			
			searchResultsPerPage: 10,
			
			searchResultsStartRow: "1",
			
			initSearch: function(){
				jQuery("#SearchPanel_Region").show();
				jQuery("#SearchPanel_Solution").show();
				jQuery("#SearchPanel_Section").show();
				
				/*
				jQuery("#SearchPanel_RegionSubmit").click(function(){
					if( jQuery("#SearchPanel_RegionList").val() != null )
					{
						INVSEARCH.Application.searchRegion = jQuery("#SearchPanel_RegionList").val();
						jQuery("#SearchPanel_Region").hide();
						jQuery("#SearchPanel_RegionSelection").html( '<div style="width:180px; float:left; font-weight:bold; color:#989898">' + jQuery('#SearchPanel_RegionList :selected').text() + '*</div>' + '<div style="float:left; margin-left:20px; color:#c3cf21" class="JQUERY_ClearSelection" selPanel="Region">Clear</div><div style="clear:both; height:0px">&nbsp;</div>' );
						jQuery("#SearchPanel_RegionSelection").fadeIn(300);
					}
					return false;										 
				});
				jQuery("#SearchPanel_SolutionSubmit").click(function(){
					if( jQuery("#SearchPanel_SolutionList").val() != null )
					{
						INVSEARCH.Application.searchSolution = jQuery("#SearchPanel_SolutionList").val();
						jQuery("#SearchPanel_Solution").hide();
						jQuery("#SearchPanel_SolutionSelection").html( '<div style="width:180px; float:left; font-weight:bold; color:#989898">' + jQuery('#SearchPanel_SolutionList :selected').text() + '*</div>' + '<div style="float:left; margin-left:20px; color:#c3cf21" class="JQUERY_ClearSelection" selPanel="Solution">Clear</div><div style="clear:both; height:0px">&nbsp;</div>' );
						jQuery("#SearchPanel_SolutionSelection").fadeIn(300);
					}
					return false;										 
				});
				jQuery("#SearchPanel_SectionSubmit").click(function(){
					if( jQuery("#SearchPanel_SectionList").val() != null )
					{
						INVSEARCH.Application.searchSection = jQuery("#SearchPanel_SectionList").val();
						jQuery("#SearchPanel_Section").hide();
						jQuery("#SearchPanel_SectionSelection").html( '<div style="width:180px; float:left; font-weight:bold; color:#989898">' + jQuery('#SearchPanel_SectionList :selected').text() + '*</div>' + '<div style="float:left; margin-left:20px; color:#c3cf21" class="JQUERY_ClearSelection" selPanel="Section">Clear</div><div style="clear:both; height:0px">&nbsp;</div>' );
						jQuery("#SearchPanel_SectionSelection").fadeIn(300);
					}
					return false;										 
				});
				*/
				jQuery("#SearchPanel_RegionList").change(function(){
					INVSEARCH.Application.searchRegion = jQuery("#SearchPanel_RegionList").val();										 
				});
				jQuery("#SearchPanel_SolutionList").change(function(){
					INVSEARCH.Application.searchSolution = jQuery("#SearchPanel_SolutionList").val();										 
				});
				jQuery("#SearchPanel_SectionList").change(function(){
					INVSEARCH.Application.searchSection = jQuery("#SearchPanel_SectionList").val();										 
				});
				jQuery("#SearchResults_PerPage").change(function(){
					//alert(INVSEARCH.Application.searchResultsPerPage);
					INVSEARCH.Application.searchResultsPerPage = jQuery("#SearchResults_PerPage").val();
					
					INVSEARCH.Application.doSearch( jQuery("#Search_Keywords").val() );
				
				});
				
				jQuery(".JQUERY_ClearSelection").livequery(function(){
					jQuery(this).css('cursor','pointer');												
				});
				jQuery(".JQUERY_ClearSelection").livequery('click', function(){
					switch( jQuery(this).attr('selPanel') )
					{
						case'Region':
							INVSEARCH.Application.searchRegion = "";
							break;
						case'Solution':
							INVSEARCH.Application.searchSolution = "";
							break;
						case'Section':
							INVSEARCH.Application.searchSection = "";
							break;
						default:
							break;
					}
					jQuery("#SearchPanel_"+jQuery(this).attr('selPanel')+"Selection").hide();
					jQuery("#SearchPanel_"+jQuery(this).attr('selPanel')).fadeIn(300);
					return false;
				});
				
				jQuery("#Search_Language").change(function(){
					INVSEARCH.Application.searchLang = jQuery(this).val();						  
				});
				
				jQuery("#Search_DoSearch").css('cursor','pointer');
				jQuery("#Search_DoSearch").click(function(){
					jQuery("#Search_Alert").hide();
					INVSEARCH.Application.doSearch( jQuery("#Search_Keywords").val() );
					return false;										  
				});
				
				jQuery("#SearchPanel_SearchSubmit").click(function(){
					jQuery("#Search_Alert").hide();
					INVSEARCH.Application.doSearch( jQuery("#Search_Keywords").val() );
					return false;										  
				});
				
				jQuery(".jQuery_OrderBy").livequery(function(){
					jQuery(this).css('cursor','pointer');
					jQuery(this).hover(
						function(){
							if( jQuery(this).attr('active') == 'n' )
							{
								jQuery(this).css('background-color','#c5d02d');
							}
						},
						function(){
							if( jQuery(this).attr('active') == 'n' )
							{
								jQuery(this).css('background-color','#ffffff');
							}
						}
					);
					jQuery(this).click(function(){
						jQuery(".jQuery_OrderBy").each(function(){ jQuery(this).attr('active', 'n'); jQuery(this).css('background-color','#ffffff'); });
						jQuery(this).attr('active', 'y');
						jQuery(this).css('background-color','#c5d02d');
						INVSEARCH.Application.searchOrderBy = jQuery(this).attr('orderBy');
						INVSEARCH.Application.doSearch( jQuery("#Search_Keywords").val() );
						return false;							
					});
				});
			},
			
			doSearch: function( keywords ){
				jQuery("#SearchResultsList_Panel").html('');
				jQuery("#Search_Alert").hide();
				
				var queryString = "order=" + INVSEARCH.Application.searchOrderBy + "&perPage=" + INVSEARCH.Application.searchResultsPerPage + "&startRow=" + INVSEARCH.Application.searchResultsStartRow + "&lang=" + INVSEARCH.Application.searchLang + "&reg=" + INVSEARCH.Application.searchRegion + "&sol=" + INVSEARCH.Application.searchSolution + "&sec=" + INVSEARCH.Application.searchSection + "&keys=" + encodeURIComponent( keywords );
				jQuery.ajax({
					url: 		INVSEARCH.Application.WEB_SERVICE,
					type: 		"POST",
					cache: 		false,
					dataType:   'json',
					data: 		queryString+"&reqCall=doMediaSearch",
					success:	function( returnObj, textStatus )
								{
									if( returnObj.header.code == "200" )
									{
										jQuery("#SearchResults_ResultCount").html( returnObj.body.data.count );
										jQuery("#SearchResults_SearchPhrase").html( keywords );
										if( returnObj.body.data.results.length > 0 )
										{
											jQuery("#SearchResultsList_Panel").append( '<div style="height:20px">&nbsp;</div><div style="float:left; width:540px">Title</div><div style="float:left; width:90px">Date</div><div style="float:left; width:130px">Location</div><div style="float:left; width:105px">Download</div><div style="clear:both; height:0px">&nbsp;</div>' );
											for( var rI=0; rI<returnObj.body.data.results.length; rI++ )
											{
												
													
												var style = '';
												
												
												//alert(style);
												var downloadSection = "";
												if( returnObj.body.data.results[rI].type != "video" && returnObj.body.data.results[rI].type != "podcast" )
												{
													downloadSection = '<div'+style+'><a href="'+returnObj.body.data.results[rI].http_loc+'/'+returnObj.body.data.results[rI].hash+'.'+returnObj.body.data.results[rI].ext+'" target="_blank"><img src="images/layout/btn_download.gif" border="0" /></a></div>';
												}
												else if( returnObj.body.data.results[rI].type == "video" )
												{
													downloadSection = '<div'+style+'><a href="'+returnObj.body.data.results[rI].http_loc+'/video/'+returnObj.body.data.results[rI].hash+'"></div><div style="color:#c9d34d; text-decoration:underline; background: url(images/layout/bg-list-item.gif) no-repeat; padding-left:12px">Watch Video</div>';
												
												} else {
																							
													downloadSection = '<div'+style+'><a href="'+returnObj.body.data.results[rI].http_loc+'/podcast/'+returnObj.body.data.results[rI].hash+'"><div style="color:#c9d34d; text-decoration:underline; background: url(images/layout/bg-list-item.gif) no-repeat; padding-left:12px">Listen</div></a></div>';
												
												}
												jQuery("#SearchResultsList_Panel").append( '<div style="border-top:1px solid #bebebe; padding:24px 0px 24px 0px;'+style+'" class="searchResult"><div style="float:left; border:1px solid #c9d34d; width:42px; margin-right:18px;"><a href="'+returnObj.body.data.results[rI].http_loc+'/'+returnObj.body.data.results[rI].hash+'.'+returnObj.body.data.results[rI].ext+'" target="_blank"><img src="'+returnObj.body.data.results[rI].http_loc+'/thumbs/'+returnObj.body.data.results[rI].hash+'.'+returnObj.body.data.results[rI].thumb_ext+'" width="42" /></a></div><div style="float:left; width:460px"><div style="color:#c9d34d; margin-bottom:10px"><a href="'+returnObj.body.data.results[rI].http_loc+'/'+returnObj.body.data.results[rI].hash+'.'+returnObj.body.data.results[rI].ext+'" target="_blank">'+returnObj.body.data.results[rI].name+'</a></div><div style="color:#666666; font-size:0.929em">'+returnObj.body.data.results[rI].desc+'</div></div><div style="float:left; width:80px; color:#666666; font-size:0.929em; margin-left:18px">'+returnObj.body.data.results[rI].date+'</div><div style="float:left; width:110px; color:#666666; font-size:0.929em; margin-left:10px">'+returnObj.body.data.results[rI].region+'</div><div style="float:left; margin-left:20px">'+downloadSection+'</div><div style="clear:both; height:0px">&nbsp;</div>' );
											}
										}
										/*var totalResults = (jQuery('.searchResult').length);
										var totalVisible = (jQuery('.searchResult:visible').length);
										alert(totalVisible + ' of ' + totalResults + ' Visible');
										
										//console.log('do binding');
										jQuery('.moreResults').live("click", function(e){
											e.preventDefault();
											if((totalResults - totalVisible) > 10){
												
												for(result = 0; result < 10; result++){
												
													jQuery('.searchResult:visible:last').next('.searchResult:hidden').slideDown();	
												}
												
												
											}
										});
										
										jQuery('#SearchResultsList_Panel').append('<a href="#" title="" class="moreResults">More</a>');*/
										
									}
									
																	
									
									else
									{
										jQuery("#SearchResults_ResultCount").html( '0' );
										jQuery("#SearchResults_SearchPhrase").html( keywords );
										jQuery("#Search_Alert").html( returnObj.body.error );
										jQuery("#Search_Alert").fadeIn(300);
									}
									jQuery("#SearchResults_Panel").fadeIn(300);
								},
					error: 		function( requestOb )
								{
									// show error
								}
				});	
			}
		};
		
		// Init the search
		INVSEARCH.Application.initSearch();
  
	});
	
}