var theahrefs = document.getElementsByTagName('a');
//fix dotted line thing when link is OnClicked
for(var x=0;x!=theahrefs.length;x++){
	theahrefs[x].onfocus = function stopLinkFocus(){this.hideFocus=true;};
}

(function(){
/**
  Toggles the background of whole navigation instead of only selected anchors on mouse over
*/
var NavigationToggler = new Class({
	sections : {
		'home' : 'a',
		'products' : 'b',
		'company' : 'c',
		'cart' : 'd'
	},
	bound : {},
	nav : null,
	initialize : function() {
		var lis = $ES('#nav > li');
		
		this.nav = $('nav');
		
		this.bound.mouseover = this.over.bindWithEvent(this);
		lis.addEvent('mouseover',this.bound.mouseover);	
		
		this.bound.mouseout = this.reset.bindWithEvent(this);
		lis.addEvent('mouseout',this.bound.mouseout);	
	},
	over : function(event) {
		var section = $(event.target.parentNode).getAttribute('id').substr(4);
		
		if (this.sections.hasOwnProperty(section)) {
			this.nav.addClass(this.sections[section]);
		}
	},
	reset : function(event) {
		for (var section in this.sections)
		{
			if (this.sections.hasOwnProperty(section)) {
				this.nav.removeClass(this.sections[section]);
			}
		}
	}
});

var nt = new NavigationToggler();
})();

//------
(function(){
	var BackgroundSwitcher = new Class({
		initialize : function(options) {
			this.bound = {};
			this.current = null;
			this.interval = null;
			this.links = [];
			this.timeout = arguments[1] || 10;
			this.options = options;
			
			this.switcher = $('splash');
			if ( ! this.switcher ) {
				return;
			}
			
			this.cloner = new Element('div',{
				'id' : 'tempClone'
			});
			this.cloner.setStyle( 'background-image', 'url('+escape( options[0].img ) + ')' );
			// ----
			this.switcher.setStyle('background-image', 'url('+escape( options[0].img ) +')' )
      replaceLinks( options[0].buttons );

			var ul = new Element('ul', {'id':'splash-pager'} );
			ul.injectInside($('splash-inner'));			
			this.bound.click = this.onclick.bindWithEvent(this);
			
			var len = this.options.length;
			
			/* build list of links */
			for (var i=0; i < len; i++) {
				var li = new Element('li');
				var a = new Element('a',{
					'href' : '#',
					onFocus: 'this.hideFocus=true'
				});
				
				/* make first active */
				if (i === 0) {
					a.addClass('current');
					this.current = a;
				}
				var pageNo = parseInt( i+1,10);
				a.setProperty('title', 'Features Page '+pageNo );
				
				a.option = this.options[i];
				a.setText(parseInt(i + 1, 10));
				a.addEvent('click',this.bound.click);
				
				this.links.push(a);
				
				a.injectInside(li);
				li.injectInside(ul);
			}
			this.preloadImages();
			this.initRotation();
		},
		initRotation : function() {
			var self = this;
			
			if (this.tout !== null) {
				window.clearTimeout(this.tout);
			}
			
			var timeout = this.current.option.timeout || this.timeout;
			
			this.tout = window.setTimeout(function(){
				self.rotate.apply(self);
			},timeout * 1000);			
		},
		rotate : function() {
			var cin = this.links.indexOf(this.current);
			var next = this.links[cin+1] || this.links[0];
			this.execute(next);			
		},
		execute : function(el) {
			this.resetCurrentAnchor(el);
			
			var temp = this.cloner.cloneNode(false);
			
			temp.setStyle('background-image',this.switcher.getStyle('background-image'));
			temp.injectTop(this.switcher);
			
			var option = el.option;
			var switcher = this.switcher;

			switcher.setStyle('background-image','url('+option.img.src +')');

			switcher.className = option.className;

//      replaceLinks( option.buttons );

			var mfx = new Fx.Style(temp, 'opacity', { 
				duration: 500, // 200 
        onStart : function() {
          var myFx = new Fx.Style( 'splash-buttons', 'opacity').set(0);
        },
				onComplete : function(){
					temp.remove();
          replaceLinks( option.buttons );
//					moreLink.setProperty('href',option.more);						
				}
			});

			mfx.start(1, 0);			
		},
		preloadImages : function() {
			var i=0;
			this.options.each( function(option) {
				var img = Asset.image( option.img );
				var moreImg = Asset.image( option.moreImg );
				option.img = img;
				option.moreImg=moreImg;
			});
		},
		resetCurrentAnchor : function(el) {
			$ES('ul a',this.switcher).each(function(item){
				item.removeClass('current');
			});
			$(el).addClass('current');
			
			this.current = el;
			/* reset interval */
			this.initRotation();		
		},		
		onclick : function(event) {
			event = new Event(event).stop();
			this.execute(event.target);
		} 
	});
	
	BackgroundSwitcher = new BackgroundSwitcher(fs_options, 15);
})();

