$(document).ready(function(){
	
	
	$("div.search").append( "<form id=\"searchForm\" action=\"\" method=\"post\">" +
			"<input id='searchTxt' type='text' name='search' value='Search' size='15' />" +
			"<input id='searchBtn' type='submit' class='searchBtn' name='searchBtn' value=''/>" +
			"</form>");
	
	
	
	$('#searchTxt').focus(function () { 
		 $('[name=search]:input:text').val("");
	});
	
	var randNum = Math.ceil((Math.random()*(30-1))+1); 
	var imageLink = randNum+"_imageLink.txt";
	
	var summaryText ="";
	var imageSrc="";

	function summary(data){
		summaryText = data;
	}
	
	$.get(
			imageLink,
			image
	);
	
	function image(data){
		imageSrc = data;
		$("div.randomImage").append(imageSrc);
		$("#nstpRandomSummaryText").wrap("<div class=\"rsummary\"></div>");
		
		//$("div.rsummary").append("<button type='button' id='refreshBtn' value='' name='refreshBtn' class='refreshBtn'/>");
		
//		$('#refreshBtn').click(function (){
//		     window.location.reload();
//		     return false;
//		});
	}

	

	
  $("form").submit(function(){ // this is used on IE7 for return key
	  
	  $('div.searchResults').empty(); // remove the prev search results	
		$query = $('[name=search]:input:text').val();
		$.ajax({
			    url:"http://nzmuseums.co.nz?option=com_widget&task=esearch&q="+$query,
				dataType:"jsonp",
				jsonp: 'jsonp_callback',
				success:function(responseText)  {
			
						$('div.randomImage').css('display' , 'none');
						$('div.searchResults').css('display' , 'block');
						$('div.searchResults').append(responseText);		
						
						$('div.results').jCarouselLite({
							btnPrev: ".previous",
							btnNext: ".next",
							start:0,
							/*this makes it a true carousel rather than a slideshow*/
							circular: false,
							visible: 2

						});
				},
				 error:function (xhr, ajaxOptions, thrownError){
				        alert(xhr);
				        alert(ajaxOptions);
				        alert(thrownError);
		       }

		});
		
		return false;

	  
  });

	$('#searchBtn').click(function () {
		
		$('div.searchResults').empty(); // remove the prev search results	
		
		$query = $('[name=search]:input:text').val();
		$.ajax({
				url:"http://nzmuseums.co.nz?option=com_widget&task=esearch&q="+$query,
				dataType:"jsonp",
				jsonp: 'jsonp_callback',
				success:function(responseText)  {
			
						$('div.randomImage').css('display' , 'none');
						$('div.searchResults').css('display' , 'block');
						$('div.searchResults').append(responseText);		
						
						$('div.results').jCarouselLite({
							btnPrev: ".previous",
							btnNext: ".next",
							start:0,
							/*this makes it a true carousel rather than a slideshow*/
							circular: false,
							visible: 2

						});
				},
				 error:function (xhr, ajaxOptions, thrownError){
				        alert(xhr);
				        alert(ajaxOptions);
				        alert(thrownError);
		       }

		});
		
		return false;
	
	});
	
	


});	

