/*function cufonInit(){
    Cufon.replace(['#cart_content a','#cart_content h4','.ajax-cart .center h2'],{
        hover: {
        }
    });
}*/


jQuery(document).ready(function($){
    $('body').addClass('js');
    // podbox window hack - do not resize window
    PodBox.Overlay.prototype.updateWindow = function(){};
    PodBox.Overlay.prototype.resize = function(){};
    if (!$('#btn_shopping_bag').data('ajaxcart')){
        $('#btn_shopping_bag').data('ajaxcart', true).click(function(evt){
            evt.preventDefault();
            ajaxCart.toggleBag($(this).attr('href'));
        });
    }
});

jQuery(document).ready(function($){
    // CSS fixes
    $('.breadcrumbs ul li:last-child').addClass('last');

    // Main menu
    var mainmenu = $('.primary-nav ul'),
        permanent_search = false,
        hide_search = false;

    
    /*mainmenu.children('li.search').mouseover(function(){
        hide_search = false;
        var $this = $(this);
        $this.find('.form-search').css('display','block');
        $this.children('a').addClass('active');
    }).mouseout(function(){
        if (permanent_search){
            hide_search = true;
            return;
        }
        var $this = $(this);
        $this.find('.form-search').css('display','none');
        $this.children('a').removeClass('active');
    }).find('input').focus(function(){
        permanent_search = true;
        hide_search = false;
    }).blur(function(event){
        permanent_search = false;
        if (hide_search){
            mainmenu.children('li.search').find('.form-search').css('display', 'none');
            mainmenu.children('li.search').children('a').removeClass('active');
        }
    });*/

    mainmenu.children('li.products, li.fetish, li.images').mouseover(function(){
        permanent_search = false;
        //mainmenu.children('li.search').find('.form-search').css('display', 'none');

        var $this = $(this);
        $this.find('.drop-down').css('display','block');
        $this.children('a').addClass('active');
    }).mouseout(function(){
        var $this = $(this);
        $this.find('.drop-down').css('display','none');
        $this.children('a').removeClass('active');
    });

    var products_dd = mainmenu.children('li.products').find('.drop-down-cont'),
        placeholder = $('<div class="image"><h3></h3><img src="#"></div>');
    placeholder.appendTo(products_dd);
    placeholder.find('h3').html(products_dd.find('ul li a:first').html());
    placeholder.find('img').attr('src', products_dd.find('ul li a img:first').attr('src'));
    products_dd.find('ul li a').mouseover(function(){
        var $this = $(this);
        placeholder.find('h3').html($this.html());
        placeholder.find('img').attr('src', $this.children('img').attr('src'));
    });

    var fetish_dd = mainmenu.children('li.fetish').find('.drop-down-cont'),
        fplaceholder = $('<div class="image"><h3></h3><img src="#"></div>');
    fplaceholder.appendTo(fetish_dd);
    fplaceholder.find('h3').html(fetish_dd.find('ul li a:first').html());
    fplaceholder.find('img').attr('src', fetish_dd.find('ul li a img:first').attr('src'));
    fetish_dd.find('ul li a').mouseover(function(){
        var $this = $(this);
        fplaceholder.find('h3').html($this.html());
        fplaceholder.find('img').attr('src', $this.children('img').attr('src'));
    });
    
     var images_dd = mainmenu.children('li.images').find('.drop-down-cont'),
        iplaceholder = $('<div class="image"><h3></h3><img src="#"></div>');
    iplaceholder.appendTo(images_dd);
    iplaceholder.find('h3').html(images_dd.find('ul li a:first').html());
    iplaceholder.find('img').attr('src', images_dd.find('ul li a img:first').attr('src'));
    images_dd.find('ul li a').mouseover(function(){
        var $this = $(this);
        iplaceholder.find('h3').html($this.html());
        iplaceholder.find('img').attr('src', $this.children('img').attr('src'));
    });

    $('.overlay').css('display','none');
    $('.products .product').mouseover(function(){
        $(this).find(".overlay").css('display','block');
    }).mouseout(function(){
        $(this).find(".overlay").css('display','none');
    });
});

