// JavaScript Document
document.observe("dom:loaded",function(){
	initTheatre();
	initLogobox();
	initGallery();
	initNews();
	arrLogos = new Array
	arrInactive = new Array
	arrActive = new Array
});

function initTheatre(){
	if ($('slides')!=undefined){
		$('slides')._slidecount=$$('#slides-child img').size();
		$('slides-child').setStyle({width:(parseInt($('slides')._slidecount)*648)+'px'})
		$('slides')._current=0;
		$('slides')._running=true;
		$('slides')._pe=new PeriodicalExecuter(function(){slideTheatre()},5);
	}
	if ($('controls')!=undefined){
		$$('#control-previous a')[0].observe('click',function(e){Event.stop(e);stopstartTheatre(true);slideTheatre(-1)})
		$$('#control-pause a')[0].observe('click',function(e){Event.stop(e);stopstartTheatre()})
		$$('#control-next a')[0].observe('click',function(e){Event.stop(e);stopstartTheatre(true);slideTheatre()})
	}
}
function slideTheatre(direction){
	if (direction===undefined){direction=1;}
	$('slides')._current=$('slides')._current+direction;
	if ($('slides')._current<0){$('slides')._current=$('slides')._slidecount-1}
	if ($('slides')._current>$('slides')._slidecount-1){$('slides')._current=0}
	new Effect.Move('slides-child',{x:0-(648*$('slides')._current),mode:'absolute',queue:'end'})
}
function stopstartTheatre(forcepause){
	if($('slides')._running||forcepause==true){
		$('slides')._pe.stop();
		$('slides')._running=false;
		$('control-pause').addClassName('paused')
	}else{
		$('slides')._pe=new PeriodicalExecuter(function(){slideTheatre()},5);
		$('slides')._running=true;
		$('control-pause').removeClassName('paused')
	}
}
function initLogobox(){
	if ($('logobox')!=undefined){
		new Ajax.Request('/ajax/gettiles.asp', {onSuccess: function(transport){populateLogobox(transport)}});
	}
}
function initNews(){
	if ($('newslinks')!=undefined){
		new Ajax.Updater('newslinks','/ajax/getnewslinks.asp');
	}
}
function populateLogobox(transport){
	arrLogos = transport.responseText.split('|')
	for (i=1;i<=arrLogos.size();i++){
		arrLogos[i-1]=arrLogos[i-1].evalJSON()
		arrInactive.push(i)
		arrLogos[i-1].image = new Image
		arrLogos[i-1].image.src = "/images/brands/tiles/"+arrLogos[i-1].strFilenameTile
	}
	tilerotator = new PeriodicalExecuter(rotatetile,2)
	$$('#logobox div').each(function(pTile){
								var newtile=arrInactive[Math.floor(Math.random()*arrInactive.size())]
								arrActive.push(newtile)
								arrInactive=arrInactive.without(newtile)
								$(pTile).update('<a href="'+arrLogos[newtile-1].url+'"><img src="/images/brands/tiles/'+arrLogos[newtile-1].strFilenameTile+'" title="'+arrLogos[newtile-1].strName+'" border="0"></a>')
								$(pTile)._value=newtile
	})
}
function rotatetile(){
	if (arrInactive.size()>0){
		var randomtile=$('logobox')._lasttile
		while (randomtile==$('logobox')._lasttile){
			randomtile=Math.floor(Math.random()*parseInt($$('#logobox div').size()))
		}
		$('logobox')._lasttile=randomtile
		var newtile=arrInactive[Math.floor(Math.random()*arrInactive.size())]
		arrInactive=arrInactive.without(newtile)
		arrActive=arrActive.without($$('#logobox div')[randomtile]._value)
		arrInactive.push($$('#logobox div')[randomtile]._value)
		arrActive.push(newtile)
		$$('#logobox div')[randomtile]._value=newtile
		flashtile($$('#logobox div')[randomtile])
	}
}
function flashtile(iTile){
	iTile=$(iTile).identify()
	oldImg=$(iTile).down('a').identify()
	$(iTile).insert({top:'<a href="'+arrLogos[$(iTile)._value-1].url+'"><img src="/images/brands/tiles/'+arrLogos[$(iTile)._value-1].strFilenameTile+'" title="'+arrLogos[$(iTile)._value-1].strName+'" border="0"></a>'})
	new Effect.Fade(oldImg,{duration:0.5,afterFinish:function(){$(oldImg).remove();}})
}
function initGallery(){
	if ($('gallerytiles')!=undefined){
		$$('#gallerytiles img').each(function(thumb){thumb.observe('click',function(e){swapImage(Event.element(e))})})
		$('placeholder').src=$$('#gallerytiles img')[0].src.replace("/thumbs","")
		$$('#gallerytiles img').invoke('setOpacity',0.5)
		$$('#gallerytiles img')[0].setOpacity(1)
		$('gallerytiles').observe('mouseover',function(){$('gallerytiles').observe('mousemove',slidetiles)})
		$('gallerytiles').observe('mouseout',function(){$('gallerytiles').stopObserving('mousemove',slidetiles)})
	}
}
function swapImage(thumb){
	strSrc=$(thumb).src.replace("/thumbs","")
	if ($('placeholder').readAttribute('src')!=strSrc){
		$$('#gallerytiles img').invoke('setOpacity',0.5)
		$(thumb).setOpacity(1)
		$('loader').src=strSrc
		$(thumb).setStyle({cursor:'progress'})
		$('loader').observe('load',function(){
												 $(thumb).setStyle({cursor:''})
												 new Effect.Opacity('placeholder',{from:1,to:0,duration:0.3})
												 Element.writeAttribute.delay(0.3,'placeholder',{src:strSrc})
												 new Effect.Opacity('placeholder',{from:0,to:1,duration:0.3,delay:0.3})
		})
	}
}
function initMap(){
	var marker;
	var map;
	var geocoder;
	var address;
	var html="<h4 style=\"margin:0;\">Licensing Management International Ltd</h4>6 Lymington Enterprise Centre,<br/>Ampress Lane,<br/>Lymington,<br/>Hampshire SO41 8LZ<br/>UK<br /><strong>Tel: 01425 403430</strong>";
		   
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map_canvas"));
	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(50.770532,-1.546242), 15);
	marker = new GMarker(new GLatLng(50.770532,-1.546242));
	map.addOverlay(marker);
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();
	marker.openInfoWindowHtml(html);
	GEvent.addListener(marker,"click", function(){marker.openInfoWindowHtml(html);});
	}
}
function initContact(){
	initMap();
	contactValidation = new Validation('frmContact');
	$('frmContact').observe('submit',contactSend)
}

