/**
 * simple dropdown select implementation
 * TODO document needed structure
 * basic: 
 * 	container
 * 		.ms-button
 * 			.ms-nubbin
 * 			.ms-title
 * 			.content
 * $('element').tao_dropdown_select
 * @author rw
 */
(function($) {
	$.fn.extend({
		
		defaults  : {'callback':false} ,
		/**
		 * simple dropdown list
		 * @param opts options array or a callback function for form events
		 */
		tao_dropdown_select : function(opts) {
			var o = this.defaults;
			if( jQuery.isFunction(opts) ) { o.callback = opts; }
			else { o = $.extend(this.defaults, opts); }
			var container = $(this);
			container.currently_select = false;	
			
			container.toggle_child = function(child) {
				var c = $(child);
				c.find('.btn-nubbin, .btn-nubbin-hover').toggleClass('btn-nubbin-open');
				c.find('.btn-title').toggleClass('btn-title-open');
				var content = c.find('.btn-content');
				var position = c.position();
				var width = c.width();
				content.css({'left':-22, 'top':35});
				var blind = content.find('.blinder');
				blind.css({'left':width+14, 'width':252-width});
				content.toggle('fast');
			};
			
			container.find('div.ms-button').each(function(idx) {
				var button = $(this);
				var selector = $(this).find('.selector');
				selector.mouseover(function() { $(this).find('.btn-nubbin').addClass('btn-nubbin-hover'); });
				selector.mouseout(function() { $(this).find('.btn-nubbin').removeClass('btn-nubbin-hover'); });
		
				selector.click(function() {
					if( container.currently_selected ) {
						if( container.currently_selected == button ) {
							container.toggle_child(button);
							container.currently_selected = false;
						} else {
							container.toggle_child(container.currently_selected);
							container.toggle_child(button);
							container.currently_selected = button;
						}
					} else {
						container.toggle_child(button);
						container.currently_selected = button;
					}
				});
				// form callback
				button.find('.btn-content input[type=radio], .btn-content input[type=checkbox]').change(function(event){
					if( $.isFunction(o.callback) ) {
						this.tao_callback = o.callback;
						this.tao_callback();
					}
					selector.click();
				});
				button.find('input[type=select],input[type=text]').change(function(){
				});
			});
		} 
	});
})(jQuery);

/**
 * nice little popup box
 * @author rw
 */
(function($) {
	$.fn.extend({		
		tao_popup : function() {
			var link = jQuery(this);
			// todo make me nicer, but jquery insists that link.tagName == "undefined"
			var text = link.attr('title');
			if( typeof text == "undefined" || (typeof text.length != "undefined" && text.length == 0) ) text = link.attr('alt');
			// build the superkewl tao popup bubble aka a simple div
			var popup = jQuery('<div class="bubble">'+text+'</div>');
			popup.css({'display':'none','position':'absolute','z-index':101,'height':43});
			jQuery('body').prepend(popup);
			var is_showing = false;
			// on hover show and hide the little sucker
			link.hoverIntent(function() {
				pos = link.offset();
				popup.css({'top':pos.top-50, 'left':pos.left});
				is_showing=true;
				if (jQuery.browser.msie) {
					popup.show();
				} else {
					popup.show(400, function(){ is_showing=false; });	
				}
				return true;
			},function(){ setTimeout(function() {popup.hide();}, is_showing ? 400 : 1); return true; 
			});
		}
	});
})(jQuery);

// show a simple container - todo: make late bindings ... 
function showContainer(container_class){
	jQuery('.content-containers div.container:visible').hide('blind', 'slow', function(){
		jQuery('.sub-menu a').removeClass('selected');
		jQuery('.sub-menu a.'+container_class).addClass('selected');
		jQuery('.content-containers div.' + container_class).show('blind', 'slow');	
	});
}


jQuery(document).ready(function(){
	jQuery('#header-size-btn').click(function(){
		if( jQuery.browser.msie ) {
			$('#header-container').animate( {'height': '476px'}, 'slow', 'linear');
			$('#header-size-container').animate({top:'536px'}, 'slow', 'linear');
		} else {
			$('#header-container').animate( {'height': '400px'}, 'slow', 'linear');
			$('#header-size-container').animate({top:'460px'}, 'slow', 'linear');
		}
	});
});

/**
 * easy js translation
 */
(function($){	
	$.fn.tao_translate = function(lang) {
		$(this).each(function(){
			var self = $(this);
			var s = self.html();
			var translated_with_google = '<span style="border:solid 1px;">translated with google</span>';
			google.language.translate(s, "de", lang, function(result) {
				if (!result.error) { self.html(result.translation); }
			});
		});
		return this;
	};
})(jQuery);