// JavaScript Document
(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}
			
		}
	});
})(jQuery);


//END PRELOAD
 

$(document).ready(function(e) {
	
	$("input.your_budget").keyup(function(){
			
			/*if( this.value.indexOf('£') !== 0 ){
				this.value = '£' + this.value;
			}
			
			if (!/^[0-9\.\£]+$/.test($(this).val())) {
				$(this).val($(this).val().replace(/[^0-9\.\£]/g, ''));
			}*/
			
		});
	
	$("#wishlist .col-2 form .add").click(function(e) {
		
		var count = $("#wishlist .col-2 form>div .field").last().find("label").attr("for");
		var new_field = parseInt(count)+1;
		
        $("#wishlist .col-2 form>div .fields").append('<div class="field"><label for="'+new_field+'">Item '+new_field+'</label><input type="text" class="name" name="item_name[]" value="Item Name"/><input type="text" class="price" name="item_value[]" value="Target Price"/></div><div class="cleared"></div>');
		$("#wishlist .col-2 form>div .field input[type=text]").removeValue();
    });
	
	$("#footer").stickyFooter(); 
	
	$.metadata.setType("attr", "validate");
	
	$(".brochure_request_body form input[type=text], #wishlist .col-2 form>div .field input[type=text]").removeValue();
	$(function() {
		$.preload([
			"media/common/characters_blink1.png",
			"media/common/characters_blink2.png",
		], {
			loaded_all: function(loaded, total) {
				setInterval(function(){
					$(".col-2").addClass("blink1");
					setTimeout(function(){
						$(".brochure_request_top").removeClass("blink1");
					}, 150);
				}, 10000);
				
				
				setInterval(function(){
					$("#header").addClass("blink2");
					setTimeout(function(){
						$("#header").removeClass("blink2");
					}, 150);
				}, 7050);
			}
		});
	});
	$("form.request").validate();
	
	$("#wishlist form").validate();

});
