$(document).ready(init_js);

function init_js()
{
  init_jreject();
  init_quantity_buttons();
  jQuery.prettyLoader();
  init_events();
  
  var searchclik = true;
  $('#search #query').click(function()
  {
    if(searchclik){
      $(this).val('');
      searchclik = false;
    }
  });
  
  var newsletterclik = true;
  $('#newsletter-txt').click(function()
  {
    if(newsletterclik){
      $(this).val('');
      newsletterclik = false;
    }
  });
}

function init_jreject()
{
  $.reject({
    reject: { all: false }, // Reject all renderers for demo  
    closeCookie: true, // Set cookie to remmember close for this session  
    imagePath: '/images/browsers/',
    overlayOpacity: 0.5,
    display: ['firefox','chrome','safari','opera','msie'],
    browserInfo: {
      firefox: {
        text: 'Mozilla Firefox',
        url: 'http://www.mozilla.com/firefox/'
      },  
      chrome: {  
        text: 'Google Chrome',
        url: 'http://www.google.fr/chrome/'  
      },
      safari: {  
        text: 'Apple Safari',  
        url: 'http://www.apple.com/fr/safari/download/'  
      },
      opera: {  
        text: 'Opera',  
        url: 'http://www.opera.com/download/'  
      },
      msie: {  
        text: 'Internet Explorer',  
        url: 'http://www.microsoft.com/france/windows/internet-explorer/telecharger-ie8.aspx'  
      }
    },  
    header: "Il est fortement recommander d'utiliser un autre navigateur.",  
    paragraph1: '<div style="clear:both"></div>',  
    paragraph2: "Vous utilisez une version de Internet Explorer qui a plus de 8 ans et qui n'est plus à jour. Votre navigateur peut ne pas être compatible avec notre site Internet et poser des problèmes de sécurité sur Internet.<br/><br/>Nous vous recommandons d'utiliser un navigateur plus récent tels que les navigateurs ci dessous qui sont les plus populaires.<br/>Cliquez sur les icônes pour en télécharger un gratuitement :", // Paragraph 2  
    closeMessage: "En fermant cette fenêtre, vous acceptez que la navigation sur notre site ne soit pas optimale.",
    closeLink: "Fermer cette fenêtre."
  });
}

function init_events()
{
  $('.ajax_form').submit(function (e) { e.preventDefault(); });
  $('.ajax_link').submit(function (e) { e.preventDefault(); });
}


function initCarrousel()
{
	$("#flowpanes").scrollable({ circular: true, mousewheel: false }).navigator({
		navi: "#flowtabs",
		naviItem: 'a',
		activeClass: 'current'
	}).autoscroll({ autoplay: true, interval: 5000, autopause: true });
}

function init_quantity_buttons()
{
  $('a.increase').click(function(e)
  {
    var input = $(this).prevAll('input').first();
    var currentVal = parseInt(input.val());
    if (currentVal != NaN && currentVal > 0)
    {
      input.val(currentVal + 1);
    }
    else
    {
      input.val(1);
    }
    return false;
  });

  $("a.decrease").click(function(e)
  {
    var input = $(this).prevAll('input').first();
    var currentVal = parseInt(input.val());
    if (currentVal != NaN && currentVal > 1)
    {
      input.val(currentVal - 1);
    }
    else
    {
      input.val(1);
    }
    return false;
  });
}

function init_prettyPhoto()
{
	jQuery('#large a').bind('click',function(e)
	{
    jQuery.prettyPhoto.open(images,titles,descriptions,num_photo);
    return false;
  });
}

function init_thumbnails(thumbs)
{
  jQuery('.product-thumbnails a.tbzoom').each
	(
		function(item_number)
		{
			jQuery(this).bind('click',function(e)
  			{
  			  jQuery('#large').html('<a href="'+images[item_number]+'"><img src="'+thumbs[item_number]+'"/><div class="but-zoom"> <\/div><\/a>');
          jQuery("#large").animate({opacity: "0"}, 0).animate({opacity: "1"}, 500);
          num_photo=item_number;
          init_prettyPhoto();
  				return false;
  			}
  		);
		}
	);
}

function init_thumbslide()
{
  var div = $('div.sc_menu'), ul = $('ul.sc_menu'), ulPadding = -20;

  //Get menu width
  var divWidth = div.width();
  
  //Remove scrollbars
  div.css({overflow: 'hidden'});

  //Find last image container
  var lastLi = ul.find('li:last-child');
  
  var r = div.parent().width()/div.width();
  ulPadding = ulPadding * r;

  //When user move mouse over menu
  div.parent().mousemove(function(e){

    //As images are loaded ul width increases,
    //so we recalculate it each time
    var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

    var left = r * (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
    div.scrollLeft(left);
  });
}

function open_inlinelightbox(container_id, width, title)
{
  if(width == undefined) width = 540;
  jQuery("a[rel^='prettyPhoto']").prettyPhoto({ 
    default_width: width, 
    allowresize: false,
    markup: '<div class="pp_pic_holder"> \
						<div class="ppt">&nbsp;</div> \
						<div class="pp_top"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
						<div class="pp_content_container '+container_id+'"> \
							<div class="pp_left"> \
							<div class="pp_right"> \
								<div class="pp_content"> \
									<div class="pp_loaderIcon"></div> \
									<div class="pp_fade"> \
										<a href="#" class="pp_expand" title="Agrandir">Expand</a> \
										<a class="pp_close" href="#">Close</a> \
										<div id="pp_full_res"></div> \
										<div class="pp_details clearfix"></div> \
									</div> \
								</div> \
							</div> \
							</div> \
						</div> \
						<div class="pp_bottom"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>'
  });
  jQuery.prettyPhoto.open('#lightbox',title);
}

function update_cartbox()
{
  jQuery.ajax(
    {
      type:'GET',
      dataType:'html',
      success:function(data, textStatus){jQuery('#toolbarbox_cart_container').html(data);},
      url: '/update_cartbox'
      }
    );
}

function open_lb(url)
{
  jQuery.ajax(
    {
      type:'POST',
      dataType:'html',
      success:function(data, textStatus){jQuery('#lightbox').html(data);},
      complete:function(XMLHttpRequest, textStatus){open_inlinelightbox('cartlightbox_container');update_cartbox()},
      url: url
      }
    );
}


