// IMAGEFLOW is configured in imageflow.js at the bottom of the code

var cf; // coverflow object

function showCoverFlowText (){

	var text = cf.CurrentCoverFlowItem().Title;
	jQuery('#coverflowlabel').html(text);
	/*
	var htm = "";
	htm += "CurrentCoverIndex = " + cf.CurrentCoverIndex() + "<br/>";
	htm += "Src = " + cf.CurrentCoverFlowItem().Src  + "<br/>";
	htm += "Title = " + cf.CurrentCoverFlowItem().Title  + "<br/>";
	htm += "Href = " + cf.CurrentCoverFlowItem().Href  + "<br/>";
	testDiv.innerHTML = htm;*/
}

// This is my CoverFlow callback function. This is called when a cover is clicked on...
function coverClick (c){
	//console.log(cf.CurrentCoverIndex());
	//console.log(c.CoverIndex);
	return false;
}

// The CoverFlow Update method does not call itself. It's up to you to schedule this...
function update ()
{
	cf.Update();
	showCoverFlowText ();
	setTimeout("update()", 20);
}

function explore_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

var SlideShowTimeout = function(currSlideElement, nextSlideElement, options, forwardFlag){

	return 5000;

	// all steps have the same timer now
	/*var $this = jQuery(currSlideElement),
	id = $this.attr('id');
	if(id == 'slideshow_img_04'){
		return 10000;
	}else{
		return 5000;
	}*/
}
/*
var SlideShowBefore = function(currSlideElement, nextSlideElement, options, forwardFlag){
	var $this = jQuery(nextSlideElement),
	id = $this.attr('id');
	if(id == 'slideshow_img_05'){
		jQuery('.box-hd-preview').hide();
	}else{
		jQuery('.box-hd-preview').show();
	}
}
*/
jQuery(document).ready(function(){


	// VIDEOSLIDER
/*    jQuery('div.thumbs ul li a img').after( jQuery('<div class="thumb_overlay hide"></div>') );
    jQuery('div.thumbs ul li a div.thumb_overlay').hover(
		function() {
			jQuery(this).animate(
				{
					opacity: 1.0,
					top: "0px",
					height: "84px"
				} , 50
			);
		},
		function() {
			jQuery(this).animate(
				{
					opacity: 0.0,
					top: "-20px",
					height: "104px"
				} , 350
			);
		}
	);
*/
    // NAVI
	var idName = [],
	idSubName = [],
	activetimeout,
	str_mouseout = "jQuery('#submenu ul').css( 'display', 'none' );" +
	    	  						"jQuery('#submenu ul li').css( 'display', 'none' );" +
	    	  						"jQuery('#submenu ul#' + activesubmenuitemid).css( 'display', 'inline' ).children('li').css( 'display', 'inline' );" +
	    	  						"jQuery('#mainnav li a').removeClass('underline');" +
	    	  						"jQuery(this).children('a').eq(0).addClass('underline');";

   jQuery("#mainnav ul").eq(0).children("li").each( function(index){

       idName[index] = "menu-item-" + (index + 1);
       idSubName[index] = "sub" + idName[index];
       jQuery(this).attr('id', idName[index]);
       jQuery('ul', this).eq(0).attr('id', idSubName[index]).appendTo('#submenu');

       jQuery(this).hover(
       		function(e){
       		   clearTimeout(activetimeout);
	    	   jQuery('#submenu ul').css( 'display', 'none' );
	           jQuery('#submenu ul li').css( 'display', 'none' );
	           jQuery('#submenu ul#' + idSubName[index]).css( 'display', 'inline' ).children('li').css( 'display', 'inline' );
	           jQuery("#mainnav li a").removeClass('underline');
	           jQuery(this).children('a').eq(0).addClass('underline');
	        },
	        function(e){
	    	  activetimeout = setTimeout(str_mouseout, 1000);
	        });
   });

   jQuery('#submenu').mouseover(function() {
   		clearTimeout(activetimeout);
   	}).mouseout(function() {
   		activetimeout = setTimeout(str_mouseout, 1000);
   	});

   // Show active submenu
  	var activemenuitemid = jQuery('#mainnav li[class*=cur]').attr('id');
  	activesubmenuitemid = 'sub' + activemenuitemid;
   	jQuery('#submenu ul#' + activesubmenuitemid).css( 'display', 'inline' ).children('li').css( 'display', 'inline' );

   //jQuery('#submenu ul#submenu-item-2 li.first').prepend( '<span id="submenu-prefix">Suche nach: </span>' );
	// NAVI end

	// expertentipp
   // Only corner top right is rounded now
	jQuery("div#boxes").corner("tr 10px");
	jQuery("div#boxes div").css("border-color", "#CBCBCB"); // safari round-corner-background-color-bug
	jQuery("div#boxes .teaser").attr("style", "");

	// flexboxes
	jQuery('.prev-flexbox').click(function() {
		var flexbox_cat = jQuery(this).attr('rel');
		jQuery('.content-flexbox.category-' + flexbox_cat).fadeOut('slow', function() {
			jQuery('.content-flexbox.category-' + flexbox_cat).load(jQuery('.prev-flexbox-hidden.category-' + flexbox_cat).attr('href'), '', function() {
				jQuery('.content-flexbox.category-' + flexbox_cat).fadeIn('slow');
			});
		});
		return false;
	});
	jQuery('.next-flexbox').click(function() {
		var flexbox_cat = jQuery(this).attr('rel');
		jQuery('.content-flexbox.category-' + flexbox_cat, this.parent).fadeOut('slow', function() {
			jQuery('.content-flexbox.category-' + flexbox_cat).load(jQuery('.next-flexbox-hidden.category-' + flexbox_cat).attr('href'), '', function() {
				jQuery('.content-flexbox.category-' + flexbox_cat).fadeIn('slow');
			});
		});
		return false;
	});

	// webbook-overview
	jQuery('.prev-wb').click(function() {
		jQuery('.wb-content').fadeOut('normal', function() {
			jQuery('.wb-content').load(jQuery('.prev-wb-hidden').attr('href'), '', function() {
				jQuery('.wb-content').slideDown(1500);
			});
		});
		return false;
	});
	jQuery('.next-wb').click(function() {
		jQuery('.wb-content').fadeOut('normal', function() {
			jQuery('.wb-content').load(jQuery('.next-wb-hidden').attr('href'), '', function() {
				jQuery('.wb-content').slideDown(1500);
			});
		});
		return false;
	});

	// FLEXITABS TABS
	var $tabs = jQuery("div#flexitabs").tabs({
		fx: { opacity: 'toggle' }
	});
	//$tabs.tabs('select', 3); // switch to 4. tab

	jQuery("div#flexitabs ul li:first").addClass("first");
	jQuery("div#flexitabs ul li:last").addClass("last");

	jQuery("#columnright div.navitabs").each( function(i){
			if(i > 3) return;
			jQuery(this).attr('id', 'flexitabs' + i);
	});

	// FLEXITABS TABS 0 GES�NDER ESSEN (100 FRAGEN)
	$tabs0 = jQuery("div#flexitabs0").tabs({
		fx: { opacity: 'toggle' }
	});

	// FLEXITABS TABS 1 GES�NDER ESSEN
	$tabs1 = jQuery("div#flexitabs1").tabs({
		fx: { opacity: 'toggle' }
	});

	// FLEXITABS TABS 2 KL�GER ABNEHMEN
	$tabs2 = jQuery("div#flexitabs2").tabs({
		fx: { opacity: 'toggle' }
	});

	// FLEXITABS TABS 3 PRESSESCHAU
	$tabs3 = jQuery("div#flexitabs3").tabs({
		fx: { opacity: 'fast' },
		show: setvcenter
	});

	// FLEXITABS TABS 4 EXPERTENTIPPS
	$tabs4 = jQuery("div#experts").tabs({
		fx: { opacity: 'toggle' }
	});

	// BUILD TABS NAVIGATION
	jQuery('#flexitabs0 a.prev').click(function(){
		selected = $tabs0.tabs('option', 'selected');
		if(selected == 0){
			selected = 4;
		}else{
			selected -= 1;
		}
		$tabs0.tabs('select', selected);
		return false;
	});

	jQuery('#flexitabs0 a.next').click(function(){
		selected = $tabs0.tabs('option', 'selected');
		if(selected == 4){
			selected = 0;
		}else{
			selected += 1;
		}
		$tabs0.tabs('select', selected);
		return false;
	});

	jQuery('#flexitabs1 a.prev').click(function(){
		selected = $tabs1.tabs('option', 'selected');
		if(selected == 0){
			selected = 4;
		}else{
			selected -= 1;
		}
		$tabs1.tabs('select', selected);
		return false;
	});

	jQuery('#flexitabs1 a.next').click(function(){
		selected = $tabs1.tabs('option', 'selected');
		if(selected == 4){
			selected = 0;
		}else{
			selected += 1;
		}
		$tabs1.tabs('select', selected);
		return false;
	});

	jQuery('#flexitabs2 a.prev').click(function(){
		selected = $tabs2.tabs('option', 'selected');
		if(selected == 0){
			selected = 4;
		}else{
			selected -= 1;
		}
		$tabs2.tabs('select', selected);
		return false;
	});

	jQuery('#flexitabs2 a.next').click(function(){
		selected = $tabs2.tabs('option', 'selected');
		if(selected == 4){
			selected = 0;
		}else{
			selected += 1;
		}
		$tabs2.tabs('select', selected);
		return false;
	});

	jQuery('#flexitabs3 a.prev').click(function(){
		selected = $tabs3.tabs('option', 'selected');
		if(selected == 0){
			selected = 5;
		}else{
			selected -= 1;
		}
		$tabs3.tabs('select', selected);
		return false;
	});

	jQuery('#flexitabs3 a.next').click(function(){
		selected = $tabs3.tabs('option', 'selected');
		if(selected == 5){
			selected = 0;
		}else{
			selected += 1;
		}
		$tabs3.tabs('select', selected);
		return false;
	});

	jQuery('#experts a.prev').click(function(){
		selected = $tabs4.tabs('option', 'selected');
		if(selected == 0){
			selected = 8;
		}else{
			selected -= 1;
		}
		$tabs4.tabs('select', selected);
		return false;
	});

	jQuery('#experts a.next').click(function(){
		selected = $tabs4.tabs('option', 'selected');
		if(selected == 8){
			selected = 0;
		}else{
			selected += 1;
		}
		$tabs4.tabs('select', selected);
		return false;
	});

	if(jQuery.browser.msie) {
		jQuery('#experts a.next').trigger('click');
		jQuery('#experts a.prev').trigger('click');
	}

	// FLEXITABS Slideshow
	//window.setTimeout("boxtabs_cycle('eatsmarterbox', 1, 4)", 5000, 100);
	//window.setTimeout("boxtabs_cycle('articlebox', 1, 5)", 5000, 100);

	// SLIDESHOW
	jQuery('.slides').eq(0).cycle({
		fx:     'fade',
		speed:  'slow',
		//before: SlideShowBefore, // Callback
		timeoutFn: SlideShowTimeout, // use timout function to slow down ads
		pager:  '#slideshow-controller'
	});
/*
	// EXPERTENVIDEOS
	jQuery('div #recipeslider a').lightpop( {
		 overlayBgColor:'#95BD4D'
		,contentFrameType:'box'
		,txtOf: 'von'
		,imageLoading:'../../js/jquery.lightpop/images/lightpop-ico-loading.gif'
		,imageBtnClose:'../../js/jquery.lightpop/images/lightpop-btn-close.gif'
	});
*/


	/*jQuery('#experttipps').jcarousel({
		scroll: 1,
        auto: 0,
        wrap: 'both'
    });*/

	jQuery('#explore').jcarousel({
		scroll: 1,
        auto: 15,
        wrap: 'both',
        initCallback: explore_initCallback
    });

    jQuery('.userlogin input.logout').removeAttr('onclick');

	jQuery('.userlogin input.login').removeAttr('onclick').click(function() {
		jQuery('.userlogin').hide();
		jQuery('#hp-register').hide();
		jQuery('#frmRezeptSchnellSuche').hide();
		jQuery('#divHeaderMessage').hide();
		jQuery('#head').css('height','338px');
		jQuery('#headright .loginbox').show();
		return false;
	});

	jQuery('.userlogin input.register').removeAttr('onclick').click(function() {
		jQuery('.userlogin').hide();
		jQuery('.loginbox').hide();
		jQuery('#frmRezeptSchnellSuche').hide();
		jQuery('#divHeaderMessage').hide();
		jQuery('#head').css('height','532px');
		jQuery('#headright').css('overflow','visible');
		jQuery('#headright #hp-register').show();
		return false;
	});

	// show error-message after login-error
	jQuery('.tx-felogin-pi1 .login-inner .messages div').each(function() {
		if(jQuery(this).text() != '' && jQuery(this).text() != 'Sie haben sich erfolgreich abgemeldet.')
			jQuery('.userlogin input.login').click();
	});

	jQuery('.tx-srfeuserregister-pi1-error').each(function() {
		if(jQuery(this).text() != '')
			jQuery('.userlogin input.register').trigger('click');
	});

	jQuery('.register-message-complete').each(function() {
		jQuery('.userlogin input.register').trigger('click');
	});

	if(jQuery.trim(jQuery('.reg-error-wrap ul').html()) == '') {
		jQuery('.reg-error-wrap').remove();
	}

	jQuery('.forgot-reg-wrapper .registration a').click(function() {
		jQuery('.loginbox').hide();
		jQuery('.userlogin input.register').trigger('click');
		return false;
	});

});



