/* -----------------------------------------------------------------

  Global variables

 ----------------------------------------------------------------- */

	var animationSpeed = 'fast';
	var hoverOpacity = 1;
	var outOpacity = 0.8;
	var currentFeature = 0;
	var slideshowSpeed = 5000;
	var stopSlideshow = false;




/* -----------------------------------------------------------------

 Function used for the featured content on the homepage.

 ----------------------------------------------------------------- */

var $feature;
feature = {
	init : function(json){
		$feature = $('div.feature:first');
		
		// Bind the clicks on the nav
		$feature.find('ul.nav a').each(function(i){
			$(this).data('content',json['item'][i]);

			// Preload the images
			featureImage = new Image();
			featureImage.src = json['item'][i]['full_image'];

			$(this).click(function(){
				feature.toggle($(this));
				currentFeature = i;
				return false;
			})
			.hover(
				function(){
					$(this).stop().fadeTo(animationSpeed,hoverOpacity);
				},
				function(){
					if($(this).parent().hasClass('selected')) return;
					$(this).stop().fadeTo(animationSpeed, outOpacity);
				}
			);

			$feature.hover(
				function(){
					stopSlideshow = true;
				},
				function(){
					stopSlideshow = false;
					clearTimeout(t);
					t=setTimeout('feature.slideshow()',slideshowSpeed);
				}
			).click(function(){ location.href = $(this).find('a:first').attr('href'); });
		});

		// Init the slide show
		t=setTimeout('feature.slideshow()',slideshowSpeed);
	},
	toggle : function(caller){
		$caller = $(caller);
		
		if($caller.parent().hasClass('selected')) return; // Make sure user is not clicking the current active item

		// Get the json object
		json = $caller.data('content');
		
		// Preload the images
		featureImage = new Image();

		// Set the selected item in the nav
		$caller.parents('ul:first').find('li.selected').removeClass('selected').find('a').stop().fadeTo(animationSpeed, outOpacity);
		$caller.parent().addClass('selected').find('a').stop().fadeTo(animationSpeed, hoverOpacity);

		featureImage.onload = function(){
			// Fade the content out
			$firstImg = $feature.find('img:first');
			$firstImg.clone().insertAfter($firstImg).css({'opacity':0,'position':'absolute','top':0,'left':0});
			$secondImg = $feature.find('img:eq(1)');
			$secondImg.attr('src',json['full_image']);

			$firstImg.fadeTo(animationSpeed, 0);
			$secondImg.fadeTo(animationSpeed,hoverOpacity, function(){ $firstImg.remove(); });
			
			$feature.find('div.details').fadeTo(animationSpeed, 0, function(){
				// Replace the content
				$this = $(this);
				
				$this.find('p.date').html(json['date']);
				$this.find('h2').html(json['title']).removeClass('sIFR-replaced');

				Cufon.replace('.feature h2');

				$this.find('p em').html(json['description']);
				
				$this.find('h3:first').attr('class','hft_'+json['channel_name']);

				$this.find('a.b_suite').attr('href',json['link']);

				$('.feature .author a').attr('href',"/membres/"+json['author_link']).find('em').text("Par " + json['author']);
				if(json['credits']!=""){
				  $('.feature .credits').html("Cr&eacute;dit: <span class='vcard credit'><span class='fn'><cite>"+json['credits']+"</cite></span></span>");
        }else{
          $('.feature .credits').html("");
        }
				// Fade the content in
				$this.fadeTo(animationSpeed,hoverOpacity,function(){
					if($.browser.msie){
						if($.browser.version == 6){
							DD_belatedPNG.fixPng($feature.find('div.details .png:eq(0)')[0]);
							DD_belatedPNG.fixPng($feature.find('div.details .png:eq(1)')[0]);
						}
						$feature.find('div.details .png').css('opacity',0.99);
					}
				});
			});
		}
		featureImage.src = json['full_image'];
	},
	slideshow : function(){
		if(stopSlideshow) return;
		currentFeature++;
		if(currentFeature >= $feature.find('ul.nav a').size()) currentFeature = 0;
		$feature.find('ul.nav a:eq('+currentFeature+')').trigger('click');
		t=setTimeout('feature.slideshow()',slideshowSpeed);
	}
}


