$(document).ready(doStuff);

function doStuff() {
	changeFinish();
	changeLayout();
	changeLength();
	moAddItem();
	moAddConfigItem();
	moDelItem();
	moIncDecItem();
	moChangeTran();
	moChangeDrop();
	moChangeIType();
	moChangeColor();
	moChangeLength();
	summUpdateQty();
	summDelItem();
	openEmailPopup();
	printSummary();
//	moUpdateQty();
//	configHead();
//	closeWindow();
}


/* ================================================================== */
/* ================================================================== */


function changeLayout() {
	
//	$(".length_options").hide();

	$(".layout").hover(
		function(){ $(this).addClass("layout_hover"); },
		function(){ $(this).removeClass("layout_hover"); }
	);

	$(".layout").click(function(event){

		if (!$(this).hasClass("layout_selected")) {
			$(".layout").each(function(){
				$(this).removeClass("layout_selected");
			});
			$(this).addClass("layout_selected");
	
//			$(".length_options").hide();
//			$(".length_options",this).show();
	
			var layoutId = $(this).attr("id");
			var layoutVal = 0;
			
			switch (layoutId) {
				case "layout_i": layoutVal = 1; break;
				case "layout_l": layoutVal = 2; break;
				case "layout_h": layoutVal = 3; break;
				case "layout_u": layoutVal = 4; break;
				case "layout_j": layoutVal = 5; break;
				case "layout_t": layoutVal = 6; break;
				case "layout_sq": layoutVal = 7; break;
				case "layout_s": layoutVal = 8; break;
				case "layout_o": layoutVal = 9; break;
				default: layoutVal = 0; break;
			}
	
			$.ajax({
				type: "GET",
				url: "/index.php?module=monorail_comp_tally",
	//			data: "shape_id="+layoutVal+"&length_1=0&length_2=0&length_3=0&length_4=0&length_5=0",
				data: "shape_id="+layoutVal,
				dataType: "text",
				success: function(data){
					$("#monorail_comp_tally").html(data);
				}
			}); // end $.ajax
	
	
			$.ajax({
				type: "GET",
				url: "/index.php?module=monorail_wiz_1_shape",
				data: "action=add_cat&shape_id="+layoutVal,
				dataType: "text",
				success: function(data){
					$("#layout_shape").html(data);
					changeLayout();
					changeLength();
					moDelItem();
					moIncDecItem();
				}
			}); // end $.ajax

			$.ajax({
				type: "GET",
				url: "/index.php?module=monorail_wiz_nav",
				success: function(data){
					$(".nav_bar").html(data);
					changeLayout();
					changeLength();
				}
			}); // end $.ajax


		} //if (!$(this).hasClass("layout_selected"))

	}); //$(".layout").click(function(event)
}


function changeFinish() {
	$(".finish").hover(
		function(){ $(this).addClass("finish_hover"); },
		function(){ $(this).removeClass("finish_hover"); }
	);

	$(".finish").click(function(event){
								
		$(".finish").each(function(){
			$(this).removeClass("finish_selected");
		});
		$(this).addClass("finish_selected");

	
		var finishId = $(this).attr("id");
		var finishVal = 0;
		
		switch (finishId) {
			case "finish_id_1": finishVal = 1; break;
			case "finish_id_2": finishVal = 2; break;
			case "finish_id_3": finishVal = 3; break;
			default: finishVal = 0; break;
		}

		$.ajax({
			type: "GET",
			url: "/index.php?module=monorail_comp_tally",
			data: "finish_id="+finishVal,
			dataType: "text",
			success: function(data){
				$("#monorail_comp_tally").html(data);
			}
		}); // end $.ajax

		$.ajax({
			type: "GET",
			url: "/index.php?module=monorail_wiz_1_shape",
			success: function(data){
				$("#layout_shape").html(data);
				changeLayout();
				changeLength();
			}
		}); // end $.ajax

	});
}

function changeLength() {
	$(".length").change(function(event){
		var lengthNo = $(this).attr("id");
		var lengthVal = $(this).val();

/*		if ($(this).val() > 0)
			$(this).removeClass("message_red");
		else if ($(this).val() == 0 && !$(this).hasClass("message_red"))
			$(this).addClass("message_red");*/

		$.ajax({
			type: "GET",
			url: "/index.php?module=monorail_comp_tally",
			data: lengthNo+"="+lengthVal,
			dataType: "text",
			success: function(data){
				$("#monorail_comp_tally").html(data);
				moDelItem();
				moIncDecItem();
			}
		}); // end $.ajax
		


	});
}