//------
(function(){
	var Menu = new Class({
		bound : {},
		items : [],
		initialize: function(){
			
			$ES('#nav li').each(function(item) {
				item.addEvent('mouseover', this.mouseover);
				item.addEvent('mouseout', this.mouseout);				
			},this);
		},
		mouseover : function(evn) {
			if ($(this).hasClass('active')) {
				return;
			}
			$(this).addClass('active');
		},
		mouseout : function(event) {
			event = new Event(event);
			
			if (event.target && event.relatedTarget) {
		  	var id1 = $(event.target).getParent().getProperty('id');
				// related target when we discuss mouseover is the item we just left.
				if ( $type( $(event.relatedTarget).getParent() ) == 'element' ) {
				 	var id2 = $(event.relatedTarget).getParent().getProperty('id');
				}
				else {
					var id2=null;
				}
		  	
		  	if (id1 !== null && id2 !== null) {
		  		var x = id1.split('-');
		  		x.length = 2;
		  		
		  		var y = id2.split('-');
		  		y.length = 2;
		  		
		  		if (x[0] == y[0] && x[1] == y[1]) {
		  			return;
		  		}
		  	}
	  	}
			
			$(this).removeClass('active');
		}
	});
	
	var m = new Menu();
})();

window.addEvent('resize', function() {
	var size=Window.getSize();
	var six = size.size.x;
	var uldots = $('splash-inner').getElement('ul');
	var viewnow =$('splash-inner').getElement('a.view-now');
	/*
	var dotFx = new Fx.Styles( uldots, {duration:1000,wait:false,transition:Fx.Transitions.Bounce.easeOut } );
	var btnFx = new Fx.Styles( viewnow, {duration:1000,wait:false,transition:Fx.Transitions.Bounce.easeOut } );
	*/
	var dotFx = uldots.effects( { duration:200 } );
	var btnFx = viewnow.effects( { duration:200 } );
	if ( six < 997 ) {
		if ( window.ie6) {
			return true;
		}
		var offset=997-six;
		var moveto=Math.floor( offset/2 );
		moveto = 580-moveto;
		dotFx.start( { 'opacity':0 }).chain(function() { 
			this.start({ 'left':moveto }); 
		}).chain( function() {
			this.start({ 'opacity':1}); 
		});
		btnFx.start( { 'opacity':0 }).chain(function() { 
			this.start({ 'left':moveto }); 
		}).chain( function() {
			this.start({ 'opacity':1}); 
		});

		/*
		dotFx.start( { 'left':moveto } );
		btnFx.start( { 'left':moveto } );
		*/

	}
	else {
		if ( uldots.getStyle('left') != '580px' ) {
			moveto = 580;
			dotFx.start( { 'opacity':0 }).chain(function() { 
				this.start({ 'left':moveto+'px' }); 
			}).chain( function() {
				this.start({ 'opacity':1}); 
			});
			btnFx.start( { 'opacity':0 }).chain(function() { 
				this.start({ 'left':moveto+'px' }); 
			}).chain( function() {
				this.start({ 'opacity':1}); 
			});
		};
		/*
		dotFx.start( { 'left':580 } );
		btnFx.start( { 'left':580 } );
		*/
	}
});

