/*
 * jQuery Cycle Lite Plugin
 * http://malsup.com/jquery/cycle/lite/
 * Copyright (c) 2008 M. Alsup
 * Version: 1.0 (06/08/2008)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires: jQuery v1.2.3 or later
 */
;(function($) {

var ver = 'Lite-1.0';
var imgheight
$.fn.cycle = function(options) {
    return this.each(function() {
        options = options || {};

        if (this.cycleTimeout) clearTimeout(this.cycleTimeout);
        this.cycleTimeout = 0;
        this.cyclePause = 0;

        var $cont = $(this);
        var $slides = options.slideExpr ? $(options.slideExpr, this) : $cont.children();
        var els = $slides.get();
        if (els.length < 2) {
            if (window.console && window.console.log)
                window.console.log('terminating; too few slides: ' + els.length);
            return; // don't bother
        }

        // support metadata plugin (v1.0 and v2.0)
        var opts = $.extend({}, $.fn.cycle.defaults, options || {}, $.metadata ? $cont.metadata() : $.meta ? $cont.data() : {});

        opts.before = opts.before ? [opts.before] : [];
        opts.after = opts.after ? [opts.after] : [];
        opts.after.unshift(function(){ opts.busy=0; });

        // allow shorthand overrides of width, height and timeout
        var cls = this.className;
			opts.width = parseInt((cls.match(/w:(\d+)/)||[])[1]) || opts.width;
        opts.height = parseInt((cls.match(/h:(\d+)/)||[])[1]) || opts.height;
        opts.timeout = parseInt((cls.match(/t:(\d+)/)||[])[1]) || opts.timeout;


        if ($cont.css('position') == 'static')
            $cont.css('position', 'relative');
        if (opts.width)
            $cont.width(opts.width);
        if (opts.height && opts.height != 'auto')
            $cont.height(opts.height);

        var first = 0;
        $slides.css({position: 'absolute', top:0, left:0}).hide().each(function(i) {
            $(this).css('z-index', els.length-i)
        });

        $(els[first]).css('opacity',1).show(); // opacity bit needed to handle reinit case
        if ($.browser.msie) els[first].style.removeAttribute('filter');

        if (opts.fit && opts.width)
            $slides.width(opts.width);
        if (opts.fit && opts.height && opts.height != 'auto')
            $slides.height(opts.height);
        if (opts.pause)
            $cont.hover(function(){this.cyclePause=1;}, function(){this.cyclePause=0;});

        $.fn.cycle.transitions.fade($cont, $slides, opts);

        $slides.each(function() {
            var $el = $(this);
            this.cycleH = (opts.fit && opts.height) ? opts.height : $el.height();
            this.cycleW = (opts.fit && opts.width) ? opts.width : $el.width();
        });

        $slides.not(':eq('+first+')').css({opacity:0});
        if (opts.cssFirst)
            $($slides[first]).css(opts.cssFirst);

        if (opts.timeout) {
            // ensure that timeout and speed settings are sane
            if (opts.speed.constructor == String)
                opts.speed = {slow: 600, fast: 200}[opts.speed] || 400;
            if (!opts.sync)
                opts.speed = opts.speed / 2;
            while((opts.timeout - opts.speed) < 250)
                opts.timeout += opts.speed;
        }
        opts.speedIn = opts.speed;
        opts.speedOut = opts.speed;

 		opts.slideCount = els.length;
        opts.currSlide = first;
        opts.nextSlide = 1;

        // fire artificial events
        var e0 = $slides[first];
        if (opts.before.length)
            opts.before[0].apply(e0, [e0, e0, opts, true]);
        if (opts.after.length > 1)
            opts.after[1].apply(e0, [e0, e0, opts, true]);

        if (opts.click && !opts.next)
            opts.next = opts.click;
        if (opts.next)
            $(opts.next).bind('click', function(){return advance(els,opts,opts.rev?-1:1)});
        if (opts.prev)
            $(opts.prev).bind('click', function(){return advance(els,opts,opts.rev?1:-1)});

        if (opts.timeout)
            this.cycleTimeout = setTimeout(function() {
                go(els,opts,0,!opts.rev)
            }, opts.timeout + (opts.delay||0));
    });
};

function go(els, opts, manual, fwd) {
    if (opts.busy) return;
    var p = els[0].parentNode, curr = els[opts.currSlide], next = els[opts.nextSlide];
    if (p.cycleTimeout === 0 && !manual)
        return;

    if (manual || !p.cyclePause) {
        if (opts.before.length)
            $.each(opts.before, function(i,o) { o.apply(next, [curr, next, opts, fwd]); });
        var after = function() {
            if ($.browser.msie)
                this.style.removeAttribute('filter');
            $.each(opts.after, function(i,o) { o.apply(next, [curr, next, opts, fwd]); });
        };

        if (opts.nextSlide != opts.currSlide) {
            opts.busy = 1;
            $.fn.cycle.custom(curr, next, opts, after);
        }
        var roll = (opts.nextSlide + 1) == els.length;
        opts.nextSlide = roll ? 0 : opts.nextSlide+1;
        opts.currSlide = roll ? els.length-1 : opts.nextSlide-1;
    }
    if (opts.timeout)
        p.cycleTimeout = setTimeout(function() { go(els,opts,0,!opts.rev) }, opts.timeout);
};

// advance slide forward or back
function advance(els, opts, val) {
    var p = els[0].parentNode, timeout = p.cycleTimeout;
    if (timeout) {
        clearTimeout(timeout);
        p.cycleTimeout = 0;
    }
    opts.nextSlide = opts.currSlide + val;
    if (opts.nextSlide < 0) {
        opts.nextSlide = els.length - 1;
    }
    else if (opts.nextSlide >= els.length) {
        opts.nextSlide = 0;
    }
    go(els, opts, 1, val>=0);
    return false;
};

$.fn.cycle.custom = function(curr, next, opts, cb) {
    var $l = $(curr), $n = $(next);
    $n.css({opacity:0});
    var fn = function() {$n.animate({opacity:1}, opts.speedIn, opts.easeIn, cb)};
    $l.animate({opacity:0}, opts.speedOut, opts.easeOut, function() {
        $l.css({display:'none'});
        if (!opts.sync) fn();
    });
    if (opts.sync) fn();
};

$.fn.cycle.transitions = {
    fade: function($cont, $slides, opts) {
        $slides.not(':eq(0)').css('opacity',0);
        opts.before.push(function() { $(this).show() });
    }
};

$.fn.cycle.ver = function() { return ver; };

// @see: http://malsup.com/jquery/cycle/lite/
$.fn.cycle.defaults = {
    timeout:       4000,
    speed:         1000,
    next:          null,
    prev:          null,
    before:        null,
    after:         null,
    height:       'auto',
    sync:          1,
    fit:           0,
    pause:         0,
    delay:         0,
    slideExpr:     null
};

})(jQuery);



