// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// Title : Visuals
// Author : Helldesign
// URL : http://en.helldesign.pl
//
// Description : Scripts altering markup to add more visual flavors ;)
//
// Created : 17-05-2006
// Modified : 16-11-2006
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Visuals = {

	container: null,
	imgWOffset: 0, // this has to be equalto the sum of horizontal paddings and borders applied to .photo-shadow, .s1, .s2, .s3, .s4, dl.images-box-2, dl.images-box-2 dt in post.css file. Sorry, but couldn't find a way to read those values automatically :( 
	
	/*
		INITIALIZES ALL THE MARKUP TRICKS
		Params:
		  mContainer (string or object) - Id of an object (or the object itself) that holds all the elements to modify
		  mException (string or object) - don't modify an element if it's a descendant of  mException
		Returns:
		  -
	*/
	Init: function( mContainer, mException ) {
		if (mContainer) {
			Visuals.container = $(mContainer);
			Visuals.exception = $(mException);
			Visuals.FixTables();
			//Visuals.FixImages();
			Visuals.FixBQ();
			Visuals.EnableAlphaImages();
		}
	},
	/*
		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 image's data
					var w = images[i].width;
					var f = Element.getStyle(images[i],'float');
					if (typeof(f) == "undefined") f = Element.getStyle(images[i],'style-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 + Visuals.imgWOffset;
					var align;
					
					images[i].style.cssFloat = "none";
					images[i].style.styleFloat = "none";
					
					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 img = images[i].parentNode.cloneNode(true);
					} else {
						var img = images[i].cloneNode(true);
					}
					c4.appendChild(img);
					c3.appendChild(c4);
					c2.appendChild(c3);
					c1.appendChild(c2);
					
					var result = c1;
					
					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);
						result = dl;
					}
					
					
					
					
					if (f != 'none') { 
						align = f;
					} else if (a != '') { 
						align = a;
						images[i].align = '';
					} else {
						align = 'none';
					};
					
					result.style.width = parentW + 'px';
					result.style.cssFloat = align;
					result.style.styleFloat = align;
					
					
					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"];
						
						if(img.indexOf("transparent") == -1)
							obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='crop')";
						else
							obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='scale')";
							
						obj.style.backgroundImage = "url('http://www.wiggler.gr/wp-content/themes/wiggler/images/grid/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 = "http://www.wiggler.gr/wp-content/themes/wiggler/images/grid/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;
	}
	
}

function validate(form)	{
	if($('comment').getValue() == "")	{
		alert("Please, write something");
		$('comment').focus();
		return false;
	}
	
	if($('name').getValue() == "")	{
		alert("Please enter your name");
		$('name').focus();
		return false;
	}
	
	if($('mail').getValue() == "")	{
		alert("Please enter your email");
		$('mail').focus();
		return false;
	}
	
	return true;
}

AccessKeys	= {
	posts	: [],
	current	: -1,
	nextPage: -1,
	prevPage: -1,
	tiles 	: -1,
	
	initialize	: function()	{
		
		var posts = $$('#content .post-wrapper');

		this.current = -1;

		if(window.location.href.indexOf('tiles=1') != -1)
			this.tiles = 1;
			
		if(posts.length > 0 && !$('single'))	{
			for(var i=0;i < posts.length; i++)	{
				var post_id = posts[i].id;
				
				AccessKeys.posts[i] = post_id;

				if(AccessKeys.tiles == 1) {
					
					var tile_body = $$('#'+ post_id +' .tile-mask');
					
					if($(post_id+'-date'))
						$(post_id+'-date').hide();
				
					if($(post_id+'-text'))
						$(post_id+'-text').hide();
					
					Event.observe($(post_id), 'mouseover', function() {	AccessKeys.toggleTileText(this.id,1);	});
					Event.observe($(post_id), 'mouseout', function() {	AccessKeys.toggleTileText(this.id,-1);	});
					
					Event.observe(tile_body[0], 'click', function(event) { 
						var ids 		= this.id.split('-');
						var tile_link 	= $$('#post-'+ ids[1] +' .post-name h2 a');
						
						window.location = tile_link[0].href; 
					});
				}			
			}

			new HotKey('s',function(event){  	//next
			    if(Content.insideSearch == 0)
					AccessKeys.traverse(1,-1);
			},{  
			    ctrlKey: false  
			});

			new HotKey('w',function(event){  	//previous
			    if(Content.insideSearch == 0)
					AccessKeys.traverse(-1,-1);
			},{  
			    ctrlKey: false  
			});
	
			new HotKey('n',function(event){  	//next
			    if(Content.insideSearch == 0)
					AccessKeys.traverse(1,-1);
			},{  
			    ctrlKey: false  
			});

			new HotKey('p',function(event){  	//previous
			    if(Content.insideSearch == 0)
					AccessKeys.traverse(-1,-1);
			},{  
			    ctrlKey: false  
			});

			new HotKey('t',function(event){ 	//tiles
			    if(Content.insideSearch == 0)
					AccessKeys.toggleTiles();
			},{  
			    ctrlKey: false  
			});
			
			new HotKey('h',function(event){ 	//go to homepage
				if(Content.insideSearch == 0)
			    	window.location = "http://www.wiggler.gr";
			},{  
			    ctrlKey: false  
			});

			var nextPage = $$('#content .nav-prev-next-post .next');
			var prevPage = $$('#content .nav-prev-next-post .prev');
			
			if(nextPage.length > 0 && nextPage[0].firstChild)
				AccessKeys.nextPage = nextPage[0].firstChild.href;
			
			if(prevPage.length > 0 && prevPage[0].firstChild)
				AccessKeys.prevPage = prevPage[0].firstChild.href;
		}
		else if(posts.length == 1)	{
			
			var nextPage = $$('#content .nav-prev-next-post .next');
			var prevPage = $$('#content .nav-prev-next-post .prev');
			
			if(nextPage.length > 0 && nextPage[0].firstChild)
				AccessKeys.nextPage = nextPage[0].firstChild.href;
			
			if(prevPage.length > 0 && prevPage[0].firstChild)
				AccessKeys.prevPage = prevPage[0].firstChild.href;
							
			 new HotKey('n',function(event){  	//next
			     if(Content.insideSearch == 0 && AccessKeys.nextPage != -1)
			 		window.location = AccessKeys.nextPage;
			 },{  
			     ctrlKey: false  
			 });
			
			 new HotKey('p',function(event){  	//previous
			     if(Content.insideSearch == 0 && AccessKeys.prevPage != -1)
			 		window.location = AccessKeys.prevPage;
			 },{  
			     ctrlKey: false  
			 });			
			
			 new HotKey('d',function(event){  	//next
			     if(Content.insideSearch == 0 && AccessKeys.nextPage != -1)
			 		window.location = AccessKeys.nextPage;
			 },{  
			     ctrlKey: false  
			 });
			
			 new HotKey('a',function(event){  	//previous
			     if(Content.insideSearch == 0 && AccessKeys.prevPage != -1)
			 		window.location = AccessKeys.prevPage;
			 },{  
			     ctrlKey: false  
			 });
			
			new HotKey('h',function(event){ 	//go to homepage
				if(Content.insideSearch == 0)
			    	window.location = "http://www.wiggler.gr";
			},{  
			    ctrlKey: false  
			});
		}
	},
	
	traverse	: function(direction,id)	{
		if(direction == 1 && id == -1)	{
			
			if(AccessKeys.current +1 == AccessKeys.posts.length && AccessKeys.prevPage != -1)
				window.location = AccessKeys.prevPage;
				
			if(AccessKeys.current +1 < AccessKeys.posts.length)	{
				
				if(AccessKeys.current != -1)
					AccessKeys.toggleTileText(AccessKeys.posts[AccessKeys.current],-1);
						
				AccessKeys.current++;

				AccessKeys.toggleTileText(AccessKeys.posts[AccessKeys.current],1);
				
				Effect.ScrollTo(AccessKeys.posts[AccessKeys.current], 200);
				//$(AccessKeys.posts[AccessKeys.current]).scrollTo();
			}
				
		}
		else if(direction == -1 && id == -1)	{
			
			if(AccessKeys.current == 0 && AccessKeys.nextPage != -1)	
				window.location = AccessKeys.nextPage;
		
			if(AccessKeys.current > 0)	{
			
				if(AccessKeys.current != -1)
					AccessKeys.toggleTileText(AccessKeys.posts[AccessKeys.current],-1);
		
				AccessKeys.current--;

				AccessKeys.toggleTileText(AccessKeys.posts[AccessKeys.current],1);
				
				Effect.ScrollTo(AccessKeys.posts[AccessKeys.current], 200);				
				//$(AccessKeys.posts[AccessKeys.current]).scrollTo();
			}

		}
		else	{
			var post;
			var position = AccessKeys.posts.indexOf(id);

			if(direction == 1)	{
				
				if(position +1 == AccessKeys.posts.length && AccessKeys.prevPage != -1)
					window.location = AccessKeys.prevPage;
					
				if(position +1 < AccessKeys.posts.length)	{
						
					position = position + 1;
					post = AccessKeys.posts[ position ];
					
					Effect.ScrollTo(post, 200);
					//$(post).scrollTo();
					
					AccessKeys.current = position;				
				}
			} 
			else  if( direction == -1) {
				
				if(position == 0 && AccessKeys.nextPage != -1)	
					window.location = AccessKeys.nextPage;
			
				if(position > 0)	{
						
					position = position - 1;
					post = AccessKeys.posts[ position ];
					
					Effect.ScrollTo(post, 200);
					//$(post).scrollTo();
					
					AccessKeys.current = position;
				}
			}

		}
	},

	clearTiles : function() {

		for(var i = 0; i < AccessKeys.posts.length; i++) {

			var id 		= AccessKeys.posts[i];
			var mpost 	= $$('#'+ id +' .mpost');

			if($(id+'-date'))	$(id+'-date').hide();
			if($(id+'-text'))	$(id+'-text').hide();	

			if(mpost.length > 0)
				mpost[0].removeClassName('mpost-over');
		}
	},

	toggleTileText : function(id,force) {

		AccessKeys.clearTiles();

		var mpost = $$('#'+ id +' .mpost');

		if(force == 0) {

			if($(id+'-date').visible()) {

				if($(id+'-date'))	$(id+'-date').hide();
				if($(id+'-text'))	$(id+'-text').hide();
			}
			else {

				if($(id+'-date'))	$(id+'-date').show();
				if($(id+'-text'))	$(id+'-text').show();
			}		
		}
		else if(force == 1) {

			if($(id+'-date'))	$(id+'-date').show();
			if($(id+'-text'))	$(id+'-text').show();

			if(mpost.length > 0)
				mpost[0].addClassName('mpost-over');
		}
		else {

			if($(id+'-date'))	$(id+'-date').hide();
			if($(id+'-text'))	$(id+'-text').hide();	

			if(mpost.length > 0)
				mpost[0].removeClassName('mpost-over');		
		}
	},

	toggleTiles	: function()	{
		
		if(window.location.href.indexOf('?s=') == -1) {
			
			if(window.location.search.indexOf('tiles=1') == -1)
				window.location = "?tiles=1";
			else
				window.location = "?tiles=0";
		}
		else 	{
			
			var find_search = window.location.search.split('&')[0];
			
			if(window.location.search.indexOf('tiles=1') == -1)
				window.location = find_search + "&tiles=1";
			else
				window.location = find_search + "&tiles=0";
		}
	}
}

Content = 	{
	insideSearch : 0,
	
	initialize	: function()	{
		
		if($('pop_button'))	{
			Event.observe($('pop_button'), 'click', function() {
				$('recent_posts').hide();
				$('popular_posts').show();
				$('pop_button').addClassName('active');
				$('rec_button').removeClassName('active');
			});
		}
		
		if($('rec_button'))	{
			Event.observe($('rec_button'), 'click', function() {
				$('recent_posts').show();
				$('popular_posts').hide();
				$('rec_button').addClassName('active');
				$('pop_button').removeClassName('active');
			});
		}
		
		if($('search'))	{			
			Event.observe($('search'), 'click', function() {
				if($('search').getValue() == "")
					$('search_lbl').hide();

				$('searchform').addClassName('over');
				Content.insideSearch = 1;
			});

			Event.observe($('search'), 'focus', function() {
				if($('search').getValue() == "")
					$('search_lbl').hide();
					
				$('searchform').addClassName('over');
				Content.insideSearch = 1;
			});
			
			Event.observe($('search'), 'blur', function() {
				if($('search').getValue() == "")
					$('search_lbl').show();

				$('searchform').removeClassName('over');
				Content.insideSearch = 0;
			});
			
			Event.observe($('btn_search'), 'click', function() {
				if($('search').getValue() != "")
					$('searchform').submit();
			});
		}
		
		if($('name'))	{			
			Event.observe($('name'), 'click', function() {
				if($('name').getValue() == "")
					$('name_lbl').hide();

				$('name_cont').addClassName('over');
				Content.insideSearch = 1;
			});

			Event.observe($('name'), 'focus', function() {
				if($('name').getValue() == "")
					$('name_lbl').hide();
					
				$('name_cont').addClassName('over');
				Content.insideSearch = 1;
			});
			
			Event.observe($('name'), 'blur', function() {
				if($('name').getValue() == "")
					$('name_lbl').show();

				$('name_cont').removeClassName('over');
				Content.insideSearch = 0;
			});
		}
				
		if($('mail'))	{			
			Event.observe($('mail'), 'click', function() {
				if($('mail').getValue() == "")
					$('mail_lbl').hide();

				$('mail_cont').addClassName('over');
				Content.insideSearch = 1;
			});

			Event.observe($('mail'), 'focus', function() {
				if($('mail').getValue() == "")
					$('mail_lbl').hide();
					
				$('mail_cont').addClassName('over');
				Content.insideSearch = 1;
			});
			
			Event.observe($('mail'), 'blur', function() {
				if($('mail').getValue() == "")
					$('mail_lbl').show();

				$('mail_cont').removeClassName('over');
				Content.insideSearch = 0;
			});
		}	
		
		if($('website'))	{			
			Event.observe($('website'), 'click', function() {
				if($('website').getValue() == "")
					$('website_lbl').hide();

				$('website_cont').addClassName('over');
				Content.insideSearch = 1;
			});

			Event.observe($('website'), 'focus', function() {
				if($('website').getValue() == "")
					$('website_lbl').hide();
					
				$('website_cont').addClassName('over');
				Content.insideSearch = 1;
			});
			
			Event.observe($('website'), 'blur', function() {
				if($('website').getValue() == "")
					$('website_lbl').show();

				$('website_cont').removeClassName('over');
				Content.insideSearch = 0;
			});
		}	
		
		if($('comment'))	{			
			Event.observe($('comment'), 'click', function() {
				if($('comment').getValue() == "")
					$('comment_lbl').hide();

				$('comment_cont').addClassName('over');
				Content.insideSearch = 1;
			});

			Event.observe($('comment'), 'focus', function() {
				if($('comment').getValue() == "")
					$('comment_lbl').hide();
					
				$('comment_cont').addClassName('over');
				Content.insideSearch = 1;
			});
			
			Event.observe($('comment'), 'blur', function() {
				if($('comment').getValue() == "")
					$('comment_lbl').show();

				$('comment_cont').removeClassName('over');
				Content.insideSearch = 0;
			});
		}
	}
}

document.observe('dom:loaded',function(){
	
	if($('feedburner'))	{
		new Ajax.Request( "/counter.php", {
			method		: 'get',
		
			onSuccess	: function(res) {
				var counter = $('feedburner');

				if(counter)
					counter.update(res.responseText+" readers");
			}
		});
	}
	
	setupZoom();
	Visuals.Init('content','post-bookmarks');
	Content.initialize();
	AccessKeys.initialize();
});