// JavaScript Document

// document.id function missing in Fx.Scroll - added here
document.id = function(id){ return $(id); }



// rollover - powers rollover effect on nav
var rollover = {
	/*colors : {
		'home' : '#a186be',
		'register' : '#bd8cbf',
		'contact' : '#f49ac1',
		'store-locator' : '#f5989d'
	},*/
	
	colors : {
		'home' : '#ffffff',
		'register' : '#ffffff',
		'contact' : '#ffffff',
		'store-locator' : '#ffffff'
	},
	
	init: function(){
		$$('#nav li a').each( function(el){
			el.addEvent('mouseover', function(){
				this.set('morph', {duration:100});
				this.morph({
					'background-color' : '#ffffff',
					'color' : '#000000'
				});
			});
			el.addEvent('mouseout', function(){
				this.set('morph', {duration:1000});
				this.morph({
					'background-color' : '#000000',
					'color' : '#ffffff'
				});
			});
			el.addEvent('click', function(){
                               clearForm();
				RM.loadPage.delay(100);
				rollover.init.delay(200);
			});
		});
	}
}

// RM - contains most of the page logic
	
var RM = {

	currentPage : 'home',
	navHTML : '<li id="nav-home"><a href="#">home</a></li><li id="nav-philosophy"><a href="#philosophy">philosophy</a></li><li id="nav-collection"><a href="#collection">collection</a></li><li id="nav-store-locator"><a href="#store-locator">stockists</a></li><li id="nav-contact"><a href="#contact">contact</a></li><li id="nav-register"><a href="#register">register</a></li>',
	scroller : '',
	section : false,
	subsection : false,
	subsubsection : false,
	page : '',
	lastH : false,
	fadeDelay : 0,
        videos : [ 'assets/RM4.flv', 'assets/bora dress2_FL8_Widescreen_960x540_RM.flv', 'assets/maestro_dress2_FL8_Widescreen_960x540_RM.flv', 'assets/scirroco dress2_FL8_Widescreen_960x540_RM.flv', 'assets/traversa_dress2_FL8_Widescreen_960x540_RM.flv' ],

	loadPage: function(){
		var hash = location.hash || '#home';
		var page, section;
		if( hash.contains('/') ){
			hash = hash.split('/');
			page = hash[0];
			RM.section = hash[1];
			if( hash.length > 2 ) RM.subsection = hash[2];
			if( hash.length > 3 ) RM.subsubsection = hash[3];
		}else{
			page = hash;
			RM.section = false;
			RM.subsection = false;
			RM.subsubsection = false;
		}
		RM.page = page;
		var o = $(RM.currentPage + '-content');
		RM.currentPage = page.replace('#', '');
		RM.updateNav();
		var id = RM.currentPage + '-content';
		document.title = 'RM by the designer Roland Mouret :  ' + RM.currentPage;
		RM.prepareElement(id);
		var n = $(id);
		if( o != n){
			RM.showElement(n, o);
		}else if( RM.section ){
			RM.setSection();
			if( RM.subsection ) RM.setSubSection.delay(1000);
			if( RM.subsubsection ) RM.setSubSubSection.delay(2000);
		}
		
	},
	
	updateNav: function(){
		$('nav').getElement('ul').innerHTML = RM.navHTML;
		var li = $('nav-' + RM.currentPage);
		var a = li.getElement('a');
		var txt = a.get('text');
		li.empty();
		li.set('html', txt);
		li.addClass('active');
		if(RM.currentPage != 'home'){
		
		}else{
		
		}
		
	},
	
	prepareElement: function(id){
		var s = $(id).getSize();
		$(id).setStyles({
			'opacity' : 0,
			'display' : 'block',
			'clip' : 'rect( 0px ' + s.x + 'px 0px 0px)'
		});
	},
	
	showElement: function(n, o){
		n.set('morph', {duration:1000, onComplete: 
			function(){
				o.setStyle('display', 'none');	
				if( RM.section ) RM.setSection();
				if(RM.subsection) RM.setSubSection();
				if(RM.subsubsection) RM.setSubSubSection();
			}
		});
		if( RM.page != '#store-locator' ){
			RM.hideAllSections();
			RM.hideAllSubSections();
			RM.hideAllSubSubSections();
		}
              
		o.set('morph', {duration:1000});
		var os = o.getSize();
		var ns = n.getSize();
		var nl = (window.getSize().x - ns.x) / 2;
		o.morph({
			'clip' : [ [ 0, os.x, os.y, 0 ].join(' '), [os.y, os.x, os.y, 0].join(' ') ],
			'opacity' : 0
		 });
		n.morph({
			'clip' : [ [ 0, ns.x, 0, 0 ].join(' '), [0, ns.x, ns.y, 0].join(' ') ],
			'opacity' : 1,
			'left' : nl
		 });
		/*if(n.id == 'home-content'){
			RM.scroller.toTop();
		}else{
			if( Browser.Engine.webkit ){
				$(window).scrollTo(0, $(window).getScrollSize().y );
			}else{
				RM.scroller.toBottom();
			}
		}*/
	},
	
	fadeOut: function(el){
		el.set('tween', {onComplete: function(){ el.setStyle('display', 'none'); } });
		el.tween('opacity', 0);
	},
	
	fadeIn: function(el, t){
		el.set('morph', {duration:1500} );
		var h;
		if( RM.lastH ){
			h = RM.lastH;
		}else{
			h = $random(0, 60);
		}
		el.setStyles({
			'opacity' : 0,
			'display' : 'block',
			'top' : h 
		});
		if( $chk(t) ){
			el.morph({
				'opacity' : 1,
				'top' : t
				
			});
			RM.lastH = t;
		}else{
			el.morph({
				'opacity' : 1
			});
		}
	},
	
	setSection: function(h){
		if( !$chk(h) ){
			if( RM.page == '#store-locator' ){
				
				var c = $('store-locator-content');
				$$('#regions a').each( function(a){
					if(a.hash.contains('/' + RM.section) ){
						h = a.getPosition(c).y;
					}
				});
				
			}
		}
		var el = $(RM.section);
		if( !$chk(el) ) return;
		RM.hideAllSubSections();
		el.getParent().getChildren().each( function(a){ a.setStyle('display', 'none'); } );
		RM.fadeIn(el, h);
		var links = el.getParent().getPrevious().getElements('a');
		links.each( function(l){ 
			if( l.hash.contains('/' + RM.section) ){
				l.addClass('active');
			}else{
				l.removeClass('active');
			}
		});
		el.getElements('a').each( function(l2){
			l2.removeClass('active');
		});
		
	},
	
	setSubSection: function(h){
		if( !$chk(h) ){
			if( RM.page == '#store-locator' ){
				var c = $('store-locator-content');
				$$('#regions a').each( function(a){
					if(a.hash.contains('/' + RM.subsection) ){
						h = a.getPosition(c).y;
					}
				});
				
			}
		}
		var el = $(RM.subsection);
		if( !$chk(el) ){
			RM.hideAllSubSections();
			RM.hideAllSubSubSections();
		}else{
			el.getParent().getChildren().each( function(a){ a.setStyle('display', 'none'); } );
			RM.fadeIn(el, h );
			var links = el.getParent().getPrevious().getElements('a');
			links.each( function(l){ 
				if( l.hash.contains('/' + RM.subsection) ){
					l.addClass('active');
				}else{
					l.removeClass('active');
				}
			});
		}
	},
	
	setSubSubSection: function(h){
		if( !$chk(h) ){
			if( RM.page == '#store-locator' ){
				var c = $('store-locator-content');
				$$('#cities a').each( function(a){
					if(a.hash.contains('/' + RM.subsubsection) ){
						h = a.getPosition(c).y;
					}
				});
				
			}
		}
		var el = $(RM.subsubsection);
		if( !$chk(el) ){
			RM.hideAllSubSubSections();
		}else{
			el.getParent().getChildren().each( function(a){ a.setStyle('display', 'none'); } );
			RM.fadeIn(el, h );
			var links = el.getParent().getPrevious().getElements('a');
			links.each( function(l){ 
				if( l.hash.contains('/' + RM.subsubsection) ){
					l.addClass('active');
				}else{
					l.removeClass('active');
				}
			});
		}
	},
	
	hideAllSubSections: function(){
		$('cities').getElements('ul').each( function(p){
			p.setStyle('display', 'none');
		});
	},
	
	hideAllSubSubSections: function(){
		$('stores').getElements('p').each( function(q){
			q.setStyle('display', 'none');
		});
	},
	
	hideAllSections: function(){
		$('regions').getElements('a').each( function(a){
			a.removeClass('active');
		});
		$('countries').getElements('ul').each( function(q){
			q.setStyle('display', 'none');
		});
	}
		
}

