/*
 * User Interface Functions
 * requires jQuery
 */

// Return new array with duplicate values removed
function unique(arrayName){
	var newArray=new Array();
	label:for(var i=0; i<arrayName.length;i++ )
	{  
	    for(var j=0; j<newArray.length;j++ )
	    {
	        if(newArray[j]==arrayName[i]) 
	            continue label;
	    }
	    newArray[newArray.length] = arrayName[i];
	}
	return newArray;
}


// jQuery
jQuery(document).ready(function(){
	// row highlighting on the latest news widget
	jQuery('.news-item:odd').addClass('highlight');
	
	
	// search field hint
	var hint_text = 'Enter Search Terms';
	jQuery('#s').addClass('blur');
	if (jQuery('#s').val() == ''){
		jQuery('#s').val(hint_text);
	}
	
	jQuery('#s').focus(function(){
		if (jQuery(this).val() == hint_text){
			jQuery(this).val('');
		}
		jQuery(this).removeClass('blur').addClass('focus');
	});
	
	jQuery('#s').blur(function(){
		if (!jQuery(this).val().length){
			jQuery(this).val(hint_text);
			jQuery(this).removeClass('focus').addClass('blur');
		}
	});
	
	
	// product filter
	// get all taxonomies currently on page
	var taxonomies = new Array();
	jQuery('.product_list_item').each(function(index){
		 var this_rel= jQuery(this).attr('rel');
		 var this_tax = this_rel.split(" ");
		 
		 for (i in this_tax){
			 taxonomies.push(this_tax[i]);
		 }
	});
	taxonomies = unique(taxonomies); // remove duplicates
	
	// auto-check current taxonomies
	jQuery('.filter_checkbox').each(function(index){
		var value = jQuery(this).val();
		
		if (jQuery.inArray(value, taxonomies) != -1){
			jQuery(this).attr('checked', 'checked');
		}else{
			jQuery(this).removeAttr('checked'); // uncheck ones that aren't relevant so the browser doesn't keep them checked on page reload. that's just confusing.
		}
	});
	
	// show/hide products when boxes get checked/unchecked
	jQuery('.filter_checkbox').click(function(){
		
		// scan all checked boxes
		var checked_tax = new Array();
		var unchecked_tax = new Array();
		
		jQuery('.filter_checkbox:checked').each(function(index){
			checked_tax.push(jQuery(this).val());
		});
		
		jQuery('.filter_checkbox').not(':checked').each(function(index){
			unchecked_tax.push(jQuery(this).val());
		});
		
		// hide products that have taxonomies that are unchecked
		jQuery('.product_list_item').each(function(index){
			var this_rel= jQuery(this).attr('rel');
			var this_tax = this_rel.split(" ");
			
			var show = true;
			
			for (i in this_tax){
				if (jQuery.inArray(this_tax[i], unchecked_tax) != -1){
					show = false;
				}
			}
			
			if (show == true){
				jQuery(this).fadeIn('100');
			}else{
				jQuery(this).fadeOut('100');
			}
		});
	});
	
	//e-newsletter signup form
	jQuery('form#newslettersignup a.cancel').click(function(e){
		e.preventDefault();
		window.parent.Shadowbox.close();
	});
	jQuery('form#newslettersignup a.submit').click(function(e){
		e.preventDefault();
		jQuery('form#newslettersignup').submit();
	});

	jQuery('form#newslettersignup').submit(function(e){
		var sendok=true;
		jQuery('form#newslettersignup p.err').remove();
		if(jQuery('input#field_firstname').val().replace(/(^\s*)|(\s*$)/g,'')==''){
			sendok=false;
			jQuery('input#field_firstname').after('<p class="err">You must enter your first name</p>');
		}
		if(!jQuery('input#field_email').val().replace(/(^\s*)|(\s*$)/g,'').match(/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)){
			sendok=false;
			jQuery('input#field_email').after('<p class="err">Please enter a valid email address</p>');
		}
		if(sendok){
			jQuery(this).hide();
			jQuery('li.alerts a.signup').after('<p class="thankyou">Thank you.</p>');
			setTimeout('window.parent.Shadowbox.close()',1000);
			return true;
		}else{
			e.preventDefault();
			return false;
		}
	});
	
});
