//Small slider
function startScroll(h,hMax,direction) {
    var i;
    new PeriodicalExecuter(function(pe) {
        i = h.currentIndex();
        if (direction == 1) {
            if ((i+1) >= hMax) {
                h.scrollTo(hMax);
                direction = 2;
            } else {
                h.scrollTo(i+1);
            }
        } else {
            if ((i-1) <= 0) {
                h.scrollTo(0);
                direction = 1;
            } else {
                h.scrollTo(i-1);
            }
        }
    }, 5);
}

function open_flip(url)
{
 url +='?width=800&height=600';
 newwin=window.open(url,'flipbook', 'height=600, width=800, top=0, left=0, toolbar=no, menubar=yes, location=no, resizable=yes, scrollbars=no, status=no');
 if (window.focus) {newwin.focus()}
 return false;
}


//Big slider
function carousel() {
	var egg_on = 'http://dev.noeuf.creasenso.fr/skin/frontend/noeuf/default/img/egg_on.png';
	var egg_off = 'http://dev.noeuf.creasenso.fr/skin/frontend/noeuf/default/img/egg_off.png';
	var myCarousel = new UI.Carousel($("home_carousel"));
	$$('#egg_container ul li img').each(function(el){el.writeAttribute('src',egg_off);});
	$$('#egg_container ul li img[alt="0"]').each(function(el){el.writeAttribute('src',egg_on);});
	$$("#egg_container ul li img").each(function(el){el.observe('click',slideCarousel)});
	function slideCarousel(event) {
		var element = event.element();
		image_count = parseInt(element.readAttribute('alt'));
		myCarousel.scrollTo(image_count);
		}
	myCarousel.observe('scroll:ended', function(event){
		current = myCarousel.currentIndex();
		$$('#egg_container ul li img').each(function(el){el.writeAttribute('src',egg_off);}); 
		$$('#egg_container ul li img[alt="'+current+'"]').each(function(el){el.writeAttribute('src',egg_on);});
		}
	);
	
		//autoslide
	 	var i;
	 	var direction = 1;
	 	var max = $("home_carousel_container").getElementsByTagName('li').length-1;
	    new PeriodicalExecuter(function(pe) {
	        i = myCarousel.currentIndex();
	        if (direction == 1) {
	            if ((i+1) >= max) {
	            	myCarousel.scrollTo(max);
	                direction = 2;
	            } else {
	            	myCarousel.scrollTo(i+1);
	            }
	        } else {
	            if ((i-1) <= 0) {
	            	myCarousel.scrollTo(0);
	                direction = 1;
	            } else {
	            	myCarousel.scrollTo(i-1);
	            }
	        }
	    }, 8);
}


//Ticker
Effect.DefaultOptions.duration = 0.3;
NewsTicker = Class.create();
Object.extend(NewsTicker.prototype, {
	
	tickerDiv: "block-actu", 
	tickerLocation: "newsletter-home-slider", 
	tickerTitle: "news-link",
	tickerLink: "http://blog.noeuf.fr",
	feedURL: "http://blog.noeuf.fr/feed/rss/",
	pauseLength: 3500,
	timer: 0,
	currentTitle: 0,
	items: null,
	initialize: function() {
		
		this.items = [];
		
		new Ajax.Request(
			this.feedURL, {
				method: "get",
				onSuccess: function(response) {
					this.parseXML(response.responseXML);
					this.buildTicker();
				}.bind(this),
				onFailure: function() {
					console.log("Please visit noeuf.com for the latest news and information.");
				},
				onException: function(req, err) {
					// throw(err);
				}
			}
		);
	},
	
	buildTicker: function() {
		// replace the placeholder content with the first news title
		if (this.items[this.currentTitle]) {
			$(this.tickerTitle).innerHTML = this.items[this.currentTitle]['title'];
			this.start();// start the timer if we have valid headlines
		}
	},
	
	parseXML: function(xml) {
		// build the array of news titles
		$A(xml.getElementsByTagName("item")).each(function(item) {
			title = item.getElementsByTagName("title")[0].childNodes[0].nodeValue;
			//desc = item.getElementsByTagName("description")[0].childNodes[0].nodeValue;
			//title = title + ' - <em>' + desc.substr(0,60) + '...</em>';
			//var link = NewsTicker.tickerLink;
			var link = item.getElementsByTagName("link")[0].childNodes[0].nodeValue;
			this.items.push({title: title, link: link});
		}.bind(this));
	},
	
	start: function() {
		this.interval = setInterval(this.showNext.bind(this), this.pauseLength);
	},
	
	stop: function() {
		clearInterval(this.interval)
	},
	
	showNext: function() {
		
		//determine next headline
		if ( this.currentTitle < this.items.length-1 ) {
			this.currentTitle = this.currentTitle+1;
		} else {
			this.currentTitle = 0;
		}
		
		new Effect.Fade('news-link', {
			afterFinish: function() {
				this.switchData();
				new Effect.Appear('news-link'); }.bind(this)});

	},
	
    switchData: function() {
		$(this.tickerTitle).setAttribute("href", this.tickerLink);
		if (this.items[this.currentTitle]) {
			$(this.tickerTitle).innerHTML = this.items[this.currentTitle]['title'];
		}
	}
});

//Tabs
Varien.Tabs = Class.create();
Varien.Tabs.prototype = {
  initialize: function(selector) {
    var self=this;
    $$(selector+' a').each(this.initTab.bind(this));
  },

  initTab: function(el) {
      el.href = 'javascript:void(0)';
      if ($(el.parentNode).hasClassName('active')) {
        this.showContent(el);
      }
      el.observe('click', this.showContent.bind(this, el));
  },

  showContent: function(a) {
    var li = $(a.parentNode), ul = $(li.parentNode);
    ul.getElementsBySelector('li', 'ol').each(function(el){
      var contents = $(el.id+'_contents');
      if (el==li) {
        el.addClassName('active');
        contents.show();
      } else {
        el.removeClassName('active');
        contents.hide();
      }
    });
  }
}

Event.observe(window, "load", function() {	
	var h = new UI.Carousel("horizontal_carousel");
	var direction = 1;
	var hMax = $("horizontal_carousel").getElementsByTagName('li').length-1;
	startScroll(h,hMax,direction);
	carousel();
	var ticker = new NewsTicker();
	new Varien.Tabs('.tabs');
});
