var video_player_opener;

jQuery(document).ready(function() {
	/* avoid image flickering in IE */
	if($.browser.msie) { $( function() { document.execCommand("BackgroundImageCache",false,true); } ); }
	
	/* preload des images */
	PreloadImages();
	
	/* effet changement de pages */
	$('body a.url').each( function()
	{
		$(this).click( function() 
		{
			var id=$(this).attr('href');
			return OpenURL(id);
		});
	});
	
	/* ouverture de video directe */
	$('body a.video').each( function()
	{
		$(this).click( function() 
		{
			var id=$(this).attr('href');
			return open_video_player($(this).attr('id'),id);
		});
	});
	
	/* images hover auto */
	$('body img.hover').each( function()
	{
		var img=$(this);
		
		img.hover(
			function()
			{
				var src=img.attr('src');
	            if (src.indexOf('_on.gif')==-1) src = src.replace('.gif','_on.gif');
				img.attr('src',src);
			},
			function()
			{
				var src=img.attr('src');
	            if (src.indexOf('_on.gif')!=-1) src = src.replace('_on.gif','.gif');
				img.attr('src',src);
			}
		);
	});
	
	/* liens désactivés */
	$('body a.disabled').each( function()
	{		
		$(this).attr('href','#').css({opacity:.35,cursor:'default'}).unbind();
	});
	
	/* liens désactivés */
	$('body a.disabled img.hover').each( function()
	{		
		$(this).unbind();
	});	
	
	/* fadeIn de la page complete
	$('#main').fadeIn(1000, function()
	{
		 fadeIn du logo
		$('#main_logo').fadeIn(500);

		 fadeIn du logo 
		$('#main_languages').fadeIn(750);

		 fadeIn du menu et lancement du contenu 
		$('#main_nav').fadeIn(500,LaunchContent);
	}); */
	
	$('#main').show();
	$('#main_logo').show();
	$('#main_languages').show();
	$('#main_nav').show();
	LaunchContent();
});

function PreloadImages()
{
	$('img').each( function()
	{
		var myImage = new Image;
		myImage.src = $(this).attr('src');
	});
}

function LaunchContent()
{
	$('#menu_1').rb_menu();
	$('#menu_2').rb_menu();
	$('#menu_3').rb_menu();
	$('#menu_4').rb_menu();
	$('#menu_5').rb_menu();
	
	PreFadeCustomContent();
	//$('#main_content').fadeIn(1000,CustomContent);
	$('#main_content').show();
	CustomContent();
}

function PreFadeCustomContent()
{	
	/* SHOP */
	$('#shop_sidebar ul.menu li.item').each( function()
	{
		var item=$(this);
		item.hover(
			function()
			{
				//item.Pulsate(2,5);
				item.addClass('hover');
			},
			function()
			{
				item.removeClass('hover');
			}
		);
	});
	
	$('#shop_listing_main div.articles div.nok').each( function()
	{
		$(this).fadeTo(500,.45);
	});
	
	$('#shop_listing_main div.articles div.item').each( function()
	{
		var item = $(this);
		item.css('opacity',.7);
		item.hover(
			function()
			{
				item.css('opacity',1);
			},
			function()
			{
				item.css('opacity',.7);
			}
		);
	});
}

