var filterIsOn = false;
var noFilterDom = undefined;
var $j = jQuery.noConflict();

function init_LB()
{
	var mouseY;
	$j('a.lightbox').click(function() {
		clickedLinkPost = $j(this).parent('.post');
		lightboxShow( $j(this).attr('href') );
		return false;
	});
	$j("h3").click(function(e){
		mouseY = e.pageY;
		document.getElementById('lightbox').style.marginTop = (mouseY-520)+"px";
		$j(document).scrollTop(mouseY-200);
	});
}

$j(document).ready(function(){
	//**** SCRIPT LIGHTBOX ***//
	init_LB();
	//**** SCRIPT HEADLINE ***//
	$j('a.theadline').each(function(){
		var dim = new Array( $j(this).innerWidth(), $j(this).innerHeight(), $j(this).find('.theadline_txt').innerWidth(), $j(this).find('.theadline_txt').innerHeight() );
		var pad = 16;
		$j(this).hover(
			function(){
				if( $j(this).hasClass('big') ){
					$j(this).find('.theadline_txt').animate({ width: dim[0]-pad }, 300, function(){ $j(this).addClass('rounded').find('p').fadeIn(100); } );
				}
				else {
					$j(this).find('.theadline_txt').animate({ height: dim[1]-pad }, 300, function(){ $j(this).addClass('rounded').find('p').fadeIn(100); } );
				}
			},
			function(){
				if( $j(this).hasClass('big') ){
					$j(this).find('p').fadeOut(100, function() {
						$j(this).parent('.theadline_txt').animate({ width: dim[2]-pad}, 300, function(){ $j(this).removeClass('rounded').find('p').hide(); });
					});
				}
				else {
					$j(this).find('p').fadeOut(100, function() {
						$j(this).parent('.theadline_txt').animate({ height: dim[3]-pad}, 300, function(){ $j(this).removeClass('rounded').find('p').hide(); });
					});
				}
			}
		);
	});
	
	$j('a.sabonner').click(function(){
		$j(this).fadeOut(300, function(){
						
			$j('.newsletterbox').animate({height: '+=150'}, 300, function() {
    				// Animation complete.
  			});
  			
			$j(this).next('form').fadeIn(300, function() {
				var hasClick = false;
				$j('#email').click(function() {
					if(!hasClick)
					{
						$j(this).val('');
						hasClick = true;
					}
				});
				
				$j('#email-btn').click(function() {
					$j('#newsletterForm').load('newsletter.php?email=' + $j('#email').val());
					$j('.newsletterbox').animate({height: '-=150'}, 300, function() {
    				// Animation complete.
  					});
					
				});
			});
		});
		return false;
	});

	
	$j('body').delegate('a.source', 'click', function() {
		$j('#col1').load( $j(this).attr('href'), function() {
			init_LB();
		});
		
		return false;
	});
	
	// contact
	$j('form#contact input#submit').click(function() {
		$j('form#contact').load('envoi_mail.php?nom='+encodeURIComponent($j('#comment_auteur').val())+'&email='+encodeURIComponent($j('#comment_email').val())+'&titre='+encodeURIComponent($j('#comment_titre').val())+'&comment='+encodeURIComponent($j('#comment').val()));
		return false;
	});
	
	// contactPub
	$j('form#contact input#submitpub').click(function() {
		$j('form#contact').load('envoi_mail_pub.php?nom='+encodeURIComponent($j('#comment_auteur').val())+'&email='+encodeURIComponent($j('#comment_email').val())+'&titre='+encodeURIComponent($j('#comment_titre').val())+'&comment='+encodeURIComponent($j('#comment').val()));
		return false;
	});
	
	// pagination brèves
	$j('body').delegate('#prev-tl', 'click', function() {
		$j('#col1').load( $j(this).attr('href'), function() {
			init_LB();
		});
		return false;
	});
	$j('body').delegate('#next-tl', 'click', function() {
		$j('#col1').load( $j(this).attr('href'), function() {
			init_LB();
		});
		return false;
	});
	
	$j('body').delegate('#article .post p a', 'click', function() {
		window.open($j(this).attr('href'), '');
		return false;
	});
	
});
 

function lightboxShow(url){
	
	var top = 0;
	
	//Détecte si l'utilisateur utilise un iPad et adapte le placement de la lightbox si c'est le cas
	if( navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPhone/i))
	{
		//top = $(document).scrollTop(mouseY);
		document.getElementById('lightbox').style.marginLeft = "-480px";
	}
	//if( url != '#' ){
		$j("#lightbox").load(url, 
			function(){

					top= 515;
					$j('#lightbox').css('top', top).append('').fadeIn(150, function(){ });
					if(screen.height >=1080){
						
						if(document.referrer == "http://electronlibre.info" || document.referrer == "www.electronlibre.info"){
							var scrolling =  document.getElementById('lightbox').offsetHeight;
							$j(document).scrollTop(scrolling - 360);
						}
					}

				
				$j('a.prev').text('');
				$j('a.prev').click(function() {
					var previous = null;
					if( clickedLinkPost )
						previous = clickedLinkPost.prev('.post');
					else
						previous = $j('#timeline').find('.post:first');
					
					if( previous.length )
					{
						previous.find('a.lightbox').click();
					}
					return false;
				});
				
				$j('a.next').text('');
				$j('a.next').click(function() {
					var previous = null;
					if( clickedLinkPost )
						previous = clickedLinkPost.next('.post');
					else
						previous = $j('#timeline').find('.post:first');
					
					if( previous.length )
					{
						previous.find('a.lightbox').click();
					}
					return false;
				});
			}
		);
	//}
	document.getElementById('lightbox').style.visibility='visible';
}

function lightboxClose(){
	$j('#lightbox').fadeOut(300, function(){
		$j(this).css('display', 'none');
		$j('#overlay').fadeOut(300, function(){
			$j(this).css('display', 'none');
		});
	});
}

function win_open(url)
{
	window.open(url, 'partager', 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no,width=600,height=350');
}

