$(document).ready(function(){
    zoomInit();
    filters_toogle();
    homeSlider();
});
function filters_toogle() {
    jQuery('#filter_form li.toggleSubMenu').each(function() {
        jQuery(this).children('span').click(function(oRef){
            var oUl=jQuery(this).siblings('ul,select').eq(0);
            if(oUl.hasClass('menu_cache'))
                oUl.slideDown("normal",function(){ jQuery(this).removeClass('menu_cache').addClass('menu_actif'); adjust();
                })

                //oUl.removeClass('menu_cache').addClass('menu_actif');
            else
                oUl.slideUp("normal",function() { jQuery(this).removeClass('menu_actif').addClass('menu_cache'); adjust();
                })
               // oUl.removeClass('menu_actif').addClass('menu_cache');
        })
    });
}

function adjust()
{
  var requiredHeight = $('#main .search-newsletter-block:first').innerHeight() + $('#main .col_left:first').innerHeight();
  var minHeight = $('#main .big_col_right:first').innerHeight();

  if (requiredHeight < minHeight)
  {
    jQuery('#corps').css('cssText', 'height: '+minHeight+'px !important');
  }
  else
  {
    jQuery('#corps').css('cssText', 'height: '+requiredHeight+'px !important');
  }
}

ImageLoader = {
   ids:new Array(),
   val:new Array(),
   ckey:0,
   preloaded:new Array(),
   display:null,
   add:function(id,val) {
      ImageLoader.ids[ImageLoader.ckey]=id;
      ImageLoader.val[ImageLoader.ckey++]=val;

   },
   preload:function(sID) {
      var img=new Image();
      img.src=ImageLoader.get(sID);
      ImageLoader.preloaded[ImageLoader.indexOf(sID)]=img;
   },
   indexOf:function(id) {
      for(var i=0;i<ImageLoader.ckey;i++) {
         if(ImageLoader.ids[i]==id)
            return i;
      }
      return false;
   },

   get:function(id) {
      var iIndex=ImageLoader.indexOf(id);
      if(iIndex !== false) {
         return ImageLoader.val[iIndex];
      }
   }
}
function zoomInit() {

  ImageLoader.display=jQuery('#visuel_zoom_catalogue');
  jQuery('a.delay').mouseover(function(){
    var sID=jQuery(this).attr('id');
    jQuery('img',ImageLoader.display).get(0).src=ImageLoader.get(sID);
  });
  $('a.delay').simpletooltip();
  
  jQuery('a.delay').click(function(){
    return false;
  });
}
function homeSlider() {
    var initialItemList = jQuery('#home_slider').clone();
    var initialLength = initialItemList.children().length;
    /*
    * Fonction de Callback appelee avant l'animation du carrousel
    */
    function homeslider_itemVisibleInCallback(carousel, item, i, state, evt)
    {
        // The index() method calculates the index from a
        // given index out of the actual item range.
        var idx = carousel.index(i, initialLength); //alert(initialLength);
        carousel.add(i, initialItemList.children().eq(idx - 1).html());
    };

    /*
    * Fonction de Callback appelee apres l'animation du carrousel
    */
    function homeslider_itemVisibleOutCallback(carousel, item, i, state, evt)
    {
        carousel.remove(i);
    };

    function homeslider_initCallback(carousel) {
        var newPrev = $('<div />').addClass('jcarousel-prev-horizontal');
        var newNext = $('<div />').addClass('jcarousel-next-horizontal');
        $('.jcarousel-container').prepend(newNext).prepend(newPrev);  

        newPrev.click(function() {
            var activeLi = carousel.list.find('a.active').closest('li');
            var activeIndex = parseInt(activeLi.attr('jcarouselindex'));
            if(!activeLi.prev('li').length)
                carousel.prev();
            carousel.list.find('li[jcarouselindex=' + (activeIndex-1) + ']').find('a').mouseover();
        });

        newNext.click(function() {
            var activeLi = carousel.list.find('a.active').closest('li');
            var activeIndex = parseInt(activeLi.attr('jcarouselindex'));
            if(activeIndex % carousel.options.scroll == 0)
                carousel.next();
            carousel.list.find('li[jcarouselindex=' + (activeIndex+1) + ']').find('a').mouseover();
        });
    }



    /*
    * Initialisation du carrousel
    */
    jQuery('#home_slider').jcarousel({
        scroll: 4,
        wrap: 'circular',
        initCallback: homeslider_initCallback,
        itemVisibleInCallback: {onBeforeAnimation: homeslider_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: homeslider_itemVisibleOutCallback},
        buttonNextHTML: null,
        buttonPrevHTML: null
    }).find('a').live('mouseover', function() {
        if(!jQuery(this).hasClass('active')) {
            jQuery('#home_slider a').removeClass('active');
            jQuery(this).addClass('active');
            initialItemList.find('a').removeClass('active').eq((jQuery(this).parent('li').attr('jcarouselindex')-1) % (initialLength)).addClass('active');
            var curHref = jQuery(this).attr('href');
            var curDiv = jQuery(curHref.substr(curHref.indexOf('#')));
            jQuery('.home_slide').stop(false, true);
            curDiv.addClass('slide_preactive').fadeIn(function() {
                $(this).removeClass('slide_preactive');
                jQuery('.home_slide').not(this).hide();
            });
        }
        return false;
    });
}

function requiredLoggedInUser()
{
  Shadowbox.open({
    content:    $('#alert-msg').parent().html(),
    player:     "html",
    height:     130,
    width:      560
  });
}

/* handle jQuery ajax error
$().ready(function(){
    $.ajaxSetup({
        error:function(x,e){
            if(x.status==0){
            alert('You are offline!!\n Please Check Your Network.');
            }else if(x.status==404){
            alert('Requested URL not found.');
            }else if(x.status==500){
            alert('Internel Server Error.');
            }else if(e=='parsererror'){
            alert('Error.\nParsing JSON Request failed.');
            }else if(e=='timeout'){
            alert('Request Time out.');
            }else {
            alert('Unknow Error.\n'+x.responseText);
            }
        }
    });
});
*/