/*
	
	Right Review
	
	Designed & Developed by Systemagic
	www.systemagic.co.uk
	
*/

$(document).bind("ready", function(){
				   
	$('body').addClass("javascript");	

	$('a.lightbox').lightBox();
	
	$('.clickable').bind("click", function(){
		window.location = $(this).find("a[rel=click]").attr("href");
	});
	
	$('div.search form').bind("submit", function(event){
		if($('.autocomplete').css("display") == "block"){
			$('div.search input#type').val($('.autocomplete li.current').html().replace(/<\/{0,1}strong>/gi, "")).addClass("filled");
			$('.autocomplete').slideUp(200);
			$(document).unbind("click");
			
			event.preventDefault();
			event.stopPropagation();
			return false;
		}
	});
	
	$('div.search input#type').attr("revert", "builder, plumber etc");
	$('div.search input#location').attr("revert", "city, town or postcode");
	
	$('div.search input[type=text]').bind("focus", function(){
		if($(this).val() == $(this).attr("revert")){
			$(this).val("");
		}
		
		$(this).addClass("filled");
	});
	
	$('div.search input[type=text]').bind("blur", function(){
		if($(this).val() == ""){
			$(this).val($(this).attr("revert")).removeClass("filled");
		}
	});
	
	$('div.search input[type=text]').each(function(){
		if($(this).val() == ""){
			$(this).val($(this).attr("revert"));
		}else{
			$(this).addClass("filled");
		}
	});
	
	$('div.search input#type').bind("keydown", function(event){
		if(event.keyCode == 9 && $('.autocomplete').css("display") == "block"){
			$('.autocomplete').slideUp(200);
			$(document).unbind("click");
		}
	});
	
	$('a[href=#autocomplete],div.search input#type').bind("click keyup focus", function(event){
		
		if(event.keyCode){
			
			action = false;
			
			if(event.keyCode == 38){
				action = "up";
			}else if(event.keyCode == 40){
				action = "down";
			}else if(event.keyCode == 13){
				action = "enter";
			}
			
			if(action == "up"){
				$('.autocomplete').getCurrentAndPrevious();
			}else if(action == "down"){
				$('.autocomplete').getCurrentAndNext();
			}else if(action == "enter" && $('.autocomplete').css("display") == "block"){
				$('div.search input#type').val($('.autocomplete li.current').html().replace(/<\/{0,1}strong>/gi, "")).addClass("filled");
				$('.autocomplete').slideUp(200);
				$(document).unbind("click");
			}else if(action == "enter" && $('.autocomplete').css("display") != "block"){
				return;
			}
			
			if(action){
				offset = $('li.current').position();
				$('.autocomplete').scrollTop(offset.top);
				event.preventDefault();
				event.stopPropagation();
				return false;
			}
			
			html = "<ul style=\"position:relative;\">";
			
			for(i=0;i<categories.length;i++){
				
				if(categories[i].toLowerCase().indexOf($('div.search input#type').val().toLowerCase()) >= 0){
					html += "<li>" + categories[i].replace(new RegExp('('+$('div.search input#type').val().toLowerCase()+')', 'gi'), "<strong>$1</strong>") + "</li>";
				}else if($('div.search input#type').val() == "" || $('div.search input#type').val() == $('div.search input#type').attr("revert")){
					var re = new RegExp($('div.search input#type').val().toLowerCase(), 'gi');
					html += "<li>" + categories[i].replace(new RegExp('('+$('div.search input#type').val().toLowerCase()+')', 'gi'), "<strong>$1</strong>") + "</li>";
				}
	
			}
			
			html += "</ul>";
		
		}else{
			
			html = "<ul style=\"position:relative;\">";
			
			for(i=0;i<categories.length;i++){
				
				html += "<li>" + categories[i].replace(new RegExp('('+$('div.search input#type').val().toLowerCase()+')', 'gi'), "<strong>$1</strong>") + "</li>";
	
			}
			
			html += "</ul>";
			
		}
		

		
		if(html.length > 40){	
			$('.autocomplete').html(html).slideDown(200);
			
			$('.autocomplete li:first').addClass("current");
			
			$(document).bind("click", function(event){
				if(event && !$(event.target).closest('.autocomplete').length && !$(event.target).closest('a[href=#autocomplete]').length && !$(event.target).closest('div.search input#type').length){
					$('.autocomplete').slideUp(200);
					$(document).unbind("click");
				}
			});
			
			$('.autocomplete li').bind("click", function(){
				$('div.search input#type').val($(this).html().replace("&amp;", "&").replace(/<\/{0,1}strong>/gi, "")).addClass("filled");
				$('.autocomplete').slideUp(200);
				$(document).unbind("click");
			});
		}else{
			$('.autocomplete').html("").slideUp(0);
		}

		if(!event.keyCode){
	//		Clicked on dropdown button, dont shop #autocomplete in the address bar
			return false;
		}
	});
	
	
	$('div.search input').bind("focus", function(){
		$('div.'+ $(this).attr("id") +'-results').slideDown(500);
	});
	
	$('div.search input').bind("blur", function(){
		$('div.'+ $(this).attr("id") +'-results').slideUp(500);
	});
	
	$('div.type-results li, div.location-results li').live("click", function(){
		$('input#'+ $(this).parent().parent().attr("class").replace("-results", "")).val($(this).html()).addClass("filled");
	});
	
	$('div.scores div.stars span').bind("click", function(){
		stars  = $(this).parent();
		select = $(this).parent().parent().find("select");
		value  = $(this).attr("title");
		
		$(stars).removeClass("score-"+$(select).val());
		
		$(select).val(value);
		
		$(stars).addClass("score-"+$(select).val());
	});
	
	$('.item-inner .details *:not(a)').bind("click", function(){
		window.location.href = $(this).closest(".details").find("a:first").attr("href");
	});
	
	$('.item-inner .categories').bind("click", function(){
		window.location.href = $(this).parent().find("a:first").attr("href");
	});
	
	/****************************************************************************************** Repeat Regions */
	
	repeatSelectChange = function(){
		parental = $(this).closest(".repeatable").parent();
		$(parental).find("input:first").val($(parental).getValues());
	}
	
	$('.repeatable select').bind("change", repeatSelectChange);
	$('.repeatable input').bind("keyup", repeatSelectChange);
	
	$('.add-repeat').live("click", function(){
		parental = $(this).closest(".repeatable").parent();
		repeat = $(this).closest(".repeatable");
		clone = $(repeat).clone();
		$(parental).append(clone);
		$(clone).slideUp(0).slideDown(200);
		
		$(parental).find("input:first").val($(parental).getValues());
		
		$('.repeatable select').unbind("change");
		$('.repeatable input').unbind("keyup");
		$('.repeatable select').bind("change", repeatSelectChange);
		$('.repeatable input').bind("keyup", repeatSelectChange);
	});
	
	$('.remove-repeat').live("click", function(){
		if($(this).closest(".repeatable").parent().find(".repeatable").length <= 1){
			parental = $(this).closest(".repeatable").parent().parent();
			$(this).closest(".repeatable").find("select").val(0);
			$(this).closest(".repeatable").find("input").val("");
			$(parental).find("input:first").val(0);
			return;
		}else{							   
			$(this).closest(".repeatable").slideUp(200, function(){
				parental = $(this).parent();
				$(this).remove();
				$(parental).find("input:first").val($(parental).getValues());
			});
		}
	});
	
	jQuery.fn.getValues = function(){
		str = "";
		$(this).find(".repeatable:not(:last)").find(".add-repeat").fadeOut(50);
		$(this).find(".repeatable:last").find(".add-repeat").fadeIn(0);
		$(this).find("select, input[type=text]").each(function(){
			if($(this).find("option:selected")){
				str += $(this).val() + '^';
			}else{
				str += $(this).find("option:selected").attr("value") + '^';
			}
		});
		
		return str;
	};
	
	$($(".repeatable").parent()).getValues()

	
});
	
jQuery.fn.extend({

	getCurrentAndNext: function(){
		current = $(this).find("li.current");
		
		len  = $(this).find("li.current").next().length;
		if(len >= 1){
			next = $(this).find("li.current").next();
		}else{
			next = $(this).find("li:first");
		}
		
		$(current).removeClass("current");
		$(next).addClass("current");
		
		return [current, next];
	},
	
	getCurrentAndPrevious: function(){
		current = $(this).find("li.current");
		
		len  = $(this).find("li.current").prev().length;
		if(len >= 1){
			prev = $(this).find("li.current").prev();
		}else{
			prev = $(this).find("li:last");
		}
		
		$(current).removeClass("current");
		$(prev).addClass("current");
		
		return [current, prev];
	}
	
});
