// JavaScript Document
//var root='http://'+((document.location.href.split('http://')[1].split('.')[0]=='www')? 'www.': '')+'lesley-moore.nl'
var swf_v=parseInt(Browser.Plugins.Flash.version);
var f=1;
var urls;
var dom=false;
var sp_logos=[];
var town_nr;
var last;
var lmMachine;
var lmTown;
var _mrg=15;
//logos=logos.reverse();

var  loop=clearRobo.periodical('25');
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);

function handleChange(event) {
    var tit='LESLEY MOORE';
    urls=String(event.path).split('/');
    urls=urls.filter(function(item){
	if(item && item !='')return item;}
    );
    if(urls.length)tit+=' - '+urls[0]+' {'+urls[1]+'}';
    if(/*urls.length>1 &&*/ $('kol') && $('kol').buts)showTarget(urls);
    SWFAddress.setTitle(tit);
    
}

function showTarget(urls){
	if(urls.length){
	    show_home_pic('hide');
	    $('kol').buts.each(function(item,n){
		if(String(item.url).toUpperCase()==String(urls[0]).toUpperCase()){
		    item.msk.getElement('.sub').getElements('.mn').each(function(item){
			if(String(item).split((root+path))[1].toUpperCase()==('/'+urls.join('/').toUpperCase())){
			    item.show();
			    if($('kol').act>-1 && $('kol').act !=n ){
				    $('kol').buts[$('kol').act].act=false;
				    $('kol').buts[$('kol').act].hide();
			    }
			}
		    });
		    $('kol').act=n;
		}
	    });
	}else{
	   show_home_pic('show');
	}
	
}



function clearRobo(){
    if($('robo')){
	$('robo').dispose();
	$clear(loop);
	c=new Request({		   
		url: path+"/cont.php",
		onSuccess: function(text){
			$('kol').set('html',text);
			init();
		},
		headers:{'Content-type': 'application/x-www-form-urlencoded; charset=utf-8'}
	}).send();
    }
}



