window.log = function(){
	log.history = log.history || [];   // store logs to an array for reference
	log.history.push(arguments);
	
	if (this.console) {
		console.log( Array.prototype.slice.call(arguments) );

	}
};

/* ------------------------------------------------
 * selectors
 * ------------------------------------------------
 * hasPlaceholder: returns all elements with placeholder attribute
 */

$.expr[':'].hasPlaceholder = function(el) {
	return (typeof $(el).attr('placeholder') !== 'undefined');
};

$.fn.extend({
	
	/* ------------------------------------------------
	 * @plugin		m3Placeholder
	 * @created		May 19, 2010
	 * ------------------------------------------------
	 * displays placeholder value when field is empty,
	 * clears the field when it is focused on or when a
	 * value is entered (other than the placeholder's value)
	 */
	
	m3Placeholder: function(options) {
		var defaults = {
			attr_placeholder: "placeholder", // attribute name
			class_placeholder: "placeholder" // class name
		};
		
		var options = $.extend(defaults, options);
		var self = this;
	
		return this.each(function() {
			
			var element = $(this);
			var placeholder_value = element.attr(options.attr_placeholder);
			
			function clear() {
				element.removeClass(options.attr_placeholder);
				
				if (element.val() == placeholder_value) {
					element.val("");
				}
			}
			
			function display() {
				if ((element.val() == "") || (element.val() == placeholder_value)) {
					element.val(placeholder_value).addClass(options.attr_placeholder).removeClass("error");
				}
			}
			
			display();
			
			element
				.change(clear)
				.focus(clear)
				.blur(display)
				.parents('form').submit(clear)
			;

		});
	},
	// end m3Placeholder
	
	/* ------------------------------------------------
	 * @plugin		m3Toggle
	 * @created		May 19, 2010
	 * -----------------------------------------------*/
	/*
		toggles the panel element corresponding with the trigger element. by default,
		the panel element is the next element after the trigger, but it can also be
		the previous element or an element to be determined by a set selector.
	 */
	
	m3Toggle: function(options) {
		
		var defaults = {
			panelSelector: null,
			opened: false,
			ajax: null,
			openedSelector: "opened",
			closedSelector: "closed",
			direction: 'next',
			labelOpen: null,
			onOpen: function(){},
			onClose: function(){},
			onAfterOpen: function() {},
			onAfterClose: function() {}
		};

		var options = $.extend(defaults, options);
		var self = this;
		
		return this.each(function() {
			
			var element = $(this);
			
			if (options.panelSelector !== null) {
				var panel = $(options.panelSelector);
			}
			else {
				if (options.direction === 'next') {
					var panel = element.next(options.panelSelector);
				}
				else if (options.direction === 'prev') {
					var panel = element.prev(options.panelSelector);
				}
			}
			
			// store some original values that we might need later
			var panelLabel = element.html(),
				panelOpacity = panel.css('opacity'),
				panelWidth = panel.width(),
				panelHeight = panel.height()
			;
			
			function markOpened() {
				element.removeClass(options.closedSelector).addClass(options.openedSelector);
				
				if (null !== typeof options.labelOpen) {
					element.text(options.labelOpen);
				}
			}
			
			function markClosed(){
				element.addClass(options.closedSelector).removeClass(options.openedSelector);
				
				if (null !== typeof options.labelOpen) {
					element.html(panelLabel);
				}
			}
			
			if (null !== options.ajax) {
				$.ajax({
					type: 'GET',
					url: options.ajax,
					success: function(content) {
						panel.html(content);
						
						// panel content has been altered, store the new width and height value
						panelWidth = panel.width(),
						panelHeight = panel.height()
					}
				});
			}
			
			if (false === options.opened) {
				panel.hide();
				markClosed();
			}
			else {
				markOpened();
			}
			
			element.click(function(e) {
				if (panel.is(":hidden")) {
					panel.show();
					options.onOpen.call(panel);
					
					// inspect the visibility, width, height and opacity of the panel. if they all equal the original values,
					// the panel is in its original state and declared 'opened'
					var trackerOpen = setInterval(function() {
						if (panel.is(':visible') && panel.css('opacity') === panelOpacity && panel.width() == panelWidth && panel.height() && panelHeight) {
							clearInterval(trackerOpen);
							markOpened();
							options.onAfterOpen.call(panel);
						}
					}, 100);
				}
				else {
					panel.hide();
					options.onClose.call(panel);
					
					// inspect the invisibility of the panel
					var trackerClose = setInterval(function() {
						if (panel.is(':hidden')) {
							clearInterval(trackerClose);
							markClosed();
							options.onAfterClose.call(panel);
						}
					}, 100);
				}
				return false;
			});
		});
	}
	// end m3Toggle
});


$(function(){
	
	// tell the DOM javascript is enabled
	$("body").addClass("js_enabled");
	
	// add target attribute to all anchors which href value starts with 'http://' (and therefore most likely an external link)
	$('a[href^="http://"]').filter(function(index) {
		
		return this.href.indexOf("careplus.eu") === -1;
		
	}).attr("target", "_blank");
	
	/*
	 * activate the m3Placeholder plugin for all textfields containing a placeholder attribute
	 */
	$("input:hasPlaceholder").m3Placeholder();
	
	$.expr[':'].hasPlaceholder = function(el) {
		return (typeof $(el).attr('placeholder') !== 'undefined');
	};
	
});
