// javascript: document.body.contentEditable = 'true'; document.designMode = 'on'; void 0

//var Animator;
//$(window).ready( function () {
//Animator = new function () {
var Animator = new function () {
	var self = this;
	
	this.state = 1;
	
	this.resizeTimer = null;
	
	this.resizeRatio = 1;
	
	this.contentSize = {
		width : 0,
		height : 0
	};
	
	this.originalContent = {
		width : 0,
		height : 0
	};
	
	this.window = {
		width : $(window).width(),
		height : $(window).height()
	};
	
	function AnimatorImage (data) {

		Assert.number( data.top );
		Assert.number( data.left );
		Assert.nonZeroNumber( data.width );
		Assert.nonZeroNumber( data.height );

		this.top = data.top;
		this.left = data.left;
		this.width = data.width;
		this.height = data.height;
		this.absolute = data.absolute;
	}
	
	this.beforeResize = function () {};
	
	this.afterResize = function () {};
	
	this.start = function () {};
	
	this.setContent = function (name) {
		self.originalContent.width = $(name).width();
		self.originalContent.height = $(name).height();
		resizeContent();
	};
	
	// Watch window size
	$(window).bind('resize', function() {
		if (self.resizeTimer) {
			clearTimeout(self.resizeTimer);
		}
		self.resizeTimer = setTimeout(function() {
			self.stop();
			resizeContent();
			self.start();
		}, 500);
	});

	var objects = {};
	
	this.add = function(name, absolute) {
		objects[name] = new AnimatorImage({
			top : $(name).position().top,
			left : $(name).position().left,
			width : $(name).width(),
			height : $(name).height(),
			absolute : (absolute) ? true : false
		});
		resizeObject(name);
		return objects[name];
	};
	
	this.get = function(name) {
		return objects[name];
	};
	
	function resizeContent () {
		// Store new window sizes
		self.window.width = $(window).width();
		self.window.height = $(window).height();
		
		// Calculate resized content size
		self.contentSize = self.calculateContentSizeInFrame(
			self.originalContent.width, self.originalContent.height,
			self.window.width, self.window.height);

		self.resizeRatio = self.contentSize.width / 
				(self.originalContent.width);	

		self.beforeResize();

		for (var objectID in objects) {
			resizeObject(objectID);
		}
		
		self.afterResize();
	};
	
	function resizeObject (objectID) {
		var r = $(objectID);
		var objectData = objects[objectID];
		if (objectData.absolute) {
			r.css("top", parseInt( (objectData.top * self.resizeRatio) + self.contentSize.top) );
			r.css("left", parseInt( objectData.left * self.resizeRatio + self.contentSize.left) );
		} else {
			r.css("top", parseInt( objectData.top * self.resizeRatio) );
			r.css("left", parseInt( objectData.left * self.resizeRatio) );
		}
		r.width( parseInt( objectData.width * self.resizeRatio ) );
		r.height( parseInt( objectData.height * self.resizeRatio ) );
	};

	this.calculateContentSizeInFrame = function(
		contentWidth, contentHeight,
		frameWidth, frameHeight) {
			
		Assert.nonZeroNumber(contentWidth);
		Assert.nonZeroNumber(contentHeight);
		Assert.nonZeroNumber(frameWidth);
		Assert.nonZeroNumber(frameHeight);
		
		var contentAspectRatio = contentWidth / contentHeight;
		var frameAspectRatio = frameWidth / frameHeight;
		var newSize = {};
		
		if (frameAspectRatio > contentAspectRatio) {
			// Height is the bottleneck
			newSize.height = frameHeight;
			newSize.width = newSize.height * contentAspectRatio;
			newSize.top = 0;
			newSize.left = Math.floor(  (frameWidth - newSize.width) / 2  );
		} else {
			// Width is the bottleneck
			newSize.width = frameWidth;
			newSize.height = newSize.width / contentAspectRatio;
			newSize.top = Math.floor(  (frameHeight - newSize.height) / 2  );
			newSize.left = 0;
		}
		
		return newSize;
	};

	this.calculateFrameStyleForContent = function(
		contentWidth, contentHeight,
		frameWidth, frameHeight) {
			
		Assert.nonZeroNumber(contentWidth);
		Assert.nonZeroNumber(contentHeight);
		Assert.nonZeroNumber(frameWidth);
		Assert.nonZeroNumber(frameHeight);
		
		var contentAspectRatio = contentWidth / contentHeight;
		var frameAspectRatio = frameWidth / frameHeight;
		var style = {};
		
		if (frameAspectRatio > contentAspectRatio) {
			// Height is the bottleneck
			style.height = contentHeight;
			style.width = style.height * frameAspectRatio;
			style.top = 0;
			style.left = Math.floor(  (style.width - contentWidth) / -2  );
		} else {
			// Width is the bottleneck
			style.width = contentWidth;
			style.height = style.width / frameAspectRatio;
			style.top = Math.floor(  (style.height - contentHeight) / -2  );
			style.left = 0;
		}
		
		return style;
	};
	
	this.mouseInOutFader = function( nameSensor, outFaded, inFaded, nameFading) {
		if ((nameFading == undefined) || (nameFading == "")) nameFading = nameSensor;
		if ((inFaded == undefined) || (inFaded == "")) inFaded = 1;

		$(nameFading).fadeTo(0, outFaded);
		$(nameSensor).bind("mouseenter", function () {
			$(nameFading).stop().fadeTo("fast", inFaded);
		}).bind("mouseleave", function () {
			$(nameFading).stop().fadeTo("slow", outFaded);
		});
	};
};
//});