function init(){
    $('home_pg').swf=false;
    var kol=$('kol');
    var but=$('but').getElement('a');
    var act=kol.getElement('.act');
    but.addEvent('mouseover',function(){
	act.sh();
    });
    but.addEvent('click',function(e){
	new Event(e).stop()
	SWFAddress.setValue(String(this).split((root+path))[1]);
    });
    kol.addEvent('mouseleave',function(){
	act.hd();
    });
    act.fx=new Fx.Tween(act,{'duration':'700','onComplete':function(ob){
	if(parseInt(ob.getStyle('height'))==0){
	    $('kol').setStyle('height','80px');
	}
    }});
    act.sh=function(){
	$('kol').setStyle('height','100%');
	this.fx.start('height',kol.getSize().y+'px')
    }
    act.hd=function(){this.fx.start('height','0')}
    kol.act=-1;
    kol.buts=kol.getElements('h3 .mn');
    kol.buts.each(function(item,n){
	item.nr=n;
	item.act=false;
	item.url=item.getElement('input').value;
	item.msk=item.getParent().getNext('.msk');
	item.msk._h=item.msk.getElement('.sub').getSize().y;
	item.addEvent('click',function(e){
	    new Event(e).stop();
	    this.show();
	 })
	item.show=function(){
	    if(!this.act){
		this.act=true;
		if(kol.act>-1){
		    kol.buts[kol.act].hide();
		    kol.buts[kol.act].act=false;
		}
		this.setStyles({'font-weight':'bold', 'cursor':'pointer'});
		this.msk.show(  this.msk._h);
		kol.act=this.nr;
		/*if(!this.msk.getElement('.mn')){
		    $('view').empty();
		    showSwf('town');
		}*/
	    }
	}
	item.hide=function(){
	    item.msk.hide();
	    this.setStyle('font-weight','normal');
	}
	//$('kol').setStyle('height','101%')$('kol').setStyle('height','100%')
	item.addEvent('mouseover',function(){if(!this.act){this.setStyle('font-weight','bold'); }})
	item.addEvent('mouseout',function(){if(!this.act){this.setStyle('font-weight','normal');}})
	var sub=item.msk.getElement('.sub');
	sub.act=-1;
	sub.buts=sub.getElements('.mn');
	sub.buts.each(function(item,n){
	    item.nr=n;
	    item.swf=false;
	    item.imgs=[];
	    item.act=false;
	    item.msk=item.getNext('.msk');
	    var p=item.msk.getElement('p');
	    var u=item.msk.getElement('u');
	    item.msk._h=((p)? p.getSize().y : 0)+((u)? u.getSize().y : 0);
	    var inp=item.msk.getElement('input');
	    if(inp){
		if(inp.hasClass('swf')){
		    item.swf=inp.value;
		    if(item.swf=='logos')lmArch=item;//town_nr=n;
		    if(item.swf=='logo')lmMachine=item;
		}else{
		    item.msk.getElements('input').each(function(_inp){
			var tmp=_inp.value.split(':');
			item.imgs.push({'src':tmp[0],'w':parseInt(tmp[1]),'h':parseInt(tmp[2]),'txt':tmp[3],'still':tmp[4],'typ':((_inp.hasClass('flv'))? 'flv' : 'img')});
		    });
		}
	    }
	    
	    item.addEvent('click',function(e){
		new Event(e).stop();
		SWFAddress.setValue(String(this).split((root+path))[1].toUpperCase());
		//this.show();
	     })
	    item.show=function(){
		if(!this.act){
		    this.act=true;
		    var sub=this.getParent();
		    if(sub.act>-1)sub.buts[sub.act].hide();
		    //this.setStyle('color','#f9f');
		    this.msk.show(this.msk._h);
		    var pr=this.getParent().getParent();
		    pr.show(this.msk._h+pr._h);
		    sub.act=this.nr;
		    
		    if($('town')){
			$('town').dispose();
			window.onscroll=null;
		    }
		    if(this.swf && this.swf!='town'){
			$('view').empty();
			showSwf(this.swf);
		    }else if(this.imgs.length){
			$('view').empty();
			if(this.msk.getElement('.hid_proj')){
			    show_hid_proj(this.msk,this.imgs);
			}else{
			    showImgs(this.imgs);
			}
		    }
		    var sel=this;
		    if(sub.act>-1){
			sub.buts.each(function(item){
			    if(item!=sel){
				item._reset();
			    }
			});
		    }
		}
	    }
	    item.hide=function(){
		item.msk.hide();
		//this.setStyle('color','#fff');
	    }
	    item._over=item.getElement('._over').value;
	    item._up=item.get('text');
	    item.addEvent('mouseover',function(){
		if(!this.act){
		    //this.setStyle('color','#f9f');
		    if(this._over!='')this.set('text','{ '+this._over+' }');
		}
	    });
	    item.addEvent('mouseout',function(){
		if(!this.act){
		    this._reset();
		}
	    });
	    item._reset=function(){
		this.act=false;
		//this.setStyle('color','#fff');
		if(this._over!='')this.set('text',this._up);
	    }
	});
    });
    kol.getElements('.msk').each(function(item){
	item.fx=new Fx.Tween(item,{'onComplete':
	    function(){
	        if(this.fx.st=='hd'){
		    this.getElements('.mn').each(function(item){
			if(item.act)item._reset();
		    });
		    var sub=this.getElement('.sub');
		    if(sub)sub.act=-1;
		    this.getElements('.msk').setStyle('height','0px');
		}
	    }.bind(item)
	});
	item.show=function(h){
	    this.fx.st='sh';
	    this.fx.start('height',h);
	}
	item.hide=function(){
	    this.fx.st='hd';
	    this.fx.start('height',0);
	}
    });
    $('logoNav').act=-1;
    $('logoNav').buts=$('logoNav').getElements('a');
    $('logoNav').setButs=function(){
	$('logoNav').buts.each(function(item,n){
	    item.act=false;
	    item.nr=n;
	    item.addEvent('click',function(e){
		new Event(e).stop();
		this.show()
	    });
	    //item.addEvent('mouseover',function(){if(!this.act){this.setStyle('color','#f9f');}});
	    item.addEvent('mouseout',function(){if(!this.act){this._reset();}});
	    item._reset=function(){
		this.act=false;
		//this.setStyle('color','#fff');
	    }
	    
	    item.show=function(){
		if(!this.act && $('logoNav').act!=this.nr){
		    this.act=true;
		    //this.setStyle('color','#f9f');
		    $('view').empty();
		    if($('logoNav').act>-1)$('logoNav').buts[$('logoNav').act]._reset();
		    $('logoNav').act=this.nr;
		    sp_logos=logos.slice((this.nr*450),(this.nr*450)+450);
		    sp_logos.n=0;
		    sp_logos.mx=Math.ceil(sp_logos.length/30);
		    shLogos();
		    if(window.getScroll().x)var myFx = new Fx.Scroll(window).toLeft();
		}
	    }
	});
    }
    $('logoNav').setButs();
    snap();
    window.addEvent('resize',snap);
    //if(urls.length>1){
	showTarget(urls);
	//}
}

