var frontend = new Frontend();

var catreal_open = false;
var catphoto_open = false;
var catrealint_open = false;
var catpostprod_open = false;

var cancel_open = false;

$(document).ready(function()
{
	/* Resizing */
	frontend.resizeContent();	
	
	/* Image On/Off effect */
	if ($('#grid-content').attr('id') != undefined) {
		$('#grid-content img.thumb').each(function()
		{
			$(this).mouseover(function()
			{
				previous = $(this).attr('src');
				var srcstring = previous.split('?src=');
				var substring = srcstring[1].split('&q=');
				var newsrc = srcstring[0] + '?src=' + substring[0] + '&q=100&h=145&w=230';			
				$(this).attr('src', newsrc);
			}).mouseout(function()
			{		
				var img = $(this).attr('src') + '&fltr[]=gray';
				$(this).attr('src', img);
			});
		});
		
		/* Keep the image ON if we are on the edito banner */
		$('#grid-content .edito_banner').mouseover(function()
		{
			var parent = $(this).parent();
			previous = $('.blockbox img.thumb', parent).attr('src');
		
			var srcstring = previous.split('?src=');
			var substring = srcstring[1].split('&q=');
			var newsrc = srcstring[0] + '?src=' + substring[0] + '&q=100&h=145&w=230';
			$('.blockbox img.thumb', parent).attr('src', newsrc);
		}).mouseout(function()
		{
			var parent = $(this).parent();
			previous = $('.blockbox img.thumb', parent).attr('src');
			
			var srcstring = previous.split('?src=');
			var substring = srcstring[1].split('&q=');
			var newsrc = srcstring[0] + '?src=' + substring[0] + '&q=100&h=145&w=230';
			$('.blockbox img.thumb', parent).attr('src', newsrc);
		});
	}
});

/* On resize action */
$(window).resize(function()
{
	frontend.resizeContent();
})

function Frontend()
{
	this.resizeContent = function()
	{
		var width = $(window).width();
		var content_width = width - 290;
		
		if (content_width >= 1180) {
			content_width = 1180;
		}
		
		$('#container').width(content_width);
	}
}

/* vGrid */
var vg;
$(function(){
	if ($('#grid-content').attr('id') != undefined) {
		vg = $("#grid-content").vgrid({
			easeing: "easeOutQuint",
			time: 400,	
			delay: 20,
			fadeIn: {
				time: 500,
				delay: 80
			}
		});
	}
});


