// Init all
jQuery(document).ready(function() {
  initAllCarousel();
  initSubCatNav();
});

function initSubCatNav() {

jQuery(".newsCatMenu a").bind("click", function() {

  // first get the main wrapper ID
  var wId = $(this).parents(".homeBox");
  var carouselId = $(this).parents("ul").attr("id");
  var catId = $(this).attr("id");

  // load content
  var url = 'services.php?act=getCat&cId='+catId+'&carousel='+carouselId;

   $('.homeBoxContent',wId).css({height:'445px'});
   $(".homeBoxWrapper",wId).fadeOut(function() {
 
   $.get(url,function(data) {
         $(".homeBoxWrapper",wId).html(data);
         $("#mycarousel"+carouselId).hide();
         $(".homeBoxWrapper",wId).fadeIn(function() {
          initCarousel("mycarousel"+carouselId);
          initBuyButtons();
          $("#mycarousel"+carouselId).show();
          $('.homeBoxContent',wId).css({height:'auto'});
        });
     });

     url = 'services.php?act=getCatHeader&cId='+catId;
     $.get(url,function(data) {
      $(".homeBoxHead",wId).html(data);
     });

  });

  return false;
  });

      //$('.homeBoxContent',wId).css( {
      // background: "#E2F1F8 url(tpl/dmadvanced/img/homebox/dummy_bg.png) center center repeat-x"
      //});

  //$('.homeBoxContent',wId).css( {
  //  background: "#E2F1F8"
  //});
}

/*function initSubCatNav() {

jQuery(".newsCatMenu a").bind("click", function() {

  // first get the main wrapper ID
  var wId = $(this).parents(".homeBox");
  var carouselId = $(this).parents("ul").attr("id");
  var catId = $(this).attr("id");

  // load content
  var url = 'services.php?act=getCat&cId='+catId+'&carousel='+carouselId;

  $(".homeBoxWrapper",wId).fadeOut(500,function() {

    $.get(url,function(data) {
      $(".homeBoxWrapper",wId).empty().append(data);
      $("#mycarousel"+carouselId).hide();
      $(".homeBoxWrapper",wId).fadeIn(500,function() {
        initCarousel("mycarousel"+carouselId);
        initBuyButtons();
        $("#mycarousel"+carouselId).show();
      });
    });

     url = 'services.php?act=getCatHeader&cId='+catId;
     $.get(url,function(data) {
      $(".homeBoxHead",wId).html(data);
     });
   });

  return false;

      //$('.homeBoxContent',wId).css( {
       // background: "#E2F1F8 url(tpl/dmadvanced/img/homebox/dummy_bg.png) center center repeat-x"
      //});

  //$('.homeBoxContent',wId).css( {
  //  background: "#E2F1F8"
  //});
});
}*/

function initCarousel(carouselId)
{
    jQuery("#"+carouselId).jcarousel({
          itemVisible: 6,
          itemScroll: 6,
          scrollAnimation: 'slow',
          itemFirstInHandler: itemFirstInHandler,
          itemVisibleInHandler: {
              onAfterAnimation: itemVisibleInHandlerAfterAnimation
          },
          itemVisibleOutHandler: {
              onAfterAnimation: itemVisibleOutHandlerAfterAnimation
          }
      });
}

function initAllCarousel()
{
  $(".dmCarousel").each( function(i) {
    var carouselId = jQuery(this).attr("id");
    jQuery("#"+carouselId).jcarousel({
          itemVisible: 6,
          itemScroll: 6,
          scrollAnimation: 'slow',
          itemFirstInHandler: itemFirstInHandler,
          itemVisibleInHandler: {
              onAfterAnimation: itemVisibleInHandlerAfterAnimation
          },
          itemVisibleOutHandler: {
              onAfterAnimation: itemVisibleOutHandlerAfterAnimation
          }
      });
  });
}

function loader(targetEl)
{
  var loaderSRC = 'img/mini_loading.gif';
  var loader = document.createElement('img');

  loader.src = loaderSRC;
		jQuery(loader)
			.attr('id', 'jsLoader')
			;

  jQuery("#"+targetEl).append([loader]);
}

