jQuery(document).ready(function() {

	var one = true;
	var count = jQuery('.sidebar > .widget').length;
	var i = 0;
	jQuery('.sidebar > .widget').each(function(e){
		i++;
		if ( one ) {
			jQuery(this).addClass('first');
			one = false;
		} else if ( count == i ) {
			jQuery(this).addClass('last');
		}
	});
	var brandCount = 0;
	
	jQuery('.home_brand a img').each(function(index){
		jQuery(this).parent().parent().css({'display':'block','position':'absolute','top':0,'left':0});
		jQuery(this).parent().parent().fadeOut(10);
		jQuery(this).parent().parent().addClass('slide_'+brandCount);
		brandCount++;
	});
	var current = Math.floor(Math.random()*brandCount);
	jQuery('.home_brand .slide_'+current).fadeIn(10);
	function nextBrand(){
		if(brandCount>1){
			if((current+1)<brandCount){
				jQuery('.slide_'+(current+1)).fadeIn(1000);
				current++;
			}else{
				jQuery('.home_brand a img').each(function(index){
					var is = jQuery(this).parent().parent();
					if(jQuery(is).hasClass('slide_'+0)){
						jQuery(is).fadeIn(10);
					}else{
						if(jQuery(is).hasClass('slide_'+(brandCount-1))){
							jQuery(is).fadeOut(1000);
						}else{
							jQuery(is).fadeOut(10);
						}
					}
				});
				current = 0;	
			}
		}
	}
	if(brandCount>1){setInterval(function() {nextBrand();}, 4000);}
	
	jQuery(".product-image-zoom").unbind("dblclick", function(){
		console.log("Double-click happened in " + this.nodeName);
		return false;
	});
	
	//jQuery(window)._scrollable();
	
	jQuery('#review-form, #customer-reviews').hide();
	jQuery('#show-review-form').toggle(function(e){
		jQuery('#customer-reviews').hide();
		jQuery('#review-form').show();
	},function(e){
		jQuery('#review-form').hide();
	});
	jQuery('#show-customer-reviews').toggle(function(e){
		jQuery('#review-form').hide();
		jQuery('#customer-reviews').show();
		jQuery.scrollTo('#customer-reviews');
	},function(e){
		jQuery('#customer-reviews').hide();
	});
	
	//bbi-att-swatches
	/*var bbi_att_sets = new Array();
	jQuery('#product-options-wrapper ul.bbi-att-swatches').each(function(){
		bbi_att_sets.push(jQuery(this).attr('rel'));
	});
	alert(bbi_att_sets);
	for(var bbi_id in bbi_att_sets){
		jQuery('#'+bbi_att_sets[bbi_id]).change(function(){
			//var bbi_att_value = jQuery(this).val();
			//alert(bbi_att_value);	
		});
	}*/
	
	jQuery('#product-options-wrapper select.super-attribute-select').change(function(){
		var bbi_img_change=jQuery('#product-options-wrapper ul.bbi-att-swatches li a img#att-'+jQuery(this).val()).parent().attr('href');
		changesrc(bbi_img_change,jQuery(this).val());
		//jQuery('#image').attr('src',bbi_img_change);
	});
	
	jQuery('#product-options-wrapper ul.bbi-att-swatches li a').click(function(){
		var bbi_id=jQuery(this).children('img').attr('id');
		bbi_id = bbi_id.replace('att-','');
		jQuery('#'+jQuery(this).parent().parent().attr('rel')).val(bbi_id);
		changesrc(jQuery(this).attr('href'),bbi_id);
		/*jQuery(this).parent().parent().children('li a').removeClass('active');
		jQuery(this).addClass('active');*/
		return false;
	});
	var bbi_refresh = false;
	function changesrc(bbi_src,bbi_id){
		jQuery('#product-options-wrapper ul.bbi-att-swatches li.active').removeClass('active');
		jQuery('#product-options-wrapper ul.bbi-att-swatches li a img#att-'+bbi_id).parent().parent().addClass('active');
		jQuery('#image').attr('src',bbi_src).bind('load',function(){
			
			/*if(bbi_refresh == false){
			var product_zoom = new Product.Zoom('image', 'track', 'handle', 'zoom_in', 'zoom_out', 'track_hint');
			bbi_refresh=true;
			}else{
				product_zoom.scale();
			}*/
		});
	}
	if(jQuery('.category-locker-lookz .category-image').html()!=null){
		jQuery('.category-locker-lookz .category-image').find('img').wrap('<a id="lockers-video-link" href="/assets/swf/nn_locker_video.swf" />').parent().fancybox({
			swf:{wmode:'transparent'},
			overlayColor:'#000',
			overlayOpacity:.8
		});
	}
});
