$(document).ready(function() {

	/* ENLARGED IMAGE */
	$("#enlarged_view").hide();
	
	$(".img_enlarge").click(function() {
		$("#enlarged_view").show();
	});
	
	$(".close_enlarge").click(function() {
		$("#enlarged_view").hide();
	});
	
	$("#more_info_tabs dt").eq(0).addClass("tab_here");
	$("#more_info_tabs dd").eq(0).addClass("tc_here");
	
	
	
	/* general layer display handlers */
	$(".spawn_layer").click(function() {
		var tgt = $(this).attr("href");
		$(tgt).show();
		$("body").addClass("hide_ui");
		
		$(tgt).draggable({
			handle:".header",
			cursor:"move",
			containment: "document"
		});
		
		
		return false;
	});
	
	$(".close_layer").click(function() {
		var tgt = $(this).attr("href");
		$(tgt).hide();
		$("body").removeClass("hide_ui");
		return false;
	});
	
	
	
	/* PRODUCT TABS */
	$("#more_info_tabs dt.tab").click(function() {
		$("#more_info_tabs dt.tab").removeClass("tab_here");
		$(this).addClass("tab_here");
		
		var tgt = $(this).attr("target");
		$("#more_info_tabs dd.tc").hide();
		$("#"+tgt).show();
	});
	
	
	/* POPUPS */
	$("a.popup_full").click(function() {
		var tgt = $(this).attr("href");
		window.open(tgt);
		return false;
	});
	
	$("a.popup_mini").click(function() {
		var tgt = $(this).attr("href");
		window.open(tgt, "popup_small", "width=400, height=400, scrolling=1, menubar=0, location=0, resizable=1");
		return false;
	});

	$("a.popup_small").click(function() {
		var tgt = $(this).attr("href");
		window.open(tgt, "popup_small", "width=400, height=600, scrolling=1, menubar=0, location=0, resizable=1");
		return false;
	});
	
	$("a.email").click(function() {
		$("#more_info_tabs dt.tab").removeClass("tab_here");
		$("#more_info_tabs dt#tab_ef").addClass("tab_here");
		
		$("#more_info_tabs dd.tc").hide();
		$("#content_ef").show();
	});
	
	
	
	/* FORM LABELS */

	var e = $("label.nolabel").next();

	for (var i = 0; i < e.length; i++) {

		if (e[i].value != "") {
			// hide the label... the field already has a value
			$(e[i]).prev().hide();
		} else {
			// show the label... the field is empty
			$(e[i]).prev().show();
		}
		
	}
	
	$("label.nolabel").next().focus(function() {
		var theField = $(this);
		var theLabel = $(this).prev();
		hideLabel(theLabel, theField);
	});
	
	$("label.nolabel").click(function() {
		var theField = $(this).attr("for");
		var theLabel = $(this);
		hideLabel(theLabel, theField);
		$("#"+theField).focus();
	});
	
	
	
});



// function makeH1(el) {
// 	var h1_txt = document.getElementById(el).innerHTML;
// 	h1_txt = flashAmp(h1_txt);
// 	
// 	var so = new SWFObject(siteRoot+"assets/flash/h1_header.swf", el, "936", "30", "7", "#FFFFFF");
// 	so.addVariable("h1_txt", h1_txt);
// 	so.addParam("wmode","transparent");
// 	so.write(el);
// }

function makeH2(el) {
	var h2_txt = document.getElementById(el).innerHTML;
	h2_txt = flashAmp(h2_txt);
	var e = document.getElementById(el);

	if (e.tagName == "A") var link = e.getAttribute("href");
	
	var so = new SWFObject(siteRoot+"assets/flash/h2_header.swf", el, "275", "26", "7", "#FFFFFF");
	so.addVariable("h2_txt", h2_txt);
	if (link) so.addVariable("link",link);

	so.addParam("wmode","transparent");
	so.write(el);
}

function hideLabel(l, e) {
	$(l).hide();
	$(e).blur(function() {
		if (this.value == "") {
			showLabel(l);
		}
	});
}

function showLabel(l) {
	$(l).show();
}

	
function flashAmp(str) {
	var out = str.replace(/\&amp\;/ig,"%26");
	out = out.replace(" & ", " %26 ");
	return out;
}


function textareaMax(el, max, remain){
	
	var char_remain = (max - el.value.length);
	if (char_remain <= 0) char_remain = 0;

	var str_char_remain = char_remain.toString();
	$("#"+remain).html(str_char_remain);
	
	
	if (el.getAttribute && el.value.length>max) {
		el.value = el.value.substring(0,max);
	}
}
