$(document).ready(function () {
	$("#feature").hide();
	var feature_array = features.split("|");
	$.get("scpt/get_home_slide_info.php",{ ID: feature_array[ticker] }, function(item_data) {
		//alert(item_data);
		$("#feature").html(build_feat(item_data));
		$("#feature").fadeIn('150',function() { 
			$("#feature").find(".add").click(function() { add_to_cart($(this)); });
			$("#feature").find(".view").click(function() { view_cart(); });
			$("#feature").find(".feature_click").click(function() {
				show_item(feature_array[ticker]); 
			});
			slide_timer = setTimeout("swap_feature()",delay);
			timer_is_on = 1;
		});
	});
	
	$("#feature_pause").click(function() {
		if (timer_is_on) {
			clearTimeout(slide_timer); timer_is_on = 0; $("#timer_paused").fadeIn("fast"); 
		} else {
			swap_feature(); $("#timer_paused").fadeOut("fast");
		}
	});
	$("#feature_next").click(function() {
		clearTimeout(slide_timer); swap_feature(); $("#timer_paused").fadeOut("fast");
	});
	$("#feature_back").click(function() {
		clearTimeout(slide_timer); 
		ticker -= 2;
		if (ticker < -1) {
			ticker = feature_array.length - 2;
		}
		swap_feature(); $("#timer_paused").fadeOut("fast");
	});
});

function swap_feature() {
	if (ticker < feature_array.length - 1) {
		ticker++;
	} else {
		ticker = 0;
	}
	$.get("scpt/get_home_slide_info.php",{ ID: feature_array[ticker] }, function(item_data) {
		//alert(item_data);
		$("#feature").fadeOut('150',function() {
			$("#feature").html(build_feat(item_data));
			$("#feature").fadeIn("150",function() { 
				$("#feature").find(".add").click(function() { add_to_cart($(this)); });
				$("#feature").find(".view").click(function() { view_cart(); });
				$("#feature").find(".feature_click").click(function() { 
					show_item(feature_array[ticker]); 
				});
				slide_timer = setTimeout("swap_feature()",delay);
				timer_is_on = 1;
			});
		});
	});
}

function build_feat(item_data) {
	var data_array = item_data.split("|");
	var items = {};
	$.each(data_array,function(key, value){
		var pair = value.split("=");
		items[pair[0]] = pair[1];
	});
	feat = "";
	feat += "<div class='feature_click' style='padding: 9px 0 0 12px; color: #fff; font-size: 21px; height: 24px; overflow: hidden;'>" + items['title'] + "</div>";
	//photo
	feat += "<table cellpadding='6' cellspacing='0' width='100%' height='230'><tr>";
	feat += "<td align='center' class='feature_click'><div style='position: relative;'>";
	
	switch (items["cat"]) {
	case "RSR":
		feat += "<img src='pixrsr/" + items["product_id"].substr(0,1) + "/" + items["product_id"] + "_1.jpg' height='" + items["height"] + "' width='" + items["width"] + "' alt='" + items["title"] + "' />";
		break;
	case "DVD":
		feat += "<img src='pixdvd/" + items["product_id"].substr(0,1) + "/" + items["product_id"] + "_1.jpg' height='" + items["height"] + "' width='" + items["width"] + "' alt='" + items["title"] + "' />";
		break;
	case "BRS":
		feat += "<img src='pixbrs/" + items["product_id"].substr(0,2) + "/" + items["product_id"].substr(2,2) + "/" + items["product_id"] + "_1.jpg' height='" + items["height"] + "' width='" + items["width"] + "' alt='" + items["title"] + "' />";

		//alert(items["left"]);
		feat += "<div style='position: absolute; bottom: -1px; left: " + items["left"] + ";'><img src='tmpl/spec_med.gif' /></div>";
		
		break;
	default:
		feat += "<img src='pixstore/" + items["product_id"] + "/" + items["photo"] + "' height='" + items["height"] + "' width='" + items["width"] + "' alt='" + items["title"] + "' />";
		break;
	}
	
	feat += "</div></td><td width='200' valign='bottom'>";
	feat += "<b>Shop more:</b>";
	if (items["mfg_name"].length > 0) {
		var mfg_link = items["mfg_name"].replace(" / ","").replace(" ","-").replace("&","-");
		feat += "<div style='margin: 6px 0;'><a href='?m=" + items["mfg"] + "::::" + mfg_link + "'>&rarr; " + items["mfg_name"] + "</div>";
	}
	var dept_link = items["dept_name"].replace(" / ","").replace(" ","-").replace("&","-");
	var aisle_link = items["aisle_name"].replace(" / ","").replace(" ","-").replace("&","-");
	feat += "<div style='margin: 6px 0'><a href='?a=" + items["aisle"] + ":::::" + dept_link + ":" + aisle_link + "'>&rarr; " + items["dept_name"] + " : " + items["aisle_name"] + "</a></div>";
	
	feat += "<div style='margin: 14px 0 14px 0;'>Item ID:&nbsp;<b>" + items["cat"].charAt(0) + "-" + items["product_id"] + "</b>";
	if (parseFloat(items["quantity"]) == 0) {
		feat += "<div>Call for availability</div>";
	}
	//feat += "</div>";
	feat += "</div>";
	if (parseFloat(items["retail_price"]) > 0) {
		feat += "<div style='margin: 0 0 6px 0;'>MSRP:&nbsp;$" + comma_formatted(items["retail_price"]) + "</div>";
	}
	if (parseFloat(items["sale"]) > 0) { 
		feat += "<div style='margin: 0 0 6px 0;'>REGULAR PRICE:&nbsp;$" + comma_formatted(items["price"]) + "</div>";
	}
	feat += "<div style='margin: 0 0 12px 0; font-size: 21px;'>";
	if (parseFloat(items["sale"]) > 0) {
		feat += "<font color='#ab2d2c'>Sale!</font> $" + comma_formatted(items["sale"]);
	} else {
		feat += "$" + comma_formatted(items["button_price"]);
	}
	feat += "</div>";
	if (parseFloat(items["quantity"]) > 0) {
		if (items["in_cart"] == "yes") {
			feat += "<input type='image' src='tmpl/button_view_cart.gif' class='view' />"; 
		} else {
			feat += "<input type='image' src='tmpl/button_add_cart.gif' item='" + items["ID"] + "' cat='" + items["cat"] + "' price='" + items["button_price"] + "' class='add' />";
		}
	}
	feat += "</td></tr></table>";
	return(feat);	
}

function clean_name(s) {
	var n = "";
	var bad = ",/#&.:!";
	var c;
	for (i = 0; i < s.length; i++) {
		c = s.substr(i,1);
		if (c == " ") {
			n = n + "-";
		} else {
			if (bad.indexOf(c) == -1) {
				n = n + s.substr(i,1);
			}
		}
	}
	return(n);
}

function comma_formatted(amount) {
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	if (a.length > 1) {
		var d = a[1];
	} else {
		var d = "00";
	}
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function show_news() {
	$("#item_details_div").html("<div style='margin: 250px 0 0 0; text-align: center;'><img src='tmpl/ajax-loader.gif' /></div>");
	$.fancybox($("#item_details_div"),{'overlayOpacity':0.7,'overlayColor':'#000000'});
	$("#item_details_div").load("scpt/get_news.php");
}