// this is the resize positioning for the main dots button on the splash
window.addEvent('domready', function() {
	var size=Window.getSize();
	var six = size.size.x;
	var uldots = $('splash-inner').getElement('ul');
	var viewnow =$('splash-inner').getElement('a.view-now');
	if ( six < 997 ) {
		var offset=997-six;
		var moveto=Math.floor( offset/2 );
		moveto = 580-moveto;
		uldots.setStyle('left', moveto+'px');
		viewnow.setStyle('left', moveto+'px');
	}
	else {
		uldots.setStyle('left', '580px' );
		viewnow.setStyle('left', '580px' );
	}
});

// ---
var preloaded = new Array(); 
// --- 
window.addEvent( 'domready', function() {
// preload the important images.
//
	if ( $type( prioritypreload) == 'array' ) {
/*
		prioritypreload.each( function( item ) {
			temp=new Image();
			temp.src=item;
			foo=img.length;
			foo++;
			img[foo]=temp;
		});
*/
		var newimgs = Asset.images( prioritypreload );
		preloaded.merge( newimgs );
	}
	else if ( $type( prioritypreload ) == 'string' ) {
		newimg=Asset.image ( prioritypreload );
		preloaded.include( newimg );
	}
	else {
		// no preloading, dude.
	}

});

function replaceLinks( buttons ) {
      // clear out the button div and insert each button.
	  var h=0;
      $('splash-buttons').empty();
      var myFx = new Fx.Style( 'splash-buttons', 'opacity').set(0);
      $('splash-buttons').setStyle('display','block');
      var parentwidth=0;
      for ( var i = 0 ; i < buttons.length; i++ ) {
        var foo = new Element('a',{
          'id' : 'fs_link_'+i
          });
        foo.injectInside( 'splash-buttons' );
        var thisButton = $( 'fs_link_'+i );
        thisButton.setText('view more');
        thisButton.setProperty('href', buttons[i].more );
        thisButton.addClass('view-now');
        thisButton.setStyle('background-image',"url('"+ buttons[i].moreImg+"')" );
        thisButton.setStyle('background-repeat', 'no-repeat');
        thisButton.setStyle('background-position', 'top left' );
        thisButton.setStyle('float', 'right' );
		if ( i > 0 ) {
				thisButton.setStyle('margin-right','11px' );
		}
        thisButton.setStyle('display','block' );
        thisButton.setStyle('text-indent', -9999);
        // 
        thisButton.addEvent('mouseover', function() { this.setStyle('background-position','bottom left' ) } );
        thisButton.addEvent('mouseout', function() { this.setStyle('background-position','top left' ) } );
        if ( buttons[i].button_width > 0 ) {
          w=buttons[i].button_width;
          thisButton.setStyle('width', w+'px' );
          parentwidth += w.toInt() ;
		  // not sure about this: mockup has images 11 px apart.
		  parentwidth += 11;
        }
        if ( buttons[i].button_height > 0 ) {
          h=buttons[i].button_height/2;
          thisButton.setStyle('height', h );
        }
      }
      // adjust the parents.
/*	  $('splash-buttons').setStyle('margin-left','96px'); */
      $('splash-buttons').setStyle('width', parentwidth+'px' );
	  if ( h > 0 ) {
		$('splash-buttons').setStyle('height', h+'px' );
	  }
      var myFx = new Fx.Style( 'splash-buttons', 'opacity', {duration:750}).start(0,1);
      //
      return;
}