function showSwf(typ){
        
    switch(typ){
	case 'logo':
	    var ph=new Element('div',{'class':'ph l', 'styles':{'width':'600px','height':'450px', 'margin-left':'300px'}});
	    $('view').grab(ph);
	    swf = new Swiff(path+'/inc/logoM.swf', {
	    container:ph,				
	    id: 'swiffer',
	    width: '100%',
	    height:'100%',
	    params: {
		    wMode:  'window',
		    bgcolor: '#e3e3e3',
		    scale:'noscale',
		    salign: 'lt'
	    }
	    
	});
	ph.setStyle('border','none');
	    if(window.getScroll().x)var myFx = new Fx.Scroll(window).toLeft();
	break;
	case 'logos':
	    if($('logoNav').act>-1){
		var act=$('logoNav').act;
		$('logoNav').act=-1;
		$('logoNav').buts[act]._reset();
		$('logoNav').buts[act].show();
	    }else{
		$('logoNav').buts[0].show();
	    }
	break;
	default://
	    var ph=new Element('div',{'id':'town','styles':{'width':((f>1)? Math.round(2000*f)+'px' : '2000px'),'height':((f>1)? Math.round(480*f)+'px' :'480px')}});
	    ph.ld=false;
	    var ww=window.getSize().x;
	    ph.inject($('view'),'before');//
	    swf = new Swiff(path+'/inc/town.swf', {
		container:ph,				
		id: 'swiffer',
		width: '100%',
		height:'100%',
		params: {
		    wMode:  'opague',
		    bgcolor: '#e3e3e3',
		    scale: 'scale',
		    salign: 'lt'
		},
		vars: {
		    'l_count':logos.length,
		    'f': $('kol').getSize().y/600
		}/*,
		callBacks:{
		    'swiffload':function(w){
			alert(w);
			$('view').setStyles({'width':'1973px','height':'100px','display':'block'});
			if($('town').ld){
			    //setScroll();
			}else{
			    $('town').ld=true;
			}
		    }		
		}*/
	    });
	    var hd=new Element('div',{'class':'hd'});
	    hd.set('text','~ LM Town, logo made '+last+' by visitor ~ ');
	    var a=new Element('a');
	    a.set('html','~ Make a logo and change the future ~');
	    a.addEvent('click',function(e){
		new Event(e).stop();
		lmMachine.show();
	    })
	    hd.grab(a);
	    ph.grab(hd);
	    if(window.getScroll().x){
		var myFx = new Fx.Scroll(window,{'onComplete':function(){
		 if($('town').ld){
			    setScroll();
			}else{
			    $('town').ld=true;
			}
		 }}).toLeft();
	    }else{
		 $('town').ld=true;
	    }
	break;
    }

}

function _swiffLoad(w){
    $('view').tw=w;
    $('view').setStyles({'height':'100px','display':'block'});
    snap();
    if($('town').ld){
	setScroll();
    }else{
	$('town').ld=true;
    }
}

function setScroll(){
    window.onscroll=function(){
	Swiff.remote($('swiffer'), 'jsMv',parseInt(window.getScroll().x)/((f>1)? f : 1),window.getSize().x,((f>1)? f : 1));
    };
}


