var fpData ; var fpBox ; var fpContent

function fpUpdate(i){
	
	fpContent.fadeOut("slow",function(){
		
		$('img',fpContent)
		.attr('src',"/site_content/site_"+fpData.sn+"/images/products/product_"
		+fpData.products[i].id+".jpg")
		.parent('a').attr("href","/product-"+fpData.products[i].id+".wtl")
		.next('a').attr("href","/product-"+fpData.products[i].id+".wtl")
		.text(fpData.products[i].title);
		
		if (fpData.products[i].poa == 1) {
			$("#fp_price",fpContent).text("POA");
		}else{
			$("#fp_price",fpContent).text("£"+fpData.products[i].price.toFixed(2));		
		};
		
	});
	fpContent.fadeIn("slow");
	//console.log('fp update occurred');
	if(i+1<fpData.products.length){ i++; } else { i = 0; }
	setTimeout('fpUpdate('+i+')',10000);
};

$(document).ready(function(){
	
	$("input.numbers, input.numbersonly").numeric();
	
	fpBox=$('#featured_product'); 
	fpContent=$('#featured_product_content');
	
	if(fpBox.is('div') && fpContent.is('div')){
		$.getJSON("/json/featured_products", function(json){
			if(json.error) {
				fpBox.remove();
			} else {
				fpData=json;
				fpUpdate(0);
			};
		});
	};
	
	$("form").submit(function(){
		
		valid = true;
		e='';
		i=0;
		$(".match", this).each(function(i){
			
			if(i){
				valid=(this.value==value);
			};
			value=this.value;
			i++;
		});
		
		if(!valid) e='Passwords do not match\n';
		
		$(".required", this).siblings('input').each(function(i){
			if(!this.value){
				$(this).addClass("warn");
				e=e+$(this).siblings().text()+"\n";
				valid=false;
			} else {
				$(this).removeClass("warn");
			}
		});
		if(!valid){alert("You didn't fill in the following fields\n\n"+e);}
		return valid;
	});

});

/*
 * Allows only valid characters to be entered into input boxes.
 * Note: does not validate that the final text is a valid number
 * (that could be done by another script, or server-side)
 *
 * @name     numeric
 * @param    decimal      Decimal separator (e.g. '.' or ',' - default is '.')
 * @param    callback     A function that runs if the number is not valid (fires onblur)
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @example  $(".numeric").numeric();
 * @example  $(".numeric").numeric(",");
 * @example  $(".numeric").numeric(null, callback);
 *
 */
jQuery.fn.numeric = function(decimal, callback)
{
	decimal = decimal || ".";
	callback = typeof callback == "function" ? callback : function(){};
	this.keypress(
		function(e)
		{
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			// allow enter/return key (only when in an input box)
			if(key == 13 && this.nodeName.toLowerCase() == "input")
			{
				return true;
			}
			else if(key == 13)
			{
				return false;
			}
			var allow = false;
			// allow Ctrl+A
			if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
			// allow Ctrl+X (cut)
			if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
			// allow Ctrl+C (copy)
			if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
			// allow Ctrl+Z (undo)
			if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
			// allow or deny Ctrl+V (paste), Shift+Ins
			if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
			|| (e.shiftKey && key == 45)) return true;
			// if a number was not pressed
			if(key < 48 || key > 57)
			{
				/* '-' only allowed at start */
				if(key == 45 && this.value.length == 0) return true;
				/* only one decimal separator allowed */
				if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) != -1)
				{
					allow = false;
				}
				// check for other keys that have special purposes
				if(
					key != 8 /* backspace */ &&
					key != 9 /* tab */ &&
					key != 13 /* enter */ &&
					key != 35 /* end */ &&
					key != 36 /* home */ &&
					key != 37 /* left */ &&
					key != 39 /* right */ &&
					key != 46 /* del */
				)
				{
					allow = false;
				}
				else
				{
					// for detecting special keys (listed above)
					// IE does not support 'charCode' and ignores them in keypress anyway
					if(typeof e.charCode != "undefined")
					{
						// special keys have 'keyCode' and 'which' the same (e.g. backspace)
						if(e.keyCode == e.which && e.which != 0)
						{
							allow = true;
						}
						// or keyCode != 0 and 'charCode'/'which' = 0
						else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
						{
							allow = true;
						}
					}
				}
				// if key pressed is the decimal and it is not already in the field
				if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) == -1)
				{
					allow = true;
				}
			}
			else
			{
				allow = true;
			}
			return allow;
		}
	)
	.blur(
		function()
		{
			var val = jQuery(this).val();
			if(val != "")
			{
				var re = new RegExp("^\\d+$|\\d*" + decimal + "\\d+");
				if(!re.exec(val))
				{
					callback.apply(this);
				}
			}
		}
	);
	return this;
};

$.fn.fastSerialize = function() {
    var a = [];

    $('input,textarea,select,button', this).each(function() {
        var n = this.name;
        var t = this.type;

        if ( !n || this.disabled || t == 'reset' ||
            (t == 'checkbox' || t == 'radio') && !this.checked ||
            (t == 'submit' || t == 'image' || t == 'button') && this.form.clicked != this ||
            this.tagName.toLowerCase() == 'select' && this.selectedIndex == -1)
            return;

        if (t == 'image' && this.form.clicked_x)
            return a.push(
                {name: n+'_x', value: this.form.clicked_x},
                {name: n+'_y', value: this.form.clicked_y}
            );

        if (t == 'select-multiple') {
            $('option:selected', this).each( function() {
                a.push({name: n, value: this.value});
            });
            return;
        }

        a.push({name: n, value: this.value});
    });

    return a;
};