var open = false;


$(document).ready(function() {

	Cufon.replace('#mainmenu a,.cr_bottom a,#sponsors p a, .subteaser_container a, .cl_footer_sub a, .cl_footer a,.cr_bottom p, .sp_main_navi a, .sp_sub_navi a', {
	hover: true,
	fontFamily: 'Proto Sans 20'
});


Cufon.replace('.cl_head h1', {
	hover: true,
	fontFamily: 'Proto Sans 23'
});

$(".mainmenulink").mouseenter(function(){
  $("#menu_bottom").stop(true,true);
  $("#mainmenu").find("a").removeClass("hover");
  caroussell_in($(this).attr("id"));
});

$("#mainmenu ul li").mouseenter(function(){
  $("#mainmenu ul li a").removeClass("open");
  $(this).find("a").addClass("open");

});


$("#mainmenu ul li").mouseleave(function(){
  //$(this).find("a").removeClass("hover");

});


$(".wrapper_short_head").click(function(){location.href = "http://www.tagderlegenden.de/news/" ;});



  $("img").load(function() {
            var imageHeight = $(this).height();
        });


$("#menu_bottom").slideUp(1);



  $(".sponsors_caroussel").jCarouselLite({
              auto: 4000,
              speed: 500,
              visible: 5,
              /*easing: "elasinout",*/
              scroll: 2
              });

		$('.sliderContent').innerfade({
						animationtype: 'fade',
						speed: 2000,
						timeout: 4000,
						type: 'random',
						containerheight: '150px'
					 });


	  $('.subteaser_images').innerfade({
						animationtype: 'fade',
						speed: 2000,
						timeout: 4000,
						type: 'random',
						containerheight: '178px'
					 });


		$('.teaser_imsages').innerfade({
						animationtype: 'fade',
						speed: 12000,
						timeout: 4000,
						type: 'random',
						containerheight: '120px'
					 });


					 function test(id){
           //return $(".teaser_images img").attr("height");
					 }



      $('.cr_content ul.teaser_images').each(function(){
        imgheight=($(this).find('li img').attr("height"));


        $(this).parent().parent().find('.cr_content_overlay').css("height",imgheight);
          $(this).css("height",imgheight-10);
        $(this).parent().parent().find('.cr_content_overlay').css("margin-top",'-8px');
        if (($(this).find('li img').size())<2){
              $(this).parent().parent().find('.cr_content_overlay').css("height",'0px');
        }
        if (($(this).find('li a').size())<1){
        $(this).parent().parent().find('.cr_content_overlay').css("height",'0px');
          }

      });


     $('ul.teaser_images').cycle();



		$('.sliderwrap').mouseenter(function(){
			 $(this).find('.sliderwrap2').hide();
			 });

		$('.sliderwrap').mouseleave(function(){
			 $('.sliderwrap2').show();
			 });


    $(".sponsors_caroussel2").jCarouselLite({
            auto: 3000,
            speed: 500,
            visible: 3,
            /*easing: "elasinout",*/
            scroll: 1
            });


	$("#block_sm li:not(.overs)").mouseenter(function(){
			$(this).hide();
			$(this).next(".overs").show();
			});

  $("#block_sm li.overs").mouseleave(function(){
			$(this).hide();
			$(this).prev().show();
			});



	$(".cr_content").mouseenter(
			function(){
			$(this).find(".cr_content_overlay").hide();
			});

	$(".cr_content").mouseleave(
			function(){
			$(".cr_content_overlay").show();
			});

	$(".subteaser_container").mouseenter(
			function(){

			$(this).find(".subteaser_content_overlay").hide();
			});

	$(".subteaser_container").mouseleave(
			function(){
			$(".subteaser_content_overlay").show();
			});



			$(".wrapper_short_head").mouseenter(
			function(){
        //$("#menu_bottom").css('z-Index','-1');
        //$("#menu_bottom").css('margin-top','-175px');
			  $("#menu_bottom").slideUp(400,function(){
			  $("#menu_bottom").hide();
			  open=false;
			  });

    		});


    	$("#mainteaser,#content").mouseenter(function(){
    	  if ( $("#mainteaser").length > 0 ) {
    	  $("#menu_bottom").css('z-Index','-1');
    	  }
			  $("#menu_bottom").slideUp(300,function(){
			  $("#menu_bottom").hide();
			  open=false;


			  });
    		});

			$("#menu_bottom").mouseleave(function(){
			  if ( $("#mainteaser").length > 0 ) {
    	  $("#menu_bottom").css('z-Index','-1');
    	  }
		  $("#menu_bottom").slideUp(300,function(){
		  $("#menu_bottom").hide();
			  });

			  open=false;

    		});


			load_menues();
});