/* ------------------------------------------------------------------------
	To view the full comments
------------------------------------------------------------------------- */

comments = {
	init : function(){
		$('.comments li p:nth-child(3)').each(function(){
			if($(this).height() > 95 && 0){	// ajouté '&& 0' pour toujours avoir false car client ne veut plus cette fonctionnalité
				$(this).parent().data('commentHeight',$(this).height());
				$(this).css({
					'overflow':'hidden',
					'height': 95
				}).parent().find('.read_more').click(function(){
				  var el = $(this);
				  if(el.parent().find('p:nth-child(3)').height() > 95 ){
				   $('a.b_plus' ,el).text('Lire').css({'background': 'transparent url(/urbania/static/images/all/buttons/b_plus.gif) no-repeat scroll right top' });
           el.parent().find('p:nth-child(3)').animate({'height':95});
          }else{
            $('a.b_plus' ,el).text(' ').css({'background': 'transparent url(/urbania/static/images/all/buttons/b_moins.gif) no-repeat scroll right top' });
            el.parent().find('p:nth-child(3)').animate({'height':$(this).parent().data('commentHeight') + 17});
					}
          return false;
				});
			}else{
				$(this).height( $(this).height()+25);
				$(this).parent().find('.read_more').hide();
			}
		})
	}
}


/* ------------------------------------------------------------------------
	Magazine navigator
------------------------------------------------------------------------- */

	var currentMag = 0;
	var totalMag = 0;
	var inAnimation = false;
	var magSpacing = 10;
	var magAnimationSpeed = 250;
	magazine = {
		init : function() {
			totalMag = $('.magazine_navigator ul:first li').size();
			$('.magazine_navigator li:gt(0) p').hide();
			$('.magazine_navigator ul:first li a').each(function(i){
				$(this).css({
					'left': (totalMag*magSpacing) - (i*magSpacing),
					'z-index': (totalMag - i) + 1
				});
				
				if($.browser.msie) $(this).parent().css({'z-index': (totalMag - i) + 1});
			});
			
			$('.magazine_navigator li.prev a').click(function(){
				$('.magazine_navigator li.next a').hide();
				magazine.changePageDown();
			});
			
			$('.magazine_navigator li.next a').click(function(){
				$(this).hide();
				magazine.changePageUp();
			});
			
			$('.magazine_navigator li:first').addClass('selected');
		},
		initFeatured : function() {
			$('div.details').click(function(){ location.href = $(this).find('a:first').attr('href'); });
		},
		changePageUp : function() {
			if(inAnimation) return;
			inAnimation = true;
			
			currentMag ++;
			if(currentMag == totalMag) currentMag = 0;
			

			$('.magazine_navigator ul:first li:not(.selected) a').each(function(i){
				$(this).animate({'left':parseFloat($(this).css('left')) + magSpacing},magAnimationSpeed,function(){
					$(this).css('z-index',parseFloat($(this).css('z-index'))+1);
					if($.browser.msie) $(this).parent().css('z-index',$(this).css('z-index')+1);
				});
			});

			$('.magazine_navigator li.selected a').animate({'left':110},magAnimationSpeed,function(){
				$(this).css('z-index',1).animate({'left':magSpacing},magAnimationSpeed);
				if($.browser.msie) $(this).parent().css('z-index',1);
				setTimeout("$('.magazine_navigator li.next a').show()",225);
			});
			
			$('.magazine_navigator li.selected p').fadeOut(magAnimationSpeed,function(){
				$(this).parent().removeClass('selected');
				$('.magazine_navigator li:eq('+currentMag+') p').fadeIn(magAnimationSpeed,function(){
					$(this).parent().addClass('selected');
					inAnimation = false;
				});
			});
		},
		changePageDown : function() {
			if(inAnimation) return;
			inAnimation = true;
			
			currentMag --;
			if(currentMag < 0) currentMag = totalMag - 1;
			
			$('.magazine_navigator ul:first li.selected p').fadeOut(magAnimationSpeed,function(){
				$('.magazine_navigator li:eq('+currentMag+')').find('p').fadeIn(magAnimationSpeed);
			});
			$('.magazine_navigator ul:first li.selected').removeClass('selected');
			$('.magazine_navigator li:eq('+currentMag+')').addClass('selected');
			
			$('.magazine_navigator ul:first li:not(.selected) a').each(function(i){
				$(this).animate({'left':parseFloat($(this).css('left')) - magSpacing},magAnimationSpeed,function(){
					$(this).css('z-index',parseFloat($(this).css('z-index'))-1);
					if($.browser.msie) $(this).parent().css('z-index',$(this).css('z-index')-1);
					setTimeout("$('.magazine_navigator li.next a').show()",275);
				});
			});
			
			$('.magazine_navigator ul:first li:eq('+currentMag+') a').animate({'left':100},magAnimationSpeed,function(){
				$(this).css('z-index',totalMag + 1);
				if($.browser.msie) $(this).parent().css('z-index',totalMag + 1);
				$(this).animate({'left':totalMag * magSpacing},magAnimationSpeed,function(){
					inAnimation = false;
				});
			});
		}
	}
	
	
