/* 
   JavaScript Document
   Written by Ikonic (Pellevillain Cédric)
   Copyright (c) 2008 Piksite.com 
   ---------------------------------------
   Some rights reserved. This work is licensed under a Creative Commons Attribution-Noncommercial 3.0 Unported License.
   
   You are free:

    * to Share — to copy, distribute and transmit the work
    * to Remix — to adapt the work

   Under the following conditions:

    * Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
    * Noncommercial. You may not use this work for commercial purposes.

    * For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.
    * Any of the above conditions can be waived if you get permission from the copyright holder.
    * Nothing in this license impairs or restricts the author's moral rights.
   
   If you want to include this work in a selling package, mail me : contact@piksite.com        

   ---------------------------------------------------------------------------------------
    Modified by mevtho (Thomas MEVEL) ==> to suit my needs 
   ---------------------------------------------------------------------------------------
*/
 
var counter = 0; 
var tempX = 0;
var elt_posX,elt_posY,b;
var elements = new Array();

// During page on load
Event.observe(window, 'load', function() {

  // create each element
  images.each(function(photo) {
      var div = Builder.node('div', { className: 'image', style: 'position:absolute;display:none;' });
      if(clickit!=false) { 
        Event.observe(div, 'click', Click);
        Event.observe(div, 'mouseover', Over); 
        Event.observe(div, 'mouseout', Out); 
        div.style.cursor = 'pointer'; 
      }
      id_div = photo.split(".");
      div.id = id_div[0];
      //var img = Builder.node('img', { src: path + photo });
      var img = document.createElement('img');
      img.src = path + photo;
      div.insert(img); 
      $('container').insert(div);  
      elements.push(div.id);
      Reflection.add(img, { height: height, opacity: opacity });
    });
    
  open();

});

// create each element
function open() { 
  // appear each element
  elements.each(function(element) {
    new Effect.Appear(element, {duration:0.8});
  });

  // start carousel   
  b = setInterval('carousel()',40);
  
  // set mouse events
  function position(e) {
    if (navigator.appName.substring(0,3) == "Net") {
      tempX = e.pageX;
    } else {
      tempX = event.x+document.body.scrollLeft;
    }
  }
  document.onmousemove = position;
  document.onmouseout = out; 
}

// on mouse move on main container
function move() {	
	speed = (tempX - centerx) / 2500;
	base = 0.05;	
}

// on mouse out from main container
function out() {	
	speed = speed;	
}

function Click(elt) {

  // get parent div id
  elt = Event.element(elt).parentNode.parentNode.id;

  window.location="mevtho/inc/page.php?img="+elt+"&what=redirect";
}

function Over(elt) {
  // get parent div id
  elt = Event.element(elt).parentNode.parentNode.id;

  requestText(elt);
}

function Out(elt) {
  new Effect.Opacity('text', { duration: 1, from: 1, to: 0 });
}

// request text
function requestText(elt) {
  var url = includes+'page.php';

  myText = new Ajax.Request( url, 
      { method: 'get', 
        parameters: {img: elt, what: 'description'}, 
        onComplete: function(xhr) {
          if (xhr.status == 200) { $('text').innerHTML = xhr.responseText;   new Effect.Opacity('text', { duration: 1, from: 0, to: 1 });} 
          else if(xhr.status == 404) { $('text').innerHTML = 'Page non found.'; }
          else { $('text').innerHTML = 'An error occurred. Please re-try.'; }
        } 
  });
}

// carousel 
function carousel() {
 elements.each(function(photo, i) {
		angle = i*2*Math.PI/elements.length;		
		posx = centerx+Math.sin(counter*(base*speed)+angle)*radiusx;
		posy = centery+Math.cos(counter*(base*speed)+angle)*radiusy;	
		$(elements[i]).style.left = posx+"px"; 
		$(elements[i]).style.top = posy+"px";
		$(elements[i]).style.zIndex = Math.round(posy/3)+100;		
		angle += speed;	
	});	
	counter++
}