function lead_weiterempfehlen(){
	// home > lead > weiterempfehlen-link

	jQuery('body').append(jQuery('<div id="lead_weiterempfehlen_form" class="overlay-bg" style="display:block; opacity:0.01; filter: alpha(opacity=1);"></div>'));
	jQuery('body').append(jQuery('<div id="lead_weiterempfehlen_form_content">	<form onsubmit="jQuery(this).parent().fadeOut(\'slow\'); jQuery(\'#lead_weiterempfehlen_form\').fadeOut(\'slow\').remove(); return false;">		<div class="row">			<label for="email">Email</label>			<input name="email" type="text" size="30" maxlength="30" value="email@email.de">		</div>		<div class="row">			<label for="text">Nachricht</label>			<textarea name="text" cols="50" rows="10"></textarea>		</div>		<div class="row">			<input type="submit" value="Senden">		</div>	</form></div>'));

	jQuery('#lead_weiterempfehlen_form').fadeTo( "slow", 0.75, function(){

		jQuery('#lead_weiterempfehlen_form_content').center().show(); // show content

	} );

	return false;
}


function boxtabs_cycle(parent, boxtabs_act, boxtabs_max, timeout) {
	jQuery('.'+parent+' .tab_'+boxtabs_act).click();
        characterfade.start(jQuery('.'+parent+' .tab_'+(boxtabs_act++)+' span'));
        if (boxtabs_act>boxtabs_max) boxtabs_act = 1;
        window.setTimeout("boxtabs_cycle('"+parent+"', "+boxtabs_act+", "+boxtabs_max+")", 5000, timeout);
}