var Logger = new function () {
	var panelString =
		"<div style=\"" +
			" z-index: 1000;" +
			" position: absolute;" +
			" top: 0px;" +
			" left: 0px;" +
			" width: 512px;" +
			" height: 320px;" +
			" background-color: #101010;" +
			" font-size: medium;" +
			" color: green;" +
			" font-style: normal;" +
			" overflow: auto;" +
			"\" " +
			"id=\"logpanel\" " +
		"></div>";
	panelString += "<div style=\"" +
			" z-index: 1001;" +
			" position: absolute;" +
			" top: 0px;" +
			" left: 0px;" +
			" width: 0px;" + // Disabled, press the "l" button instead
			" height: 320px;" +
			" background-color: #511010;" +
			" font-size: medium;" +
			" color: green;" +
			" font-style: normal;" +
			" overflow: auto;" +
			"\" " +
			"id=\"logtoggle\" " +
		"></div>";
	$(document).ready( function () {
		$(panelString).appendTo( $("body") );
		$('#logtoggle').click( function () {
			$("#logpanel").toggle("slow");
			
		});
		$(document).keypress(function (e) {
			if (e.which == "l".charCodeAt(0)) {
				$("#logpanel").toggle("slow");
			}
		});
		$(document).keypress(function (e) {
			if (e.which == "s".charCodeAt(0)) {
				$(":animated").stop();
			}
		});
		refreshWindow();
		objDiv = $("#logpanel").hide(0).get(0);
	});
	
	var objDiv;
	
	var logString = "Log window\n<br />";
	
	var isWindowAppended = false;
	
	function refreshWindow () {
		$("#logpanel").html(logString);
		/*
		if (typeof(objDiv) != 'undefined') {
			objDiv.scrollTop = objDiv.scrollHeight;
		}*/
	};
	
	this.log = function (message) {
		switch(typeof(message)) {
		case 'object':
			logString = logString + "<br />";
			for (var i in message) {
				logString = logString + '"' + i + '" = "' + message[i] + '"<br />';
			}
			logString = logString + "<br />";
			break;
			
		default:
			logString = logString + message + "<br />";
		}	
		refreshWindow();
	};
		
};

var Assert = new function () {
	
	this.number = function ( number ) {
		if (typeof(number) === 'number') {
			return true;
		}
		Logger.log("Assert: not a number '" + number + "'");
		return false;
	};
	
	this.int = function ( integer ) {
		if (Assert.number( integer )) {
			return true;
		}
		return false;
	};
	
	this.nonZeroNumber = function ( number ) {
		if (!Assert.number( number )) return false;
		if ( number == 0.0 ) {
			Logger.log("Assert: not non-zero '" + number + "'");
			return false;
		}
		return true;
	};
};