function escKey() {
	$(document).keyup(function(event){
		if (event.which == 27) {
			if ($("#prod_detail").css("display") == "block") {
				$("#prod_detail").hide();
				$("#loading_screen").hide();
				$("#modal_background").hide();
				return 1;
			}
		}
	});
	return 0;
}

function closeWindow() {
	$(".prod_details_window_close a").click(function(event){
		event.preventDefault();
		$("#prod_detail").hide();
		$("#loading_screen").hide();
		$("#modal_background").hide();
	});

	escKey();
}

function moHeadConfigSwapDropdownId(){
	$(".related_id").change(function(){
		$(".add_item",this).attr("id","add_item_"+$(this).val());
		$(".element_qty").attr("id","element_qty_"+$(this).val());
	});
}

function moAddItem() {
	$(".add_item").click(function(event){
		var prodId = $(this).attr("id").substr("add_item_".length);
		var prodQty = $("#element_qty_"+prodId).val();
		var catId = $("#cat_id_"+prodId).val();
		var lampId = $("#lamp_id_"+prodId).val();
		var type = null;

		$("#message_"+prodId+" div").remove();
		$("#message_"+prodId).append("<div class='message_yellow'>adding...</div>");


		$.get("/index.php?module=monorail_comp_tally", { action:"add", prod_id:prodId, prod_qty:prodQty, cat_id:catId, lamp_id:lampId, type:type },
			function(data) {
				$("#monorail_comp_tally").html(data);
				moDelItem();
				moIncDecItem();
				$("#message_"+prodId+" div").removeClass("message_yellow").addClass("message_green").html("Added").fadeOut(3000);;
			}
		);

	});
}

function moAddConfigItem() {
	$(".add_config_item").click(function(event){
		var prodId = $("#prod_id").val();
		var catId = $("#cat_id").val();
		var prodQty = $("#element_qty").val();
		var lampId = $("#lamp_id").val();
		var type = null;
		$("#monorail_head_config #message").remove();
		$("#monorail_head_config").append("<div id='message' class='message_yellow'>");
		$("#message").css({"margin-top":"10px"}).html("adding...");

		$.get("/index.php?module=monorail_comp_tally", { action:"add", prod_id:prodId, prod_qty:prodQty, cat_id:catId, lamp_id:lampId, type:type },
			function(data) {
				$("#monorail_comp_tally").html(data);
				moDelItem();
				moIncDecItem();
				$("#message").removeClass("message_yellow").addClass("message_green").html("Added").fadeOut(3000);
			}
		);

	});
}

function moDelItem() { 
	$("#monorail_comp_tally .del_item").click(function(event){
		var prodId = $(this).attr("id").substr("del_item_".length);
		var fjConnProdId = $("#fj_connector_prod_id_"+prodId).val();
		$("#remove_item_"+prodId).html("removing...").css({"color":"#f00"});

		$.get("/index.php?module=monorail_comp_tally", { action:"del", prod_id:prodId, fj_connector_prod_id:fjConnProdId },
			function(data) {
				$("#monorail_comp_tally").html(data);
				moDelItem();
				moIncDecItem();
			}
		);
	});
}

function moIncDecItem() {
	$(".dec_item, .inc_item").click(function(event){
		var action = $(this).attr("class").substr(0,3);
		var prodId = $(this).attr("id").substr("dec_item_".length);
		var qty = $("#qty_value_"+prodId).text();
		var fjConnProdId = $("#fj_connector_prod_id_"+prodId).val();
		
		if (action == "inc" || (action == "dec" && qty > 1)) {
			$.get("/index.php?module=monorail_comp_tally", { action:action, prod_id:prodId, fj_connector_prod_id:fjConnProdId },
				function(data) {
					$("#monorail_comp_tally").html(data);
					moDelItem();
					moIncDecItem();
				}
			);
		}

	});
}

function moUpdateQty(prod_id,prod_qty) { 

	var url= "../module/cart_view.php?action=3&prod_id="+prod_id+"&prod_qty="+prod_qty;

	f_state_changed("checkout_cart_top");

}