function load_menues(){
 for (var i = 0; i <= 200; i++){

  if (($("#subNavi_" + i + " ul li").length)) {


        $("#subNavi_" + i).before('<div class="arrow rw" id="rw_' + i + '" style="float:left"></div>');
        $("#subNavi_" + i).after('<div class="arrow ff" id="ff_' + i + '"  style="float:right"></div>');


     $("#subNavi_" + i).jCarouselLite({
  		 btnNext: "#ff_" + i,
       btnPrev: "#rw_" + i,
       speed: 400,
       visible: 5,
       scroll: 1
       });


     $("#subNavi_" + i).addClass('caroussell');
     $("#subNavi_" + i).show();
     $("#subNavi_" + i).css('left','0px');

   }

	}
	$("#menu_bottom").css('margin-left','0px');

	$('.caroussell').hide();


}

	function caroussell_in(id){

	  $(".mainmenulink a").removeClass("open");
	  $(".mainmenulink a").removeClass("active");
	  $("#"+id).find("a").addClass("active");



	  /*TDL-Men�punkt verschieben*/
  id = id.substring(3);

   $("#menu_bottom").css('z-index','-21');
	  if ($("#subNavi_" + id).is('.caroussell') )
	  {
        if ($("#mainteaser").length) {
          //$("#menu_bottom").css('z-Index','-1');
           }


      $("#menu_bottom").stop(true,true).slideUp(0,function() {
open=false;
      if ($("#mainteaser").length){
        $("#menu_bottom").css('top','495px');
    //  $("#menu_bottom").css('z-Index','-1');
        $("#menu_bottom").css('margin-top','-175px');
        }


        else {
          $("#menu_bottom").css('top','211px');
          $("#menu_bottom").css('z-Index','50000');
          $("#menu_bottom").css('margin-top','0px');
         }

  	  $(".caroussell").not("#subNavi_" + id).hide();
  	  $("#subNavi_" + id).show();
      $("#menu_bottom").slideDown(590,function(){
        open=true;
        if ($("#mainteaser").length){
         $("#menu_bottom").css('z-Index','1');
        }
      });

       $(".arrow").hide();

       if(($("#subNavi_" + id + " ul li").length)>10)
        {
          $("#ff_" + id).show();
          $("#rw_" + id).show();
        }
    });
  }
  else{
	 $("#menu_bottom").css('z-Index','-21');
   $("#menu_bottom").stop(true,true).slideUp(30);
   open=false
  }
  }

    function caroussell_out(id){

     $("#menu_bottom").css('z-Index','-21');
     open:false;
    }