/*var Gallery = function(base, options){

    var gallery, cacheEl, overlay, current;
    var limit = {
        'width': 555,
        'height': 670
    };
    var initial_size = {
        'width': 100,
        'height': 100
    };

    gallery = jQuery('<div/>',{
        'class': 'image-gallery-overlay',
        'css':{
            'display': 'none'
        }
    }).appendTo('body');

    gallery.html('<div class="image-gallery"><a href="#" class="close">Close</a><div class="tools"><div class="description"/><a href="#" class="prev">Prev</a><a href="#" class="next">Next</a></div><div class="image-detail"><img class="fullsize"/></div></div>');
    gallery.find('.description a.external').live('click', function(){
        window.open($(this).attr('href'));
        return false;
    });

    cacheEl = jQuery('<div/>',{
        'css':{
            'display': 'none'
        }
    }).appendTo(gallery);

    function get_img(img_element){
        var t = new Image();
        t.src = (img_element.getAttribute ? img_element.getAttribute("src") : false) || img_element.src;
        return t;
    }

    var scale_image = function(img, source){
        var this_img = get_img(source),
            width = this_img.width,
            height = this_img.height;
        if (width > limit.width){
            height /= width / limit.width;
            width = limit.width;
        }
        if (height > limit.height){
            width /= height / limit.height,
            height = limit.height;
        }

        img.stop().attr('src', jQuery(source).attr('src')).animate({
            'width': width,
            'height': height
        }, 'fast');
    };

    var load_image = function(link){
        var rel = link.attr('rel'),
            cache = cacheEl.find('img[rel='+rel+']'),
            img = gallery.find('.image-detail img'),
            info = base.find('.details[rel='+rel+']');

        var viewWidth = img.width() || initial_size.width,
            viewHeight = img.height() || initial_size.height;

        gallery.data('rel', rel); // save current loading image

        if (!cache.size()){
            gallery.find('.image-detail').addClass('loading');
            img.css({
                'width': viewWidth,
                'height': viewHeight
            });
            cache = jQuery('<img/>')
                .attr('rel', rel)
                .attr('src', link.attr('href'))
                .load(function(){
                    if (gallery.data('rel') == rel){
                        gallery.find('.image-detail').removeClass('loading');
                        scale_image(img, this);
                    }
                }).appendTo(cacheEl);
        } else {
            scale_image(img, cache[0]);
        }
        gallery.find('.description').html(info.html());
    };

    var gallery_open = function(rel){
        var link = base.find('.image[rel='+rel+']');
        current = rel;
        load_image(link);
        update_tools();
        gallery.fadeIn();
    };

    var gallery_close = function(){
        current = null;
        gallery.fadeOut();
    };

    var get_next = function(rel){
        var alink = base.find('.image[rel='+rel+']'),
            link = alink.nextAll('.image').eq(0);
        if (!link.size()){
            link = alink.parent().next().find('.image:first');
        }
        return link;
    }

    var get_prev = function(rel){
        var alink = base.find('.image[rel='+rel+']'),
            link = alink.prevAll('.image').eq(0);
        if (!link.size()){
            link = alink.parent().prev().find('.image:last');
        }
        return link;
    };

    var update_tools = function(){
        var next = gallery.find('.tools a.next'),
            prev = gallery.find('.tools a.prev');
        if (get_next(current).size()){
            next.show();
        } else {
            next.hide();
        }
        if (get_prev(current).size()){
            prev.show();
        } else {
            prev.hide();
        }
    };

    var gallery_next = function(){
        var link = get_next(current);
        if (link.size()){
            current = link.attr('rel');
            load_image(link);
            update_tools();
        }
    };

    var gallery_prev = function(){
        var link = get_prev(current);
        if (link.size()){
            current = link.attr('rel');
            load_image(link);
            update_tools();
        }
    };

    gallery.find('a.close').click(function(event){
        event.preventDefault();
        gallery_close();
    });
    gallery.find('.tools a.next').click(function(event){
        event.preventDefault();
        gallery_next();
    });
    gallery.find('.tools a.prev').click(function(event){
        event.preventDefault();
        gallery_prev();
    });


    base.find('a.image').live('click', function(event){
        event.preventDefault();
        var $this = jQuery(this);
        gallery_open($this.attr('rel'));
    });

};


// Image gallery
jQuery(document).ready(function($){
    $('.images-index-index form.description select[name=category]').change(function(){
        $(this).parent('form').submit();
    });

    var base = $('.images-index-index .images');

    Gallery(base);

});*/

