/* DOM is ready */

$(document).ready(function() {

	$(window).load(function () {
		showFirstTable();
		initShadows();
	});

	$("tr").click(function() {
		var tr_name = $(this).attr("id");
		if (tr_name != "" && tr_name != "undefined") {
			if (tr_name.substr(0, 3) == "tbl") {
				
				var tr_parts = tr_name.split("-");
				
				if (tr_parts[1] == "study") {
					var tbl = $("#" + tr_parts[0] + "-study-toggle");
					if (tbl.is(':visible')) {
						hideTable(tr_parts[0]);
					} else {
						hideOtherTables();
						showTable(tr_parts[0]);
					}
					redrawShadows();
				}
				if (tr_parts[1] == "play") {
					$mid = tr_parts[2];
					$eid = tr_parts[3];
					$cid = tr_parts[4];
					if ($cid==0) {
						$cid = $("#" + tr_parts[0] + "-cboCompany-" + $eid).val();
					}
					$("#" + tr_parts[0] + "-form-mid").val($mid);
					$("#" + tr_parts[0] + "-form-eid").val($eid);
					$("#" + tr_parts[0] + "-form-cid").val($cid);
					$("#" + tr_parts[0] + "-form").submit();
				}
			}
		}
	});

	function showFirstTable() {
		$("table[id$='study-toggle']").each(function (i) {
			var tmp_parts = $(this).attr("id").split("-");
			if (i>0) {
				hideTable(tmp_parts[0]);
			} else {
				showTable(tmp_parts[0]);
			}
		});
	}

	function showTable(tbl_prefix) {
		var main_tbl     = $("#" + tbl_prefix + "-study-toggle");
		var main_ico     = $("#" + tbl_prefix + "-study-icon");
		var main_ico_src = main_ico.attr("src").substring(0, main_ico.attr("src").lastIndexOf("/")+1) + "close.png";
		main_tbl.show();
		main_ico.attr("src", main_ico_src);
		
	}

	function hideOtherTables() {
		$("table[id$='study-toggle']").each(function (i) {
			if ($(this).is(':visible')) { 
				var tmp_parts = $(this).attr("id").split("-");
				hideTable(tmp_parts[0]);
			}
		});
	}

	function hideTable(tbl_prefix) {
		var main_tbl     = $("#" + tbl_prefix + "-study-toggle");
		var main_ico     = $("#" + tbl_prefix + "-study-icon");
		var main_ico_src = main_ico.attr("src").substring(0, main_ico.attr("src").lastIndexOf("/")+1) + "open.png";
		main_tbl.hide();
		main_ico.attr("src", main_ico_src);
	}

	function redrawShadows() {
		$("table[id$='study-main']").each(function (i) {
			$(this).redrawShadow();
		});
	}

	function initShadows() {
		$("table[id$='study-main']").each(function (i) {
			$(this).dropShadow();
		});
	}

});

function startSWF(pid, sid, eid, mid, cid) {
	var link = 'index.php?id=' + pid + '&sid=' + sid + '&eid=' + eid + '&mid=' + mid + '&cid=' + cid;
	alert(link);
	//win = window.open(link, '', 'width=900,height=700,left=0,top=0,location=no,resizable=yes');
};
