var selected;

(function(){

	window.addEvent('domready', function(){

		$$(".menu-list").setStyle("height", 0).set("tween", { duration: 500, transition: Fx.Transitions.Expo.easeInOut });
		$$(".menu-list a").setStyle("right", -131).set("tween", { duration: 500, transition: Fx.Transitions.Expo.easeInOut });

		$("menu-switcher").addEvent("click", function(e){

			if($$(".menu-list")[0].retrieve('open')) {

				$$(".menu-list a").reverse().each(function(item, index){
					(function(){ item.tween("right", -131); }).delay(index * 150);
				});

				(function(){
					$$(".menu-list")[0].tween("height", 0).store('open', false);
				}).delay(1000);
			}
			else {

				$$(".menu-list")[0].tween("height", 117).store('open', true);

				(function(){
					$$(".menu-list a").each(function(item, index){
						(function(){ item.tween("right", 0); }).delay(index * 150);
					});
				}).delay(300);
			}
		});

		var scrollFx = new Fx.Scroll(window, { duration: 2000, transition: Fx.Transitions.Expo.easeInOut });

		$$(".menu-list a").addEvent("click", function(e) {

			e.preventDefault();

			if(selected) selected.set("id", "");
			this.set("id", "menu-list-selected");
			selected = this;

			if(this.get("href") == "#top") {

				scrollFx.start(0, 0);
				return;
			}

			var el = $(this.get("href").replace("#", ""));
			scrollFx.start(0, el.getPosition().y - (window.getSize().y - parseInt(el.get("data-title-height"))) * 0.5);
		});

		$$("a[href='#collection']").addEvent("click", function(e){

			var el = $("collection");
			e.preventDefault();
			scrollFx.start(0, el.getPosition().y - (window.getSize().y - parseInt(el.get("data-title-height"))) * 0.5);
		});

		$$("a[href='#press-focus']").addEvent("click", function(e){

			var el = $("press-focus-list");
			e.preventDefault();
			scrollFx.start(0, el.getPosition().y - (window.getSize().y - parseInt(el.getStyle("height"))) * 0.5);
		});

		$$("a[href='#philosophy-focus']").addEvent("click", function(e){

			var el = $("philosophy-focus");
			e.preventDefault();
			scrollFx.start(0, el.getPosition().y - (window.getSize().y - parseInt(el.getStyle("height"))) * 0.5);
		});

		$$(".more-button a[href='#less']").addEvent("click", function(e) {

			e.preventDefault();

			var el = this.getParent().getPrevious();
			var index = el.retrieve('index');

			if(index) {
				index--;
				el.store('index', index);
				new Fx.Scroll(el, { duration: 1000, transition: Fx.Transitions.Expo.easeInOut }).start(960 * index, 0);
			}
		});

		$$(".more-button a[href='#more']").addEvent("click", function(e) {

			e.preventDefault();

			var el = this.getParent().getPrevious();
			var index = el.retrieve('index');

			if(index < parseInt(el.get("data-count") -1)) {
				index++;
				el.store('index', index);
				new Fx.Scroll(el, { duration: 1000, transition: Fx.Transitions.Expo.easeInOut }).start(960 * index, 0);
			}
		});

		// COLECTION
		$$(".collection-product").addEvent("mouseover", function(){

			this.getElements("p, img.collection-gallery-frame").fade(1);
		}).addEvent("mouseout", function(){

			this.getElements("p, img.collection-gallery-frame").fade(0);
		});

		$$(".collection-product > p, .collection-product img.collection-gallery-frame").fade("hide");

		// ADV
		$$(".adv-cover").addEvent("mouseover", function(){

			this.getElements("p, img.adv-gallery-frame").fade(1);
		}).addEvent("mouseout", function(){

			this.getElements("p, img.adv-gallery-frame").fade(0);
		});

		$$(".adv-cover > p, .adv-cover img.adv-gallery-frame").fade("hide");

		configureNewsNavigation();

		// parallax
		$$("#models-0, #models-01, #models-02, #models-1, #models-11, #models-12, #models-2, #models-21, #models-22, #models-3, #models-31, #models-32").each(function(item){
			item.store('top', parseInt(item.getStyle('top')));
			item.store('y', item.getPosition().y);
			item.store('friction', parseFloat(item.get("data-friction")));
		});

		this.addEvent('scroll', verticalParallax);

		$("press-logo").setStyle("opacity", .8);
		if(!Browser.ie)
			configureCustomScrollSpyCallBack();

		$$("#language-bar-line, #language-bar-line, #language-bar, #menu, #sections, #models").setStyle("display", "none");
		$$("html, body").setStyle("height", "100%");
		$$("body, html").setStyle("overflow-y", "scroll");

	}).addEvent('load', function(){

		$("loading").set("tween", { duration: 1000, transition: Fx.Transitions.Quint.easeInOut, unit: "%", onComplete: function(){
			$$("html, body").setStyle("height", "");
			$$("body, html").setStyle("overflow-y", "");
		}}).tween.delay(100, $("loading"), ["top", -100]);

		$$("#language-bar-line, #language-bar-line, #language-bar, #menu, #sections, #models").setStyle("display", "");

		if(Browser.ie)
			return;

		scrollSpyElements = $$('').fade('hide');
	});

	function verticalParallax(e){

		var windowScrollY = window.getScroll().y;

		$$("#models-0, #models-01, #models-02, #models-1, #models-11, #models-12, #models-2, #models-21, #models-22, #models-3, #models-31, #models-32").each(function(item){

			if((windowScrollY) >= item.getPosition().y)
				item.setStyle("top", item.retrieve('top') + (windowScrollY - item.retrieve('y'))  * item.retrieve('friction'));
		});
	};

	function configureNewsNavigation() {

		var newsNavigationItems = $$(".press-pagination a");

		newsNavigationItems.each(function(item, index){

			item.addEvent('click', function(e){

				e.preventDefault();

				if(newsNavigationSelected == this)
					return;

				newsNavigationSelected.set("id", "");
				newsNavigationSelected = this;
				newsNavigationSelected.set("id", "press-pagination-selected");

				new Fx.Scroll('press-focus-list', { duration: 500, transition: Fx.Transitions.Expo.easeInOut }).start(index * 928, 0);
			});
		});

		newsNavigationSelected = newsNavigationItems[0];
	};

	function configureShareLinks(e){

		e.preventDefault();

		var title = this.getParent().getParent().getElements('h3').get('text');
		var eventid = this.getParent().getParent().getFirst().get('data-eventid');
		var windowprops = 'width=600,height=400,top=0,left=0,toolbar=no,location=no,directories=no,status=no,scrollbars=no,menubar=no';
		var popup;

		if(this.hasClass('share-on-facebook'))
			popup = window.open('http://www.facebook.com/sharer.php?u=http://dromeclub.it/?eid=' + eventid + '&t='+title, 'remote', windowprops);
		else
			popup = window.open('http://twitter.com/home?status=@dromeclub // ' + title + ' // http://dromeclub.it', 'remote', windowprops);

		popup.moveTo(0, 0);
		popup.resizeTo(600, 400);
		popup.focus();
	};

	function configureFooterLogos() {

		$$("#footer-loghi a")
			.setStyle('opacity', .7)
			.set('tween', { duration: 403, transition: Fx.Transitions.Expo.easeOut })
			.addEvent('mouseenter', function(){ this.fade(1); })
			.addEvent('mouseleave', function(){ this.fade(.7); });
	};

	function scrollSpy(){

		var windowScroll;
		var windowScrollY = win.getScroll().y;
		var windowSizeY = win.getSize().y;

		if(!scrollSpyElements.length)
			window.removeEvent('scroll', scrollSpy);

		var elements = new Array();

		Array.clone(scrollSpyElements).each(function(item, index) {

			windowScroll = windowScrollY + windowSizeY - (item.scrollSpyOffset ? item.scrollSpyOffset : 300);

			if(windowScroll >= item.getPosition().y) {

				elements.push(item);
				scrollSpyElements = scrollSpyElements.erase(item);
			}
		});

		elements.each(function(item, index){

			if(typeof item.scrollSpyShow == "function")
				item.scrollSpyShow.delay(index * 200, item);
			else
				(function(){ item.morph({ 'opacity': 1 }); }).delay(index * 300);
		});
	};

	function configureCustomScrollSpyCallBack() {

		$$("hr").scrollSpyOffset = 100;
		$$("hr").each(function(item){
		
			item.scrollSpyShow = function() {

				this.setStyle('width', 0);
				this.setStyle('opacity', 1);
				new Fx.Tween(this, { duration: 1000, transition: Fx.Transitions.Back.easeInOut }).start('width', 950);
			};
		});
	}

})();

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-11382876-5']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
