var CLT = {
  offset: 0,
  movement: 0,
  slider_image_width: 124,
  
  basename: function( path ) {
    return path.replace(/\\/g,'/').replace( /.*\//, '' );
  },

  dirname: function( path ) {
    return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');;
  },
  
  init: function() {
    CLT.init_gallery();
    CLT.news_slider_init();
    CLT.init_textblock();
    CLT.init_maingallery();
    CLT.init_maingallery_teaser();
    setTimeout( CLT.footer_pos, 1000 );
  },
  
  init_textblock: function() {
    jQuery('.txtgrp .txtblk').hide();
    jQuery('.txtgrp h2').addClass('txt-pointer txt-link');
    jQuery('.margin.txtgrp h3, .margin.txtgrp img').removeClass('txt-link').addClass('txt-pointer');
    jQuery('.txtgrp h2, .margin.txtgrp img').click( 
      function() {
        grp = jQuery(this).attr('id').substr(3);
        if ( jQuery( '#txt-' + grp ).css('display') == 'block' ) text_is_visible = true; else text_is_visible = false;
        jQuery('.txtgrp .txtblk').slideUp();
        jQuery('.txtgrp h2').css({'background-position' : '0 3px'});
        if ( !text_is_visible ) {
          jQuery( '#txt-' + grp ).slideDown();
          jQuery('#hl-' + grp).css({'background-position' : '0 -18px'});
        }
      }
    );
    jQuery('.txtgrp.chgkv h2, .margin.txtgrp.chgkv img').click( 
      function() {
        grp = jQuery(this).attr('id').substr(3);
        jQuery( '#single-kv').attr( 'src', '/img/kv/' + grp + '.jpg' );
      }
    );
  },
  
  init_gallery: function() {
    CLT.gallery_total = jQuery('#preload .kv').length;
    CLT.gallery_pos = 1;
    CLT.gInterval = setInterval( CLT.gallery_auto, 8000 );
    
    jQuery('#pictures').append('<div class="kv_control" id="kv_previous"></div>');
    jQuery('#pictures').append('<div class="kv_control" id="kv_next"></div>');
   
    jQuery('#kv_previous').click( function() {
      CLT.kv_control(-1);
    });
    jQuery('#kv_next').click( function() {
      CLT.kv_control(1);
    });
   jQuery('#pictures').hover( 
      function() {
        clearInterval( CLT.gInterval );
      }, 
      function() {
        CLT.gInterval = setInterval( CLT.gallery_auto, 8000 );
      }
    );
  },
  
  init_maingallery_teaser: function() {
    setTimeout( CLT.maingallery_teaser, 8000 );
  },
  
  maingallery_teaser: function() {
    jQuery('#maingallery-teaser p').append( jQuery('#maingallery-teaser p a:first-child').clone(true) );
    jQuery('#maingallery-teaser p a:first-child').remove();
    setTimeout( CLT.maingallery_teaser, 8000 );
  },
  
  init_maingallery: function() {
    jQuery('#maingallery-containment img').click( function() {
      src = jQuery(this).attr('src').replace(/lo/, 'hi' );
      jQuery('#single-kv').attr( 'src', src );
      return false;
    });
    jQuery('#maingallery').append('<div id="maingallery-prev"></div><div id="maingallery-next"></div>')
    jQuery('#maingallery-next').click( function() {
      for( x = 0; x < 9; x++ ) {
        jQuery('#maingallery-containment').append( jQuery('#maingallery-containment img:first-child').clone(true) );
        jQuery('#maingallery-containment img:first-child').remove();
      }
    });
    jQuery('#maingallery-prev').click( function() {
      for( x = 0; x < 9; x++ ) {
        jQuery('#maingallery-containment').prepend( jQuery('#maingallery-containment img:last-child').clone(true) );
        jQuery('#maingallery-containment img:last-child').remove();
      }
    });
    /*
    if ( direction > 0 ) {
      jQuery('#thumbnails').append( jQuery('#thumbnails img:first-child').clone() );
      jQuery('#thumbnails img:first-child').remove();
    } else {
      jQuery('#thumbnails').prepend( jQuery('#thumbnails img:last-child').clone() );
      jQuery('#thumbnails img:last-child').remove();
    }
    */
  },
  
  footer_pos: function() {
    jQuery('#content').css( 'min-height', jQuery('.margin').height() + 100 );
  },
  
  get_img: function( src ) {
    var Expr = /([0-9]{2})s\.jpg/;
    Expr.exec( src );
    retval = RegExp.$1;
    CLT.gallery_pos = Number( retval );
  },

  kv: function() {
    var pos = CLT.gallery_pos;
    if ( pos < 10 ) pos = '0' + pos;

    jQuery('#kv').fadeOut( 800, function() { 
      jQuery('#kv').attr( 'src', CLT.dirname( jQuery('#kv').attr( 'src' ) ) + '/' + pos + '.jpg' ).fadeIn( 800 );
    } );
  },
  
  gallery_auto: function() {
    CLT.gallery_pos += 1;
    if ( CLT.gallery_pos > CLT.gallery_total ) {
      CLT.gallery_pos = 1;
    }
    CLT.kv();
  },
  
  kv_control: function( direction ) {
    CLT.gallery_pos += direction;
    if ( CLT.gallery_pos > CLT.gallery_total ) {
      CLT.gallery_pos = 1;
    } else if ( CLT.gallery_pos < 1 ) {
      CLT.gallery_pos = CLT.gallery_total;
    }
    CLT.kv();
  },
  
  news_slider_init: function() {
    jQuery('#lang-sel').after('<div id="big-news"></div>');
    jQuery('#big-news').append( jQuery('#aktuelles .items').clone() );
    if( jQuery('#aktuelles .items div').length > 1 ) {
      jQuery('.items div a').click( function() {
        url = jQuery(this).attr('href').replace(/aktuell/, 'aktuelldetails' );
        jQuery('#article').fadeOut(500, function() {
          jQuery('#article').load( url, 0, function() {
            jQuery('#article').fadeIn(500);
          })
        })
        jQuery('.scrollable').fadeOut(500, function() {
          jQuery('#big-news').fadeIn(500);
        });
        return false;
      });

      jQuery(".scrollable").scrollable({circular: true,speed: 1200}).navigator().autoscroll({
        interval: 5000
      });
      
    } else {
      jQuery('#aktuelles .next').hide();
      jQuery('#aktuelles .prev').hide();
      jQuery('#aktuelles .slidernav').hide();
    }
    
  }
  
}

jQuery(document).ready(function(){
	CLT.init();
});