function moChangeTran(){
	$(".transformer_watt").change(function(event){
		var tranTypeId = $(this).attr("id");
		var tranWatt = $(this).val();
		var tranVolt = 12;
		var tranDrop = 1;
		var tranIType = 0;
		var tranType = 0;
		switch (tranTypeId) {
			case 't_watt': 
				$("#t_drop").val() > 0 ? tranDrop = $("#t_drop").val() : tranDrop = 1;
				$("#t_itype").val() > 0 ? tranIType = $("#t_itype").val() : tranIType = 1;
				tranType = 1; 
				break;
			case 'mt_watt': 
				$("#mt_drop").val() > 0 ? tranDrop = $("#mt_drop").val() : tranDrop = 1; 
				$("#mt_itype").val() > 0 ? tranIType = $("#mt_itype").val() : tranIType = 1;
				tranType = 2; 
				break;
			case 'mte_watt': tranType = 3; break;
		}

		$.ajax({
			type: "GET",
			url: "/index.php?module=monorail_comp_tally",
			data: "action=addtran&type="+tranType+"&watt="+tranWatt+"&volt="+tranVolt+"&drop="+tranDrop+"&itype="+tranIType,
			dataType: "text",
			success: function(data){
				$("#monorail_comp_tally").html(data);
				moDelItem();
				moIncDecItem();
			}
		}); // end $.ajax

		$.ajax({
			type: "GET",
			url: "/index.php?module=monorail_wiz_3_tran",
			success: function(data){
				$("#transformer_params").html(data);
				moChangeTran();
				moChangeDrop();
				moChangeIType();
			}
		}); // end $.ajax


	});
}



function moChangeDrop() {
	$(".transformer_drop").change(function(event){
		var tranTypeId = $(this).attr("id");
		if ($(this).val() > 0) var tranDrop = $(this).val(); else var tranDrop = 1;
		var tranWatt = 0;
		var tranVolt = 12;
		var tranIType = 1;
		var tranType = 0;
		switch (tranTypeId) {
			case 't_drop':
				if ($("#t_watt").val() == 0)
					tranWatt = $("#t_watt option:eq(1)").val();
				else
					tranWatt = $("#t_watt").val();
				tranIType = $("#t_itype").val();
				tranType = 1; 
				break;
			case 'mt_drop':
				if ($("#mt_watt").val() == 0)
					tranWatt = $("#mt_watt option:eq(1)").val();
				else
					tranWatt = $("#mt_watt").val(); 
				tranIType = $("#mt_itype").val();
				tranType = 2; 
				break;
		}

		$.ajax({
			type: "GET",
			url: "/index.php?module=monorail_comp_tally",
			data: "action=addtran&type="+tranType+"&watt="+tranWatt+"&volt="+tranVolt+"&drop="+tranDrop+"&itype="+tranIType,
			dataType: "text",
			success: function(data){
				$("#monorail_comp_tally").html(data);
				moDelItem();
				moIncDecItem();
			}
		}); // end $.ajax

		$.ajax({
			type: "GET",
			url: "/index.php?module=monorail_wiz_3_tran",
			success: function(data){
				$("#transformer_params").html(data);
				moChangeTran();
				moChangeDrop();
				moChangeIType();
			}
		}); // end $.ajax


	});
}

function moChangeIType() {
	$(".transformer_itype").change(function(event){
		var tranTypeId = $(this).attr("id");
		var tranIType = parseInt($(this).val());
		var tranDrop = 1;
		var tranWatt = 0;
		var tranVolt = 12;
		var tranType = 0;
		switch (tranTypeId) {
			case 't_itype': 
				if ($("#t_watt").val() == 0)
					tranWatt = $("#t_watt option:eq(1)").val();
				else
					tranWatt = $("#t_watt").val();
				$("#t_drop").val() > 0 ? tranDrop = $("#t_drop").val() : 1;
				tranType = 1; 
				break;
			case 'mt_itype': 
				if ($("#mt_watt").val() == 0)
					tranWatt = $("#mt_watt option:eq(1)").val();
				else
					tranWatt = $("#mt_watt").val();
				$("#mt_drop").val() > 0 ? tranDrop = $("#mt_drop").val() : 1;
				tranType = 2; 
				break;
		}

		switch (tranIType) {
			case 1: tranDrop = 1; break;
			case 2: tranDrop = 6; break;
			case 3: tranDrop = 2; break;
			case 4: tranDrop = 6; break;
		}

		$.ajax({
			type: "GET",
			url: "/index.php?module=monorail_comp_tally",
			data: "action=addtran&type="+tranType+"&watt="+tranWatt+"&volt="+tranVolt+"&drop="+tranDrop+"&itype="+tranIType,
			dataType: "text",
			success: function(data){
				$("#monorail_comp_tally").html(data);
				moDelItem();
				moIncDecItem();
			}
		}); // end $.ajax

		$.ajax({
			type: "GET",
			url: "/index.php?module=monorail_wiz_3_tran",
			success: function(data){
				$("#transformer_params").html(data);
				moChangeTran();
				moChangeDrop();
				moChangeIType();
			}
		}); // end $.ajax


	});
}