/* ------------------------------------------------------------------------
	For the tabs
------------------------------------------------------------------------- */

	var tabs = {
		hideSection: function(el){
			$(el).parent().parent().slideUp("slow");
		}
	}
	

/* ------------------------------------------------------------------------
	For the login link in every page
------------------------------------------------------------------------- */

	login = {
		toggle : function(caller) {
			if($(caller).parent().hasClass('selected')){
				$('#header div.login').fadeOut(function(){
					$(this).remove();
					
					if($(caller).parent().parent().find('li.selected').size() == 0) {
						$('div.magazine_navigator,#t_qc12').fadeIn();
					}
				});
				
				if($('body').hasClass('webtv')) {  // If we're in the webTV, we need to expand the header a bit					
					$('#backgroundline').animate({'top':r_backgroundTop});
					$('#header').animate({'height':r_headerHeight});
					$('#page').animate({'paddingTop':r_pagePadding});
				}
			}else{
				$(caller).parent().parent().find('li.selected a').click();
				
				$.ajax({
					type: "GET",
					url: $(caller).attr('href'),
					dataType: 'html',
					success: function(responseHTML){
						$('#header').append('<div class="login"></div>');
						$('div.magazine_navigator,#t_qc12').fadeOut();
						$('#header div.login').html(responseHTML).fadeIn();
						
						if($('body').hasClass('webtv')) {  // If we're in the webTV, we need to expand the header a bit
							r_openHeight = 70;
						
							if(!r_backgroundTop) r_backgroundTop = parseFloat($('#backgroundline').css('top'));
							if(!r_headerHeight) r_headerHeight = parseFloat($('#header').height());
							if(!r_pagePadding) r_pagePadding = parseFloat($('#page').css('padding-top'));
							
							$('#backgroundline').animate({'top':r_backgroundTop + r_openHeight});
							$('#header').animate({'height':r_headerHeight + r_openHeight});
							$('#page').animate({'paddingTop':r_pagePadding + r_openHeight});
						}
					}
				});
			}

			$(caller).parent().toggleClass('selected');
		}
	}


/* ------------------------------------------------------------------------
	For the register link in every page
------------------------------------------------------------------------- */
	var r_backgroundTop;
	var r_headerHeight;
	var r_pagePadding;
	var r_openHeight = 189;

	register = {
		toggle : function(caller) {
			if($.browser.msie && $.browser.version == 6) { location.href = $(caller).attr('href'); return; }
			
			if(!r_backgroundTop) r_backgroundTop = parseFloat($('#backgroundline').css('top'));
			if(!r_headerHeight) r_headerHeight = parseFloat($('#header').height());
			if(!r_pagePadding) r_pagePadding = parseFloat($('#page').css('padding-top'));
			
			if($('body').hasClass('webtv')) { r_openHeight = 235; } // The open height needs to be bigger in the webTV
			
			if($(caller).parent().hasClass('selected')){
				$('#backgroundline').animate({'top':r_backgroundTop});
				$('#header').animate({'height':r_headerHeight});
				$('#page').animate({'paddingTop':r_pagePadding});
				
				if($('#header .css_relative').is('div'))
				{
					$('#header .css_relative').animate({top: 0});
				}
				
				$('div.register').fadeOut(function(){
					if($(caller).parent().parent().find('li.selected').size() == 0) {
						$('div.magazine_navigator,#t_qc12').fadeIn();
					}
				});
			}else{
				$(caller).parent().parent().find('li.selected a').click();

				$('#backgroundline').animate({'top':r_backgroundTop + r_openHeight});
				$('#header').animate({'height':r_headerHeight + r_openHeight});
				
				if($('#header .css_relative').is('div'))
				{
					$('#header .css_relative').animate({top: r_openHeight});
				}
				
				$('#page').animate({'paddingTop':r_pagePadding + r_openHeight});

				$('div.magazine_navigator,#t_qc12').fadeOut();
				$('div.register').fadeIn();
			}

			$(caller).parent().toggleClass('selected');
		}
	}
	

