function l(text){
	console.log(text);
}

function equalHeight(group) {
	tallest = 0; 
	special = 0;
	group.each(function(c) {
		thisHeight = parseInt(c.getDimensions().height); 
		
	  
   		
		if(thisHeight > tallest) {
			tallest = thisHeight; 
			
			
		} 
		
            c.childElements().each(function(s){ 
				s.childElements().each(function(f){ 
					if( f.className == 'special-price'){
					   tallest += 10;   
					special = 10;
						//alert(tallest);
					} 
					
					
				});
			});

		

	}); 
	
	var offset = 25; 
	
	

	
   
   // alert(tallest-offset); 
	
	if(document.getElementById('homepage-carosel')) {
		group.invoke("setStyle", {height: (tallest-offset) + 'px'})  ;
		
		//group.invoke("setStyle", {height: '240px'})  ;
	}  else {
		group.invoke("setStyle", {height: (tallest-offset-special) + 'px'})  ;
	}
   //$$('.item .button.button.btn-cart, .item .link-wishlist, .products-grid .product-image, .products-grid .price-box, .products-grid .product-more-info').invoke('setStyle', {position: 'absolute'});
    $$('.item .button.button.btn-cart, .item .link-wishlist, .products-grid .product-image, .products-grid .price-box, .products-grid .product-more-info, .products-grid .ratings').invoke('setStyle', {position: 'absolute'});
    //$$('.item .button.button.btn-cart, .item .link-wishlist, .products-grid .product-image, .products-grid .price-box, .products-grid .product-more-info, .products-grid .ratings').invoke('setStyle', {position: 'absolute'});
}


function equalHeight2(group) {
	tallest = 0;
	group.each(function(c) {
		thisHeight = parseInt(c.getDimensions().height); 
		
		//l(c.getDimensions().height);
   		
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}

	}); 
		
	group.invoke("setStyle", {height: (tallest) + 'px'})  ;
  
}



document.observe("dom:loaded", function() { 
	
	l('loaded staak app.js');  
	
	
	if(document.getElementById('product-options-wrapper')){
		
		var addToList = document.getElementsByClassName('add-to-links')[0];
		//addToList.remove();
		l(addToList);
		  
		new Insertion.Bottom($$('.product-options-bottom div.add-to-cart')[0],addToList); 
		//add-to-cart
		
	}
	
	
	if(document.getElementById('carousel-wrapper')){
	
		if($$('#carousel-content .slide').length > 1) {
			l("$$('#carousel-content .slide')" + $$('#carousel-content .slide').length);
			var dummySlide = $$('#carousel-content .slide')[0].innerHTML;
			//new Insertion.Top($('carousel-wrapper'),dummySlide);
			new Insertion.Bottom($('carousel-content'),'<div class="slide">'+dummySlide+'</div>'); 
		
		} 
	
		new Carousel('carousel-wrapper', $$('#carousel-content .slide'), $$('a.carousel-control', 'a.carousel-jumper')); 
    
	}
	
	
	$$('.product-image img').each(function(element){
	   
		
		$(element).observe('mouseover',function(event){
			var _element = Event.element(event);
			_element.fade({ duration: 0.2, from: 1, to: 0.7 });
		})	.observe('mouseout',function(event){
				var _element = Event.element(event);
				_element.fade({ duration: 0.2,  to: 1 });
			});  
			
			//
	});
	
	var pTitles = $$('.page-title,  .catalog-product-view #primary .product-name, .widget-title');
	
   
	pTitles.each(function(s, index){
		new Insertion.Bottom($(s),'<span class="turn-over"></span>');
	});   
	
    
	$$('#secondary .block-title, #narrow-by-list dt, #narrow-by-list2 dt').each(function(s, index){
		new Insertion.Bottom($(s),'<span class="icon"></span>');
	});
	

    
    equalHeight2($$(".discount, .totals"));  
	equalHeight($$(".products-grid li.item"));  
   
	if(!document.getElementById('customer-reviews')){

  	if(document.getElementById("product-collateral-inner")) {
		new Insertion.Top($('product-collateral-inner'),'<ul id="product-collateral-tabs"></ul>');
    }
        
   
	$$('.box-collateral').each(function(e, index){
		var id =  e.className.replace(' ','-');
		$(e).setAttribute('id', id);
		
		new Insertion.Bottom($('product-collateral-tabs'),'<li id="tab-'+id+'"><a>'+$(e).firstDescendant('h2').hide().innerHTML+'</a></li>'); 
		
		if(index > 0){
			$(e).hide();
		}
		
	   
		
	}); 
	     
	
	$$('#product-collateral-tabs a').each(function(e,index){
			$(e).observe('click', respondToClick);
			
			if(index == 0) { $(e).addClassName('active'); } 
	}); 
	
	function respondToClick(event) {
		
		 $$('#product-collateral-tabs a').invoke('removeClassName', 'active');
		var element = Event.element(event);
	  	element.addClassName('active'); 
       	var parentid = $(element).up('li').id;

		
		
		$$('.box-collateral').invoke('hide');
		
		$(parentid.replace('tab-','')).show();
		
	}    
	}
});
