$(document).ready(function(){
	// open and close toggle content
	$(".togglebtn").click(function(){
		var myid = $(this).attr('id');
		var nextSibID = "#"+$(this).parent().next().attr('id');
		$(nextSibID).slideToggle("normal", function(){
			toggleBlockIcon(myid);
		});
		return false;
	});
});

// tech popup links
function techInfo(techid,techcat,gender){
	var object = "#techbtn-"+techid;
	var offset = $("#buyit").offset();
	$(".tech-info-popup").remove();
	$("body").prepend("<div class=\"tech-info-popup tech-info-popup-"+gender+"\" style=\"display:none;left:"+(offset.left-5)+"px;top:"+(offset.top-10)+"px;\"><span style=\"font-size:10px;\">loading tech information...</span></div>");
	$(".tech-info-popup").show("normal");
	$.ajax({
		url: "/includes/ajax_techinfo.php?techid="+techid+"&techcat="+techcat,
		cache: true,
		success: function(html){
			$(".tech-info-popup").html(html);
		}
	});
};

// tech popup links
function techInfoCompare(techid,techcat,gender,num){
	//var object = "#techbtn-"+techid;
	var object = "#compare-product-"+num;
	var offset = $(object).offset();
	$(".tech-info-popup").remove();
	$("body").prepend("<div class=\"tech-info-popup tech-info-popup-"+gender+"\" style=\"display:none;left:"+(offset.left-25)+"px;top:"+(offset.top+100)+"px;\"><span style=\"font-size:10px;\">loading tech information...</span></div>");
	$(".tech-info-popup").show("normal");
	$.ajax({
		url: "/includes/ajax_techinfo.php?techid="+techid+"&techcat="+techcat,
		cache: true,
		success: function(html){
			$(".tech-info-popup").html(html);
		}
	});
};

// tech popup links
function ajaxBuyIt(productid,materialid,colorcode,category,num){
	//var object = "#buyit-"+productid;
	var object = "#compare-product-"+num;
	var offset = $(object).offset();
	$(".tech-info-popup").remove();
	$("body").prepend("<div class=\"tech-info-popup tech-info-popup-men\" style=\"display:none;left:"+(offset.left-25)+"px;top:"+(offset.top+100)+"px;\"><span style=\"font-size:10px;\">loading stock information...</span></div>");
	$(".tech-info-popup").show("normal");
	$.ajax({
		url: "/includes/ajax_buyit.php?productid="+productid+"&materialid="+materialid+"&colorcode="+colorcode+"&category="+category,
		cache: true,
		success: function(html){
			$(".tech-info-popup").html(html);
		}
	});
};

// toggle collapsable content block icons (+/-)
function toggleBlockIcon(button){
	if (document.getElementById(button).innerHTML == "+"){
		document.getElementById(button).innerHTML = "-";
	} else {
		document.getElementById(button).innerHTML = "+";
	}
}

function replaceImg(id, src){
	var img = document.getElementById(id);
	var i = img.cloneNode(true);
	i.setAttribute("src", src);
	var parent = img.parentNode;
	parent.replaceChild(i, img);
}

// this function is need to work around 
// a bug in IE related to element attributes
function hasClass(obj){
	var result = false;
	if (obj.getAttributeNode("class") != null){
		result = obj.getAttributeNode("class").value;
	}
	return result;
}

function stripe(id){
	var even = false;
	var evenColor = arguments[1] ? arguments[1] : "#FFFFFF";
	var oddColor = arguments[2] ? arguments[2] : "#E8E8E8";
	var table = document.getElementById(id);
	if (! table) { return; }
		var tbodies = table.getElementsByTagName("tbody");
		for (var h = 0; h < tbodies.length; h++) {
			var trs = tbodies[h].getElementsByTagName("tr");
			for (var i = 0; i < trs.length; i++) {
				// avoid rows that have a class attribute
				// or backgroundColor style
			//	if (!hasClass(trs[i]) && ! trs[i].style.backgroundColor) {
					var tds = trs[i].getElementsByTagName("td");
					for (var j = 0; j < tds.length; j++) {
						var mytd = tds[j];
						// avoid cells that have a class attribute
						// or backgroundColor style
					//	if (! hasClass(mytd) && ! mytd.style.backgroundColor) {
							mytd.style.backgroundColor = even ? evenColor : oddColor;
					//	}
					}
			//	}
			even =  ! even;
		}
	}
}