;(function($){

	window.matra = { fn:{} };
	
	matra.fn.initPurchaseToggle = function(){
	  var jPurchaseBox = jQuery('.prd-purchase'),
	
			jPurchaseCtl = jPurchaseBox.find('.heading').css({cursor:'pointer'}),
			 
			jPurchaseCt = jPurchaseBox.find('.content').hide();
			
			
		jPurchaseCtl.click(function(){
		  if (jPurchaseCt.is(':hidden')){
				jPurchaseBox.addClass('expanded');
				jPurchaseCt.slideDown(150);
			} else { 
				jPurchaseBox.removeClass('expanded');
				jPurchaseCt.slideUp(150);
			}
		});
	}
	
	matra.fn.initItemDetails = function(box){
		jQuery(box).find('.prd').each(function(){
			var jPrd = jQuery(this),
				jLink = jQuery(this).find('a'),
				sURI = jLink.attr('href');
			
			if(!jPrd.hasClass('is-corporate') && !jLink.hasClass('no-detail-link'))
			{
			  jLink.click(function(e){
  				if ( jQuery.browser.msie && jQuery.browser.version < 8) {  } else {
  					e.preventDefault();
  					//jQuery.superbox.close();
  					jQuery.superbox.wait(function(){
  						jQuery.ajax({
  							url : sURI,
  							data : {layered: 1},
  							type: 'POST',
  							success : function(data){
  							  jQuery('#receptacle-modal').html(data);
  							  //var jItemDetails = jQuery(data).find('#item-details');
  							  var jItemDetails =  jQuery('#receptacle-modal').find('#item-details');
  							  jQuery.superbox.open(jItemDetails,{boxWidth:'828',boxHeight:'auto',overlayOpacity:.5});
  						    //matra.fn.initPurchaseToggle();
  							}
  						});
  					});
  				}
  			})
			}
		});
	}
	
	jQuery.fn.uiSelect = function(params){
		
		var params = jQuery.extend({
			effect : false,
			effectSpeed : 150,
			callback : function(){}
		},params);
		
		return this.each(function(){
			
			var jSelect = jQuery(this),
				sSelectWidth = jSelect.outerWidth(),
				jOptions = jSelect.find('option'),
				jSelectedOpt = jOptions.filter(function(){ return jQuery(this).is(':selected'); }),
				jUISelect = jQuery('<a class="ui-select" tabindex="0"><strong class="ui-select-content"><span class="ui-selected">'+ jSelectedOpt.html() +'</span><span class="ui-select-controler"></span></strong><ul class="ui-option-group"></ul></a>'),
				jUISelected = jUISelect.find('.ui-selected'),
				jRelLabel = jQuery('label[for='+jSelect[0].id+']'),
				jUIOptGroup = jUISelect.find('.ui-option-group');
			
			jSelect.hide();
			
			jOptions.each(function($i){
				var jOpt = jQuery(this),
					jUIOpt = jQuery('<li class="ui-option">'+ jOpt.html()  +'</a></li>').data(jSelect.attr('name'),jOpt.val()).appendTo(jUIOptGroup);
				
				function Optselect(opt){
					var jRelOpt = jOpt.filter(function(){ return jQuery(this).val() == opt.data(jSelect.attr('name')); });
					jRelOpt.attr('selected','selected');
					jUISelected.html(jRelOpt.html());
				}
				
				jUIOpt.click(function(e){ 
					e.stopPropagation();
					Optselect(jUIOpt);
					params.callback(jUIOpt.data);
					jUIOptGroup.trigger('collapse');
					jSelect.change();
				});
				
			});
			
			function getSelectedUIOpt(){
				sSelectedOpt = jOptions.filter(function(){ return jQuery(this).is(':selected'); }).index();
				return sSelectedOpt;
			}
			
			jUISelect.bind({
				click : function(e){ e.stopPropagation(); if ( jUIOptGroup.is(':hidden') ){ jUIOptGroup.trigger('expand'); } else { jUIOptGroup.trigger('collapse'); } },
				focus : function(){
					jQuery(document).keypress(function(e){
						if ( e.keyCode == 40 ) { e.preventDefault(); jQuery(jUIOptGroup.find('.ui-option')[getSelectedUIOpt() + 1]).click(); }
						if ( e.keyCode == 38 ) { e.preventDefault(); jQuery(jUIOptGroup.find('.ui-option')[getSelectedUIOpt() - 1]).click(); }
					});
					jQuery(document).click(function(e){
						jUIOptGroup.trigger('collapse');
					});
				},
				blur : function(){ 
					jQuery(document).unbind('keypress');
					jUIOptGroup.trigger('collapse');
				}
			}).insertAfter(jSelect);
			
			jUIOptGroup
				.bind({
					expand : function(){
						switch(params.effect){
							case false : jUIOptGroup.show(); break;
							case 'slide' : jUIOptGroup.slideDown(params.effectSpeed); break;
							case 'fade' : jUIOptGroup.fadeIn(params.effectSpeed); break;
						}
					}, 
					collapse : function(){
						switch(params.effect){
							case false : jUIOptGroup.hide(); break;
							case 'slide' : jUIOptGroup.slideUp(params.effectSpeed); break;
							case 'fade' : jUIOptGroup.fadeOut(params.effectSpeed); break;
						}
					}
				});
				
			
			var sUISelectWidth = jUIOptGroup.outerWidth();
			if ( sUISelectWidth < sSelectWidth ) sUISelectWidth = sSelectWidth;
			
			jUISelect.css({minWidth:sUISelectWidth + 'px' });
			jUIOptGroup.css({minWidth: sUISelectWidth + 'px' }).hide();
			jRelLabel.click(function(){ jUISelect.focus(); });
		});
		
	}; 
})(jQuery);
jQuery(function(){
	matra.fn.initItemDetails('#content .box-prds-list');
	matra.fn.initItemDetails('#content .box-special-product');
	matra.fn.initItemDetails('#content .box-novelties');
	jQuery('.ui-select').uiSelect({ effect : 'fade' });
	jQuery.superbox();
});