function moChangeColor() {
	$("#color_options_dropdown").change(function(event){
		var prodId = $(this).attr("value");
		var catId = $("#cat_id").attr("value");
		window.location.href = "/index.php?module=monorail_element_config&prod_id="+prodId+"&cat_id="+catId;
	});
}

function moChangeLength() {
	$("#prod_id").change(function(event){
		var prodId = $(this).attr("value");
		var catId = $("#cat_id").attr("value");
		window.location.href = "/index.php?module=monorail_element_config&prod_id="+prodId+"&cat_id="+catId;
	});
}

function summUpdateQty() {
	$("#monorail_summary .update_qty").click(function(event){
		var prodId = $(this).attr("id").substr("update_qty_".length);
		var prodQty = $("#prod_qty_"+prodId).val();
		$("#update_item_"+prodId).html("updating...").css({"color":"#999"});

		$.get("/index.php?module=monorail_summary", { action:"update", prod_id:prodId, prod_qty:prodQty },
			function(data) {
				$("#monorail_summary").html(data);
				summDelItem();
				summUpdateQty();
			}
		);
	});	
}
function summDelItem(){
	$("#monorail_summary .del_item").click(function(event){
		var prodId = $(this).attr("id").substr("del_item_".length);
		var fjConnProdId = $("#fj_connector_prod_id_"+prodId).val();
		$("#remove_item_"+prodId).html("removing...").css({"color":"#f00"});

		$.get("/index.php?module=monorail_summary", { action:"del", prod_id:prodId, fj_connector_prod_id:fjConnProdId },
			function(data) {
				$("#monorail_summary").html(data);
				summDelItem();
				summUpdateQty();
			}
		);
	});
}

function openEmailPopup() {
	$("#email_summary").click(function(event){
		event.preventDefault();
		window.open("../../module/monorail_email.php","emailsummary","width=420, height=300, toolbar=no, scrollbars=no, resizable=no, menubar=no, status=no, directories=no, location=no, screenX=200, left=350, screenY=60, top=150");
	});
}

function printSummary() {
	$("#print_summary").click(function(event){
		event.preventDefault();
		window.print();
   });
}


function configHead(prod_id) {
	$(".config_head").click(function(event){
		$("#modal_background").show();
		var position = popupBoxPosition("#loading_screen");
		$("#loading_screen").show().html("<div>Loading...</div>").css({"left":position[0],"top":position[1]});
		var prodId = $(this).attr("id").substr("config_head_".length); //works with config_him_ and config_hnam_
		
		$.ajax({
			type: "GET",
			url: "/index.php?module=monorail_head_detail_get_xml",
			data: "prod_id="+prodId,
			dataType: "xml",
			success: function(data){
				$("#loading_screen").fadeOut(500);
				rhConfigHead(data);
				overlibInit();
				changeRelated();
				var position = popupBoxPosition("#prod_detail");
				$("#prod_detail").fadeIn(500).css({"left":position[0],"top":position[1]});
				moHeadConfigSwapDropdownId();
				moAddConfigItem();
			},
			error: function(){
				$("#loading_screen").fadeOut(500);
				$("#prod_detail").html("").append("<div class='prod_loading_error'><strong>Product details currently not available</strong></div><div class='prod_loading_error'>Press ESC to exit</div>");
				var position = popupBoxPosition("#prod_detail");
				$("#prod_detail").fadeIn(500).css({"left":position[0],"top":position[1]});
			}
		});

	});
}

function overlibInit() {
	$(".pic_options a").click(function(event){
		event.preventDefault();
	});
/*	$(".pic_options a").mouseover(function(event){
		var url = $(this).attr("href");
		var str = "<img src='"+url+"' />";
		overlib(str,OFFSETX, -220, OFFSETY, -220);
	});
	$(".pic_options a").mouseout(function(event){
		nd();
	});*/
}

function changeRelated() {
	$(".related_id").change(function(event){
		var prodId = $(this).val();
		var catId = $("#cat_id_"+prodId).val();
		$(".add_config_item").attr("id","add_item_" + prodId);
	});
}


