$(document).ready(function() {
	init();
	
	/* effect at the close of messages */
	$('.message').live("click",function(){
		$(this).animate({opacity: 0}, 250, function(){
			$(this).slideUp(250, function(){$(this).remove()})});
			return false;
		}
	);
	
	/* === Tables javascripts [begin] === */
	/* optional menu in the table to each row */
	$("table td a.action").live("click", function(){
		var pos = $(this).position();
		$(this).addClass("active").next().css({top: pos.top + 24, left: pos.left}).show().find("li:last a").css("border-bottom", 0);
		return false;
	});
	$("table th a.action").live("click", function(){return false;});
	/* when you click to close the active optional menu and select box */
	$("body").click(function(event){
		$.each($("a.action.active"), function(){
			$(this).removeClass("active").next().hide();
		});
		if($(event.target).is("div.select") || $(event.target).parent().is("div.select")){
			$("div.select").not($(event.target)).each(function(index){
				if($(this).data("active")) $(this).data("active", false).next().children("ul").hide();
			});
		}else{
			$("div.select").each(function(index){
				if($(this).data("active")) $(this).data("active", false).next().children("ul").hide();
			});
		}
	});
	
	/* the effect of zebra */
	$("table.infotable tbody tr:odd").css({backgroundColor: "#fbfbfb"});
	
	/* Check all */
	$('input:checkbox.checkall').click(function() {
		$(this).parents('.infotable').find('input:checkbox').attr('checked', $(this).is(':checked')).parents("tr").each(function(){
			if($(this).find('input:checkbox').is(':checked')) $(this).addClass("selected");
			else $(this).removeClass("selected");
		});  
	});
	
	/* selection row with the active checkbox */
	$("table.infotable input:checkbox").change(function(){
		if($(this).is(':checked')) $(this).parents("tr").addClass("selected");
		else $(this).parents("tr").removeClass("selected");
	});
	/* === Tables javascripts [end] === */
	
	/* === Custom select box javascripts [begin] === */
	// This code creates a stylized copy of select with class .select
	$("select.select").each(function(index){
		var select = $("select.select:eq("+index+")");
		/*
		If you specify an additional class "nohide",  // <select class="select nohide">...</select> - displayed
		then the original item will remain,           // <select class="select">...</select> - be hidden
		as in the demo version, if you do not 
		specify this class will be displayed only 
		stylized selectivity
		*/
		if(!$(this).hasClass("nohide")) select.hide();
		$(this).after('<div class="select"><span>'+$("option:selected", this).text()+'</span></div>');
		var divselect = $(this).next();
		divselect.after('<div class="select-box"><ul></ul></div>');
		var ul = $(this).nextAll(".select-box").children("ul");
		ul.css({width: divselect.outerWidth() + 10 +"px", overflow: "auto"});
		/*
		Here is handled nested items, the grouping
		of the list observed as in the original
		*/
		$("option", this).each(function(){
			if($(this).parent().is("optgroup")) {
				if($(this).index($(this).parent().children("option")) == 0) ul.append($("<li/>", {'class': "group", text: $(this).parent().attr("label")}));
				ul.append($("<li/>", {'class': "ingroup", text: $(this).text(), rel: $(this).val()}));
			}else ul.append($("<li/>", {text: $(this).text(), rel: $(this).val()}));
		});
		ul.find("li[rel="+$("option:selected", this).val()+"]").addClass("selected");
	}).change(function(){
		/*
		This code follows the changes of the original 
		item and makes changes to a stylized version of the select
		*/
		$(this).next().find("span").text($("option:selected", this).text());
		var ul = $(this).nextUntil(".select-box").next().children("ul");
		ul.find("li:not(.group)").removeClass("selected");
		ul.find("li[rel="+$("option:selected", this).val()+"]").addClass("selected");
	});
	
	// This code handles the click and hover events on items in the list, you can click on the items, but not in group
	$(".select-box ul li:not(.group)").live("mouseover", function(){
		$(this).addClass("auto-focus");
	}).live("mouseout", function(){
		$(this).removeClass("auto-focus");
	}).live("click", function(){
		$(this).siblings().removeClass("selected");
		$(this).addClass("selected");
		var divselect = $(this).parents(".select-box").prev();
		var select = divselect.prev();
		select.find("option").removeAttr('selected');
		divselect.find("span").text($(this).text());
		select.find("option[value="+$(this).attr("rel")+"]").attr('selected', 'yes');
	});
	
	// establish the state, active or not
	$("div.select").data("active", false).click(function(){
		if($(this).data("active")) $(this).data("active", false).next().children("ul").hide();
		else $(this).data("active", true).next().children("ul").width($(this).outerWidth(true)).show();
	});
	
	/* === Custom select box javascripts [end] === */
	
	// if the width of the parent element is less then the width of selects and inputs decreases
	$("div.select, select.select").each(function(){
		var pw = $(this).parent().width();
		if (pw < $(this).outerWidth()) {
			if($.browser.msie) {
				if($(this).is("div")) $(this).width(pw-parseFloat($(this).css("marginLeft"))).css("position", "relative");
			}
			if($(this).is("div")) $(this).width(pw-parseFloat($(this).css("marginLeft")));
			else $(this).width(pw);
		}
	});
});

function init(){
	if($.browser.msie) $("body").addClass("ie");
	
	// :focus support in IE7
	if($.browser.msie && $.browser.version == 7) {
		$("input.inputtext, textarea").bind("focusin", function(){
			$(this).addClass("focus");
		}).bind("focusout", function(){
			$(this).removeClass("focus");
		});
	}
	
	// hide content blocks on tabs, except for the first
	$(".content").each(function(){
		if($(this).children(".bcont").size() > 1) $(this).children(".bcont:gt(0)").hide();
	});
	
	// outline fix
	$("a").each(function() {
		$(this).attr("hideFocus", "true").css("outline", "none");
	});
}