/* Grid control */
current_selected = '';
block_click = false;
$(document).ready(function()
{
	if ($('#grid-content').attr('id') != undefined) {
		$('#grid-content div div.corpo_thumb a.corpo').click(function() 
		{
			if (is_ipad == true) {
				window.scrollTo(0, 0);
				$('#the_videoviewer').css('display', '');
				$('#the_videoviewer').css('top', 0);
				var parent = $(this).parent().parent();
				var content = $('.thecontent', parent).html();
				$('#the_videoviewer #theplayer').html(content);
				$('#the_videoviewer p iframe').width('99%');
				return;
			}
		
			if (block_click == false) {
				if (cancel_open == false) {
					block_click = true;
					if (current_selected != '') {
						current_selected.css('marginLeft', 5);
						$('.corpo_full', current_selected).css('display', 'none');
						$('.corpo_thumb', current_selected).css('display', '');
						
						current_selected.width(230);
						vg.vgrefresh();
						current_selected.height(210);
						vg.vgrefresh();	
					}	
				
					/* Resize element and refresh the grid */
					$(this).parent().css('display', 'none');
					current_selected = $(this).parent().parent();
					current_selected .width(700);
					vg.vgrefresh();
					current_selected .height(450);
					vg.vgrefresh(null, null, null, function()
					{
						/* Change the element a little bit */
						$('.corpo_full', current_selected).fadeIn(500);
						current_selected.css('marginLeft', 9);
						block_click = false;
					});	
				} else {
					cancel_open = false;
				}
				
				return false;
			} else {
				return false;
			}
		});	
		
		$('#grid-content div div.contact_thumb a.contactbox').click(function() 
		{
			if (block_click == false) {
				if (cancel_open == false) {
					block_click = true;
					if (current_selected != '') {
						current_selected.css('marginLeft', 5);
						$('.contact_full', current_selected).css('display', 'none');
						$('.contact_thumb', current_selected).css('display', '');
						
						current_selected.width(230);
						vg.vgrefresh();
						current_selected.height(245);
						vg.vgrefresh();	
					}	
				
					/* Resize element and refresh the grid */
					$(this).parent().css('display', 'none');
					current_selected = $(this).parent().parent();
					current_selected .width(700);
					vg.vgrefresh();
					current_selected .height(490);
					vg.vgrefresh(null, null, null, function()
					{
						/* Change the element a little bit */
						$('.contact_full', current_selected).fadeIn(500);
						current_selected.css('marginLeft', 9);
						block_click = false;
					});	
				} else {
					cancel_open = false;
				}
				
				return false;
			} else {
				return false;
			}
		});		
	}
	
	/* Twitter navigation button */
	$('#twitter').click(function()
	{
		$(this).addClass('twitlink');
		$('#twitterbox').animate({height: 'toggle'}, 300, function()
		{
			if ($('#twitterbox').css('display') == 'none') {
				$('#twitter').removeClass('twitlink');
			}
		});
		
		return false;
	});
	
	/* Realisateur - Categorie */
	$('#cat_real').click(function()
	{
		if (catphoto_open == true) {
			$('#photo_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#photo_navlist').css('display') == 'none') {
					$('#cat_photo').removeClass('opencatlink');
				}
			});
			catphoto_open = false;
		} else if (catrealint_open == true) {
			
			$('#realint_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#realint_navlist').css('display') == 'none') {
					$('#cat_realint').removeClass('opencatlink');
				}
			});
			catrealint_open = false;
		} else if (catpostprod_open == true) {
			$('#postprod_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#postprod_navlist').css('display') == 'none') {
					$('#cat_postprod').removeClass('opencatlink');
				}
			});
			catpostprod_open = false;
		}
	
		catreal_open = true;
		$(this).addClass('opencatlink');
		$('#real_navlist').animate({height: 'toggle'}, 500, function()
		{
			if ($('#real_navlist').css('display') == 'none') {
				$('#cat_real').removeClass('opencatlink');
				catreal_open = false;
			}
		});
		
		return false;
	});
	
	/* Photographes - Categorie */
	$('#cat_photo').click(function()
	{
		if (catreal_open == true) {
			$('#real_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#real_navlist').css('display') == 'none') {
					$('#cat_real').removeClass('opencatlink');
				}
			});
			catreal_open = false;
		} else if (catrealint_open == true) {
			$('#realint_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#realint_navlist').css('display') == 'none') {
					$('#cat_realint').removeClass('opencatlink');
				}
			});
			catrealint_open = false;
		} else if (catpostprod_open == true) {
			$('#postprod_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#postprod_navlist').css('display') == 'none') {
					$('#cat_postprod').removeClass('opencatlink');
				}
			});
			catpostprod_open = false;
		}
		
		catphoto_open = true;
		$(this).addClass('opencatlink');
		$('#photo_navlist').animate({height: 'toggle'}, 500, function()
		{
			if ($('#photo_navlist').css('display') == 'none') {
				$('#cat_photo').removeClass('opencatlink');
				catphoto_open = false;
			}
		});
		
		return false;
	});
	
	/* Producteurs - Categorie */
	$('#cat_realint').click(function()
	{
		if (catreal_open == true) {
			$('#real_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#real_navlist').css('display') == 'none') {
					$('#cat_real').removeClass('opencatlink');
				}
			});
			catreal_open = false;
		} else if (catphoto_open == true) {
			$('#photo_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#photo_navlist').css('display') == 'none') {
					$('#cat_photo').removeClass('opencatlink');
				}
			});
			catphoto_open = false;
		} else if (catpostprod_open == true) {
			$('#postprod_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#postprod_navlist').css('display') == 'none') {
					$('#cat_postprod').removeClass('opencatlink');
				}
			});
			catpostprod_open = false;
		}
		
		catrealint_open = true;
		$(this).addClass('opencatlink');
		$('#realint_navlist').animate({height: 'toggle'}, 500, function()
		{
			if ($('#realint_navlist').css('display') == 'none') {
				$('#cat_realint').removeClass('opencatlink');
				catrealint_open = false;
			} 
		});
		
		return false;
	});
	
	/* Producteurs - Categorie */
	$('#cat_postprod').click(function()
	{
		if (catreal_open == true) {
			$('#real_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#real_navlist').css('display') == 'none') {
					$('#cat_real').removeClass('opencatlink');
				}
			});
			catreal_open = false;
		} else if (catrealint_open == true) {
			$('#realint_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#realint_navlist').css('display') == 'none') {
					$('#cat_realint').removeClass('opencatlink');
				}
			});
			catrealint_open = false;
		} else if (catphoto_open == true) {
			$('#photo_navlist').animate({height: 'toggle'}, 500, function()
			{
				if ($('#photo_navlist').css('display') == 'none') {
					$('#cat_photo').removeClass('opencatlink');
				}
			});
			catphoto_open = false;
		}
		
		catpostprod_open = true;
		$(this).addClass('opencatlink');
		$('#postprod_navlist').animate({height: 'toggle'}, 500, function()
		{
			if ($('#postprod_navlist').css('display') == 'none') {
				$('#cat_postprod').removeClass('opencatlink');
				catpostprod_open  = false;
			}
		});
		
		return false;
	});
	
	/* Search on/off effect */
	$('#s').focus(function()
	{
		if ($(this).val() == 'Recherche') {
			$(this).val('');
		}
	}).blur(function()
	{
		if ($(this).val() == '') {
			$(this).val('Recherche');
		}
	});
	
	/* Search Button */
	$('#search_btn').click(function()
	{
		$('#search_frm').submit();
	});
	
	/* Form submit */
	$('#search_frm').submit(function()
	{
		if ($('#s').val() == '' || $('#s').val() == 'Recherche') {
			return false;
		} else {
			return true;
		}
	});
	
	$(window).scroll(function() 
	{
		$('#the_videoviewer').css('top', window.pageYOffset);
	});
	
	$('#close_player').click(function()
	{
		$('#the_videoviewer').css('display', 'none');
	});
});


