function getbestfit(s,im) {
	var screen = window.getSize();
	var sizes = (s.split(":"));
	var imgi = 0;
	sizes.each(function(sz,i) {
		if (sz.split("x")[0] < screen.x && i > imgi) imgi = i;
		if (sz.split("x")[1] < screen.y && i > imgi) imgi = i;
	});
	if (sizes[imgi].split("x")[0] < screen.x && sizes[imgi].split("x")[1] <screen.y) imgi = imgi +1 ;
	var dirs = new Array('sml','med','lrg');
	var imgx = sizes[imgi].split('x')[0];
	var imgy = sizes[imgi].split('x')[1];
	if (imgx > screen.x) var sizex = screen.x;
	if (imgy > screen.y) var sizey = screen.y;
	var img =  '_img/'+dirs[imgi]+"/"+im.split('/')[2];
	if ($('largeimg')) $('largeimg').destroy();
	var imgdiv = new Element('img',{
		id:'largeimg',
		src: img,
		styles: {
			'width':sizex,
			'height':sizey
		},
		events: {
			'click': function() {
				this.destroy();
			}
		}
	}).inject(document.body);
}

function loadpic(pic) {
	var shadow = 10;
	disp = new Fx.Morph($('display'));
	var screeny  = window.getSize().y;
	var screenx = window.getSize().x;
	var request = new Request.JSON({
		url: '_view.php',
		onRequest: function() {
			$('greyout').setStyle('display','block');
			$('greyout').setStyles({
				'height': window.getScrollSize().y
			});
			$('display').set('html','');
			disp.set({'width':(screenx/2),'height':(screeny/2),'top':(screeny/4),'left':(screenx/4)});
			$('display').setStyles({'display':'block','background':'#111 url(imgs/loadinfo.net.gif) no-repeat center center'});
		},
		onComplete: function(jsonObj) {
			imgwidths = jsonObj.imgwidths;
			imgheights = jsonObj.imgheights;
			imgname = jsonObj.imgname;
			
			var yi = new Array();
			imgheights.each(function(item, i){
				if (item > screeny) yi.push(i);
			});
			var xi = new Array();
			imgwidths.each(function(item, i){
				if (item > screenx) xi.push(i);
			});
			if (yi.length==0) yi.push(2);
			if (xi.length==0) xi.push(2);
			var imgsize = (yi[0] > xi[0]) ? xi[0] : yi[0];
			var dirs = new Array('sml','med','lrg');
			$('display').set('html','');
			var wred = imgwidths[imgsize]/screenx;
			var hred = imgheights[imgsize]/screeny;
			if (wred>hred) {
				if (wred<1) {
					h = imgheights[imgsize];
					w = imgwidths[imgsize];
					t = (screeny - h)/2;
					l = (screenx - w)/2;
				} else {
					h = Math.round((screenx/imgwidths[imgsize]*imgheights[imgsize]) - (shadow*2));
					w = Math.round(screenx - (shadow*2));
					t = (screeny - h)/2  + window.getScroll().y + shadow;
					l = shadow;
				}
			} else {
				if (hred<1) {
					h = imgheights[imgsize];
					w = imgwidths[imgsize];
					t = (screeny - h)/2;
					l = (screenx - w)/2;
				} else {
					h = Math.round(screeny - (shadow*2)) - 1;
					w = Math.round((screeny/imgheights[imgsize]*imgwidths[imgsize]) - (shadow*2));
					l = (screenx - w)/2 + shadow
					t = window.getScroll().y +shadow;
				}
			}
			var img = new Asset.images('_img/'+dirs[imgsize]+'/'+jsonObj.imgname, {
				onComplete: function() {
					img.setProperties({'id':'view','alt':pic.replace(/-/g," ")});
					img.setStyles({'width':w,'height':h});
					
					
					
					disp.start({'width':w,'height':h,'top':t,'left':l}).chain(function() {
					
						$('display').setStyle('background','none');
						img.inject($('display'));
					});
				}
			});
		}
	}).send('pic='+pic);
}

