//----------------------------------------------------------------------------
// © Michal Michna - plugin for JQuery, dynamic images
//----------------------------------------------------------------------------

// inicialize after script and document load
$(document).ready(function(){ 
 	var snapshot = new Snapshot("snapshot", 170, 128);
});

// snapshot object
function Snapshot(DOM_element, width, height, swap_time, show_time){
	this.swap_time = swap_time || 1000;		// rychlost prepinani obrazku
	this.show_time = show_time || 5000;		// cas zobrazeni obrazku
	
	// references on images
	this.images_snapshot = [ 
	    {
			'title' : "Evolution v-tail",
			'src' : "../data/products/f3b/evolution_v/pic1_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-v.html#pictures"
	    },
	    { 
			'title' : "Guenter with Evolution v-tail",
			'src' : "../data/products/f3b/evolution_v/pic2_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-v.html#pictures"
	    },
	    { 
			'title' : "Guenter with Evolution v-tail on start",
			'src' : "../data/products/f3b/evolution_v/pic3_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-v.html#pictures"
	    },
	    { 
			'title' : "Guenter with Evolution v-tail",
			'src' : "../data/products/f3b/evolution_v/pic4_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-v.html#pictures"
	    },
	    { 
			'title' : "Evolution v-tail on snow",
			'src' : "../data/products/f3b/evolution_v/evo5_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-v.html#pictures"
	    },
	    { 
			'title' : "Evolution x-tail...",
			'src' : "../data/products/f3b/evolution_x/pic30_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-x.html#pictures"
	    },
	    { 
			'title' : "Evolution x-tail and Rudi",
			'src' : "../data/products/f3b/evolution_x/pic4_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-x.html#pictures"
	    },	
	    { 
			'title' : "Evolution x-tail",
			'src' : "../data/products/f3b/evolution_x/pic6_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-x.html#pictures"
	    },	
	    { 
			'title' : "Evolution x-tail and Guenter",
			'src' : "../data/products/f3b/evolution_x/pic7_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-x.html#pictures"
	    },	
	    { 
			'title' : "Evolution x-tail and Rudi",
			'src' : "../data/products/f3b/evolution_x/pic8_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-x.html#pictures"
	    },
	    { 
			'title' : "Evolution x-tail",
			'src' : "../data/products/f3b/evolution_x/pic67_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-x.html#pictures"
	    },
	    { 
			'title' : "Evolution x-tail",
			'src' : "../data/products/f3b/evolution_x/evol1_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-x.html#pictures"
	    },
	    { 
			'title' : "Evolution x-tail",
			'src' : "../data/products/f3b/evolution_x/evol2_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-x.html#pictures"
	    },	
	    { 
			'title' : "Evolution x-tail",
			'src' : "../data/products/f3b/evolution_x/evol3_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/evolution-x.html#pictures"
	    },
	    { 
			'title' : "Eagle x-tail",
			'src' : "../data/products/f3b/eagle_x/obr1_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/eagle-x.html#pictures"
	    },
	    { 
			'title' : "Eagle x-tail",
			'src' : "../data/products/f3b/eagle_x/obr8_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/eagle-x.html#pictures"
	    },
	    { 
			'title' : "Eagle x-tail",
			'src' : "../data/products/f3b/eagle_x/obr6_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/eagle-x.html#pictures"
	    },
	    { 
			'title' : "Eagle x-tail",
			'src' : "../data/products/f3b/eagle_x/obr8_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/eagle-x.html#pictures"
	    },
	    { 
			'title' : "Eagle x-tail",
			'src' : "../data/products/f3b/eagle_x/obr3_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/eagle-x.html#pictures"
	    },
	    { 
			'title' : "Eagle v-tail",
			'src' : "../data/products/f3b/eagle_v/obr6_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/eagle-v.html#pictures"
	    },	    
	    { 
			'title' : "X-21",
			'src' : "../data/products/f3b/x_21/obr3_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/x-21.html"
	    },	    
	    { 
			'title' : "X-21",
			'src' : "../data/products/f3b/x_21/obr12_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3b/x-21.html"
	    },	    
	    { 
			'title' : "Xantipa II package",
			'src' : "../data/products/f3j/xantipa_II/obr2_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3j/xantipa-II.html#pictures"
	    },	
	    { 
			'title' : "Xantipa II",
			'src' : "../data/products/f3j/xantipa_II/obr3_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3j/xantipa-II.html#pictures"
	    },	
	    { 
			'title' : "Xantipa II v-tail",
			'src' : "../data/products/f3j/xantipa_II/obr12_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3j/xantipa-II.html#pictures"
	    },	
	    { 
			'title' : "Xantipa II bottom",
			'src' : "../data/products/f3j/xantipa_II/obr17_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3j/xantipa-II.html#pictures"
	    },	
	    { 
			'title' : "Xantipa II v-tail bottom",
			'src' : "../data/products/f3j/xantipa_II/obr21_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3j/xantipa-II.html#pictures"
	    },	
	    { 
			'title' : "Xantipa II throwhook",
			'src' : "../data/products/f3j/xantipa_II/obr20_min.jpg",
			'href' : "http://www.jitom.com/pages/products/airplanes/f3j/xantipa-II.html#pictures"
	    },	
	    { 
			'title' : "Evolution x-tail",
			'src' : "../data/magazine/muenchen-07/mnichov2_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },	
	    { 
			'title' : "Evolution start",
			'src' : "../data/magazine/muenchen-07/mnichov3_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },	
	    { 
			'title' : "Guenter",
			'src' : "../data/magazine/muenchen-07/mnichov4_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },	
	    { 
			'title' : "Evolution x-tail",
			'src' : "../data/magazine/muenchen-07/mnichov5_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution x-tail",
			'src' : "../data/magazine/muenchen-07/mnichov6_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },	
	    { 
			'title' : "Evolution x-tail",
			'src' : "../data/magazine/muenchen-07/mnichov7_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution x-tail start",
			'src' : "../data/magazine/muenchen-07/mnichov8_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution x-tail nad Rudi",
			'src' : "../data/magazine/muenchen-07/mnichovII1_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution x-tail nad Rudi",
			'src' : "../data/magazine/muenchen-07/mnichovII3_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution x-tail nad Rudi",
			'src' : "../data/magazine/muenchen-07/mnichovII1_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution in Salzburg",
			'src' : "../data/pictures/competitions/salzburg-07/pic1_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution in Salzburg",
			'src' : "../data/pictures/competitions/salzburg-07/pic2_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution in Salzburg",
			'src' : "../data/pictures/competitions/salzburg-07/pic3_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution in Salzburg",
			'src' : "../data/pictures/competitions/salzburg-07/pic4_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution in Salzburg",
			'src' : "../data/pictures/competitions/salzburg-07/pic5_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution in Salzburg",
			'src' : "../data/pictures/competitions/salzburg-07/pic6_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution in Salzburg",
			'src' : "../data/pictures/competitions/salzburg-07/pic7_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution in Salzburg",
			'src' : "../data/pictures/competitions/salzburg-07/pic8_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution in Salzburg",
			'src' : "../data/pictures/competitions/salzburg-07/pic9_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    },
	    { 
			'title' : "Evolution in Salzburg",
			'src' : "../data/pictures/competitions/salzburg-07/pic10_min.jpg",
			'href' : "http://www.jitom.com/pages/downloads/magazine/f3b_competitions/muenchen-07.html"
	    }
	];
	
	// get element
	this.element = $("#"+DOM_element);
	// set size
	this.element.css("width",width);
	this.element.css("height",height);
	this.element.css("line-height",height);
	this.animate_enable = true;

	// random number for select secondary image
	var num = Math.floor(Math.random() * this.images_snapshot.length);
	var secondary_img = new Image();
	
	// get and set SRC attribute for image
	$(secondary_img).attr('src',this.images_snapshot[num]['src']);
	// some CSS set
	$(secondary_img).css('width',width);
	$(secondary_img).css('height',height);
	$(secondary_img).css('display',"block");
	$(secondary_img).css('position',"relative");
	$(secondary_img).css('border',"none");
	// store reference to primary and secondary layer
	this.secondary_img = $(secondary_img);
	// determine witch layer is hide and show
	this.show_img = this.secondary_img; 
	
	// generate new random number for primary image
	num = Math.floor(Math.random() * this.images_snapshot.length);
	while(this.show_img.attr('src') == this.images_snapshot[num]['src']){
		num = Math.floor(Math.random() * this.images_snapshot.length);
	}
	
	var primary_img = new Image();
	$(primary_img).attr('src',this.images_snapshot[num]['src']);
	$(primary_img).css('width',width);
	$(primary_img).css('height',height);
	$(primary_img).css('display',"block");
	$(primary_img).css('position',"relative");
	$(primary_img).css('margin-bottom',"-"+height+"px");
	$(primary_img).css('border',"none");
	
	// store reference to primary and secondary layer
	this.primary_img = $(primary_img);
	// determine witch layer is hide and show
	this.hide_img = this.primary_img;
	
	// create common hyperlink
	this.link = document.createElement('A');
	$(this.link).attr('href',"");

	// append images to hyperlink element
	$(this.link).append(primary_img);
	$(this.link).append(secondary_img);

	// append hyperlink element to root element of object
	this.element.append(this.link);

	// start animate! ;)
	this.animate();
}

// next image function switch images with fade swap
Snapshot.prototype.nextImage = function(){
		// switch visibility reference link
		var tmp = this.hide_img;
		this.hide_img = this.show_img;
		this.show_img = tmp;
	
		// show and hide that layers
		this.hide_img.fadeOut(this.swap_time);
		this.show_img.fadeIn(this.swap_time);

		// random number for hidden image
		var num;
		num = Math.floor(Math.random() * this.images_snapshot.length);
		while(this.show_img.attr('src') == this.images_snapshot[num]['src']){
			num = Math.floor(Math.random() * this.images_snapshot.length);
		}
		// preserve this object for function
		var tmp = this;
		
		// after is layer hidden, switch image attribute for new one 
		setTimeout(function() { 
			tmp.hide_img.attr('src',tmp.images_snapshot[num]['src']);
			tmp.hide_img.attr('title',tmp.images_snapshot[num]['title']);
			$(tmp.link).attr('href',tmp.images_snapshot[num]['href']);

		}, this.swap_time);
};

// animate function
Snapshot.prototype.animate = function (){
	// switch image
	this.nextImage();

	// preserve this object for function
	var tmp = this;
	// is animate enable?
	if(this.animate_enable) {
		// yes, wait and then run animate function again
		setTimeout(function () { tmp.animate(); },this.show_time);
		return;
	}
	// no, stop animating
	else return;
}; 

// simple wait function from elsewhere...
$.fn.wait = function(time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $(self).dequeue();
        }, time);
    });
};
