/**
 *
 */
/* create a timeOut function in jQuery */
jQuery.fn.idle = function(time) {
	return this.each(function() {
		var i = $(this);
		i.queue(function() {
			setTimeout(function() {
				i.dequeue();
			}, time);
		});
	});
};

jQuery.browser.msie6 = $.browser.msie&&($.browser.version == "6.0")&&!window.XMLHttpRequest;
jQuery.browser.msie7 = $.browser.msie&&($.browser.version == "7.0");

var wwsg = {

	/**
	 * Function which will be called by the $(document).ready
	 */
	onReadyFunction: function() {
//		wwsg.initMenuBehavior();
		wwsg.initSearchBehavior();
		wwsg.initEmotionChanger();
		wwsg.initDownloadLinks();
		wwsg.initScrollable();
	},

	onReadyFunctionStart: function() {
		wwsg.hideAllBoxes();
		wwsg.showBoxesAnimated();
	},

	initMenuBehavior: function(){
		$(".boxes-behind a").click(function(event){
			var boxesInFront = $('#content-box').css('z-index');
			if (boxesInFront < 200) {
				event.preventDefault();
				var href = $(this).attr('href');
				wwsg.moveBoxesBehindContentBox(href);
			}

		});

		$(".boxes-moveinfront a").click(function(event){
			var boxesInFront = $('#content-box').css('z-index');
			if (boxesInFront > 100) {
				event.preventDefault();
				var href = $(this).attr('href');
				wwsg.moveBoxesInFrontOfContentBox(href);
			}

		});		
		
	},

	initSearchBehavior: function(){
		if($('#boxesBehindOnStart').length > 0){
			//$('#content-box').css('z-index','900');
			wwsg.setZIndexSpc('#content-box','900');
			$("#box-back").show();			
			$("#box-front").hide();
			
			$('#search').css('z-index','500');
			$('#follow').css('z-index','500');		
		} else {
			//$('#content-box').css('z-index','1');
			wwsg.setZIndexSpc('#content-box','100');
			$("#box-front").show();
			$("#box-back").hide();

			$('#search').css('z-index','500');
			$('#follow').css('z-index','500');
		}
		
		$("#box-front").click(function(event) {
      wwsg.moveBoxesBehindContentBox();		  
		$(this).hide();
		$("#box-back").show();
		})
		
		$("#box-back").click(function(event) {
      wwsg.moveBoxesInFrontOfContentBox();
      $(this).hide();
      $("#box-front").show();
		})
		
	},
	
	initScrollable: function(){
		$("div.scrollable").scrollable({
			size: 3,
			loop: true
		});
		
		/*latest news*/
		$('.news-latest-item .image img').each(function(){
			var width = $(this).attr('width');
			var height = $(this).attr('height');
			if(width > 86){
				
				$(this).css('left', -1*(width-86)/2) ;
			}
			if(height > 86){
				$(this).css('top', -1*(height-86)/2 );				
			}
			
		});
		
	},

	hideAllBoxes: function() {
		$('#container > div').css({
			display:'none',
			opacity: 0
		});
	},

	showBoxesAnimated: function(){
		wwsg.showVivescoLogo(0);
		wwsg.showHeader(250);
		wwsg.showPharmacyLogo(500);
		wwsg.showContent(750);
		wwsg.showMenu(1000);
		wwsg.showEmotion(1250);
		wwsg.showSearch(1500);
		wwsg.showFollow(1750);
	},

	showHeader: function(timeout) {
		$('#header')
			.idle(timeout)
			.css('display', 'block')
			.animate({opacity:1}, 250);
			setTimeout("$('#header').css('filter', 'none');", timeout);			
	},

	showPharmacyLogo: function(timeout) {
		$('#pharmacy-logo')
			.idle(timeout)
			.css('display', 'block')
			.animate({opacity:1}, 250);
			setTimeout("$('#pharmacy-logo').css('filter', 'none');", timeout);			
	},

	showMenu: function(timeout) {

		$('#menu')
			.idle(timeout)
			.css({
				display: 'block',
				left: (parseInt($('#menu').css('left')) - 100)
			})
			.animate(
				{
					opacity: 1,
					left: 0
				},
				250
			);
			setTimeout("$('#menu').css('filter', 'none');", timeout);			
	},

	showContent: function(timeout) {
		$('#content-box')
			.idle(timeout)
			.css('display', 'block')
			.animate({opacity:1}, 250);
			
		setTimeout("$('#content-box').css('filter', 'none');", timeout);	
		
	},

	showSearch: function(timeout) {		
		$('#search')
			.idle(timeout)
			.css({
				display: 'block',
				right: (parseInt($('#search').css('right')) - 100)
			})
			.animate(
				{
					opacity: 1,
					right: 0
				},
				250
			);
		setTimeout("$('#search').css('filter', 'none');", timeout);	
	},

	showFollow: function(timeout) {	
		$('#follow')
			.idle(timeout)
			.css({
				display: 'block',
				right: (parseInt($('#follow').css('right')) - 100)
			})
			.animate(
				{
					opacity: 1,
					right: 0
				},
				250
			);
		setTimeout("$('#follow').css('filter', 'none');", timeout);	
			
	},

	showEmotion: function(timeout) {
		$('#emotion')
			.idle(timeout)
			.css('display', 'block')
			.animate({opacity:1}, 250);
		setTimeout("$('#emotion').css('filter', 'none');", timeout);		
	},

	showVivescoLogo: function(timeout) {
		$('#vivesco-logo')
			.idle(timeout)
			.css('display', 'block')
			.animate({opacity:1}, 250);
		setTimeout("$('#vivesco-logo').css('filter', 'none');", timeout);			
	},

	moveBoxesBehindContentBox: function(href) {

		$('#search')
			.animate(
				{right: -160},
				200,
				'swing',
				function(){
					//$('#content-box').css('z-index','1');
					wwsg.setZIndexSpc('#content-box','100');
					
					$('#search').animate(
						{right:0},
						200
					);
				}
			);

		$('#follow')
			.idle(100)
			.animate(
				{right: -160},
				200,
				'swing',
				function(){
					//$('#content-box').css('z-index','900');
					wwsg.setZIndexSpc('#content-box','900');

					$('#follow').animate(
						{right:0},
						200,
						'swing',
						function(){
							if (typeof(href) == 'string') {
								//location.href = '/' + href;
								wwsg.navigateWithReferrer('/' + href);
							}
						}
					);
				}
			);
	},

	moveBoxesInFrontOfContentBox: function(href) {

		$('#search')
			.idle(100)
			.animate(
				{right: -160},
				200,
				'swing',
				function(){
					//$('#content-box').css('z-index','1');
					wwsg.setZIndexSpc('#content-box','100');
					
					$('#search').animate(
						{right:0},
						200,
						'swing',
						function(){
							if (typeof(href) == 'string') {
								// location.href = '/' + href;
								wwsg.navigateWithReferrer('/' + href);
							}
						}
					);
				}
			);

		$('#follow')
			.animate(
				{right: -160},
				200,
				'swing',
				function(){
					//$('#content-box').css('z-index','1');
					wwsg.setZIndexSpc('#content-box','100');
					
					$('#follow').animate({right:0},	200);
				}
			);
	},
	
	
	setZIndexSpc: function(selector,index){
			$(selector).css('z-index',index);
			var prev1 = $(selector).prev();	
			var prev2 = $(prev1).prev();

			//$(prev1).css('z-index',index);
			//$(prev2).css('z-index',index);			
	},

	navigateWithReferrer: function(url){
		var fakeLink = document.createElement ("a");
		if (typeof(fakeLink.click) == 'undefined') {
			location.href = url; // sends referrer in FF, not in IE
		} else {
			fakeLink.href = url;
			document.body.appendChild(fakeLink);
			fakeLink.click(); // click() method defined in IE only
		} 
	},

	
	initEmotionChanger: function(){
		wwsg.emotions = {};
		wwsg.emotions.items = $('#emotion .item .emotionimagebox');
		wwsg.emotions.textitems = $('#emotion .item .textimagebox');

		wwsg.emotions.textitems.each(function(){
			$("#header").append($(this));
		});
		
		if(wwsg.emotions.items.length > 1){
			wwsg.emotions.items.hide();
			wwsg.emotions.textitems.hide();

			$(wwsg.emotions.items[0]).show();
			$(wwsg.emotions.textitems[0]).show();
			wwsg.emotions.current = 0;
			setTimeout('wwsg.emotionNextItemActivate()', 10000);
		}

	},
	
	emotionNextItemActivate: function(){
				
			if (!jQuery.browser.msie6 && !jQuery.browser.msie7) {
				$(wwsg.emotions.items[wwsg.emotions.current]).fadeOut();
				$(wwsg.emotions.textitems[wwsg.emotions.current]).fadeOut();
			} else {
				$(wwsg.emotions.items[wwsg.emotions.current]).hide();
				$(wwsg.emotions.textitems[wwsg.emotions.current]).hide();				
			}			
			next = wwsg.emotions.current + 1;
			if(next >= wwsg.emotions.items.length){
				next = 0;
			}
			if(!jQuery.browser.msie6 && !jQuery.browser.msie7){
				$(wwsg.emotions.items[next]).fadeIn();
				$(wwsg.emotions.textitems[next]).fadeIn();				
			} else {
				$(wwsg.emotions.items[next]).show();
				$(wwsg.emotions.textitems[next]).show();
			}

			wwsg.emotions.current = next;
			setTimeout('wwsg.emotionNextItemActivate()', 10000);
	},
	
	initDownloadLinks: function(){
		$('.download').each(function(){
			var downloadlink = $(this).attr("href");
			var downloadclass =  'download-' + downloadlink.substr(downloadlink.lastIndexOf('.')+1);

			$(this).addClass(downloadclass);
		});
	},
	
	yt_init: function(){		
		$("ul.tabs").tabs("div.smw-youtube-tab > div");
		
		setTimeout(function() {
			$('.play_img').mouseover(function() {
				$(this).removeClass('play_img').addClass('play_img_hover');
			}).mouseout(function() {
				$(this).removeClass('play_img_hover').addClass('play_img');
			});
			$('.play_img_small').mouseover(function() {
				$(this).removeClass('play_img_small').addClass('play_img_hover_small');
			}).mouseout(function() {
				$(this).removeClass('play_img_hover_small').addClass('play_img_small');
			});
		
		}, 3000);
		
		
	}
	
	
	
};





if(document.referrer.search(/wir-wollen-sie-gesund/) == -1 || document.referrer.search(/vivesco/) == -1){
	if (!jQuery.browser.msie6) {
//		$(document).ready(wwsg.onReadyFunctionStart);
	}
}

$(document).ready(function(){
	$(document).ready(wwsg.onReadyFunction);
	wwsg.yt_init();
	
	if ($(".google_search_form_layout_1").html() != null) {
		$("#emotion").html("");
	}
});