function addLogo(logo_id, logo_dat){
    last=logo_dat;
    logos.unshift(logo_id);
    updateArch();
    sw2town.delay(500);
}

function sw2town(){
    lmArch.show();
}

function updateArch(){
    var nav=$('logoNav').getElements('a');
    if(Math.ceil(logos.length/450)==nav.length){
	var a=new Element('a');
	$('logoNav').grab(a);
	$('logoNav').setButs();
	var nav=$('logoNav').getElements('a');
    }
    var c=logos.length;
    nav.each(function(item,n){
	var s=c;
	c=((c>450)? (c-450) : 0);
	item.set('text',s+' - '+c);
    });
}

function showImgs(imgs){
    var m_h=$('kol').getSize().y;
    f=m_h/600;
    var mrg=((f>1)? Math.round(_mrg*f) : _mrg);
    imgs.each(function(item,n){
	if(item.typ=='flv'){
	    var _w=((f>1)? Math.round(384*f) : 384);
	    var _h=((f>1)? Math.round(288*f) : 288);
	}else{
	    var _w=((f>1)? Math.round(item.w*f) : item.w);
	    var _h=((f>1)? Math.round(item.h*f) : item.h);
	}
	var ph=new Element('div',{'class':'ph l', 'styles':{'width':_w+'px','height':_h+'px','margin-right':mrg}});
	ph.typ=item.typ;
	ph._w=item.w;
	ph._h=item.h;
	if(item.txt){
	    var hd=new Element('div',{'class':'hd'});
	    hd.set('text',item.txt);
	    ph.grab(hd);
	}
	var off=((n)? parseInt($('view').getStyle('width')): 30);
	$('view').setStyle('width',(off+parseInt(_w)+mrg)+'px');
	$('view').grab(ph);
    });
    ldImgs(0,imgs);
}

function show_hid_proj(ob,imgs){
    var projs=ob.getElements('.hid_proj');
    var m_h=$('kol').getSize().y;
    f=1;//m_h/600;
    var ff=(((f-1)*0.2)+1);
    var mrg=((f>1)? Math.round(_mrg*ff) : _mrg);
    var p=0;
    var off=0;
    projs.each(function(proj){
	var pro=new Element('div',{'class':'pro l', 'styles':{'height':((f>1)? 450*ff : 450)+'px' }});
	var w=0;
	proj.getElements('input').each(function(inp){
	    var item=imgs[p];
	    var _w=((f>1)? Math.round(326*ff) : 326);
	    var _h=((f>1)? Math.round(284*ff) : 284);
	    var ph=new Element('div',{'class':'ph l', 'styles':{'width':_w+'px','height':_h+'px','margin':0,'margin-left': mrg}});
	    ph.typ=item.typ;
	    ph._w=326;
	    ph._h=284;
	    if(item.txt){
		var hd=new Element('div',{'class':'hd'});
		hd.set('text',item.txt);
		ph.grab(hd);
	    }
	    pro.grab(ph);
	    p++;
	    w+=_w+2+mrg;
	});
	pro.setStyle('width',w+mrg+'px');
	var txt=new Element('p',{'class':'l','styles':{'margin-left': mrg}});
	txt.set('html',proj.get('html'));
	pro.grab(txt);
	off=((off)? parseInt($('view').getStyle('width')): 30);
	$('view').setStyle('width',(off+w+mrg+2)+'px');
	$('view').grab(pro);
    })
    ldImgs(0,imgs);
   /* imgs.each(function(item,n){
	
	    var _w=((f>1)? Math.round(item.w*f) : item.w);
	    var _h=((f>1)? Math.round(item.h*f) : item.h);
	var ph=new Element('div',{'class':'ph l', 'styles':{'width':_w+'px','height':_h+'px','margin-right':mrg}});
	ph.typ=item.typ;
	ph._w=item.w;
	ph._h=item.h;
	if(item.txt){
	    var hd=new Element('div',{'class':'hd'});
	    hd.set('text',item.txt);
	    ph.grab(hd);
	}
	var off=((n)? parseInt($('view').getStyle('width')): 30);
	$('view').setStyle('width',(off+parseInt(_w)+mrg)+'px');
	$('view').grab(ph);
    });
    ldImgs(0,imgs);*/
}