function rhConfigHead(data) {
		var xmlDoc = data;

		var root = $("prod:first",data);
		var prodId = $(root).attr("id");


		// ============================================== IMAGE
		var img = $("img:first",root);
		var spec_img = $("spec_img:first",img);
		var spec_img_inst = $("spec_img_inst1:first",img);
		var spec_img_inst2 = $("spec_img_inst2:first",img);
		var spec_draw = $("spec_draw:first",img);
		$("#product_photos").html("");
		if (img != null) {
			$("#product_photos").append("<img />");
			var alt_title = $("name:first",root).text();
			var src = "";
			spec_img.text() == "" ? src = "../image/nophoto.gif" : src = "../image/prod/medium/"+spec_img.text();
			$("#product_photos img").attr({"src":src,"alt":alt_title,"title":alt_title});

			if (spec_img_inst.text() == "" && spec_img_inst2.text() == "" && spec_draw.text() == "") {
				//do nothing	
			} else {
				$("#product_photos").append("<div id='product_photos_additional'></div>");
				$("#product_photos_additional").append("<span><strong>Additional Photos & Drawings:</strong></span>");

				if (spec_img_inst.text() != "")
					$("#product_photos_additional").append("<a><img src='../image/prod/medium/"+spec_img_inst.text()+"' class='product_photos_additional' alt='"+alt_title+" Spec Installation Image' title='"+alt_title+" Spec Installation Image' width='60' /></a>");

				if (spec_img_inst2.text() != "")
					$("#product_photos_additional").append("<a><img src='../image/prod/medium/"+spec_img_inst2.text()+"' class='product_photos_additional' alt='"+alt_title+" Spec Installation Image #2' title='"+alt_title+" Spec Installation Image #2' width='60' /></a>");

				if (spec_draw.text() != "")
					$("#product_photos_additional").append("<a><img src='../image/prod/medium/"+spec_draw.text()+"' class='product_photos_additional' alt='"+alt_title+" Spec Installation Drawing' title='"+alt_title+" Spec Installation Drawing' width='60' /></a>");
			}
		}


		// ============================================== COLORS
		var color_name = $("color_name:first",root);
		$("#color_options").html("");
		if (color_name.text() != "") {
			$("#color_options").append("<span id='color_name'><strong>Color:&nbsp;</strong></span>");
			var color_options = $("color_options:first",root);
			if (color_options != null) {
				var color_divs = "";
				$("color",color_options).each(
					function() {
						var color_image = "";
						$("img",this).text() == "" ? color_image = "noimage.png" : color_image = $("img",this).text();
						color_divs += "<div class='pic_options'><a href='../image/color_image/"+color_image+"'><img id='color_id_"+$(this).attr("id")+"' src='../image/color_image/"+color_image+"' alt='"+$("name",this).text()+"' title='"+$("name",this).text()+"' width='20' height='20'></a></div>";
					}
				);
				$("#color_options").append(color_divs);
			}
		}

		// ============================================== FINISHES
		var finish_name = $("finish_name:first",root);
		$("#finish_options").html("");
		if (finish_name.text() != "") {
			$("#finish_options").append("<span id='finish_name'><strong>Finish:&nbsp;</strong></span>");
			var finish_options = $("finish_options:first",root);
			if (finish_options != null) {
				var finish_divs = "";
				$("finish",finish_options).each(
					function() {
						var finish_image = "";
						$("img",this).text() == "" ? finish_image = "noimage.png" : finish_image = $("img",this).text();
						finish_divs += "<div class='pic_options'><a href='../image/finish_image/"+finish_image+"'><img id='finish_id_"+$(this).attr("id")+"' src='../image/finish_image/"+finish_image+"' alt='"+$("name",this).text()+"' title='"+$("name",this).text()+"' width='20' height='20' /></a></div>";
					}
				);
				$("#finish_options").append(finish_divs);
			}
		}

/*
		var specsheet = root.getElementsByTagName("specsheet")[0];
		if (specsheet != null) {
			if (specsheet.getElementsByTagName("catalog")[0].firstChild != null)
				document.getElementById("prod_spec_pdf_catalog").innerHTML = specsheet.getElementsByTagName("catalog")[0].firstChild.nodeValue;
			else
				document.getElementById("prod_spec_pdf_catalog").innerHTML = "";
			if (specsheet.getElementsByTagName("instruction")[0].firstChild != null)
				document.getElementById("prod_spec_pdf_instruction").innerHTML = specsheet.getElementsByTagName("instruction")[0].firstChild.nodeValue;
			else
				document.getElementById("prod_spec_pdf_instruction").innerHTML = "";
		}
*/

		// ============================================== SPECS TABLE
		var specifications = $("specifications:first",root);
		if (specifications != null) {
			
			var specs_table_div = $("#specs_table").html("");
			var specs_table_caption = "";
			var specs_table = "<table class='specs_table' width='320'><caption>Product Specifications</caption><tbody></tbody></table>";
			specs_table_div.append(specs_table);
			var specs_tr = "";
			function createTRNode(tag,label) {
				if ($(tag+":first",specifications).text() != "")
					specs_tr += "<tr><td class='label'>"+label+"</td><td>"+$(tag+":first",specifications).text()+"</td></tr>";
			}
		
			createTRNode("bulb_string","Bulb: ");
			createTRNode("prod_dimensions","Dimensions: ");
			createTRNode("lamp_source_name","Lamp Source: ");
			createTRNode("lumens","Lumens: ");
			createTRNode("lamp_rated_life","Average Rated Life (hrs): ");
			createTRNode("lamp_kelvin","Correlated Color Temperature CCT (K): ");
			createTRNode("lamp_cri","Color Rendering Index CRI: ");
			createTRNode("lamp_beam_angle","Beam Angle (degrees): ");
			createTRNode("lamp_cbcp","Center Beam Candle Power (CBCP): ");
			createTRNode("function_name","Function: ");
			createTRNode("ceiling_type_name","Ceiling Type: ");
			createTRNode("housing_type_name","Housing Type: ");
			createTRNode("trim_type_name","Trim Type: ");
			createTRNode("aperture_shape_name","Aperture Shape: ");
			createTRNode("aperture_size","Aperture Size: ");
			createTRNode("ship_weight","Weight: ");
			createTRNode("dimmer_name","Dimmer: ");
			createTRNode("labels","Label: ");
			createTRNode("warranty","Warranty: ");
			createTRNode("designer","Designer: ");

			$("table:first tbody",specs_table_div).append(specs_tr);
		} // end SPECS TABLE

		// ============================================== PRODUCT NAME
		var prod_name = $("name:first",root);
		if (prod_name.text() != "")
			$("#prod_name").html(prod_name.text());

		// ============================================== PRODUCT MODEL
		var prod_model = $("model:first",root);
		if (prod_model.text() != "")
			$("#prod_model").html(prod_model.text());

		// ============================================== VENDOR NAME
		var vendor = $("vendor:first",root);
		var vendor_name = $("name:first",vendor).text();
		if (vendor_name != "")
			$("#vend_name").html("by "+vendor_name)

		// ============================================== PRICING
		var price = $("price:first",root);
		var price_list = $("list:first",price).text();
		var price_retail = $("retail:first",price).text();
		var company_name = $("company_name:first",root).text();
		if (price_list != "")
			$("#prod_price_list").html("List Price: $"+price_list);
		if (price_retail != "") {
			company_name == "" ? $("#prod_price_retail_label").html("Our Price: ") : $("#prod_price_retail_label").html(company_name+" Price: ");
			$("#prod_price_retail").html("$"+price_retail);
		}

		// ============================================== ENERGYSMART LOGO
		$("#img_es").html("");
		var callouts = $("callouts:first",root);
		var energysmart = $("energysmart:first",callouts).text();
		if (energysmart != "" && energysmart == 1)
			$("#img_es").append("<img src='../image/energy_smart_lighting_logo.gif' width='410' height='20' />");

		// ============================================== BRIEF DESCRIPTION
		$("#prod_brief_desc").html("");
		var description = $("description:first",root);
		var description_brief = $("brief:first",description).text();
		if (description_brief != "") {
			description_brief += "<a href='#full_description' rel='nofollow'><strong>&nbsp;More&raquo;</strong></a>";
			$("#prod_brief_desc").html(description_brief);
		}

		// ============================================== INITIALIZE CALLOUTS DIV
		$("#callouts_img").html("");
		// ============================================== FREE SHIPPING LOGO
		if (price_retail >= 50)
			$("#callouts_img").append("<img src='../image/icon_free_shipping.gif' alt='Free Shipping' title='Free Shipping' />");

		// ============================================== TOP SELLER LOGO
		var topseller = $("topseller:first",callouts).text();
		if (topseller != "" && topseller >= 2)
			$("#callouts_img").append("<img src='../image/icon_top_seller.gif' alt='Top Seller' title='Top Seller' />");

		// ============================================== ON SALE LOGO
		var onsale = $("onsale:first",callouts).text();
		if (onsale != "" && onsale == 1)
			$("#callouts_img").append("<img src='../image/icon_on_sale.gif' alt='On Sale' title='On Sale' />");

		// ============================================== NEW PRODUCT LOGO
		var prodnew = $("new:first",callouts).text();
		if (prodnew != "" && prodnew == 1)
			$("#callouts_img").append("<img src='../image/icon_new_item.gif' alt='New Product' title='New Product' />");

		// ============================================== SPEC SHEET LINK
		//$("#specsheet").attr("href","/index.php?module=pdf_specification_sheet&prod_id="+prodId);

		// ============================================== ASK A QUESTION LINK
		//$("#askquestion").attr("href","javascript: askquestion("+prodId+")");

		// ============================================== EMAIL A FRIEND LINK
		//$("#emailfriend").attr("href","javascript: emailfriend("+prodId+")");

		// ============================================== STOCK INFO
		var qty_in_stock = $("qty_in_stock:first",root).text();
		var leadtime = $("leadtime:first",root).text();
		$("#stock").html("");
		if (qty_in_stock != "" && qty_in_stock > 0) {
			$("#stock").html("<strong>In Stock: </strong>This item is in stock & available for immediate shipment.");
		} else if (leadtime != "" && leadtime > 0) {
			$("#stock").html("<strong>Not In Stock: </strong>The delivery time for this item is "+leadtime+" days.");
		} else {
			$("#stock").html("<strong>Not In Stock</strong>");
		}
		
		// ============================================== FULL DESCRIPTION
		$("#full_description").html("");
		var description = $("description:first",root);
		var description_full = $("full:first",description).text();
		if (description_full != "") {
			$("#full_description").append("<a name='full_description'></a><h3>Product Description</h3><span>"+description_full+"</span>");
		}

		// ============================================== PRODUCT OPTIONS
		$("#monorail_head_config").html("");
		$("#monorail_head_config").append("<h3>Product Options</h3>");
		var prod_options = $("options:first",root);
		var prod_options_length = $("length:first",prod_options);
		var prod_options_length_prod = $("prod",prod_options_length);
		var prod_options_lamp = $("lamp:first",prod_options);
		var prod_options_lamp_prod = $("prod",prod_options_lamp);
		
		// ======= FORM
		$("#monorail_head_config").append("<form action='' id='add_to_cart_form_"+prodId+"' method='post'></form>");

		// ======= LENGTH
		if (prod_options_length_prod.length > 0) {
			$("#add_to_cart_form_"+prodId).append("<div class='prod_option_label'><label>Color/Length:</label></div>");
			if (prod_options_length_prod.length > 1) {		
				$("#add_to_cart_form_"+prodId).append("<div><select class='related_id' id='related_id_"+prodId+"' name='related_id'></select></div>");
				
				
				var div_str = "<div class='div_dropdown'>"; //////////////////////// TEST DIV DROPDOWN
				div_str += "<table cellpadding='0' cellspacing='0' border='0'>"; //////////////////////// TEST DIV DROPDOWN
				
				prod_options_length_prod.each(
					function() {
						var val = "";
						var colorId = $(this).attr("colorid");
						var colorName = $(this).attr("colorname");
						var colorImg = $(this).attr("colorimg");
						if (colorName != "")
							val += colorName + " / ";
						else
							val += "n/a / ";
						val += $(this).text();
						$("#add_to_cart_form_"+prodId+" select#related_id_"+prodId).append("<option value='"+$(this).attr("id")+"'>"+val+"</option>");
						var relProdId = $(this).attr("id");
						var relCatId = $(this).attr("catid");
						var input_cat_id = "<input type='hidden' class='cat_id' id='cat_id_"+relProdId+"' name='cat_id_"+relProdId+"' value='"+relCatId+"' />";
						$("#add_to_cart_form_"+prodId).append(input_cat_id);

						
						

						div_str += "<tr>";
						div_str += "<td>";
						div_str += "<img id='color_id_"+colorId+"' src='../image/color_image/"+colorImg+"' alt='"+colorName+"' title='"+colorName+"' width='20' height='20' />";
						div_str += "</td>";
						div_str += "<td>";
						div_str += "<span>"+colorName+"</span>";
						div_str += "</td>";
						div_str += "</tr>";


						
						
					}
				);
				
				div_str += "</table>"; //////////////////////// TEST DIV DROPDOWN
				div_str += "</div>"; //////////////////////// TEST DIV DROPDOWN
				$("#add_to_cart_form_"+prodId).append(div_str); //////////////////////// TEST DIV DROPDOWN
				
			} else if (prod_options_length_prod.length == 1) {
				var val = "";
				if (prod_options_length_prod.attr("colorname") != "")
					val += prod_options_length_prod.attr("colorname") + " / ";
				else
					val += "n/a / ";
				val += prod_options_length_prod.text();
				$("#add_to_cart_form_"+prodId).append("<div class='prod_option'><span class='related_id' id='related_id_"+prodId+"' name='related_id'>"+val+"</span></div>");
			}
		}

		// ======= LAMP
		if (prod_options_lamp_prod.length > 0) {
			$("#add_to_cart_form_"+prodId).append("<div class='prod_option_label'><label>Lamp:</label></div>");
			$("#add_to_cart_form_"+prodId).append("<div><select class='lamp_id' id='lamp_id_"+prodId+"' name='lamp_id'></select></div>");
			$("#add_to_cart_form_"+prodId+" select#lamp_id_"+prodId).append("<option value=''>--Optional--</option>");
			var lamp_select_options = "";
			prod_options_lamp_prod.each(
				function() {
					lamp_select_options += "<option value='"+$(this).attr("id")+"'>"+$(this).text()+"</option>";
				}
			);
			$("#add_to_cart_form_"+prodId+" select#lamp_id_"+prodId).append(lamp_select_options);
		}


		// ======= TYPE
/*		var label_tag = document.createElement("label");
		label_tag.appendChild(document.createTextNode("Type:"));
		var br_tag = document.createElement("br");
		label_tag.appendChild(br_tag);

		var type_box = document.createElement("input");
		type_box.setAttribute("type","text");
		type_box.setAttribute("id","type_"+root.getAttribute("id"));
		type_box.setAttribute("name","type");
		type_box.setAttribute("value","");
		label_tag.appendChild(type_box);
		var br_tag = document.createElement("br");
		label_tag.appendChild(br_tag);
		document.getElementById("add_to_cart_form_"+root.getAttribute("id")).appendChild(label_tag);*/
		

		// ======= QUANTITY
		$("#add_to_cart_form_"+prodId).append("<div class='prod_option_label'><label>Quantity:</label></div>");
//		var a_img_add_to_cart = "<a class='add_item' id='add_item_"+prodId+"'><img src='../image/add_button.gif' width='16' height='16' alt='' /></a>";
//		var input_cat_id = "<input type='hidden' class='cat_id' id='cat_id_"+$v_prod_id+"' name='cat_id_"+$v_prod_id+"' value='"+$v_cat_id+"' />";
		var input_img_add_to_cart = "<input type='button' class='add_config_item' id='add_item_"+prodId+"' name='add_item' value='Add' />";
		$("#add_to_cart_form_"+prodId).append("<div><input type='text' class='element_qty' id='element_qty_"+prodId+"' name='element_qty_"+prodId+"' value='1' size='2' maxlength='2' />&nbsp;"+input_img_add_to_cart+"</div>");

} // rhConfigHead