function contactSend(e){
	if (contactValidation.validate()){
		$('btnSend').hide()
		$('contactAjaxLoader').show()
		var strBody=$F('strMessage')
		new Ajax.Request("/ajax/sendenquiry.asp",{method:'post',parameters:$('frmContact').serialize(true),
					 onSuccess: function(transport){
						 try{pageTracker._trackEvent('Contact Form','Sent',$F('strName'))}
						 catch(err){};
						 var response=transport.responseText.strip() || "no response";
						 if (response=="no response"){
							 $('frmContact').update('<h4><abbr title="(f) '+response.stripTags().strip()+'">Oops.</abbr></h4>There was a problem submitting the form. Sorry about that... you might want to <a href="mailto:enquiries@lmiuk.com">email us</a> instead.')
						 }else{
							 $('frmContact').update(response)
							 var _gaq = _gaq || [];
							 _gaq.push(['_trackEvent', 'Contact Form', 'Enquiry']);
						 }
					 },
						 onFailure: function(transport){
						 var response=transport.responseText.strip() || "no response";
							 $('frmContact').update('<h4><abbr title="(f) '+response.stripTags().strip()+'">Oops.</abbr></h4>There was a problem submitting the form. Sorry about that... you might want to <a href="mailto:enquiries@lmiuk.com?body='+encodeURI(strBody)+'">email us</a> instead.')
						 }
					});
	}
	if(e){
		Event.stop(e)
	}
}
function slidetiles(e){
	parentWidth=$('gallerytiles').getWidth()
	childWidth=$('gallerytileschild').down('table').getWidth()
	if (childWidth>parentWidth){
		offsetRange=childWidth-parentWidth
		posPointer=Event.pointerX(e)
		posContainer=$('gallerytiles').cumulativeOffset().left
		posOffset=posPointer-posContainer
		posOffsetPercent=posOffset/parentWidth
		posOffsetPercent=Effect.Transitions.sinoidal(posOffsetPercent)
		newPosition=0-offsetRange*posOffsetPercent
		$('gallerytileschild').setStyle({left:newPosition+'px'})
	}
}