window.addEvent('domready', function(){
									 
	$$('a').each( function(l){
		if(l.hostname != window.location.hostname && !l.href.contains('javascript') ){
			l.addEvent('click', function(e){
				new Event(e).stop();
				window.open(this.href);
			});
		}
	});
	
	/*$('download-link').addEvent('click', function(e){
		new Event(e).stop();
		new IFrame({'src':this.href, 'styles':{'width':1,'height':1,'visibility':'hidden'}}).inject('iframe');
	});*/
									 
	// add in flash player, if not iphone
        if (window.showModalDialog){
		new Swiff('flash/player.swf', {
			id: 'rmMovie',
			container : 'video',
			width: '100%',
			height: '100%',
			params: {
				wmode: 'opaque',
				bgcolor: '#dcdcdc'
			},
			vars: {
				vid: RM.videos.getRandom()
			 }
		});
	}
									 
	RM.scroller = new Fx.Scroll( $(window) );

	RM.loadPage();
	rollover.init();

      
	
	// code to show contacts and cities on mouseover
/*	$$('#contact-content a').each( function(a){
		a.addEvent('click', function(){
			RM.section = this.hash.split('/')[1];
			var c = $('contact-content');
			var y = this.getPosition(c).y;
			RM.setSection( y );
			location.hash = this.hash;
		});
	});*/
	
	// code to show countries on mouseover
	$$('#store-locator-content #regions a').each( function(a){
		a.addEvent('click', function(){
			RM.section = this.hash.split('/')[1];
			var c = $('store-locator-content');
			var y = this.getPosition(c).y;
			RM.hideAllSubSubSections();
			RM.setSection(y);
			location.hash = this.hash;
		});
	});
	
	// code to show cities on mouseover
	$$('#countries a').each( function(a){
		a.addEvent('click', function(){
			RM.subsection = this.hash.split('/')[2];
			var c = $('store-locator-content');
			var y = this.getPosition(c).y;
			RM.hideAllSubSubSections();
			RM.setSubSection(y);
			location.hash = this.hash;
		});
	});
	
	// code to show addresses on mouseover
	$$('#cities a').each( function(a){
		a.addEvent('click', function(){
			RM.subsubsection = this.hash.split('/')[3];
			var c = $('store-locator-content');
			var y = this.getPosition(c).y;
			RM.setSubSubSection(y);
			location.hash = this.hash;
		});
	});
	
	// remove default text when clicking on input box - replace it if nothing enters
	$$('input.text').each( function(i){
		i.store('default', i.title);
		i.addEvent('focus', function(){
			if(this.value == this.retrieve('default') ) this.value = '';
		});
		i.addEvent('blur', function(){
			if(this.value.trim() == '') this.value = this.retrieve('default');
		});
	});

       clearForm();
	
	// resize pic
	var h = $('video').getSize().y;
	if( h < 360 ) h = 360;
	var w = ( h / 9 ) * 16;
	$('video').setStyle('width', w);
	
	window.addEvent( 'resize', function(){
		var h = $('video').getSize().y;
		var w = ( h / 9 ) * 16;
		$('video').setStyle('width', w);
		if( Browser.Engine.webkit ){
			var y = $('rmMov').getSize().y;
			if( y < 383 ){
				$('rmMov').height = '383px';
			}
		}
		
	});
});

function clearForm(){
  
    $('log_res').setStyle('display', 'none');
    $('form-errors').setStyle('display', 'none');
      $('register-form').setStyle('display', 'block');
    $$('input.text').each( function(i){
       i.value = i.retrieve('default');
    });
}
	