/** IMAGE PREVIEWS **/

imagePreview = function(){

	var xOffset = -560,
	yOffset = -30;

	jQuery("a.preview_book").hover(
		function(e){

			this.t = this.title;
			this.title = "";
			var c = (this.t != "") ? "<br/>" + this.t : "";

			jQuery("body").append( '<p id="preview" class="book"><img src="'+ this.href +'" alt="Image preview" />'+ c +'</p>' );
			jQuery("#preview")
				.css("top", 		(e.pageY - yOffset) + "px")
				.css("left", 		(e.pageX + xOffset) + "px")
				.css("position",	"absolute")
				.css("border",		"6px solid #ffffff")
				;

		},
		function(){
			this.title = this.t;
			jQuery("#preview")
				.remove()
				;
		});
	jQuery("a.preview").hover(
		function(e){

			this.t = this.title;
			this.title = "";
			var c = (this.t != "") ? "<br/>" + this.t : "";

			jQuery("body").append( '<p id="preview"><img src="'+ this.href +'" alt="Image preview" />'+ c +'</p>' );
			jQuery("#preview")
				.css("top", 		(e.pageY - yOffset) + "px")
				.css("left", 		(e.pageX) + "px")
				.css("position",	"absolute")
				.css("border",		"6px solid #ffffff")
				;

		},
		function(){
			this.title = this.t;
			jQuery("#preview")
				.remove()
				;
		});

		jQuery("a.preview-drinksmarter").hover(
		function(e){

			this.t = this.title;
			this.title = "";
			var c = (this.t != "") ? "<br/>" + this.t : "";

			jQuery("body").append( '<p id="preview"><img src="'+ this.href +'" alt="Image preview" />'+ c +'</p>' );
			jQuery("#preview")
				.css("top", 		(e.pageY - yOffset) + "px")
				.css("left", 		(e.pageX + xOffset) + "px")
				.css("position",	"absolute")
				.css("border",		"6px solid #ffffff")
				;

		},
		function(){
			this.title = this.t;
			jQuery("#preview")
				.remove()
				;
		});


		jQuery("a.preview_book").mousemove(function(e){

			var ti_winHeight	= jQuery(window).height(),
			ti_bigImgHeight	= 430;

			if( e.pageY-jQuery(window).scrollTop() < (ti_winHeight-ti_bigImgHeight) ){
			    xOffset = -320;
			    yOffset = -20;
			}else{
			    xOffset = -320;
			    yOffset = ti_bigImgHeight;
			}

			jQuery("#preview")
				.css("left",	(e.pageX + xOffset) + "px")
				.css("top", 	(e.pageY - yOffset) + "px")
				;
		});

		jQuery("a.preview").mousemove(function(e){

			var ti_winHeight	= jQuery(window).height(),
			ti_bigImgHeight	= 420;

			if( e.pageY-jQuery(window).scrollTop() < (ti_winHeight-ti_bigImgHeight) ){
			    xOffset = -560;
			    yOffset = -30;
			}else{
			    xOffset = -560;
			    yOffset = ti_bigImgHeight;
			}
			jQuery("#preview")
				.css("left",	(e.pageX ) + "px")
				.css("top", 	(e.pageY - yOffset) + "px")
				;
		});

		jQuery("a.preview-drinksmarter").mousemove(function(e){

			var ti_winHeight	= jQuery(window).height(),
			ti_bigImgHeight	= 420;

			if( e.pageY-jQuery(window).scrollTop() < (ti_winHeight-ti_bigImgHeight) ){
			    xOffset = -560;
			    yOffset = -30;
			}else{
			    xOffset = -560;
			    yOffset = ti_bigImgHeight;
			}
			jQuery("#preview")
				.css("left",	(e.pageX + xOffset) + "px")
				.css("top", 	(e.pageY - yOffset) + "px")
				;
		});

};

// starting the script on page load
jQuery(document).ready(function(){
    imagePreview();
    jQuery('.imglink').click(function(){
    	var href = jQuery(this).attr('href');
    	if (typeof href != 'undefined' && href != null)
    		location.href = href;
    });
});

var setvcenter = function(x,y) {
	  //Center vertically all items with class .vcenter
   jQuery('.vcenter').each( function(i){
	   jQuerythis = jQuery(this);
	   jQueryparent = jQuerythis.parent();
	   var childHeight = jQuerythis.height(),
	   parentHeight = jQueryparent.height(),
	   topMargin,
	   topMarginValue = parseInt((parentHeight - childHeight) / 2);
	   if (topMarginValue < 0) topMarginValue = 0;
	   topMargin = topMarginValue.toString() + 'px';
	   jQuerythis.css('margin-top', topMargin).css('visibility', 'visible');
   });
}