jQuery.noConflict();
(function($) {
$(document).ready(function(){
	/* !primary nav ============================== */
	$('#primary-nav li').hover(function(){
		$(this).toggleClass('hover').children('ul').show();
	},function(){
		$(this).toggleClass('hover').children('ul').hide();
	});
	
	
	/* !advanced search ============================== */
	$('a.advanced-search').click(function(){
		$(this).toggleClass('active');
		$('#advanced-search').toggle('fast');
		return false;
	});
	
	$('.remove-filter').live('click', function(){
		$(this).parent().next('select').resetSelect();
		$(this).remove();
		return false;
	});
	
	$('#reset-options').click(function(){
		$('#advanced-search .remove-filter').remove();
		$('#advanced-search select').each(function(){
			$(this).resetSelect();
		});
		$('.ajax-loader').ajaxComplete(function(){
			$(this).hide();
		});
		return false;
	});
	
	$('.viewAllCatalogLinks').click(function(){
		
		list = $(this).attr('title');
		$('ul#'+list).slideToggle();
		
		return false;
	});
	
	
	/* !resize content images to be no taller than 175px ============================== */
	$('.content-img').hide();
	$('.content-img').each(function(){
		var size = 100;
		// define w and h variables
		w = $(this).width();
		h = $(this).height();
		
		// if w is already less than 100 display the image and return true.
		if(w <= size) { $(this).fadeIn(500); return true;}

		r = w / size;
		h = h / r;
		
		$(this).width(100).height(h).fadeIn(500);	
	});
	
	$('#advanced-search select').updateSelect({scope:'#advanced-search select'});
	$('#app-filters select').updateSelect({scope:'#app-filters select'});
	/* !plugins ============================== */
	
	$('#figureNoSearch').clearInput({defaultVal: ''});
});

})(jQuery);

(function($){

	$.fn.resetSelect = function()
	{
		this.before('<img src="/images/ajax-loader.gif" class="ajax-loader" />');
		var field = this.attr('name');
		var selected= $('option:selected', this).text();
		$.ajax({
			url: 'assets/snippets/productmanager/ajax-requests/get-select-options.php?field=' + field,
			cache: false,
			success: function(html){
				$("#advanced-search select[name=" + field + "]").html(html);
				$('.ajax-loader').remove();
			}
		});			
	};
	
	/* ! repopulate dropdown menus according to what is selected ============================== */
	$.fn.updateSelect = function(options)
	{
		var defaults = {
			scope : false,
			loadingImage : '/images/ajax-loader.gif',
			loadingClass : 'ajax-loader'
		};
		var options = $.extend(defaults, options);
		
		// Do we have a select element
		if(options.scope == false) alert('The selectUpdate plugin needs a scope.');
		
		return this.change(function() {
			var col = $(this).attr('name');
			var newval = $(this).val();
			
			$(options.scope).each(function(){
				$(this).after('<img src="'+options.loadingImage+'" class="'+options.loadingClass+'" />');
				var selected = $('option:selected', this).text();
				var field = $(this).attr('name');
				
				// If this is our current field, return without doing anything.  We don't want to modify the field we originally changed.
				if(field == col) return true;
				
				$(this).prev('label').children('.remove-filter').remove().end().prepend('<a href="#" class="floatright remove-filter" style="color:#fff">remove filter</a>');
				
				// Make the ajax call.
				$.ajax({
					url: 'assets/snippets/productmanager/ajax-requests/get-select-options.php?col=' + col + '&newval=' + newval + '&field=' + field + '&selected=' + selected,
					cache: false,
					success: function(html){
						if( ! html) html = '<option value="">No options available</option>';
						$(options.scope+"[name=" + field + "]").html(html);
					}
				});		
			});
			
			// Remove the loading graphic
			$('.ajax-loader').ajaxComplete(function(){
				$(this).animate({opacity: 1}, 1000, function(){
					$(this).remove();
				});
			});
		});
	};
})(jQuery);

(function($){  
	$.fn.clearInput = function(options) {  
		settings = jQuery.extend({
			defaultVal: 'Search'
		}, options);
		
		return this.each(function() {
			if($(this).val() == '')
			{
				$(this).val(settings.defaultVal);
			}
			$(this).focus(function(){
				if($(this).val() == settings.defaultVal)
				{
					$(this).val('');
				}
			});
			$(this).blur(function(){
				if($(this).val() == '')
				{
					$(this).val(settings.defaultVal);
				}
			});
		});  
	};  
})(jQuery); 