function removeLoader(targetEl)
{
  $("#"+targetEl+" #jsLoader").remove();
}

function loadDetails(crId,index,pId)
{
  var el  = crId+"fold"+index;
  var url = "services.php?act=prodDetails&pId="+pId;

  if( $("#"+crId+"fold"+index).attr("class") == 'selected' ) {
    hideRow(crId,index);
    return;
  }

    // do we already have content ?
    var loaded = true;
    if( $("#"+crId+"fold"+index).html() == '' ) {
      loaded = false;
    }

    //animateProd();
    showRow(crId,index);

    if( loaded == false ) {
      loader(crId+"fold"+index);
      $("#"+el).load(url);
    }
}

function showRow(crId,index)
{
  $("#"+crId+"tab dd.selected").slideToggle("normal").removeClass("selected");
  $("#"+crId+"fold"+index).slideToggle("normal").addClass("selected");
}

function hideRow(crId,index)
{
  $("#"+crId+"fold"+index).slideToggle("normal").removeClass("selected");
  $("#"+crId+"row"+index+" .name a").removeClass("selected");

}

function highlightPic(crId,index)
{
  $("#"+crId+" li").removeClass("selected");
  $("#"+crId+"prod"+index).addClass("selected");
}

/* CAROUSEL FUNCTIONS
-------------------------------------------------------------------------------*/

function itemFirstInHandler(carousel, li, idx, state) {

   if (state != 'init')
        return;

    var cr = carousel;
    var el = li;
    var crId = carousel.scope().id;

    jQuery("#"+crId+" li img").bind("click", function() {

      var index = $(this.parentNode).attr("id");
      index     = index.replace(/[a-z]+/g,'');

      var productId = $("#"+crId+"row"+index+" .pIdLink").attr("id");

      loadDetails(crId,index,productId);
      highlightPic(crId,index);

      showPicture(crId,index);

      return false;
    });

    jQuery("#"+crId+"tab .name ").bind("click", function() {

        var index = jQuery(this).attr("id");
        index = index.replace(/[a-z]+/g,'');
        cr.scroll(parseInt(index));
        var productId = $("#"+crId+"row"+index+" .pIdLink").attr("id");

        loadDetails(crId,index,productId);
        highlightPic(crId,index);
        showPicture(crId,index);
        return false;
    });
}

function showPicture(crId,index)
{
  var sourcePic = $("#"+crId+"prod"+index+" img").attr("alt");
  var destPic   = $("#"+crId+"picHolder img").attr("alt");

  if( destPic == sourcePic ) {
    return;
  }

  var mediumImg = '';
  var r = new RegExp('^\(.+?)\(small)\(.+?)$','');
  var src = $("#"+crId+"prod"+index+" img").attr("src");
  var path      = src.replace(r,'$1');
  var filename  = src.replace(r,'$3');
  var mediumImg = filename;
  var content = $("#"+crId+"prod"+index).html();

  $("#"+crId+"picHolder").fadeOut("normal",function() {

    $(this).html(content);
    
    if(mediumImg=="http://www.discountcomix.com/tpl/dmadvanced/img/no_picture.jpg")
    {
     $("img",this).attr("src",mediumImg);
    }
    else
    {
     $("img",this).attr("src","images/produits/medium"+mediumImg);
    }
    
    $(this).fadeIn();
    $(".buy",this).css('visibility','visible');

    initBuyButtons();
  });
}

function itemVisibleInHandlerAfterAnimation(carousel, li, idx, state) {
    var row = carousel.scope().id+"row"+idx;

    $("#"+row+".odd ").highlightFade({start:'#fcecec',end:'#f3c1c1',speed:600}) ;
    $("#"+row+".even ").highlightFade({start:'#FFFFFF',end:'#f6d2d2',speed:600}) ;
}

function itemVisibleOutHandlerAfterAnimation(carousel, li, idx, state) {
    var row = carousel.scope().id+"row"+idx;

    $("#"+row+".odd ").highlightFade({start:'#f3c1c1',end:'#fcecec',speed:600});
    $("#"+row+".even ").highlightFade({start:'#f6d2d2',end:'#FFFFFF',speed:600});
}