jQuery(document).ready(function($) {

	/* ============ GENERAL UTILS ============== */
	window.GeneralUtils = {
		/* constructor */
		init : function() {
			window.GeneralUtils = this; /* needed for initialization */
			this.attachHandlers();
			this.checkIfIe();
			return this;
		},
		wide : true,
		isIeBrowser : false,
		autocomplete : null,
		autocompleteSemaphoreOpened : true,
		setContentWidth : function() {
			var viewportWidth = $(window).width();
			if (!$('body').is('set')) {
				var contentWidth = 910;
				if (viewportWidth >= 1240 && !GeneralUtils.wide) {
					contentWidth = 1210;
					$("#content").addClass('wide').removeClass('narrow');
					GeneralUtils.wide = true;
					$('#content').width(contentWidth);
				}
				if (viewportWidth < 1240 && GeneralUtils.wide) {
					contentWidth = 910;
					$("#content").addClass('narrow').removeClass('wide');
					GeneralUtils.wide = false;
					$('#content').width(contentWidth);
				}
			}

		},
		checkIfIe : function() {

			/*
			 * $.support is collection of properties that represent the presence
			 * of different browser features or bugs. The values of all the
			 * support properties are determined using feature detection
			 * 
			 * objectAll: Is equal to true if doing getElementsByTagName("*") on
			 * an object element returns all descendant elements (is currently
			 * false in IE 7 and IE 8!!!!).
			 * 
			 * We are using this to determine whether browser is ie to fix ie
			 * browser issues.
			 * 
			 */

			GeneralUtils.isIeBrowser = !jQuery.support.objectAll;
			return GeneralUtils.isIeBrowser;
		},

		attachHandlers : function() {
			/* follow, settings forms */
			$('form .chooser > label').live('click', function() {
				if (!$(this).hasClass("selected")) {
					/* if on settings page - mark settings as changed */
					if ($(this).parents("form#settings_form").length > 0) {
						Settings.markSettingsAsChanged();
					}
					$(this).siblings('input').attr('checked', 'checked');
					$(this).parent().siblings().children('input').removeAttr('checked').end().children('label').andSelf().removeClass('selected');
					$(this).parent().andSelf().addClass('selected');
				}
			});

			$('form.commonFormSubmit .control.primary .button').live('click', function() {
				$('form.commonFormSubmit').submit();
				return false;
			});

		},
		setThumbnailsPositions : function() {
			$('.processing .thumbnail').each(function() {
				var thumbnail = $(this);
				if (this.complete) { /* if image is loaded (cahced?) */
					GeneralUtils.setThumbnailPosition(thumbnail);
				} else { /* else wait for it to be loaded */
					thumbnail.load(function() {
						GeneralUtils.setThumbnailPosition(thumbnail);
					});
				}
			})
		},
		setThumbnailPosition : function(thumbnail) {
			var w = thumbnail.width() + 20;
			var h = thumbnail.height() + 20;
			thumbnail.parents('.img_holder').css( {
				'width' : w + 'px',
				'height' : h + 'px',
				'margin-top' : parseInt(-h * 0.5, 10) + 'px',
				'margin-right' : 'auto',
				'margin-bottom' : '0',
				'margin-left' : 'auto',
				'top' : '50%'
			});
		},
		debug : function(msg) {
			if (window[console]) {
				console.debug(GeneralUtils.asString(msg));
			} else {
				var console = $("#console");
				if (console.length == 0) {
					$("body").append("<div id='console' style='background-color: white; position: absolute; left: 50px; top: 50px;'><a href='#'>close</a></div>");
					console = $("#console");
					console.css( {
						backgroundColor : "white",
						position : "absolute",
						left : "100px",
						top : "100px",
						width : "400px",
						border : "1px solid darkgray",
						padding : "10px 10px 10px 10px"
					});
					console.find("a").css( {
						position : "absolute",
						left : "180px",
						top : "5px"
					}).click(function() {
						$("#console").remove();
					});
				}
				console.append(GeneralUtils.asString(msg)).append("<br/>");
			}
		},

		asString : function(value) {
			if ($.isArray(value)) {
				var res = "[";
				$.each(value, function(i, v) {
					res += GeneralUtils.asString(v) + ", ";
				});
				res += "]";
				return res;
			}
			if ("object" == typeof value) {
				var res = "";
				$.each(value, function(k, v) {
					if (v != null) {
						res += "\"" + k + "\": " + GeneralUtils.asString(v) + ", ";
					}
				});
				if (res == "")
					res += value; /* if no properties - implicitly */
				else
					res = "{" + res + "}";
				return res;
			}
			return "\"" + value + "\"";
		}

	}.init();
});