function ldImgs(n,imgs){
    var ph=$('view').getElements('.ph')[n];
    if(imgs[n].typ=='flv'){//path+
	swf = new Swiff('/inc/FLV_player'+((swf_v>7)? '_f' : '')+'.swf', {
	    container:ph,				
	    id: 'swiffer'+n,
	    width: ((swf_v>7)? ((f>1)? Math.round(imgs[n].w*f) : imgs[n].w) : '100%'),
	    height:((swf_v>7)? 30+((f>1)? Math.round(imgs[n].h*f): imgs[n].h): '100%'),
	    params: {
		    wMode:  'opaque',
		    bgcolor: '#ffffff',
		    scale:((swf_v>7)? 'noscale' : 'scale'),
		    salign: 'lt'
	    },
	    vars: {//path+
		    'flv':imgs[n].src,
		    'still':imgs[n].still,
		    'f':f,
		    'w':imgs[n].w,
		    'h':imgs[n].h
	    }
	});
	ph.setStyle('border','none');
	if(n<imgs.length-1){
	    if(!n){
		if(window.getScroll().x)var myFx = new Fx.Scroll(window).toLeft();
	    }
	    ldImgs(++n,imgs);
	}
    }else{//path+
	var im = new Asset.image(imgs[n].src,{/*'width':'100%','height':'100%',*/
		onload:function(){
		    if(ph.getParent().hasClass('pro')){
			var tw=((imgs[n].w>imgs[n].h)? 328 : imgs[n].w*(286/imgs[n].h));
			var th=((imgs[n].h>imgs[n].w)? 284 : imgs[n].h*(328/imgs[n].w));
			var _w=((f>1)? Math.round(tw*f) : tw);
			var _h=((f>1)? Math.round(th*f) : th);
			var ph2=new Element('div',{'class':'ph2', 'styles':{'width':_w+'px','height':_h+'px'}});
			ph2._w=tw;
			ph2._h=th;
			ph2.grab(this);
			ph.grab(ph2);
		    }else{
			ph.grab(this);
		    }
		    
		    ph.setStyle('border','none');
		    if(n<imgs.length-1){
			if(!n){
			    if(window.getScroll().x)var myFx = new Fx.Scroll(window).toLeft()
			}
			ldImgs(++n,imgs);
			
		    }
		}
	});
    }
}

