// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// Title : Visuals
// Author : Helldesign
// URL : http://helldesign.net
//
// Description : Scripts altering markup to add more visual flavors ;)
//
// Created : 17-05-2006
// Modified : 13-12-2006
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Visuals = {
	
	/*
		INITIALIZES ALL THE MARKUP TRICKS
		Params:
		  oOptions (object) - options. Currently following options are supported:
		    options (string or HTML object) - ID of an object (or the object itself) that holds all the elements to modify
		    exception (string or HTML object) - ID or an object of the container that children of should be excluded from all the Visuals operations
		    imgWOffset (number) - Width offset applied to generated img's parent - no subtitles version (so it applies to span.photo-shadow)
		    imgWOffsetS (number) - Width offset applied to generated img's parent - subtitles version (applies to dl.images-box-2) 
		    ieAlpha (boolean) - if true, enables PNG alpha support in IE 5.5 and 6 
 
		Returns:
		  -
	*/
	Init: function( oOptions ) {
		for (var name in oOptions)
			Visuals[name] = oOptions[name];
		if (Visuals.exception) Visuals.exception = $(Visuals.exception);
		if (Visuals.container) Visuals.container = $(Visuals.container);
		if (!Visuals.imgWOffset) Visuals.imgWOffset = 0;
		if (!Visuals.imgWOffsetS) Visuals.imgWOffsetS = 0;
		if (Visuals.container) {
			Visuals.FixTables();
			Visuals.FixImages();
			Visuals.FixBQ();
		if (Visuals.ieAlpha) Visuals.EnableAlphaImages();
		} else {
			alert('No container specified for Visuals Class. Aborting.');
			return false;
		}
	},
	/*
		MODIFIES ALL THE TABLES WITHIN MCONTAINER TO CREATE SO CALLED "ZEBRA TABLES"
		Params:
		  -
		Returns:
		  -
	*/
	FixTables: function() {
		if (document.getElementsByTagName) {
			var tables = $(Visuals.container).getElementsByTagName('table');
			for (var i=0;i<tables.length;i++) {
				var rows = tables[i].getElementsByTagName('tr');
				for (var j=1;j<rows.length;j+=2) {
					Element.addClassName(rows[j], "odd");
				}
				Element.addClassName(rows[0], "first-row");
				Element.addClassName(rows[rows.length - 1], "last-row");
				if (typeof(NiceDOM) != "undefined") {
					Element.addClassName(NiceDOM.first_child(rows[0]),"l");
					Element.addClassName(NiceDOM.last_child(rows[0]),"r");
					Element.addClassName(NiceDOM.first_child(rows[rows.length - 1]),"l");
					Element.addClassName(NiceDOM.last_child(rows[rows.length - 1]),"r");
				}
			}
		}
	},
	/*
		WRAPS ALL THE IMAGES WITHIN MCONTAINER IN EXTRA MARKUP TO ALLOW DISPLAYING OF IMAGES SUBTITLES
		Params:
		  -
		Returns:
		  -
	*/
	FixImages: function() {
		if (document.createElement && document.getElementsByTagName) {
			var images = $(Visuals.container).getElementsByTagName('img');
			for (var i=0;i<images.length;i++) {
				if (!Visuals.childOf(images[i],Visuals.exception)) {
				
					// let's collect data needed to properly set image parent's dimensions
					var w = (!parseInt(Element.getStyle(images[i],'width'))) ? images[i].width : parseInt(Element.getStyle(images[i],'width'));
					var f = (typeof(Element.getStyle(images[i],'float')) == "undefined") ? Element.getStyle(images[i],'style-float') : Element.getStyle(images[i],'float');
					var a = images[i].align;
					var ipl = parseInt(Element.getStyle(images[i],'padding-left'));
					var ipr = parseInt(Element.getStyle(images[i],'padding-right'));
					var ibl = parseInt(Element.getStyle(images[i],'border-left-width'));
					var ibr = parseInt(Element.getStyle(images[i],'border-right-width'));
					var parentW = w + ipl + ipr + ibl + ibr;
					
					// now let's check the image alignment
					var align;
					if (f != 'none') { 
						align = f;
					} else if (a != '') { 
						align = a;
						images[i].align = '';
					} else {
						align = 'none';
					};
					
					// ok, now we wrap the image in nonsemantic hooks for eventual CSS
					var c1 = document.createElement('span');
					c1.className = 'photo-shadow';
					var c2 = document.createElement('span');
					c2.className = 's1';
					var c3 = document.createElement('span');
					c3.className = 's2';
					var c4 = document.createElement('span');
					c4.className = 's3';
					if (images[i].parentNode.tagName == "A") {
						var clonedNode = images[i].parentNode.cloneNode(true);
						var clonedImg = clonedNode.getElementsByTagName('img')[0];
					} else {
						var clonedNode = images[i].cloneNode(true);
						var clonedImg = clonedNode; 
					}
					c4.appendChild(clonedNode);
					c3.appendChild(c4);
					c2.appendChild(c3);
					c1.appendChild(c2);
					var result = c1;
					
					clonedImg.style.cssFloat = "none";
					clonedImg.style.styleFloat = "none";

					// in case he image has nonempty alt or title attribs - we need to show the image's title
					// so we wrap it in DL

					if (images[i].title != "" || images[i].alt != "") {
						var dl = document.createElement('dl');
						dl.className = "images-box-2";
						var dt = document.createElement('dt');
						dt.appendChild(c1);
						dl.appendChild(dt);
						var dd = document.createElement('dd');
						if (images[i].title != '') {
							dd.innerHTML = images[i].title;
						} else {
							dd.innerHTML = images[i].alt;
						}
						dl.appendChild(dd);
						if (align != '' && align != 'none') Element.addClassName(dl,align);
						dl.style.width = parentW + Visuals.imgWOffsetS + 'px';
						result = dl;
					} else {
						// If the image doesn't need to have any subtitles
						if (align != '' && align != 'none') Element.addClassName(c1,align);
						c1.style.width = parentW + Visuals.imgWOffset + 'px';
					}
					
					
					
					// Finally, let's insert the new wrapped image into the DOM
					if (images[i].parentNode.tagName == "A") {
						var parent = images[i].parentNode.parentNode;
						parent.replaceChild(result, images[i].parentNode);
					} else {
						var parent = images[i].parentNode;
						parent.replaceChild(result, images[i]);
					}
					
				}
			}
		}
	},
	/*
		WRAPS ALL THE BLOCKQUOTES WITHIN MCONTAINER IN EXTRA MARKUP TO ALLOW EXTRA VISUALS
		Params:
		  -
		Returns:
		  -
	*/
	FixBQ: function() {
		if (document.getElementsByTagName) {
			var bqs = $(Visuals.container).getElementsByTagName('blockquote');
			for (var i=0;i<bqs.length;i++) {
				var newContent = '<div>'+bqs[i].innerHTML+'</div>';
				bqs[i].innerHTML = newContent;
			}
		}
	},
	
	/*
		ENABLES TRANSPARENT PNGS IN SHITTY BROWSERS (IE OF COURSE)
		Params:
		  -
		Returns:
		  -
	*/
	EnableAlphaImages: function () {
		if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
			var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
			var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5 &&  Number(rslt[1]) < 7);
			if (itsAllGood) {
				for (var i=0; i<document.all.length; i++){
					var obj = document.all[i];
					var bg = obj.currentStyle.backgroundImage;
					var img = document.images[i];
					if (bg && bg.match(/\.png/i) != null) {
						var img = bg.substring(5,bg.length-2);
						var offset = obj.style["background-position"];
						obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='crop')";
						obj.style.backgroundImage = "url('blank.gif')";
						obj.style["background-position"] = offset; // reapply
					} else if (img && img.src.match(/\.png$/i) != null) {
						var src = img.src;
						if (!Visuals.childOf(img,'post-bookmarks')) {
							img.style.width = img.width + "px";
							img.style.height = img.height + "px";
							img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='crop')"
							img.src = "blank.gif";
						}
					}
				}
			}
		}
	},
	/*
		ADDS "LAST" CLASS NAME TO THE LAST ELEMENT OF THE COLLECTION PASSED AS ARGUMENT. 
		ONCE AGAIN - THANKS IE FOR LACK OF SUPPORT FOR BASIC CSS SELECTORS.
		Params:
		  oCollection (collection) - collection of elements that last element of will get "last" class
		Returns:
		  -
	*/
	addLast: function (oCollection) {
		Element.addClassName(oCollection[oCollection.length - 1], 'last');
	},
	/*
		ADDS <SPAN> TO WITHIN EACH ELEMENT IN COLLECTION. 
		Params:
		  oCollection (collection) - collection of elements that need to have <span> as the direct child
		Returns:
		  -
	*/
	addSpan: function (oCollection) {
		for (var i=0;i<oCollection.length;i++) {
			var tempHTML = oCollection[i].innerHTML;
			oCollection[i].innerHTML = '<span>' + tempHTML + '</span>';
		}
	},
	/*
		HELPER FUNCTION - CHECKS IF MCHILD IS A DESCENDANT OF MPARENT 
		Params:
		  mChild (string or object) - descendant element
		  mParent (string or object) - parent element
		Returns:
		  true - if mChild is a descendant of mParent
		  false - otherwise
	*/
	childOf: function(mChild, mParent) {
		var child = $(mChild);
		var parent = $(mParent);
		while(child.tagName != "BODY") {
			if (child.parentNode == parent) return true;
			child = child.parentNode
		}
		return false;
	}
	
}
