/**
 * triplexProjekte
 * 
 * @author Robert Roemer, triplex neue medien GmBbH
 * @version 1.0.0
 * @date 24.03.2011
 */
(function(jQuery){
	jQuery.fn.kontaktSlide = function(options) {
	
		var settings = {
			'duration':			500,
			'filter_prefix':	'kontakt_',
			'customEvents':		{
									'beforeInitNavigation': function(){},
									'afterInitNavigation': function(){},
									'onNavEntryMouseover': function(){},
									'onNavEntryMouseout': function(){},
									'beforeShowSlide': function(){},
									'afterShowSlide': function(){}
			}
		};
	
		
		return this.each(function() {
			if (options) { 
				jQuery.extend(true, settings, options);
			}
			
			initEntryToggle();
			
			var hash = window.location.hash.substring(1);
			if ( settings.filter_prefix.length > 0 && hash.substring(0, settings.filter_prefix.length) == settings.filter_prefix ) {
				jQuery('#contact').trigger('click');
				toggleEntry('.'+hash.substring(settings.filter_prefix.length), true);
			}
			return true;
		});

		function triggerCustomEvent(name, params) {
			if ( settings.customEvents[name] !== undefined && typeof settings.customEvents[name] == 'function' ) {
				return settings.customEvents[name](params);
			}

			return null;
		}

		function debug(variable) {
			if (window.console !== undefined) {
				console.log(variable);
			}
		}
	
		function toggleEntry(id, fast) {
			var panelContent = jQuery('.kontakt').find(id);
			
			//console.log(id);	
						
			if(panelContent.length == 1) {	
				
				var minHeight = panelContent.parent().children('.kontakt_content').height();
				var parentHeight = panelContent.parent().height();
				panelContent.parent().css('height', parentHeight +'px');

				if ( $('body').hasClass('home') ) {
					panelContent.parent().find('.panels').css({'height': minHeight+'px'});
				} else {
					panelContent.parent().find('.panels').css({'height': 'auto'});
				}

				var activeHeight = panelContent.height();
				
								
				if ( minHeight > activeHeight ) {
					panelContent.css('padding-bottom', (minHeight - activeHeight) + 'px');
					activeHeight = minHeight;									
				}

				//is panel active move it to the right side
				if ( panelContent.hasClass('active') ) {
					panelContent.animate({'left':'-'+panelContent.outerWidth()+'px'}, settings.duration).removeClass('active');	
					window.location.hash = '';							
					//console.log(window.location.hash);
					
					//anpassung der Höhe des div.kontakt
					panelContent.parent().animate({'height': minHeight+'px'}, settings.duration, function(){
						panelContent.parent().find('.panels').css('height', '10px');
						panelContent.parent().css('height', 'auto');
					});
				}
				//switch between the panels
				else {															
					if(fast == true) {						
						panelContent.parent().find('.panels').not(panelContent).css({'height': '10px'});
						panelContent.css({'left':0}).addClass('active');
						panelContent.parent().css('height', 'auto');
						panelContent.css('overflow-y', 'auto');
					}
					else {
						//anpassung der Höhe des div.kontakt
						panelContent.parent().animate({'height': activeHeight+'px'}, settings.duration)
						panelContent.parent().find('.panels').filter('.active').css({'z-index': 500}).removeClass('active');
						
						panelContent.css({'left':'-'+panelContent.outerWidth()+'px', 'z-index': 1000}).animate({'left':0}, settings.duration, function(){
							panelContent.css('overflow-y', 'auto');
							panelContent.parent().find('.panels').not(panelContent).css({'height': '10px', 'left':'-'+panelContent.outerWidth()+'px'});
							panelContent.parent().css('height', 'auto');
						}).addClass('active');
					}					
				}				
			}
		}

		function initEntryToggle() {
			jQuery('.kontakt').find('a.kontakt_button').unbind('click').bind('click', function() {
				var thisEl	= jQuery(this);

				//debug(thisEl.next().filter('div').length);
				var id = thisEl.attr('id');				
				
				window.location.hash = settings.filter_prefix+id;

				toggleEntry('.'+id);

				return false;
			});
		}
	};
})(jQuery);
