//TDO Communications 2009
//Costume Image Retrieve & Slide

//indicate max/min promoID's from tblCostumeContest09 
var top = 78;
var bottom = 56;
var browse = "yes";
var leftmost = "yes"

var scroll_speed = 1000;
var offsetVar = 608; //single image + appropriate spacing = 76

// Functions
	
function getResults(){
 
	$.get("getimages.php",{query: $("#search").val(), type: "results"}, function(data){
 
	$("#searchResultsUL").html(data);

	});
}


function slideRight(){

	if(browse == "yes"){
		var newsize =  $('#thumbs-inner').width() + offsetVar;
		$("#thumbs-inner").css({'width':newsize});
		$("#browseUL").css({'width':newsize});
	
		var pos =  $('#browseUL').position().left + offsetVar;
		$("#browseUL").animate({left: pos}, scroll_speed, 'swing');
		//bottom--;
		//decrement(bottom);
		
	}else{
		var pos =  $('#searchResultsUL').position().left + offsetVar;
		$("#searchResultsUL").animate({left: pos}, scroll_speed, 'swing');
		
	}
}

function slideLeft(){
	
	if(browse == "yes"){
		var newsize =  $('#thumbs-inner').width() + offsetVar;
		$("#thumbs-inner").css({'width':newsize});
		$("#browseUL").css({'width':newsize});
		
		var pos =  $('#browseUL').position().left - offsetVar;
		$("#browseUL").animate({left: pos}, scroll_speed, 'swing');
		top = top + 9;
		increment(top);
	}else{
		var pos =  $('#searchResultsUL').position().left - offsetVar;
		$("#searchResultsUL").animate({left: pos}, scroll_speed, 'swing');
		
	}
}



function decrement(where){
	
	$.get("getimages.php",{direction: "down", start: where, type: "increment" }, function(data){
		$("#browseUL > li:first-child").before(data);
		
	});
}

function increment(where){
	
	$.get("getimages.php",{direction: "up", start: where, type: "increment" }, function(data){
	$("#browseUL > li:last-child").after(data);

	});
}



			
$(document).ready(function() {

//Event Handlers

	$("#thumbs-inner > ul > li > a").live("click",function(e) {
		e.preventDefault();
		var pic = $(this).attr("href");
		$("#main_costume_img1").attr("src", pic);
	});

	$("#left").live("click",function(e){
		e.preventDefault();
		
		if(browse == "yes"){
			if( $('#browseUL').position().left == "40"){
				leftmost = "yes";
			}
		}
		if(browse == "no"){
			if( $('#searchResultsUL').position().left == "40"){
				leftmost = "yes";
			}
		}
		
		
		if (!$("#searchResultsUL").is(':animated') && !$("#browseUL").is(':animated') && leftmost != "yes"){
			slideRight();
		}
		
	 });

	
	$("#right").live("click",function(e){
		e.preventDefault();
		leftmost = "no";	
		if (!$("#searchResultsUL").is(':animated') && !$("#browseUL").is(':animated')){
			slideLeft();
		}
	 });


 	$("#search").keyup(function(event){
	 
		if(event.keyCode == "13"){
			browse = "no";
			getResults();
			
			if($("#browseUL").is(":visible")){
				$("#browseUL").hide("slow");
				$("#searchResultsUL").show("slow");
  			}
			
			if($("#clear-search").is(":visible")){
				//
			}else{
				$("#clear-search").show("slow");
				$("#searchResultsUL").animate({left: "40"}, 200);
			}		
		}
	 
	});
	
	$("#submitbutton").click(function(){
		browse = "no";
		getResults();
		
		if($("#browseUL").is(":visible")){
			$("#browseUL").hide("slow");
			$("#searchResultsUL").show("slow");
		}
		
		if($("#clear-search").is(":visible")){
			//
		}else{
			$("#clear-search").show("slow");
			$("#searchResultsUL").animate({left: "40"}, 200);

		}
		
	});

	$("#clear-search").live("click",function(e){
		e.preventDefault();	
		browse = "yes";
		$("#clear-search").hide("slow");
		$("#searchResultsUL").hide("slow");
		$("#browseUL").show("slow");
		$("#search").val("Enter your pet's name!");
		
	 });
		
	//live search bit - disabled temp.
	/*$("#search").keyup(function(){
	 
		$.get("getimages.php",{query: $("#search").val(), type: "count"}, function(data){
	 
		$("#buttontext").html(data + " Matching Pets");
	 
		});
	});*/

	
	 
});
