
// fallback für console.log
if (typeof console == 'undefined') console={log:function(){}};

// global
sl_block = false;
slDir = 'right';

$(document).ready(function(){
	jmDev.docReady();
	
	$('a.fancybox').fancybox({
		'onClosed'	: autoSlideStart
	});
	
	
	$('.sl_container > img').bind('click', function(){
	
		
		var $cont = $(this).parents('.sl_container')
			$sl = $cont.find('.sl'),
			$sld = $cont.find('.sl > div'),
			$step = $sld.find('img').not('.active').first().outerWidth(true),
			speed = parseInt($cont.find('.sl_speed').html());
		
		var $active = $cont.find('.active'),
			dir = $(this).hasClass('sl_btn_left')? 'left' : 'right';		
		
		// stop on last image		
		if ( ! $active.parent().prev().length && dir == 'left' ) {
			slDir = 'right';
			// console.log( 'DIRL' );
			return;
		}
		if ( ! $active.parent().next().not('.clear').length && dir == 'right') {
			slDir = 'left';
			// console.log( 'DIRR' );
			return false;
		}
		
		// prevent multiple clicking
		if ( sl_block ) return;
		sl_block = true;
		
		$sld.animate({
			left: (dir == 'left' ? '+=' : '-=') + $step,
			}, speed, function() {
				// Animation complete.
		});
		
		$active.animate({
			height: 86,
			top: 64,
			width:114,
			}, speed, function() {
				$(this).removeClass('active');
		});

		var $next = (
			dir == 'left' ? 
				$active.parent().prev().children().first() : 
				$active.parent().next().children().first() 
		)
		$next.animate({
			height: 150,
			top: 0,
			width: 200,
			}, speed, function() {
				$(this).addClass('active');
				sl_block = false;
		});
		
		// titlebar
		$cont.find('.sl_titlebar').html( $cont.find('.active').parent().attr('title') );
	})
	
	if (typeof $cont != 'undefined' && $cont.length){
		autoSlideStart();
	}
	
	// open events in new tab/window
	$('#navi #subMenuOf3 .linksTo9').children('a').attr('target', '_blank');
})

function slAutochange(){

	if (slDir == 'right')
		var ret = $('.sl_container > img').eq(1).click();
	else
		var ret = $('.sl_container > img').eq(0).click();
}

function autoSlideStart(){
	var autoSpeed = parseInt($cont.find('.sl_autoSpeed').html()),
			myInterval = window.setInterval("slAutochange()", autoSpeed);
		
	// this is to manualy clear the interval
	$('.sl_container > img').bind('mousedown', function(){
		window.clearInterval(myInterval);
	})
	$('.sl_container .sl > div > a').bind('mousedown', function(){
		window.clearInterval(myInterval);
	})
}


var jmDev = {
	
	/* ***************************** extend the jQuery object ***************************** */
	
	container : function($){ 
		$.fn.extend({
			/* 
			* autoclear
			* deletes input fileds on click and restores default value on blur
			*/
			autofill : function (){
				$(this)
					.click(function(e){
						// get value
						var val = $(this).val();
						
						// if not saved before, save
						if ( ! $(this).attr('old_val') ) $(this).attr('old_val', val);
						
						if ( val && (val == $(this).attr('old_val')) ) {
							$(this).val('');
						}
					})
					.blur(function(){
						if ( ! $(this).val() ) {
							$(this).val($(this).attr('old_val'));
						}
					})
			},
			
			/* 
			* autoclear
			* only deletes input fileds on click
			*/
			autoclear : function (){
				$(this).focus(function(e){						
					$(this).val('');
				})
			}
		})
	}(jQuery),
	
		/* ***************************** Standardfunktionen initialiseiren ***************************** */
	init : function(){
		if ( typeof console == 'undefined')	{
			var console = {
				log	: function(){}
			};
		}
	}(),
	
	docReady : function(){
		
		// externe Links in neuem Fenster öffnen
		$('a[href^=http://]').attr({target: "_blank"});
		$('area[href^=http://]').attr({target: "_blank"});
		
		console.log( 'docReady' );
	}
}
