var hero = [];
var blurb = [];
var thumb = [];
var urls = [];
var gb_delay = 0;
var gunnyboxInv = 0;
var gunnybox_index = 0;
var preLoader = new Image();
var images = [];
var Gspeed = 0;


jQuery.fn.gunnybox = function (options){
	
	 var defaults = {
		  	xml : "x95.xml",
			speed: 6000,
			speed: 1000
			};

	var opts = jQuery.extend (defaults, options);

	return this.each (function () {
		$(this)
			.css("width", "950px")
			.css("height","327px")
			.css("background-color","white");
		// is xml set?
		var obj = $(this);
		Gspeed = opts.speed;
		gb_delay = opts.fade;
		//opts.xml = "Prebuilt/askdata.html";
		if(!opts.xml){
			obj.html('<span style="color:red">Critical Fail:</span> XML file not specified. Please specify an XML file with the images in the initialization.');
			return false;
		}
		// does xml exist, if so load it
		$.ajax({
			type: "GET",
			url: opts.xml ,
			dataType: "xml",
			error:
				function(){
					// if xml file cannot be found, say so
					obj.html('<span style="color:red">Critical Failure:</span> Source file not found.  Please check the location of the file.');
				    return false;		
				},
			success:
				function(xml){
					// does xml contain items?
					if($(xml).find("Item").length == 0){
							obj.html('<span style="color:red">Critical Fail:</span> No items found in the XML file. Please check its structure.');
				    		return false;		
					}
					obj.html('<div id="gunnybox-hero-div"></div><div id="gunnybox-sidebar-div"></div><div style="clear:both;"></div>');
					// if xml contains items process it
					var count = 0;
					$("#gunnybox-hero-div").fadeTo(1,0);
					$(xml).find("Item").each(function(){
							hero.push($(this).find("HeroImage").text());
							preLoader.src = $(this).find("HeroImage").text();
							
							images.push(preLoader.src);
							blurb.push($(this).find("TextBlurb").text());	
							thumb.push($(this).find("SmallImage").text());
							preLoader.src = $(this).find("SmallImage").text();
							urls.push($(this).find("url").text());	
							if(count == 0){
								var button = '<div class="gunnybox-button selected" id="gb-button-'+count+'" onclick="selectMe('+count+')"></div>';
							} else {
								var button = '<div class="gunnybox-button" id="gb-button-'+count+'" onclick="selectMe('+count+')"></div>';
							}
							$("#gunnybox-sidebar-div").append(button);
							if(($.browser.version == 8 || $.browser.version == 7) && $.browser.msie){
								var tops = "top:-75px";
								
							} else {
								var tops = "";	
							}
							$("#gb-button-"+count).append('<div class="gunnybox-side-thumb" ><img src="'+$(this).find("SmallImage").text()+'" /></div><div class="gunnybox-side-blurb">'+$(this).find("TextBlurb").text()+'</div><div class="buttonImage" style="'+tops+'"></div>');
							$("#gb-button-"+count).fadeTo(1,0);
							$("#gunnybox-hero-div").prepend('<img style="display:none;" src="'+$(this).find("HeroImage").text()+'" class="gunnybox-image" id="gunnybox-image-'+count+'" />');
							count ++;
					});
					count --;
					$(".buttonImage").fadeTo(1,0);
					$("#gunnybox-image-"+count).load(function(){
						$(".gunnybox-image").remove();
						buttonFade(0);										  
					});
					
					
				}
		});
			
	});
}

function buttonFade(c){
	if(c < urls.length){
		$("#gb-button-"+c).fadeTo(gb_delay,1,function(){
			c++;
			buttonFade(c);											  
		});		
	} else {
		selectMe(gunnybox_index);
		gunnyboxInv = setInterval("rotateMe()",Gspeed);
		
	}
	
}

function selectMe(i){
				gunnybox_index = i;
				$(".buttonImage").fadeTo(gb_delay/2,0);
				$("#gb-button-"+i).find(".buttonImage").fadeTo(gb_delay,1);
				$("#gunnybox-hero-div").fadeTo(gb_delay/2,0,function(){
				$("#gunnybox-hero-div").attr("style","");
				preLoader.src =  hero[i];
				$("#gunnybox-hero-div").attr("style","background-image:url(" + hero[i] + ");display:none;");
				$("#gunnybox-hero-div").unbind("click").bind("click",function(){ window.location = urls[i]; });
				$("#gunnybox-hero-div").fadeTo(gb_delay,1);
				
			});
}


function rotateMe(){
	if(gunnybox_index < urls.length - 1){
		gunnybox_index++;
	} else {
		gunnybox_index = 0;
	}
	$("#gb-button-"+gunnybox_index).trigger("click");
	
}


// JavaScript Document
