Core={
	image:$('#bg img'),
	float:$('#float'),
	head:$('#head').hide(),
	cont:$('#cont'),
	win:$(window),

	top:function(){try{window.scrollTo(0,0);document.scrollLeft=0;document.scrollTop=0}catch(e){}},
	open:function(){if(this.getAttribute('href',2).replace(/:\/\/(www\.)?trondelagsutstillingen/gi,'').indexOf('://')>0)return !window.open(this.href)},
	input:function(e){var i=$(this),v=i.val(),t=this.title;i.val(e.type=='blur'?v||i.addClass('empty')&&t:(i.removeClass('unvalid empty').val()==t?'':v))},
	validate:function(fail){
		if(fail=$.grep($('input:text.required,textarea.required,select[title].required',this),function(el){
			return (el=$(el))[!el.val()||el.val()==el.attr('title')?'addClass':'removeClass']('unvalid').hasClass('unvalid');
		}).length)alert('Alle påkrevde felter må fylles ut med gyldig info.');
		else $('input:text,textarea').each(function(i,el){if(el.value==el.title)el.value=''});
		return !fail;
	},
	
	resize:function(v){
		var w=Core.win.width(),h=Core.win.height(),dim;
		if(!Core.dim)Core.dim={width:parseInt((dim=Core.image.attr('alt').split('x'))[0]),height:parseInt(dim[1])};
		if((w!=Core.width||h!=Core.height)&&(Core.width=w)&&(Core.height=h)){
			var width=Math.round(h*(Core.dim.width/Core.dim.height));
			Core.image.css({width:(Core.dim.width>Core.dim.height&&w>width)?'50%':width,height:'auto'});
		}
	},
	tip:function(e){
		var text=Core.tips[e.target.tagName.toLowerCase()]||Core.tips[Core.menued?'close':'open'];
		if(Core.cont[0]==e.target||$.contains(Core.cont[0],e.target))text='';
		return Core.text==text?Core.float:Core.float.html(Core.text=text);
	},
	tips:{
		'a':'åpne link',
		'button':'send skjema',
		'input':'skriv her',
		'textarea':'skriv her',
		'select':'velg',
		'close':'lukk meny',
		'open':'klikk for meny'
	}
}

$(window).resize(Core.resize);
$('#cont a').live('click',Core.open);
$('form').submit(Core.validate).find('input:text,textarea').bind('focus blur',Core.input).blur();
$('body').mousemove(function(e){Core.tip(e).offset({top:e.clientY+15,left:e.clientX+15})}).click(function(e){
	Core.menued=Core.cont[0]!=e.target&&!$.contains(Core.cont[0],e.target)&&Core.tip(e).html().charAt(0)=='k';
	Core.head.animate({top:e.clientY-5,left:e.clientX-5,height:Core.menued?'show':'hide'},200);
});

setTimeout(Core.top,500);
