// 
//  main.js
//  kulturrad
//  
//  Created by Tommy Stomlien on 2010-12-07.
//  Copyright 2010 Jimmy Royal. All rights reserved.
// 


// =========
// = Fonts =
// =========
Cufon.replace('h2', { hover: true } );
Cufon.replace( 'div.nav ul li a', {  hover:true } );
Cufon.replace('span.cufon, div.cufon, a.cufon, h3.cufon', { hover:true });
Cufon.replace('p.cufon');
Cufon.replace('body#page h4');
Cufon.replace('blockquote');
Cufon.replace('#page_menu', { hover: true } );


// ===========
// = Plugins =
// ===========
;(function($) {

$.fn.quotes = function(options) {
  var opts = $.extend({}, $.fn.quotes.defaults, options);

  return this.each(function() {
    var $this = $(this);

    // Support for the Metadata Plugin.
    var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
  	

	var h = 0;
	var qs = $this.find('.quotes');
	
	qs.each(function() {
		if( $(this).height() > h ) {
			h = $(this).height();
		}
	});
	
	$('#quote').css('height', h + 'px' );


	$this.find('.quotes:not(:first)').hide();

	
	if( qs.length > 1 ) {
	setInterval( function(ev) {
		var visible = $this.find('.quotes:visible');
		var next_quote = null;
		
		if( visible.next('.quotes').length ) {
			next_quote = visible.next('.quotes');
		} else {
			next_quote = $this.find('.quotes:first');
		}
		
		visible.fadeOut('slow', function() {
			next_quote.fadeIn('slow');
		});
		
	}, 10000 );
	
	}

  });

  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
};



// default options
$.fn.quotes.defaults = {
	classname: '.quote'
};

})(jQuery);


;(function($) {

$.fn.inputfocus = function(options) {
  var opts = $.extend({}, $.fn.inputfocus.defaults, options);

  return this.each(function() {
    var $this = $(this);

    // Support for the Metadata Plugin.
    var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
  	
	var text = $this.val();
	$this.bind('focus blur', function(ev) {
		if( ev.type == 'focus' ) {
			if( $this.val() == text ) {
				$this.val('');
			}
		} else {
			if( $this.val().length == 0 ) {
				$this.val( text );
			}
		}
	});


  });

  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
};

// default options
$.fn.inputfocus.defaults = {	
};

})(jQuery);


;(function($) {
// What does the calendar plugin do?
$.fn.calendar = function(options) {
  var opts = $.extend({}, $.fn.calendar.defaults, options);

  return this.each(function() {
    var $this = $(this);

    // Support for the Metadata Plugin.
    var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
  	var prev = $( opts.prev );
	var next = $( opts.button );
	var offset = $this.offset().left;
	// add passive class to prev button on init
	prev.addClass( 'passive' );
	
	var last_margin = 0;
	var lis = $this.find('li');
	var index = 0;
	
	if( lis.length <= 3 ) {
		next.addClass( 'passive' );
		prev.addClass( 'passive' );
	}
	
	lis.filter(':gt(2)').addClass( 'slide_hidden' );
	
	var first_visible = lis.not( '.slide_hidden' ).filter( ':first' );
	var first_offset = first_visible.offset().left;
	
	next.bind('click', function(ev) {
		if( !$this.is(':animated') && !$(this).hasClass( 'passive' ) ) {
			var next_el = lis.not( '.slide_hidden ').filter(':last').next('li');
				
			if( next_el.length ) {
				var next_offset = next_el.offset().left;
				var next_index = lis.index( next_el );
				var next_lis = lis.slice( next_index, next_index + 3 );
				
				next_lis.removeClass('slide_hidden');
				$this.animate( { left: -( next_offset - offset ) }, 500, function() {
					lis.not( next_lis ).addClass('slide_hidden');
					offset = $this.offset().left;
					index++
					
					if( !lis.filter( ':last' ).hasClass( 'slide_hidden' ) ) {
						next.addClass( 'passive' );
					}
					
					if( index > 0 ) {
						prev.removeClass( 'passive' );
					}
				});				
			}
			
		}
		ev.preventDefault();
	});
	
	prev.bind('click', function(ev) {
		if( !$this.is(':animated') && !$(this).hasClass( 'passive' ) ) {
			var prev_el = lis.not( '.slide_hidden' ).filter(':first').prev('li');
			
			next.removeClass( 'passive' );
			
			if( prev_el.length ) {
				var prev_index = lis.index( prev_el );
				var prev_el = lis.filter(':eq(' + ( prev_index - 2 ) + ')');
				var prev_lis = lis.slice( ( prev_index - 2 ), ( prev_index + 1 ) );
				var prev_offset = prev_el.offset().left;
				
				prev_lis.removeClass('slide_hidden');
				
				$this.animate( { left: -( prev_offset - offset ) }, 500, function() {
					lis.not( prev_lis ).addClass('slide_hidden');
					offset = $this.offset().left;
					index--
					
					if( index > 0 ) {
						prev.removeClass( 'passive' );
					} else {
						prev.addClass( 'passive' );
					}
				});
				
			}
		}
		ev.preventDefault();
	});
		
  });

  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
};

// default options
$.fn.calendar.defaults = {
  button: '#slide_button',
  prev : '#slide_prev',
  el_width: 860
};

})(jQuery);



// =============
// = DOM Ready =
// =============
$(function() {
	$('#slide_container').find('ul').calendar();
	$('.input').inputfocus();
	
	// clean tables
	$('#right_column').find('table').each(function() {
		$(this).find('tr').each(function() {
			$(this).find('td:last').css('borderRight', 'none');
		});
	});
	
	$('#quote').quotes();
	
	
    // hover aktuelt
    var aktuell_list = $('#aktuell_list');
    if( aktuell_list.length ) {
        
        aktuell_list.find('a').bind('click', function(ev) {
            $(this).closest('li').trigger('click');
            ev.preventDefault();
            return false;
        });
        
        aktuell_list.find('li').hover(
            function() {
                $(this).find('a').css('color', '#000000');
                Cufon.refresh();
            },
            function() {
                $(this).find('a').css('color', '#544F46');  
                Cufon.refresh();
            }
        )
        .css('cursor', 'pointer')
        .bind('click', function(ev) {
            if( $(this).find('a').attr('target') == '_blank' ) {
                window.open($(this).find('a').attr('href'));
            } else {
                location.href = $(this).find('a').attr('href');
            }
        });
    }
		
});
