// Element behavior is a one-file that improves user-interface experience for all solutions  

// (TD) enable_box_handling() turns on feature that stores in the cookies state of boxes that have an id.
// slide down slide up animations are used for visual feedback
function enable_box_handling() {
	$j(".Box .Title .Button").live("click", function() {
		var box_button = $j(this);
		var state = box_button.data("hidden") ? true : false;
		var own_title = box_button.parent();
		var own_box = own_title.parent();
		var own_cont = own_box.find(".Cont");
		var id = own_box.attr("id");
		if (id) {
			if (state == true) {
				own_cont.slideDown();

			} else {
				own_cont.slideUp();
			}
			box_button.toggleClass("ButtonSel");
			box_button.data("hidden", !state); // toggle
			$j.cookie("boxes_" + id, !state, { path: '/'});
		}
	});

	$j(".Box").each(function() {
		var box = $j(this);
		var box_cont = box.find(".Cont");
		var box_button = box.find(".Title .Button");
		var id = box.attr("id");
		if (id) {
			if ($j.cookie("boxes_" + id, undefined, { path: '/'}) == "true") {
				box_button.data("hidden", true); // toggle
				box_cont.hide();
				box_button.toggleClass("ButtonSel");
			}
		}
		else {
			box_button.hide();
		}
	});
}

// (NB) center_popup() centers popup dialogs and flesh messages vertically on page:
//
function center_popup() {
	$j("div.Box.Popup").each(function(index) {
		$j(this).draggable();
		$j("div.Box.Login, div.Box.Popup, div.FlashMessages").each(function(index) {
			if ($j(this).height() != 0 && $j('#layout_sidebar').length > 0) {
				$j(this).css({
					'margin-top': (Math.round(0 - $j(this).innerHeight() / 2) + "px"),
					'margin-left': (Math.round($j('#layout_sidebar').innerWidth() / 2 - $j(this).innerWidth() / 2) + "px")
				});
			}
			else if ($j(this).height() != 0) {
				$j(this).css({
					'margin-top': (Math.round(0 - $j(this).innerHeight() / 2) + "px")
				});
			}
		});
	});
}

function simulate_button_click() {
	$j(".LinkBar>a, button, a.Button, input[type=submit], input[type=button]").each(function(index) {
		$j(this).mousedown(function() {
			$j(this).addClass('pressed')
		});
		$j(this).mouseup(function() {
			$j(this).removeClass('pressed')
		});
		$j(this).mouseout(function() {
			$j(this).removeClass('pressed')
		});
	});
}

var blink_state = true;

function enable_blink() {
	$j(document).ready(function() {
		// set data with source colors:
		setInterval(function() {
			blink_state = !blink_state;
			if (blink_state) {
				$j(".Blink").each(function() {
					$j(this).animate({ opacity: 0 }, 100);
				})
			} else {
				$j(".Blink").each(function() {
						$j(this).animate({ opacity: 1 }, 100);
				})
			}
		}, 400);
	});

}

$j(document).ready(function () {
	enable_box_handling();
	center_popup();
	simulate_button_click();
	enable_blink();
});