/* ------------------------------------------------------------------------
	For the dock
------------------------------------------------------------------------- */

	dock = {
		init : function(){
			$(window).scroll(function(){
				scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
				scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
			
				if($.browser.opera) {
					windowHeight = window.innerHeight;
					windowWidth = window.innerWidth;
				}else{
					windowHeight = $(window).height();
					windowWidth = $(window).width();
				};
				
				$('#dock').css('top',scrollTop + (windowHeight / 2) - ($('#dock').height() / 2));
			});
		},
		toggle : function(caller){
			$dock = $('#dock');
			
			if($dock.hasClass('open')){
				$('div.prettyPhotoOverlay').fadeTo('fast',0,function(){ $(this).remove(); });
				$dock.animate({'left':-496},'fast',function(){
					$dock.removeClass('open');
				});
			}else{
				if($(caller).attr('href') != "#")
					$dock.load($(caller).attr('href'));
				
				$('body').append('<div class="prettyPhotoOverlay"></div>');
				$('div.prettyPhotoOverlay').css({'opacity':0,'height':$(document).height()}).fadeTo('fast',0.8);
				
				$dock.animate({'left':0},'fast',function(){
					$dock.addClass('open');
				});
			}
		},
		changeTab : function(caller) {
			$dock = $('#dock');
			if($(caller).attr('href') != "#")
				$dock.load($(caller).attr('href'));
		}
	}


/* ------------------------------------------------------------------------
	For the canaux submenu
------------------------------------------------------------------------- */


	canaux = {
		init : function() {
			$('#m_sections').hover(
				function(){ // Hover
					$(this).find('ul:first').stop().css('display','block');
				},
				function(){ // Out
					$(this).find('ul:first').fadeTo(500,1,function(){ $(this).css('display','none'); });
				}
			);
		}
	}
	

/* ------------------------------------------------------------------------
	For the webtv submenu
------------------------------------------------------------------------- */


	webtv = {
		init : function() {
			$('#m_webtv').hover(
				function(){ // Hover
					$(this).find('ul:first').stop().css('display','block');
				},
				function(){ // Out
					$(this).find('ul:first').fadeTo(500,1,function(){ $(this).css('display','none'); });
				}
			);
		}
	}
	

/* ------------------------------------------------------------------------
	Sidebar slider animation
------------------------------------------------------------------------- */

	sidebar = {
		init : function() {
			var initialHeight = 0;
			
			$('#sidebar>.content').hover(
			function(){ // Hover
				$overlay = $(this).find('.overlay');
				
				if(!initialHeight) initialHeight = $(this).find('.overlay').height();
				newHeight = initialHeight + $overlay.find('.toggleView').height();

				$overlay.animate({height:newHeight},'fast');
				$overlay.find('.toggleView').fadeIn('fast');
			},
			function(){ // Out
				$(this).find('.overlay').animate({height:initialHeight},'fast');
  				if($overlay){ $overlay.find('.toggleView').fadeOut('fast'); }
			}).click(function(){ location.href = $(this).find('a:first').attr('href'); });
		}
	}