// Product view tabs
jQuery(document).ready(function($){
    $('.product-view .tabs>.tab').each(function(){
        $(this).click(function(evt){
            evt.preventDefault();

            var $this = $(this),
                tabs = $this.parent(),
                active = tabs.parent().find('.tab-container#' + $this.attr('href').replace(/^#/,''));
            tabs.parent().find('.tab-container').not(active).hide();
            active.css('visibility', 'visible').show();
            tabs.find('.tab.active').removeClass('active');
            $this.addClass('active');
        });
    });
    $('.product-view .tab-container').hide();
    $('.product-view .tabs>.tab.active').click();
});


// Product view custom options and Qty selection
jQuery(document).ready(function($){

    jQuery('.products-grid .item img').hover(function(e){
		/*
		//alert("done");
		
		console.log('passe');
		
		e.preventDefault();
		var $this = jQuery(this);
		$this.attr('href',$this.attr('src'));
		$this.attr('src',$this.attr('rel'));
		},function(e){
		var $this = jQuery(this);
		e.preventDefault();
		$this.attr('src',$this.attr('href'));
		*/
	});
	
			
    var form = $('.product-view form#product_addtocart_form'),
        location = form.find('.product-options .product-custom-option-location').not(':input'),
        qty_delivery = form.find('#qty-delivery'),
        qty_instore = form.find('#qty-in-store'),
        qty = $('<input name="qty" value="1"/>');

    var mark_required = function(rows){
        var dt = rows.filter('dt'),
            dd = rows.filter('dd');
        if (!dt.find('span.required').size()){
            dt.find('label').append('<span class="required"> *</span>');
        }
        dt.find('span.required').show();
        dd.find(':input').addClass('required-entry');
    };
    var unmark_required = function(rows){
        var dt = rows.filter('dt'),
            dd = rows.filter('dd');
        dt.find('span.required').remove();
        dd.find(':input').removeClass('required-entry');
    };

    qty_delivery.attr('name', 'qty-delivery');
    qty_instore.attr('name', 'qty-in-store');
    qty.hide().prependTo(form);

    if (!qty_delivery.size() && qty_instore.size()){
        mark_required(location);
    }

  //  if (!qty_delivery.size() || !qty_instore.size())
      //  return;

    location.hide();

    //if ($('.product-view .product-options .product-custom-option-location').size()){
        //qty_delivery.prepend('<option value=""/>');
    //}

    var useQty = function(qtyUse, qtyNotUse) {
    	    	
        qtyUse.addClass('validate-greater-than-zero').removeClass('validation-failed');
        qtyNotUse.removeClass('validate-greater-than-zero').removeClass('validate-greater-than-zero').removeClass('validation-failed');
        
        jQuery('#advice-validate-greater-than-zero-qty-in-store').hide();
        jQuery('#advice-validate-greater-than-zero-qty-delivery').hide();
    }
    
    qty_delivery.change(function(){
    	
        var $this = $(this);
        qty.val($this.val() || 1);
        if ($this.val().strip()){
            //location.hide();
            qty_instore.val('');
            //qty_delivery.find('option[val=""]').remove();
            //qty_instore.removeClass('required-entry');
            useQty(qty_delivery, qty_instore);
        } else {
            //unmark_required(location);
            // do nothing?
        }
    });
    
    qty_instore.change(function(){
        	
        var $this = $(this);
        qty.val($this.val() || 1);
        if ($this.val().strip()){
            //location.show();
            //qty_delivery.append('<option val=""></option>');
            qty_delivery.val('');
            //qty_delivery.removeClass('required-entry');
            useQty(qty_instore, qty_delivery);
        } else {
            //qty_delivery.val(1);
            //location.hide();
            //unmark_required(location);
        }
    });
});
//events calendar popup
jQuery(document).ready(function($){
    $('a#view_all_events').fancybox({
        width: 500,
        height: 500
    });
});

function hideRegionFranceOnly(countryEl, regionEl)
{
    //hide state for France only
    jQuery(countryEl).keyup(function() {
        func(this);
    });
    jQuery(countryEl).change(function() {
        func(this);
    });

    var func = function (obj) {
        var county = jQuery(regionEl).parent().parent();
        if (jQuery(obj).val() == 'FR') {
            county.hide();
        } else {
            county.show();
        };
    }
}