function snap(){
    var m_h=$('kol').getSize().y;
    f=m_h/600;
    var ph_s=$('view').getElements('.ph');
    var logo_s=$('view').getElements('.logos');
    var w=0;
    if(ph_s){
	var mrg=((f>1)? Math.round(_mrg*f) : _mrg);
	var projs=$('view').getElements('.pro');
	var ff=(((f-1)*0.1)+1);
	if(projs.length){
	   /* projs.each(function(pro){
		var pw=0;
		pro.getElements('.ph').each(function(item){
		    
		    var _w=((f>1)? Math.round(326*ff) : 326);
		    var _h=((f>1)? Math.round(284*ff) : 284);
		    pw+=(mrg+_w);
		    item.setStyles({'width':_w+'px','height':(_h+30)+'px','margin-left':mrg});
		    /*if(item.typ=='flv' && swf_v>7){
			$('swiffer'+n).width=_w;
			$('swiffer'+n).height=30+_h;
			Swiff.remote($('swiffer'+n), 'jsScale',_w,_h);
		    }*/
		    /*var ph2=item.getElement('.ph2');
		    var _w=((f>1)? Math.round(ph2._w*ff) : ph2._w);
		    var _h=((f>1)? Math.round(ph2._h*ff) : ph2._h);
		    ph2.setStyles({'width':_w+'px','height':_h+'px'});
		});
		pro.getElement('p').setStyle('margin-left',mrg);
		pro.setStyles({'width':(pw+mrg)+'px','height':((f>1)? 450*ff : 450)+'px'});
		w+=mrg+pw;
	    })*/
	}else{
	    ph_s.each(function(item,n){
		var _w=((f>1)? Math.round(item._w*f) : parseInt(item._w));
		var _h=((f>1)? Math.round(item._h*f) : parseInt(item._h));
		w+=(mrg+_w);
		item.setStyles({'width':_w+'px','height':(_h+30)+'px','margin-right':mrg});
		if(item.typ=='flv' && swf_v>7){
		    $('swiffer'+n).width=parseInt(_w);
		    $('swiffer'+n).height=30+parseInt(_h);
		    Swiff.remote($('swiffer'+n), 'jsScale',_w,_h);
		}
		
	    });
	    $('view').setStyles({'width':(w+mrg)+'px'});
	}
	//'top':((f>1)? Math.round(80*f) : 80),
    }
    if(logo_s.length){
	w=30;
	logo_s.each(function(item,n){
	    
	     var _w=((f>1)? Math.round(1200*f) : 1200);
	     var _h=((f>1)? Math.round(385*f) : 385);
	     w+=_w;
	    item.setStyles({'width':_w+'px','height':_h+'px'});
	});
	$('view').setStyles({'width':w+'px'});//'top':((f>1)? Math.round(80*f) : 80)+'px',
    }
    
    if($('town')){
	var _w=((f>1)? Math.round($('view').tw*f +Math.round((280*f)-280)) : $('view').tw);
	var _h=((f>1)? Math.round(480*f) : 480);
	$('town').setStyles({'width':((f>1)? Math.round(2000*f) : 2000)+'px','height':_h+'px'});
	$('view').setStyles({'width':_w+'px'});//_w-((f>1)? Math.round(280*f) : 280)'top':((f>1)? Math.round(80*f) : 80)+'px',
	Swiff.remote($('swiffer'), 'jsMv',parseInt(window.getScroll().x)/((f>1)? f : 1),window.getSize().x,((f>1)? f : 1));
    }
}

function shLogos(){
   if(sp_logos.n<sp_logos.mx){
    var tmp=((sp_logos.length>=30)? sp_logos.splice(0,30) : sp_logos);
    var _w=((f>1)? Math.round(1200*f) : 1200);
    var _h=((f>1)? Math.round(385*f) : 385);
    $("view").setStyle('width',((sp_logos.n)? parseInt($("view").getStyle('width')) : 0)+_w+32+'px');
    var div=new Element('div',
	{'class':'logos l',
	'styles':{
	    'width':_w+'px',
	    'height':_h+'px',
	    'background':'#fff',
	    'padding':((sp_logos.n)? '30px 0 0 0' : '30px 0 0 30px')
	    }
	});
    $('view').grab(div);
    swf = new Swiff(path+'/inc/logos.swf', {
	container:div,				
	id: 'swiffer'+logos.n,
	width: '100%',
	height:'100%',
	params: {
		wMode:  'opaque',
		bgcolor: '#fff',
		scale: 'scale',
		salign: 'lt'
	},
	vars: {
	    'logos':tmp.join(':')
	}
    });
    sp_logos.n++;
   }
}

function show_home_pic(typ){
    if(typ=='hide'){
	$('home_pg').setStyle('display','none');
    }else{
	$('view').empty();
	if(!$('home_pg').swf){
	
		var vals=JSON.decode(String($('home_pg').get('text')));
	    
		var params = {'menu': 'false','salign':'tl','wmode':'transparent','bgcolor':'#ffffff', 'allowScriptAccess':'allways'};
		var flashvars = {img:vals['img'],w:vals['w'],h:vals['h'], full:vals['full']}; 
		var home=$(new Swiff(path+'/inc/home.swf', {//?nocache=+(Math.random()*9000)
			container:$('home_pg'),				
			id: '',
			width: '100%',
			height:'100%',
			params: params,
			vars:flashvars, 
			callBacks:{
			    'swiff_load':function(msg){
			    }
			}
		    
		}));
		$('home_pg').swf=true;
	}
	$('home_pg').setStyle('display','inline');
    }
}

