/**
	author: Bela Vizer
	last modification: 31.12.09
*/

function setImage(item, img) {
    if ( item.attr("src") == img) {
        return;
    } else {
        item.attr("src",img);
    }
}

var $hoofdFlake = 0;
;(function($) {
	$.fn.snowing = function(param) {
		return this.each(function() {
			var ojjektum = $.extend({
							direction :0,	// flakes movement direction 0: random; negative(x) # = left #, positive(x) # = right # on each timeout
							yvelocity: 10,	// on each timeout flakes movement # pixel on y axix down
							xvelocity: 10,  // on each timeout flakes movement # pixel on x axis based on direction
							density: 60,	// timeout*density = new flake, for example density = 10 timeout = 10 -> every 100ms a new flake will appear, exept when one flake reach the bottom of the document, in this case this counter will be decreased by 5
							timeout: 500,	// flake movement and new flake appearing will happen # ms, exept.... (default 30)
							maxflake : 1,	// maximum flakes number on the screen
                            flakeState : 0, // State van de mower.
                            startX : 250,   // startplek van mower
                            startY : 0,     // Startplek van mower
                            stateY : 0     // Startplek van deze state (Om te kijken hoever we zijn)

						},param || {});
			var ez = this;	// current element in the wrapped set
			//var snowflakes = new Array("mow1.png", "mow1.png", "mow1.png");	//flakes images, numbers and images can be vary
			var flakes = new Array();	// array containing the flakes on the screen
			var kovi = 0;	//	next flake counter, ld. density and timeout and bottom reached flake

			/**
				add new flake to the screen
			*/
			var addNewFlake = function() {
				var div = $("<div></div>")
							.css("top",ojjektum.startY)
							.css("left",ojjektum.startX)
							.addClass("snowflake");

				var img = $("<img></img>");
				$(img).attr("src","http://lecoba.nl/wp-content/themes/lecoba/images/mow1.png");
				$(div).append(img);

				flakes[flakes.length] = div;
				$(ez).append(div);
                $hoofdFlake = flakes[flakes.length-1];


                $("div.snowflake").click(function(){
                        window.location = "http://lecoba.nl/automower/";
                });



			}

			/**
				move existing flakes on the screen; ld
			*/
			var moveFlakes = function() {
				//$("#x").text(flakes.length);
				var docy = $(ez).height();	// document height
				var docx = $(ez).width();	// document widht

				for (var i = 0; i < flakes.length; i++) {	// traverse on the flake array to move them
					var x = window.parseInt( $(flakes[i]).css("left") );
					var y = window.parseInt( $(flakes[i]).css("top") );

					if ((y + ojjektum.yvelocity) < (docy-50)) {
                        // We zijn niet onderaan.

                        goX = 0;
                        goY = 0;
                        if (ojjektum.flakeState == 0) {

                            // Start state, op 0 neerzetten:
                            $(flakes[i]).css("left",ojjektum.startX);
                            $(flakes[i]).css("top",ojjektum.startY);
                            goX = 0;
                            goY = 0;
                            ojjektum.flakeState = 1;

                        } else if (ojjektum.flakeState == 1) {
                            // Move naar onder:

                            goX = 0;
                            goY = ojjektum.yvelocity;
                            if (y >= 250) {

                                ojjektum.flakeState = 2;
                            }
                        } else if (ojjektum.flakeState == 2) {
                            // rechts
                            setImage($(flakes[i]).children(),  "http://lecoba.nl/wp-content/themes/lecoba/images/mowr.png");

                            goX = ojjektum.xvelocity;
                            goY = 0;
                            if (x >= (docx-250)) {

                                ojjektum.flakeState = 3;
                            }
                        } else if (ojjektum.flakeState == 3) {
                            // Beneden
                            setImage($(flakes[i]).children(), "http://lecoba.nl/wp-content/themes/lecoba/images/mow1.png");
                            if (ojjektum.stateY == 0) {
                                ojjektum.stateY = y;
                            }

                            goY = ojjektum.yvelocity;
                            if (y > (ojjektum.stateY + 50)) {
                                // Limit reached

                                ojjektum.stateY = 0;
                                ojjektum.flakeState = 4;
                            }
                        } else if (ojjektum.flakeState == 4) {
                            // Links
                            setImage($(flakes[i]).children(), "http://lecoba.nl/wp-content/themes/lecoba/images/mowl.png");

                            goX = -1*ojjektum.xvelocity;
                            goY = 0;
                            if (x <= 250) {
                                ojjektum.flakeState = 5;
                            }
                        } else if (ojjektum.flakeState == 5) {
                            // Beneden
                            setImage($(flakes[i]).children(), "http://lecoba.nl/wp-content/themes/lecoba/images/mow1.png");

                            if (ojjektum.stateY == 0) {
                                ojjektum.stateY = y;
                            }
                            goY = ojjektum.yvelocity;
                            if (y >= (ojjektum.stateY + 50)) {
                                // Limit reached
                                ojjektum.stateY = 0;
                                ojjektum.flakeState = 2;
                            }
                        }

                        if ((x+goX)-30 < docx && (x+goX) > 15) {
                            $(flakes[i]).css("left",(x+goX));
                        }

                        $(flakes[i]).css("top",(y+goY));

					} else {	//in case of one of the flakes has reached the bottom of the container
                        $(flakes[i]).css("left",ojjektum.startX);
                        $(flakes[i]).css("top",ojjektum.startY);
                        goX = 0;
                        goY = 0;

                        ojjektum.flakeState = 0;
					}
				}

				if (++kovi == ojjektum.density && ojjektum.maxflake > flakes.length) {	//add new flake until the ojjektum.maxflake number is not reached
					addNewFlake();
					kovi = 0;	// if a flake reached the bottom then to avoid thicken flakes, decrease a little bit (maybe useless)
				}
			}

			addNewFlake();	// Columbus-egg-hen case
			var i = window.setInterval(function() { moveFlakes() },ojjektum.timeout);
		});
	}
})(jQuery)

