// JavaScript Document

$(document).ready(function(){
	$("#search_button").show();
	
	// default text
	$(".searchbox").focus(function(srcc)
	{
		if ($(this).val() == $(this)[0].title)
		{
			$(this).removeClass("defaultTextActive");
			$(this).val("");
		}
	});
	
	$(".searchbox").blur(function()
	{
		if ($(this).val() == "")
		{
			$(this).addClass("defaultTextActive");
			$(this).val($(this)[0].title);
		}
	});
	
	$(".searchbox").blur();
	
	$("#search_form").submit(function() {
		$(".searchbox").each(function() {
			if($(this).val() == this.title) {
				$(this).val("");
			}
		});
	});
	// end default text
	
	
	
	$(".tooltip[title]").tooltip({		 
		tip: '#info_tooltip',
		position: 'bottom right',
		offset: [10, 0]
	}); 
	
	$("#recommended_infotip_trigger").tooltip({		 
		tip: '#recommended_infotip',
		position: 'bottom right',
		offset: [-15, -852]
	}); 
	
	
	$("#mopay_dropdown_trigger").tooltip({
		position: 'bottom right', 
		tip: '#mopay_recommended_box',
		offset: [-13, -14],
		delay: 50
	});
	
	$("#o2-recycle_dropdown_trigger").tooltip({
		position: 'bottom right', 
		tip: '#o2-recycle_recommended_box',
		offset: [-13, -14],
		delay: 50
	});
	
	$("#phone-recycle-bank_dropdown_trigger").tooltip({
		position: 'bottom right', 
		tip: '#phone-recycle-bank_recommended_box',
		offset: [-13, -14],
		delay: 50
	});
	
	$("#earth-mobile_dropdown_trigger").tooltip({
		position: 'bottom right', 
		tip: '#earth-mobile_recommended_box',
		offset: [-13, -14],
		delay: 50
	});
	
	$("#money-4-my-mobile_dropdown_trigger").tooltip({
		position: 'bottom right', 
		tip: '#money-4-my-mobile_recommended_box',
		offset: [-13, -14],
		delay: 50
	});
	
	$("#rpc-recycle_dropdown_trigger").tooltip({
		position: 'bottom right', 
		tip: '#rpc-recycle_recommended_box',
		offset: [-13, -14],
		delay: 50
	});
	
	$("#mobile-phone-xchange_dropdown_trigger").tooltip({
		position: 'bottom right', 
		tip: '#mobile-phone-xchange_recommended_box',
		offset: [-13, -14],
		delay: 50
	});

	$("#cash-4-phones_dropdown_trigger").tooltip({
		position: 'bottom right', 
		tip: '#cash-4-phones_recommended_box',
		offset: [-13, -14],
		delay: 50
	});

		
	
	$("#search_button_submit").click(function() {
		$("#search_form").submit();
		return false;
	});
					   
	$("#autoComplete").autocomplete("/search/",
	{
		autoFill: false,
		minChars: 1,
		max: 30,
		delay: 300,
		width: 410,
		scrollHeight: 350,
		selectFirst: false,
		//cacheLength: 100,
		matchContains: true,
		matchSubset: false,
		formatItem: formatItem,
		formatMatch: formatMatch,
		formatResult: formatResult
		//mustMatch: true,
	 }).result(function(event, row){
		if(row[0] != "c") {
			location.href = row[2];
		}
	});
	
	
	$('a.lrwc').fancybox({
		//'hideOnContentClick': true,
		//'speedIn'		:	200, 
		//'speedOut'	:	200, 
		//'overlayShow'	:	false,	
		'scrolling'		: 	'no',
		'titleShow'		: 	false,        		
		'autoDimensions': 	false,
		'width'         : 	500,
		'height'        : 	'auto'
	});
		
	$(".low_rating_warning_message a.close").click(function() {
		$.fancybox.close();
		return false;
	});
	
	/*
	$(".low_rating_warning_message a.continue").click(function() {
		$.fancybox.close();
	});
	*/
	
	$("a.continue").click(function() {
		$.fancybox.close();
	});
	
	// Recycler details box
	$("a.recycler_fancybox").click(function(){
		$.fancybox({ 
			'width'				: 580,
			'height'			: 550,
			'autoScale'			: false,
			'type'				: 'iframe',
			'href'				: $(this).attr("href") + '?iframe=true'
		});
		return false;
	});
	
	// Recycler details box
	var outclick_fancybox_done = false;
	$("a.outclick_fancybox_click").click(function(){
		if(outclick_fancybox_done == false) {
			$("a#outclick_fancybox_click").trigger("click");
		}
		else {
			$.fancybox.close();	
		}
		outclick_fancybox_done = true;
	});
	
	// Outclick box (forum/signup/competition message)
	$('a#outclick_fancybox_click').fancybox({
		//'hideOnContentClick': true,
		//'speedIn'		:	200, 
		//'speedOut'	:	200, 
		//'overlayShow'	:	false,	
		'scrolling'		: 	'no',
		'titleShow'		: 	false,        		
		'autoDimensions': 	false,
		'width'         : 	500,
		'height'        : 	'auto'
	});
	
	// end document ready
});


function formatItem(row) {
	var formatted = "";
	if(row[0] == "p") {
		// products or suggested products
		formatted = "<span class='ac_product'><span class='ac_image'><img src='"+row[3]+"' align='middle' class='product_image_64x64' /></span><span class='ac_text'>"+row[1]+"</span></span>";
	}
	else if(row[0] == "m") {
		// manufacturers
		formatted = "<span class='ac_manufacturer'>List all <span class='ac_match_word'>"+row[1]+"</span> products</span>";
	}
	else if(row[0] == "s") {
		// suggestion comment
		formatted = "<span class='ac_suggest'>"+row[1]+"</span>";
	}
	else {
		// comments
		formatted = "<span class='ac_comment'>"+row[1]+"</span>";
	}
	return formatted;
}

function formatMatch(row) {
	if(row[0] == "c") {
		return [""];
	}
	else {
		return row[1];
	}
}

function formatResult(row) {
	if(row[0] == "c") {
		return [""];
	}
	else {
		return row[1];	
	}
}

// Price Comparison
$("#price_comparison tr.off").mouseover(function() {
	$(this).addClass("over");
});

$("#price_comparison tr.off").mouseout(function() {
	$(this).removeClass("over");
});


$("#gold_comparison_intro_expand").click(function() {
	$("#gold_comparison_intro_text").toggle('slow');
	return false;
});

$("#gold_comparison_intro2_expand").click(function() {
	$("#gold_comparison_intro2_text").toggle('slow');
	return false;
});


