
var defaultSearchVal = "Enter your product name, brand, or model here";
function searchTabClicked(_element) {
	document.getElementById('search_all').className = "";
	document.getElementById('search_recycling').className = "";
	document.getElementById('search_offers').className = "";
	_element.className = "current";
}
function search() {
	var searchText = $('#search_input').val();
	if (searchText == defaultSearchVal) {
		searchText = '';
	}
	window.location.href = "products/search.htm?query=" + escape(searchText);
}

//js block for the default search text on focus and blur
var active_color = '#000'; // Colour of user provided text
var inactive_color = '#858f86'; // Colour of default text


function initHomePage() {
	
	$('#footer_share_toolbox').hide();
	getRecentOffersAjaxCall();

	//defaultSearchVal =$('#search_input').val();
	defaultSearchVal = "Enter your product name, brand, or model here";
	$("input.as-input").css("color", inactive_color);
	var default_values = new Array();
	$("input.as-input").focus( function() {
		$("#search_tooltip").hide();
		if (!default_values[this.id]) {
			default_values[this.id] = this.value;
		}
		if (this.value == default_values[this.id]) {
			this.value = '';
			this.style.color = active_color;
		}
		$(this).blur( function() {
			if (this.value == '') {
				this.style.color = inactive_color;
				this.value = default_values[this.id];
			}
		});
	});
}

function getRecentOffersAjaxCall() {
	$.ajax( {
		type : "POST",
		url : "getRecentOffers.htm",
		dataType : "json",
		data : "",
		success : getRecentOffersResponse,
		error : function(xhr, ajaxOptions, thrownError) {
			//alert('debug: ' + xhr.status);
			//alert('debug: ' + thrownError);
	}
	});
}

function getRecentOffersResponse(data) {
	var length = data.offers.length;

	var limitOffers = [];
	for (var i=0;i<length;i++) {
		if (data.offers[i] != null) 
			limitOffers.push(data.offers[i]);
	}
	var length = limitOffers.length;
	
	var divItemId = 0; // for keeping custom rollover div in sync with item div
	$.each(
					// data.offers,
					limitOffers,
					function(i, offer) {
						var image_height = '85px';
						var image_url = offer.image_url;
						var offer_value = offer.offer_value;
						var offer_id = offer.offer_id;
						var offer_category = offer.offer_category;
						var offer_type = offer.offer_type;
						var condition_sub = offer.condition_sub;
						if (condition_sub == null) {
							condition_sub = "";
						}
						var info_link = offer.info_link;
						var product_name = offer.product_name;
						var partner_id = offer.partner_id;
						var partner_name = offer.partner_name;
						var partner_logo = offer.partner_logo;
						var html_str = '<div class="carousel_item'
								+ ' itemId'
								+ divItemId
								+ '">'
								+ '<div class="item_img"><a href="products/'
								+ info_link
								+ '"><img height="' + image_height + '" src="'
								+ image_url
								+ '" title="'
								+ product_name
								+ '"/></a></div>'
								+ '<div><span class="bigfont">'
								+ offer_value
								+ '</span></div>'
								+ '<div><span class="smallfont">'
								+ offer_type + '</span></div>' + '</div>';
						$("#carousel_items_container").append(html_str);
						if (i < length - 1) {
							html_str = '<div class="carousel_border"></div>';
							$("#carousel_items_container").append(html_str);
						}

						var marginLeft = divItemId * 108;
						var rollover_html = '<div id="hover" class="carousel_hover hover'
								+ ' itemId'
								+ divItemId
								+ '" style="margin-left:'
								+ marginLeft
								+ 'px;">'
								+ '<div class="condition">'
								+ condition_sub
								+ '</div>'
								+ '<div class="model">'
								+ product_name
								+ ' </div>'
								+ '<div><a href="'
								+ info_link
								+ '"><img height="70px" src="'
								+ image_url
								+ '"/></a></div>'
								+ '<div><span class="bigfont" style="clear: both;">'
								+ offer_value
								+ '</span></div>'
								+ '<div><span class="smallfont" style="padding-bottom: 5px;clear: both;">'
								+ offer_type + '</span></div>';
						if (partner_logo == null) {
							rollover_html += '<div class="service">' + partner_name + '</div>';
						} else {
							rollover_html += '<div style="padding-top: 1px;"><img height="27px" src="' + partner_logo + '"/></div>';
						}
						rollover_html += '</div>';
						$("#carousel_hovers_container").append(
								rollover_html);
						divItemId++;
					});

	$(".carousel_item").mouseenter( function() {
		var divItemId = $(this).attr('class').match(/itemId.+/);
		var targHover = '#carousel_hovers_container .' + divItemId;

		$(".carousel_hover").each( function() {
			if ($(this) != $(targHover))
				$(this).hide();
		});

		$(targHover).show();
		
		$(targHover).mouseleave( function(event) {
			$("#carousel_hovers_container").children().hide();
			$("#carousel_hovers_container").css('left', '-1000px');
		});
		$("#carousel_hovers_container").css('left', '0px');
	}

	);

}