/* ------------------------------------------------------------------------
	Vox-Pop bubble animation
------------------------------------------------------------------------- */

	vox_pop = {
		init : function(json){
			$('a.b_envoyer_reponse:first').click(function(){ vox_pop.answer('text'); return false; });
			$('a.b_envoyer_reponse_video:first').click(function(){ vox_pop.answer('video'); return false; });
			$('div.vox-pop-reply h4 a').click(function(){ vox_pop.close(); return false; })
			
			var mouseX = 0;
			var mouseY = 0;
			
			$allItems = $('ul.users li');
			$textItems = $('ul.users li.answer_text');
			
			$textItems.mousemove(function(e){
				mouseX = e.pageX;
				mouseY = e.pageY;
				
				boxPositionY = mouseY - $('div.vox-pop-holder:last').height() - 30;
				if($.browser.msie) boxPositionY -= 10;
				boxPositionX = mouseX - $('div.vox-pop-holder:last').width() + 235;
				
				$('div.vox-pop-holder:eq(1)').css({'top':boxPositionY,'left':boxPositionX});
			});			
			
			$allItems.each(function(i){
				$(this).data('count',i);
				$(this).data('content',json['item'][i]);
			});
			
			$textItems.hover(
			function(){ // Hover
				voxPopNumber = $(this).data('count');
				voxPopContent = $(this).data('content')['content'];
				
				$('div.vox-pop-holder:first').clone().addClass('vox-pop-' + voxPopNumber).appendTo('body');
				
				$('div.vox-pop-' + voxPopNumber).find('span').text(voxPopContent);
				$('div.vox-pop-' + voxPopNumber).stop().show().fadeIn('fast');
			},
			function(){ // Out
				$('div.vox-pop-' + voxPopNumber).remove();
			});
		},
		answer : function(type){
			// Position the overlay
			offsetHeight = 140;
			questionHeight = $('h2:first').height() + offsetHeight; // offsetHeight being the button height + the top offest
			
			$('div.vox-pop-overlay').css({'top':questionHeight,'height':$('ul.users').height() + 40}).fadeIn();
			
			$('.vox-pop-reply>div:not(.comment-login)').hide();
			$('.vox-pop-reply').css('top',questionHeight).fadeIn().find('.'+type).show();
		},
		close : function(){
			$('div.vox-pop-overlay,.vox-pop-reply').fadeOut();
		}
	}
	

	
	/* ------------------------------------------------------------------------
		Appel à tous reaction post
	------------------------------------------------------------------------- */

	reactions = {
		init : function() {
			$('a.b_envoyer_reponse:first').click(function(){ reactions.answer('text'); return false; });
			$('div.vox-pop-reply h4 a').click(function(){ vox_pop.close(); return false; })
		},
		answer : function(type){
			// Position the overlay
			offsetHeight = 138;
			questionHeight = $('div.question').height() + offsetHeight; // 40 being the button height + the top offest
			$('div.vox-pop-overlay').css({'top':questionHeight,'height':$('ul.answers').height() + 40}).fadeIn();
			
			$('.vox-pop-reply>div:not(.comment-login)').hide();
			$('.vox-pop-reply').css('top',questionHeight).fadeIn().find('.'+type).show();
		},
		close : function(){
			$('div.vox-pop-overlay,.vox-pop-reply').fadeOut();
		},
		post : function(form) {
		  // alert("form.html"+$(form).html());
      if ($(form).is('form')) 
        $form = $(form);
      else
        $form = $(form).parents("form");
			// alert(" $form "+$form.html());
			var type = $form.find('#reaction-type').val() || 'answer_text';
      
			heap.api.reactions.create(this.callback, {
				'type': type,
				'atom_id': $form.find('#atom-id').val(),
				'parent_id': $form.find('#parent-id').val(),
				'body': $form.find('#comment-body').val()
			});
		  return false;	
		},
		callback: function(resp) {
      if (resp.type != heap.SUCCESS) {
        alert(resp.msg);
        return;
      }
			window.location.reload();
		}
	}



/* Resizing height Footer */
var footer = function()
{
	this.doc = $(window);
	this.docH;
	this.contentH;
	this.balance;
	
	this.wrapper = $('#wrapper_page');
	this.footer = $('#footer');
	//this.holder = $('#holder_footer');
	this.footer_init = this.footer.height();
	
	this.init();
};

footer.prototype.init = function()
{
	var that = this;
	this.doc.resize(function(){ that.sizing() });
	this.sizing();
};

footer.prototype.sizing = function()
{
	this.docH = this.doc.height();
	this.contentH = $('#wrapper_content').height();
	this.balance = Math.ceil(this.docH-this.contentH);
	this.wrapper.height(this.docH);
	
	if(this.footer_init < this.balance)
	{
		this.footer.height(this.balance);
	}
};

