/* 

	hhmck.com
	Javascript file for funkiness
	
	created by Ryan Mitchell on 21/08/2008

*/

// lets not do anything till we're ready
window.addEvent('domready',function() {
									
	page = 1;
									
	// announcements
	$('announcements').setStyles({
		position: 'absolute',
		top: $('text').getPosition().y + 30,
		left: $('text').getPosition().x
	});
					
	// function to load pages
	_load = function(p,pg) {
		
		// stop slideshow
		goslideshow = false;
		
		$('announcements').setStyle('display','none');
		
		page = pg || 1;

		// handle json loader
		var loader = new Request.JSON({
			url: 'json/'+p,
			onComplete: _complete
		}).get(null);
		
	};
	
	// function to handle json response
	_complete = function(json,text) {
		
		// does big image need changed?
		if($defined(json.bigimage)) {
			if(json.bigimage == '') 
				$('big_image').set('html','');
			else
				$('big_image').set('html','<p><img src="'+json.bigimage+'" alt="big image"></p>');
		}
		
		// do we need to change the main links?
		if($defined(json.linklevel1)) {
			$('link-level1').set('html',json.linklevel1);		
		}
		
		// do we need to change the sub links?
		if($defined(json.linklevel2)) {
			$('link-level2').set('html',json.linklevel2);
			$('link-level2').getParent().removeClass('nobg');
		
			if(json.linklevel2 == '')
				$('link-level2').getParent().addClass('nobg');	
		}
		
		// do we need to change the level 3 links?
		if($defined(json.linklevel3)) {
			$('link-level3').set('html',json.linklevel3);
			$('link-level3').getParent().removeClass('nobg');
		
			if(json.linklevel3 == '')
				$('link-level3').getParent().addClass('nobg');	
		}
		
		// do we need to change the level 4 links?
		if($defined(json.linklevel4)) {
			$('link-level4').set('html',json.linklevel4);		
			$('link-level4').getParent().removeClass('nobg');
		
			if(json.linklevel4 == '')
				$('link-level4').getParent().addClass('nobg');	
		}
		
		// do we need to change the text?
		if($defined(json.text)) {
			$('text').set('html',json.text);		
		}
		
		// do we need to change the image numbers?
		if($defined(json.images)) {
			$('image-numbers').set('html',json.images);		
		}
		
		// do we need to change the level 4 links?
		if($defined(json.textpages)) {
			$('text-numbers').set('html',json.textpages);		
		}
		
		if(page > 1){
			$('text-numbers').getElements('a')[page-1].onclick();
		}
		
	};
	
	// function to change an image
	_imageChange = function(lnk,img) {
		
		var tmp = new Date();
		var suffix = tmp.getTime();
	
		// remove on state from images
		$('image-numbers').getChildren('a').each(function(a) {
			a.getChildren('img').each(function(i) {	
				if(i.getProperty('src').indexOf("-on.gif") != -1) {
					var t = new Element('img', { src: i.getProperty('src').replace('-on.gif','.gif') + "?" + suffix });
					t.onload = function() {
						i.setProperty('src',i.getProperty('src').replace('-on.','.') + "?" + suffix);
					}
				}
			});
		});
				
		// load text
		var t = new Element('img', { src: img + "?" + suffix });
		t.onload = function() {

			// change the image
			$('big_image').set('html','<p><img src="'+img + "?" + suffix +'" />');
			
		};
		
		// set on state for this image
		$(lnk).getChildren().filter('img').each(function(i) {
			var t = new Element('img', { src: i.getProperty('src').replace('.gif','-on.gif') + "?" + suffix });
			t.onload = function() {
				i.setProperty('src',i.getProperty('src').replace('.gif','-on.gif') + "?" + suffix);	
			};
		});
	
	};
	
	// function to change text
	_textChange = function(lnk,txt,map,maplink) {
		
		var map = map || '';
		var tmp = new Date();
		var suffix = tmp.getTime();
				
		// remove on state from images
		$('text-numbers').getChildren('a').each(function(a) {
			a.getChildren('img').each(function(i) {	
				if(i.getProperty('src').indexOf("-on.gif") != -1) {
					var t = new Element('img', { src: i.getProperty('src').replace('-on.gif','.gif') + "?" + suffix });
					t.onload = function() {
						i.setProperty('src',i.getProperty('src').replace('-on.','.') + "?" + suffix);
					}
				}
			});
		});
		
		// load text
		var t = new Element('img', { src: txt + "?" + suffix });
		t.onload = function() {
		
			// change the text
			$('text').set('html','<p><img src="'+txt + "?" + suffix + '" usemap="#Map" />'+(map!='' ? '<map name="Map" id="Map"><area shape="rect" coords="'+map+'" href="'+maplink+'" />' : '')+'</p>');
			
		};
		
		// set on state for this image
		$(lnk).getChildren('img').each(function(i) {
			var t = new Element('img', { src: i.getProperty('src').replace('.gif','-on.gif') + "?" + suffix });
			t.onload = function() {
				i.setProperty('src',i.getProperty('src').replace('.gif','-on.gif') + "?" + suffix);	
			};
		});
		
	};
	
	var current = 1;
	var goslideshow = true;
	
	// slideshow
	_slideshow = function() {
		
		if(goslideshow) { 
		
			var tmp = new Date();
			var suffix = tmp.getTime();
						
			var i = new Element('img', { src: 'images/home/Home-0' + current + '.jpg' + "?" + suffix });
			i.onload = function() {
				//$('big_image').set('html','<p><img src="' + 'images/home/Home-0' + current + '.jpg' + "?" + suffix + '" alt="home" /></p>');	
				$('big_image').set('html','<p><img src="' + 'images/projects/vartov/Vartov-0' + current + '.jpg' + "?" + suffix + '" alt="home" /></p>');	
				
				//var t = new Element('img', { src: 'images/home/Text-0' + current + '.gif' + "?" + suffix });
				var t = new Element('img', { src: 'images/home/Text-09.gif' + "?" + suffix });
				t.onload = function() {
					//$('text').set('html','<p><img src="' + 'images/home/Text-0' + current + '.gif' + "?" + suffix + '" alt="home" /></p>');	
					$('text').set('html','<p><img src="' + 'images/home/Text-09.gif' + "?" + suffix + '" alt="home" /></p>');	
					
					current++;
					
					if(current > 6) current = 1;
					
					_slideshow.delay(6000);
					
				};
													
			};
					
		}
		
	};
	
	// home
	_home = function() {
		
		if(!goslideshow) {
	
			_load('index.php');
			(function(){ $('announcements').setStyle('display','block')}.delay(1000));
			goslideshow = true; 
			current = 1; 
			_slideshow();
		
		}
	
	}
	
	// screen width settings
	if(screen.width > 800) $$('td.width').setStyle('width','320px');
	
	// preload number images
	for(i=1;i<=15;i++) {
	
		var img = new Element('img',{ src: 'images/number'+i+'.gif' });
		var img2 = new Element('img',{ src: 'images/number'+i+'-on.gif' });
	
	}
	
	_slideshow();
									
});