jQuery('document').ready(function() {

	previewTooltipInit(document);

    productTabsInit();

    productReviewsInit();

    arrowsInit();

});

function previewTooltipInit(rootDiv) {
    
    jQuery(rootDiv).find(".apercu").hide();
	jQuery(rootDiv).find(".produit").mouseenter(function() {
		jQuery(this).children(".apercu").fadeIn();
	});
	jQuery(rootDiv).find(".produit").mouseleave(function() {
		jQuery(this).children(".apercu").fadeOut();
	});

    jQuery(rootDiv).find(".produit .apercu a").click(function() {
        jQuery.facebox({ajax: this.href});
        return false;
    });

    jQuery(rootDiv).find(".produit .apercu").mouseenter(function() {
       jQuery(this).addClass('apercu_case');
    });

    jQuery(rootDiv).find(".produit .apercu").mouseleave(function() {
       jQuery(this).removeClass('apercu_case');
    });
    
    jQuery(rootDiv).find(".dispo_case a.minisheet").click(function() {
        jQuery.facebox({ajax: this.href});
        return false;
    });

}

function productTabsInit() {
	jQuery(".product_tab").live('click',function() {

		jQuery(this).find('a').blur();
		jQuery(".product_tab").removeClass('active');
		jQuery(this).addClass('active');

		var tabId = "#" + (this.id.replace("-tab","-item"));
		jQuery('.product_items').addClass('no-display');
		jQuery(tabId).removeClass('no-display');

		return false;

	});

}

/**
 * Used to initialize the product reviews zone
 */
function productReviewsInit() {

    var anchor = getAnchor();

	jQuery("#post_review").live('click',function() {
		return goToForm();
	});

	jQuery("#review-form .fermer").live('click',function() {
		jQuery("#review_form_container").slideUp();
        jQuery('html,body').animate({scrollTop: jQuery("#product-reviews").offset().top},'slow');
		return false;
	});
    
    
    if (anchor) {
        if (anchor == "comment") {
            return goToForm();
        }
        else if (anchor == "seecomments") {
            jQuery('html,body').animate({scrollTop: jQuery("#product-reviews").offset().top},'slow');
        }
     }

	jQuery(".bethefirst").live('click',function() {
        return goToForm();
	});

    sortReviewsInit();
    return false;
}

/**
 * Used to toggle the comment form
 **/
function goToForm() {
    jQuery("#review_form_container").slideDown();
    jQuery('html,body').animate({scrollTop: jQuery("#review_form_container").offset().top},'slow');
    return false;
}

/**
 * Used to initialize the arrow buttons
 */
function arrowsInit() {

    jQuery("#fleche_avis_haut").live('click', function() {
        list_scrollToPrevious();
        return false;
    });

    jQuery("#fleche_avis_bas").live('click', function() {
        list_scrollToNext();
        return false;
    });
    
}

function list_scrollToPrevious() {
    list_scroll(-100);
    return false;
}

function list_scrollToNext() {
    list_scroll(100);
    return false;
}

/**
 * Used to animate the list
 */
function list_scroll(scrollOffset) {
    jQuery('#list_avis_container').animate({scrollTop: '+='+scrollOffset+'px'}, 400, function(){
        initScrollButtons();
    });
}

/**
 * We check if the list can be scrolled down, using our last_review class
 */
function list_canScrollDown()
{
    var arrowDown = jQuery('#fleche_avis_bas')[0];
    var last_review = jQuery('.last_review');
    if (typeof(last_review.offset()) != 'undefined') {
        var container = jQuery('#list_avis_container');
        var lastBottom = last_review.offset().top + last_review.outerHeight();
        var containerBottom = container.offset().top + container.outerHeight();
        
        if (lastBottom > containerBottom)
        {
            return true;
        }
    }
    arrowDown.src = addDisabledToImageName(arrowDown.src);
    return false;
}

/**
 * We check if the list can be scrolled up, using our first_review class
 */
function list_canScrollUp()
{
    var first_review = jQuery('.first_review');
    if (typeof(first_review.offset()) != 'undefined') {
        var container = jQuery('#list_avis_container');
        var firstTop = first_review.offset().top;
        var containerTop = container.offset().top;
        if (firstTop < containerTop)
        {
            return true;
        }
    }
    return false;
}

/**
 * Here we bind the tabs to the correct <li> elements we want to display or hide
 *
 * We also add/remove each time the classes 'first_review' and 'last_review' to animate properly
 *
 *  **/
function sortReviewsInit() {
    jQuery(".review_tab").live('click', function() {

        if (jQuery(this).attr('id') == "see_all_comments" ){
            jQuery(".item_review").first().addClass('first_review');
            jQuery(".item_review").last().addClass('last_review');
            jQuery("#see_all_comments").hide();
            jQuery("#hide_all_comments").show();
            jQuery(".liste_avis").height('auto').css('minHeight', '300px');
        }else if (jQuery(this).attr('id') == "hide_all_comments"){
            jQuery("#see_all_comments").show();
            jQuery("#hide_all_comments").hide();
            jQuery(".liste_avis").height('300px').css('minHeight', '300px');;
            //scroll back to top comment
            x = 0;
            y = jQuery("#product-reviews").offset().top;
            window.scroll(x,y);
        } else
        {
            var class_to_show = jQuery(this).attr('class').replace('review_tab ', '');
            jQuery('.item_review:not(.'+class_to_show+')').addClass('no-display');
            jQuery('.item_review:not(.'+class_to_show+')').removeClass('first_review');
            jQuery('.item_review:not(.'+class_to_show+')').removeClass('last_review');
            jQuery('.item_review.'+class_to_show).removeClass('no-display');
            jQuery('.item_review.'+class_to_show).first().addClass('first_review');
            jQuery('.item_review.'+class_to_show).last().addClass('last_review');
//            jQuery(".liste_avis").height('300px');
        }
        
        jQuery('#list_avis_container').animate({scrollTop: '0px'}, 400, function(){
            initScrollButtons();
        });
        return false;
    });
}


/**
 *
 * Here we sets the buttons visibility
 *
 * Warning, functions add* and remove* are on fleurance-review.js
 * 
 */

function initScrollButtons()
{
    var arrowUp = jQuery('#fleche_avis_haut_img');
    var arrowDown = jQuery('#fleche_avis_bas_img');



    if (list_canScrollUp()) {
        arrowUp.attr('src',removeDisabledFromImageName(arrowUp.attr('src')));
    } else {
        arrowUp.attr('src',addDisabledToImageName(arrowUp.attr('src')));
    }

    if (list_canScrollDown()) {
        arrowDown.attr('src',removeDisabledFromImageName(arrowDown.attr('src')));
    } else {
        arrowDown.attr('src',addDisabledToImageName(arrowDown.attr('src')));
    }

    
}

/**
 *
 * We use this function to retrieve anchor in URL
 *
 */
function getAnchor() {
    var myFile = document.location.toString();
    if (myFile.match('#')) {
    return myFile.split('#')[1];
    } else {
    return false;
    } 
}
