//clear input fields at first click
function clearText(field){
    if (field.defaultValue==field.value)
        field.value = ""
}
jQuery(document).ready(function(){
    //main navigation hover
    jQuery('#nav li.level0').hover(
        function(){
            jQuery(this).addClass('over');
            jQuery('.layer').hide() ;
            jQuery('.layer').fadeIn(500) ;
        },
        function(){
            jQuery(this).removeClass('over');
        }
    );
    //product list hover
    jQuery('ul.products-grid li.item').hover(
        function(){
            jQuery(this).addClass('over');
            jQuery('.layer').fadeIn(100) ;
        },
        function(){
            jQuery(this).removeClass('over');
        }
    );
    
    // shop by : close every other additionnal filters when opening a new filter
    jQuery(".layered-nav .layered-nav-column. .additionnal-filters a.action").click(function(){
        a = jQuery(this);
        parentdiv = a.parents(".layered-nav-column");
        if (a.hasClass('see-more')) {
            parentdiv.find("ol.list").slideUp();
            parentdiv.find(".see-less").hide();
            parentdiv.find(".see-more").css('display','block');
        }
        return false;
    });
    // shop by : see more/less options
    jQuery(".layered-nav .layered-nav-column a.action").click(function(){
        a = jQuery(this);
        parentdiv = a.parents(".see-other");
        if (a.hasClass('see-more')) {
            a.hide();
            parentdiv.children(".see-less").css('display','block');
            parentdiv.children(".list").slideDown();
        } else {
        	a.hide();
        	parentdiv.children(".see-more").css('display','block');
        	parentdiv.children(".list").slideUp();
        }
        return false;
    });
    
    // eboutique : close every other additionnal filters when opening a new filter
    jQuery("li.eboutique div.layer .menu-links-blocks a.action").click(function(){
        a = jQuery(this);
        parentdiv = a.parents(".menu-links-blocks");
        if (a.hasClass('see-more')) {
            parentdiv.find("ol.list").slideUp();
            parentdiv.find(".see-less").hide();
            parentdiv.find(".see-more").css('display','block');
        }
        return false;
    });
    // eboutique : see more/less options
    jQuery("li.eboutique div.layer .menu-links-blocks a.action").click(function(){
        a = jQuery(this);
        parentdiv = a.parents(".parent");
        console.log(parentdiv) ;
        if (a.hasClass('see-more')) {
            a.hide();
            parentdiv.children(".see-less").css('display','block');
            parentdiv.children(".list").slideDown();
        } else {
        	a.hide();
        	parentdiv.children(".see-more").css('display','block');
        	parentdiv.children(".list").slideUp();
        }
        return false;
    });
    
    //shop by : add the class "parent" on shop-by container if a filter is selected
    jQuery('div.selected-filters').parent().addClass('filtered')
    // order history : open/close orders
    jQuery("#sales_order_history .action a").click(function(){
        a = jQuery(this);
        order = a.parents(".order");
        if (a.hasClass('open')) {
            order.children(".order-content").slideDown();
            order.addClass('opened');
            order.removeClass('closed');
        } else {
        	order.children(".open").show();
        	order.children(".order-content").slideUp();
        	order.addClass('closed');
        	order.removeClass('opened');
        }
        return false;
    });

    //product page : see more/less product caracteristics
    if(jQuery(".product_info .more_product_properties").length > 0) {
        var longHeight = jQuery(".product_info .product_properties").height();
        var smallHeight = (jQuery(".product_info .product_properties li:first").outerHeight() * 4); //hieght of 4 first li
        jQuery(".product_info .product_properties").height(smallHeight);
        jQuery(".more_product_properties a.action").click(function(){
            a = jQuery(this);
            parentdiv = a.parents(".more_product_properties");
            if (a.hasClass('see-more')) {
                parentdiv.siblings(".product_properties").animate({height:longHeight},"slow",function () {
                    a.hide();
                    parentdiv.children(".see-less").css('display','block');
                });
            } else {
                parentdiv.siblings(".product_properties").animate({height:smallHeight},"slow",function() {
                    a.hide();
                    parentdiv.children(".see-more").css('display','block');
                });
            }
            return false;
        });
    }

    //product page : show review form
    if(jQuery("#review-form").length > 0) {
        if(jQuery(".review-error-messages").length > 0) {
           jQuery("a#show-review-form").hide();
        }
        else {
            jQuery("#review-form").css('display','none');
            jQuery("#review-list .comments-list").css('margin-top','-1px');
        }
        jQuery("a#goto-review-form").click(function(){
           jQuery("a#show-review-form").click();
        });
        jQuery("a#show-review-form").click(function(){
           jQuery("#review-list .comments-list").css('margin-top','0');
           jQuery("#review-form").slideDown('slow');
           jQuery("a#show-review-form").hide();
           return false;
        });

    }

    //product page caroussel
    if(jQuery(".product_thumbnails #product_thumbnails_caroussel").length > 0) {
        if(jQuery(".product_thumbnails #product_thumbnails_caroussel ul li").length > 2) {
            jQuery(".product_thumbnails #product_thumbnails_caroussel").jCarouselLite( {
                    btnNext:".product_thumbnails span.next",
                    btnPrev:".product_thumbnails span.prev"
            });
        }
    }

    //create account - additionnal address
    if(jQuery(".create-account #create_additional_address").length > 0) {
        //binding click function on checkbox
        jQuery(".create-account #define-main-as-billing").click(
            function() {
                if(jQuery(".create-account #define-main-as-billing").is(':checked')) {
                    jQuery(".create-account #additional-address").hide();
                    jQuery(".create-account input#create_additional_address").val(0);
                }
                else {
                    jQuery(".create-account #additional-address").show();
                    jQuery(".create-account input#create_additional_address").val(1);
                }
            }
        );
    }

    //caroussel in the footer
    if(jQuery('.footercarousel ul li').length > 2) {
	    jQuery('.footercarousel').jCarouselLite({
	            auto: 5000
	    });
    }
    else if(jQuery('.footercarousel ul li').length == 2) {
    	jQuery('.footercarousel ul li').css('float','left');
    };
    

    //Store details page
    if(jQuery("#itinerary-popup").length > 0) {
        //Need to adjust google map iframe size
        jQuery("#itinerary-popup iframe").width(552);
        jQuery("#itinerary-popup iframe").height(334);
        //And move link to actions button
        jQuery("#itinerary-popup .access_map a").hide();
        jQuery("#itinerary-popup .actions a").attr('href',jQuery("#itinerary-popup .access_map a").attr('href'));
    }

	
});