function OnScreenPicture (url, imageObj) {
	var self = this;
	
	this.imgObj = imageObj;
	
	this.url = url;
	
	this.original = {};
	
	this.isReady = false;
	if (typeof(self.imgObj.src) === 'string') {
		if (self.imgObj.src.length > 3) {
			self.isReady = true;
			
			self.original.width = $(self.imgObj).width();
			self.original.height = $(self.imgObj).height();
		}
	}
	
	this.originalWidth = function () {
		//Logger.log(self.original.width);
		return self.original.width;
	};

	this.originalHeight = function () {
		//Logger.log(self.original.height);
		return self.original.height;
	};
	
	this.getReady = function ( readyCallback ) {
		
		if ( self.isReady === false ) {
		
			self.imgObj.onload = function () {
				Logger.log(self.imgObj.id + " image onload");
				self.isReady = true;
				
				self.original.width = $(self.imgObj).width();
				self.original.height = $(self.imgObj).height();
				
				readyCallback();
			};

			self.imgObj.src = self.url;
		} else {
			
			readyCallback();
		}
	};
	
	this.noNeedToReady = function () {
		
		self.imgObj.onload = function () {};
		
		if ( self.isReady === false ) {
			self.imgObj.src = "";
		}
	};
};

function Album (childrenNum) {
	var self = this;
	
	this.isRound = true;
	
	// private data fields
	var childrenNumber = childrenNum;
	var currentChild = -1;
	var desiredChild = -1;
	
	// Desire next/previous child
	
	this.next = function () {
		if (desiredChild + 1 > childrenNumber - 1) {
			if (self.isRound) {
				// Show the first child
				self.setDesiredChild(0);
			}
		} else {
			// Show the next child
			self.setDesiredChild(desiredChild + 1);
		}
	};
	
	this.prev = function () {
		if (desiredChild - 1 < 0) {
			if (self.isRound) {
				// Show the last child
				self.setDesiredChild( childrenNumber - 1);
			}
		} else {
			// Show the previous child
			self.setDesiredChild( desiredChild - 1 );
		}
	};
	
	// Callback functions to implement
	
	this.prepareChange = function ( preparedCallback ) { preparedCallback() };

	this.cancelChange = function ( cancelledChild ) {};
	
	this.doChange = function ( doneCallback ) { doneCallback() };
	
	this.doneChange = function () {};
	
	// Set/get desiredChild
	
	this.getDesiredChild = function () {
		return desiredChild;
	};
	
	this.setDesiredChild = function (number) {
		// Check if this call is negligible
		if (desiredChild == number) return;
		
		// Check boundaries
		if (number < 0) return;
		if (number > childrenNumber - 1) return;
		
		// If the old desiredChild is not loaded yet, stop loading
		if (desiredChild != currentChild) {
			this.cancelChange( desiredChild );
		}
		
		// Save the desired number
		desiredChild = number;
		
		// Start the change : prepare then do change
		this.prepareChange( function () {
			self.doChange( function () {
				currentChild = desiredChild;
				self.doneChange();
			} );
		});
	};
	
	// Get currentChild
	
	this.getCurrentChild = function () {
		return currentChild;
	};
};

function SlideShow (album) {
	var self = this;
	
	this.start = function (msec) {
		album.doneChange = function () {
			var timer = setTimeout( function () { album.next(); }, msec );
		};
		album.next();
	};
};

var Utils = new function () {
	this.repeatTimeoutUntilTrue = function( booleanFunction, millis ) {
		var timeout;
		function addTimeout() {
			if (booleanFunction() != true) {
				timeout = setTimeout( addTimeout, millis);
			}
		};
		addTimeout();
	};
	
	this.waitForImageComplete = function (image, callbackForImage) {
		Utils.repeatTimeoutUntilTrue( function() {
			if (image.complete == true) { callbackForImage( image ) };
			return image.complete }, 50);
	};
};

Utils.repeatTimeoutUntilTrue ( function() {
	var i = 0;
	return function() {
		if (i > 0) {
			Logger.log("i = " + i--);
			return false;
		}
		return true;
	};
}(), 2000);

if (jQuery.browser.msie === true && jQuery.browser.version.substr(0,1) === "6") {
	$(window).ready( function() {
		Logger.log("msie6");
		CFInstall.check({
			node:"googleChromeInstall",
			cssText:"z-index:1000",
			destination:"http://www.margaretaflora.hu/index.php",
			preventPrompt:true,
			onmissing: function() {
				Logger.log("install CF");
				$('<a href="common/ChromeFrameSetup.exe">Install</a>').appendTo("body").click();
				}
			});
		});
}

function getMovie(movieName) {
	return (navigator.appName.indexOf("Microsoft") != -1) ? window[movieName] : document[movieName];
}