function CustomContent()
{	

	/* shop sidebar
	$('#store_sidebar').SlideInLeft(1500,
		function()
		{
			$(this).fadeTo(500,0.8);
		},'bounceout'
	); */
	$('#store_sidebar').show();
	
	$('a.image_preview',this).each(function()
	{
		var href = $(this).attr('href');
		$(this).click(function()
		{
			return image_preview(href);
		});
	});

	/* shop article details ajax window */
	$('a.profile_edit').each(function()
		{
			var href = $(this).attr('href');
			var item = $(this);
			
			if ($('div.tooltip',item).length>0)
			{
				item.Tooltip(
					{
						body: $('div.tooltip',item),
						track: true,
						delay: 200
					});
			}
			item.click(
					function()
					{
						return open_profile_edit(href);
					}
				);
		}
	);

	/* shop article details ajax window */
	$('a.article_detail').each(function()
		{
			var href = $(this).attr('href');
			var item = $(this);
			
			if ($('div.tooltip',item).length>0)
			{
				item.Tooltip(
					{
						body: $('div.tooltip',item),
						track: true,
						delay: 200
					});
			}
			item.click(
					function()
					{
						return open_article(href);
					}
				);
		}
	);

	/* moving cross */
	//$('#content_cross').SlideInUp(5000);

	/* FRONTPAGE*/
	$('#frontpage_images div').each( function()
	{
		var self=$(this);
		self.show();
		self.hover(
			function()
			{
				self.fadeTo("normal",0.8)
			},
			function()
			{
				self.fadeTo("fast",1)			
			}
		);
	}); 
	$('#frontpage_dev').css('opacity',0.8)
	
	/* FADE CONTENT */
	//$('.content').fadeTo(500,0.9)
	
	/* VIDEOS
	$('.videos_image').each( function()
	{
		var self=$(this);
		self.parent().fadeIn(500);
		self.hover(
			function()
			{
				self.fadeTo(250, 0.5);
			},
			function()
			{
				self.fadeTo(500, 1);
			}
		);
	}); */
}

function part_preview(href)
{
	$('#article_content').hide();

	var preview = $('#article_details div.part_preview');
	$('img',preview).attr('src',href).one('click',function()
	{
		$(this).parent().hide();
		$('#article_content').show();
	});
	preview.show();

	return false;
}

function image_preview(href)
{
	$('#main_content').hide();
	var preview = $('div.image_preview');
	$('img',preview).attr('src',href).one('click',function()
	{
		$(this).parent().hide();
		$('#main_content').show();
	});
	$('a',preview).one('click',function()
	{
		$(this).parent().hide();
		$('#main_content').show();
	});
	preview.show();

	return false;
}

function open_profile_edit(href)
{
	$('#main').hide()
	$('<div id="overlay"></div>')
		.css('opacity',0.25)
		.one('click',close_profile_edit)
		.prependTo('body')
		.show();

	$('#profile_edit')
		.load(href+'/embed')
		.css('opacity',.8)
		.show();
	return false;
}
function open_article(href)
{
	$('#main').hide();
	$('<div id="overlay"></div>')
		.css('opacity',0.25)
		.one('click',close_article)
		.prependTo('body')
		.show();

	$('#article_details')
		.load(href+'/embed')
		.css('opacity',.8)
		.show(function()
		{
			$('a.preview',this).each(function()
			{
				var href = $(this).attr('href');
				$(this).click(function()
				{
					return part_preview(href);
				});
			});
		});
	return false;
}

function open_video_player(id,href)
{
	$('#main').hide()
	$('<div id="overlay"></div>')
		.css('opacity',0.25)
		.one('click',close_video_player)
		.prependTo('body')
		.show();

	$('<div id="video_player"></div>')
		.load(href)
		.prependTo('body')
		.show();
	return false;
}

function close_article()
{
	$('#article_details').hide();
	$('#overlay').remove();
	$('#main').show();
}

function close_profile_edit()
{
	$('#profile_edit').hide();
	$('#overlay').remove();
	$('#main').show();
}

function close_video_player()
{
	$('#video_player').remove();
	$('#overlay').remove();
	$('#main').show();
}

function OpenURL(id)
{
	$('#main_content').hide();
	$('#main').hide();
	window.location=id;	
	return false;
}

function cart_add()
{
	var RadioButtons=$('#article_details input[@type=radio]');
	if (RadioButtons.length>0)
	{
        var checked=$('#article_details input[@type=radio]:checked');
		if (checked.length<=0)
		{
	    	var choose_alert = $('#article_details input.hidden').val();
	    	alert(choose_alert);
		}
		else
		{
			/* add to cart */
			$('#article_form').get(0).submit();
		}
	}
	else
	{
		/* add to cart */
		$('#article_form').get(0).submit();
	}
}