function loadgallery(id) {
	if (!$('thumbs')) var ul = new Element('ul',{'id':'thumbs'}).inject($('home'));
	var thumbs = new Fx.Morph($('home'));
	thumbs.start({'opacity':0}).chain(function(){
		if ($('contactpage')) $('contactpage').destroy();
		$('thumbs').getChildren().each(function(li){li.destroy()});
		if ($('msg')) $('msg').destroy();
		var msg = new Element('p',{'text':'Loading images...','id':'msg'}).inject($('home'),'before');	
		var jsonRequest = new Request.JSON({
			url: '_thumbs.php',
			onSuccess: function(json){
				var imgpaths = new Array();
				Object.each(json, function(o) {
					imgpaths.push('_img/thumb/'+o.img);
					var li = new Element('li').inject($('thumbs'));
					var a = new Element('a',{'href':'_img/sml/'+o.img,'title':o.title}).inject(li);
					var img = new Element('img',{
						'src':'_img/thumb/'+o.img,
						events: {
							'click': function(e) {
								e.stop();
								loadpic(this.getParent().get('title'));
							}
						}
					}).inject(a);
				});
				var cnt = new Element('span',{'html':'<span id="counter">0</span> of '+imgpaths.length}).inject(msg);
				var myImages = Asset.images(imgpaths, {
					onProgress: function(counter, index, source) {
						$('counter').set('text',counter);
					},
					onComplete: function(){
						msg.destroy();
						thumbs.start({'opacity':1});
					}
				});
			}
		}).send('g='+id);
	});
}

var formhints = new Class({
	Implements: [Options],
	options: {
		collection: $$('input[type=text]')
	},
	initialize: function(options) {
		this.setOptions(options);
		this.defaults();
	},
	defaults: function() {
		this.options.collection.each(function(el) {
			el.addClass('hint');
			if (el.get('value')=='') {
				el.set('value',el.get('title'));
				el.addEvent('focus', function() { 
					if(el.get('value') == el.get('title')) { 
						el.set('value',''); 
						el.removeClass('hint');
					}
				});
				el.addEvent('blur', function() {
					if(el.get('value') == '') {
						el.set('value',el.get('title'));
						el.addClass('hint');
					}
				});
			}
		});
	}
});



window.addEvent('domready',function() {
	/*
	document.body.addEvent('contextmenu',function(e) {
      e.stop();
    });
    
*/
	$('greyout').addEvent('click',function(e) {
		e.stop();
		$('display').setStyles({
			'display':'none',
			'left':'25%',
			'top':'25%',
			'width':'50%',
			'height':'50%'
		});
		$('display').set('html','');
		this.setStyle('display','none');
	});
	$('display').addEvent('click',function(e) {
		e.stop();
		$('greyout').setStyle('display','none');
		$('display').set('html','');
		this.setStyles({
			'display':'none',
			'left':'25%',
			'top':'25%',
			'width':'50%',
			'height':'50%'
		});
	});
	$$('#home li a img').each(function(img,i) {
		img.addEvent('click',function(e) {
			e.stop();
			loadpic(this.getParent().get('title'));
		});
	});
	$$('#header li').each(function(cat) {
		var over = new Fx.Morph(cat, {duration: 500,link:'cancel', transition: Fx.Transitions.Sine.easeOut});
		cat.addEvents({
			'mouseenter': function() {
				if (!cat.hasClass('sel')) over.start({'background-color':'#333','color':'#ccc'});
			},
			'mouseleave': function() {
				if (!cat.hasClass('sel')) over.start({'background-color':'#222','color':'#666'});
			},
			'click':function(e) {
				e.stop();
				if (!cat.hasClass('sel')) {
					$$('.sel').removeClass('sel');
					this.removeProperty('style');
					this.addClass('sel');
					over.cancel();
					if (cat.get('id')=='contact') {
						var thumbs = new Fx.Morph($('home'));
						thumbs.start({'opacity':0}).chain(function(){
							$('thumbs').setStyle('display','none');
							var req = new Request.HTML({
								'url':'_contact.php',
								'update':$('home'),
								'evalScripts':true,
								evalResponse:true,
								onComplete: function(){
									thumbs.start({'opacity':1});
								}
							}).send();
						});
						
					} else {
						loadgallery(this.getFirst('a').get('href').split('=')[1]);
					}
				}
			}
		});

	});
	
	
	

	
	
});