function f_prod_detail_system(v_prod_id,width,height) {
	window.open("index.php?module=prod_detail_system&prod_id="+v_prod_id,"askquestion","width="+width+", height="+height+", toolbar=no, scrollbars=no, resizable=yes, menubar=yes, status=yes, directories=no, location=no, screenX=200, left=350, screenY=60, top=150");
}

function redirect(jumplocation) {
	self.location=jumplocation;
}

/*
function refreshManufacturer() {
	$("#manufacturer").change(function(event){ alert("");
		var _URL_HTTP = "http://"+document.domain;
		var vendorId = $("#vend_id").val();
		redirect(_URL_HTTP + "/index.php?module=vendor&vend_id=" + vendorId);
	});
}
function refreshColor() {
	
}
function refreshWattage() {
	
}
*/

/* ============================================================================================================================== */
function browserWidth() {
	return window.innerWidth != null ? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null ? document.body.clientWidth:null;
}
function browserHeight() {
	return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
}
function popupBoxPosition(divName) {
	var browser_w = browserWidth();
	var browser_h = browserHeight();
	var box_w = $(divName).outerWidth();
	var box_h = $(divName).outerHeight(); //alert(box_h);
	if (box_h > 100 && box_h < 580) box_h = 580;
	var left = Math.round((browser_w - box_w) / 2);
	var top = Math.round((browser_h - box_h) / 2);
	if (top < 0) top = 0;
	if (left < 0) left = 0;
//	if (top < 300 && box_h > 100) top = 30;
	var position = new Array(left,top);
	return position;
}

