var inera = function() {
	return {
		isIE6: false,
		init: function() {
			/*@cc_on
			if (@_jscript_version==5.6 ||
				(@_jscript_version==5.7 && 
				navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1)) {
			   	inera.isIE6 = true;
			}
			@*/
			inera.buttonHover.init();
			inera.anchor.init([
				{ element: ".puffarea-item" },
				{ element: ".pufflayout" },
				{ element: ".top" }
			]);
			inera.subscription.init();
		}
	};
}();


/**
 * Adds hover functionality for input[type=image]. This is done by,
 * on hover, replacing the image with a transparent image
 * and applying a css class ("<image name>-hover") on the input.
 * The rest is done through css using this class name.
 *
 * This class also contains some image preloading to avoid flickering.
 */
inera.buttonHover = function() {
	var DOMReady = function() {
		$("input[type=image]").live("mouseover", function() {
			var $self = $(this);
			var hoverClassName = $self.data("hoverClassName");
			if (hoverClassName == null) {
				var fileName = this.src.replace(/.*\/(.*)\..*/, "$1").toLowerCase();
				hoverClassName = fileName + "-hover";
				$self.data("hoverClassName", hoverClassName);
				$self.data("src", this.src);
				$self.width($self.width());
				$self.height($self.height());
			}
			$self.addClass(hoverClassName);
			this.src = "/Images/Design/px.gif";
		});
		$("input[type=image]").live("mouseout", function() {
			var $self = $(this);
			this.src = $self.data("src");
			$self.removeClass($self.data("hoverClassName"));
		});
	};
	
	return {
		init: function() {
			if (!inera.isIE6) {		
				$(document).ready(DOMReady);
				var i1 = new Image();
				i1.src = "/Images/Design/buttons_hover.png";
				var i2 = new Image();
				i2.src = "/Images/Design/buttons/Nasta_hover.png";
			}
		}
	};
}();


/**
 * Makes a whole element function as an anchor.
 * This will work only if there aren't any anchors in
 * P or LI tags (ie tags added through). The href of the first found
 * anchor is used when the element is clicked.
 */
inera.anchor = function() {	
	return {
		init: function(settings) {
			if (settings) {
				for (var i = 0, ln = settings.length; i < ln; i++) {
					if (settings[i].element) {
						var selector = settings[i].element;
						$(selector).live("mouseover", function() {
							var $self = $(this);
							if (!$self.data("tried-anchoring")) {
								$self.data("tried-anchoring", true);
								var $a = $self.find("a");
								var noOfAs = $a.length;
								if (noOfAs > 0) {
									var doAnchor = true;
									for (var i = 0; i < noOfAs; i++) {
										var parentTag = $a[i].parentNode.tagName.toLowerCase();
										if (parentTag == "p" || parentTag == "li") {
											doAnchor = false;
											break;
										}
									}
									if (doAnchor) {
										$self.data("anchored", true);
										$self.data("hoverClassName", this.className + "-hover");
										var href = $a[0].href;
										$self.data("href", href);
										$self.find("p, img, .pufftextclass:not(p), .mainbodytextclass:not(p)").each(function() {
											$self = $(this);
											$a = $("<a href=\"" + href + "\" class=\"added-anchor\"></a>");
											if ($self.html() == 0) {
												$self.before($a);
												$a.wrapInner($self);
											} else if ($self.find("a").length == 0) {
												$self.wrapInner($a);
											}										
										});
									}
								}
							}
							$self.addClass($self.data("hoverClassName"));
						});
						$(selector).live("mouseout", function() {							
							var $self = $(this);
							if ($self.data("anchored")) {
								$self.removeClass($self.data("hoverClassName"));
							}
						});
						$(selector).live("click", function(e) {
							var $self = $(this);
							if ($self.data("anchored")) {
								e.preventDefault();
								window.location = $self.data("href");
								return false;	
							}
							return true;
						});
					}
				}
			}
		}
	};
}();


/**
 * Moves the subscription email label into the email field
 */
inera.subscription = function() {
	var DOMReady = function() {
		var $label = $(".subscription .emaillabel");
		if ($label.length > 0) {
			var label = $label.html();
			$label.css({"display": "none"});
			$input = $label.parent().find("input[type=text]");
			if ($input.length > 0) {
				var input = $input[0];
				if (input.value.length == 0) {
					input.value = label;			
				}
				$input.focus(function() {
					if (this.value == label) {
						this.value = "";
					}
				});
				$input.blur(function() {
					if (this.value.trim().length == 0) {
						this.value = label;
					}
				});
			}
		}
	};
	
	return {
		init: function() {
			$(document).ready(DOMReady);
		}
	};
}();


inera.init();