var p1 = [];
var index = 0;
var winner;

var jJSON = {			
	getValues: function(obj,num) {
		return jJSON[obj]["auction"].slice(0,((num == null) ? jJSON[obj]["auction"].length : num));
	},
	
	getCount: function(obj) {
		return jJSON[obj]["count"];
	},
	
	getPhoto: function(obj,num) {
		return jJSON[obj]["user_photo"].slice(0,((num == null) ? jJSON[obj]["user_photo"].length : num));
	},

	getName: function(obj,num) {
		return jJSON[obj]["name"].slice(0,((num == null) ? jJSON[obj]["name"].length : num));
	},

	getAuctionLink: function(obj,num) {
		return jJSON[obj]["link"].slice(0,((num == null) ? jJSON[obj]["link"].length : num));
	},
	
	getProductPhoto: function(obj,num) {
		return jJSON[obj]["product_photo"].slice(0,((num == null) ? jJSON[obj]["product_photo"].length : num));
	},

	getBidAmount: function(obj,num) {
		return jJSON[obj]["winning_bid"].slice(0,((num == null) ? jJSON[obj]["winning_bid"].length : num));
	},

	getNotes: function(obj,num) {
		return jJSON[obj]["notes"].slice(0,((num == null) ? jJSON[obj]["notes"].length : num));
	},

	getCity: function(obj,num) {
		return jJSON[obj]["city"].slice(0,((num == null) ? jJSON[obj]["city"].length : num));
	},

	getState: function(obj,num) {
		return jJSON[obj]["state"].slice(0,((num == null) ? jJSON[obj]["state"].length : num));
	},

	getSaved: function(obj,num) {
		return jJSON[obj]["saved"].slice(0,((num == null) ? jJSON[obj]["saved"].length : num));
	},

	getRetail: function(obj,num) {
		return jJSON[obj]["retail"].slice(0,((num == null) ? jJSON[obj]["retail"].length : num));
	},

	getRandomValues: function(obj,num) {
		var a = [];
		var b = jJSON[obj]["auction"];
		var c = b.length;
		
		if (num != null && num < c) {
			c = num;
		}

		for (i = 0; i < c; i++) {
			var e = Math.floor(Math.random() * b.length);
			a[i] = b[e];
			b.splice(e,1);
		}
		
		return a;
	}
};

$.fn.wait = function(time, type) {
	time = time || 1000;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
			$(self).dequeue();
		}, time);
	});
};

function runIt() {
	if( index == winner.auction.length )
		index = 0;
	
	var tmp = index++;
	$('#winner').html('<div class="user_photo"><img src="thumb.php?src='+winner.user_photo[tmp]+'&h=125&w=125&zc=1&q=80" alt="'+winner.name[tmp]+'" /></div><div class="uinfo"><p>'+winner.name[tmp]+' from '+winner.city[tmp]+', '+winner.state[tmp]+' has won '+winner.auction[tmp]+' with a winning bid of '+winner.bid[tmp]+'!</p><p class="saved">'+winner.name[tmp]+' <span>saved '+winner.saved[tmp]+'</span> off of the retail price for this item.</p></div><div class="auction_photo"><img src="thumb.php?src='+winner.product_photo[tmp]+'&h=100&w=100&zc=1&q=80" alt="'+winner.auction[tmp]+'" /></div><h2 class="testimonial">Testimonial:</h2><p class="testimonial">'+winner.testimonial[tmp]+'</p>').fadeIn(5000);
	setTimeout( function() { $('#winner').fadeOut(5000, runIt) } , 10000);
}

$(document).ready(function() {
	//$('#winner').hide();
	//alert("bar");
	$.getJSON("gwData.php",function(json){

	jJSON["winners"] = (function() {
		 winner = {
			auction: [],
			user_photo: [],
			product_photo: [],
			testimonial: [],
			winning_price: [],
			auction_link: [],
			bid: [],
			name: [],
			city: [],
			state: [],
			saved: [],
			retail: [],
			count: 0
		};
		$.each(json.winners,function(i,awinner) {
			if (awinner.auction != "undefined" || awinner.auction!="") {
				winner.auction[i] = awinner.auction;
				winner.user_photo[i] = awinner.user_photo;
				winner.product_photo[i] = awinner.product_photo;
				winner.name[i] = awinner.name;
				winner.auction_link[i] = awinner.link;
				winner.bid[i] = awinner.winning_bid;
				winner.testimonial[i] = awinner.notes;
				winner.city[i] = awinner.city;
				winner.state[i] = awinner.state;
				winner.saved[i] = awinner.saved;
				winner.retail[i] = awinner.retail;
				//alert(winner.auction+"\n"+winner.user_photo);
			}
		});
		//alert(winner.auction[1]);
		//alert("foo");
		runIt();
	})();
});

});