jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],{
	random: function(a, i, m, r){
		if(i == 0){
			jQuery.jQueryRandom = Math.floor(Math.random() * 3);
		};
		return i == jQuery.jQueryRandom;
	}
});

jQuery(document).ready(function(){
	// enews
	$('.e-signup a').click(function(){
		$('#enews-wrap').slideToggle();
		return false;
	});
	
	// var id_is_this = $('form#enews #name').val();
	// alert(id_is_this);

	// enews validation
	$("form#enews").validate({
		rules: {
			name: "required",
			// name: {
			// 	required: function(element){
			// 		return $('form#enews #name').val() != "name";
			// 	}
			// },			
			// name: {
			// 	required: $('form#enews #name').val() != 'name'
			// },
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			name: "Please enter your name",
			email: "Please enter a valid email address"
		},
		errorElement: "em",
		errorPlacement: function(error, element) {
			var id_is = element.attr("id");
			error.insertAfter( $('#fs-submit') );
	   	}
	});

	// ask an expert validation
	$("#hammer_ask_a_pro").validate({
		rules: {
			question: "required",
			askemail: {
				required: true,
				email: true
			}
		},
		messages: {
			question: "Please enter your question",
			askemail: "Please enter a valid email address"
		},
		errorElement: "em",
		errorPlacement: function(error, element) {
			var id_is = element.attr("id");
			error.insertAfter( $('#fs-'+id_is) );
	   	}
	});
	
	// $("#hammer_ask_a_pro")[0].reset();
	// 	$("#enews-wrap form")[0].reset();
	
	// clear out text fields with defualt values
	$('#search-input').autoclear();
	$('#askemail').autoclear();
	$('#question').autoclear();
	$('#keyword-search-box').autoclear();
	$('#enews input.name').autoclear();
	$('#enews input.email').autoclear();

	// open links in new window
	$('a[rel="ext"]').click(function(){
		window.open(this.href); 
		return false;
	});

	// change language
	$('.language').click(function(){
		var language = $(this).attr('rel');
		$('body').translate(language);
		$.cookie('language', language, { path: '/', expires: 365 });
		return false;
	});
	
	if($.cookie('language') && $.cookie('language') != 'en'){
		$('body').translate($.cookie('language'));
	}

	// product detail images
	$('#product_hero .img-wrap').cycle({ 
		fx:    'fade', 
		speed:  2500 
	});

	// form validation
	$("#hammer_product_registration").validate({
		rules: {
			name: "required",
			address1: "required",
			city: "required",
			state: "required",
			country: "required",
			zip: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			name: "Please enter your name",
			address1: "Please enter your address",
			city: "Please enter your city",
			state: "Please enter your state",
			country: "Please enter your country",
			zip: "Please enter your zip code",
			email: "Please enter a valid email address",
			agree: "Please accept our policy"
		}
	});

	// tabbed area
	$('#tabbed-content').cycle({
		fx: 'fade',
		speed: 300,
		timeout: 0,
		pager: '#tab-btns',
		height: '319px',
		pagerAnchorBuilder: function(idx, slide){
			// return selector string for existing anchor
			return '#tab-btns li:eq(' + idx + ') a';
		}
	});

	randomElements = $(".flash-ball").get().sort(function(){ 
		return Math.round(Math.random())-0.5
	}).slice(0,3)

	// Homepage Flash Switching
	$('.flash-ball').click(function(){ return false; });
	$('.flash-ball').click(function(){
		if($('#playing')){
			swfobject.removeSWF("playing");
			var d = document.createElement("div");
			d.setAttribute("id", "flash-head");
			document.getElementById("masthead").appendChild(d);
		}
		var flashvars = {};
		var params = {
			menu: "false",
			wmode: "transparent"
		};
		var attributes = {
			id: "playing",
			name: "playing"
		};
		swfobject.embedSWF($(this).attr('rel'), "flash-head", "690", "286", "9", false, flashvars, params, attributes);
	});

	var flashvars = {};
	var params = {
		menu: 'false',
		wmode: "transparent",
		scale: "exactFit"
	};
	var attributes = {
		id: 'playing',
		name: 'playing'
	};
	swfobject.embedSWF($(randomElements).attr('rel'), "flash-head", "690", "286", "9", false, flashvars, params, attributes);
});