function timedCount()
{
//alert(open);
t=setTimeout("timedCount()",4000);
}

function showGal($galid)
{
	if($("#imgdump").length == 0)
	{
		$(document.body).append('<div id="imgdump"></div>');
	}


	IMGTITLES = {};

	$("#imgdump").empty();

	var content = galleries[$galid];
	for(var i = 0,o; o = content[i]; i++)
	{
		$("#imgdump").append('<a class="gal2item" href="uploads/tx_gruengal/'+o.img+'" rel="shadowbox[gal-'+$galid+']" title="'+gallerytitles[$galid] +'"></a>');
		IMGTITLES[window.location.protocol+'//'+window.location.hostname+'/uploads/tx_gruengal/'+o.img] = o.title;
	}

	Shadowbox.gallery = [];
	Shadowbox.cache = [];

	Shadowbox.setup("a.gal2item",{
        displayNav : true,
        gallery : 'shadowbox[gal-'+$galid+']'
    });

	Shadowbox.open($("a.gal2item")[0]);


	//setTimeout('pauseShadowBoxAfterOpen()',2000);

}


  function thisMovie(movieName) {
		         if (navigator.appName.indexOf("Microsoft") != -1) {
		             return window[movieName];
		         } else {
		             return document[movieName];
		         }
		     }


		     function openPlaneByID(value) {
		       $("#azlist").fadeOut(100);
		         thisMovie("tdlGalleryNAME").openPlaneByID(value);
		     }


		     function azlist_toggle(){
					$("#azlist").fadeIn(100);

			 }

  function getUrlVars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('?');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
    }


function animationComplete(bool){
$.ajax({
  url: "http://www.tagderlegenden.de/2010/index.php?eID=halloffamexml&legende="+(getUrlVars()[0]),
  success: function(data) {
    if (data!='') openPlaneByID(data);
  }
});




}
timedCount();
