/* 
   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        
*/
 
var counter = 0; 
var tempX = 0;
var elt_posX,elt_posY,b;
var elements = new Array();
var clear="clear.gif"

// During page on load
window.onload = function() { 

  // create each element
  var count = 0;
  images.each(function(photo) {
      var div = document.createElement('div');
      div.className = 'image';
      div.style.position = 'absolute';
      div.style.display = 'none';
      
      id_div = photo.split(".");
      div.id = id_div[0];
      var img = document.createElement('img');
      // deaktiviert von CW fuer Batix CMS
      //img.src = 'reflect.php?img='+path+photo+'&height='+height+'&start_fade='+opacity;
      img.src = path+photo;
      // DIV tilte mit Produktname anpassen
      if(count > 0 && imgText[count] != null) div.title = imgText[count];
      div.bxPos = count;
      if(clickit!=false) { 
        Event.observe(div, 'click', Click);
        div.style.cursor = 'pointer'; 
      }
      div.onmouseover = function() {
        BxStop();
      };
      div.onmouseout = function() {
        BxMove();
      };
      div.appendChild(img);
      $('container').appendChild(div);  
      elements.push(div.id);
      count++;
    });

  if(navigator.userAgent.toLowerCase().indexOf("msie 6") != -1) {  
    elements.each(function(element) {
      var el = $(element).firstDescendant();
      if(el.width==0 && el.height==0) { 
        new_img = new Image();
        new_img.src = el.src;
      }
      el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";
      el.style.backgroundImage = 'none';
      el.src = clear;
    });  
  }
    
  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;
    }
 	  move();
  }
  //document.onmousemove = position;
  document.onmouseout = out; 
  
}

// on mouse move on main container
function move() {	
	speed = (tempX - centerx) / 2500;
	base = 0.05;	
}

// Batix onmouseover Stop
function BxStop() {	
	clearInterval(b);
}
// Batix onmouseout move
function BxMove() {	
	open();
}
// on mouse out from main container
function out() {	
	speed = speed;	
}

// Change image event to back
function textIt(elt) {
  Event.stopObserving(elt, 'click', Click);
  Event.observe(elt, 'click', Back);
}

// Change image event to click
function untextIt(elt) {
  Event.stopObserving(elt, 'click', Back);
  Event.observe(elt, 'click', Click);
}

// Back to carousel
function Back(elt) {

  // get parent div id if clicked element is an object
  if(typeof(elt)=='object') { elt = Event.element(elt).parentNode.id; }
  
  // define actions
  var appearer = 'new Effect.Parallel([';
  for(i=0;i < elements.length;i++) {
    if(elements[i]!=elt) { 
      appearer += "new Effect.Appear('"+elements[i]+"', {sync:true})";      
    } else {
      appearer += "new Effect.Morph('"+elt+"', {style: 'left: '+elt_posx+'px; top: '+elt_posy+'px;'})";
    }
    appearer += ', ';
  }
  appearer += 'new Effect.Fade(\'text\', {sync: true})], {duration:0.5, afterFinish : function() { untextIt(\''+elt+'\'); $(\'text\').innerHTML = \'\'; b = setInterval(\'carousel()\',40); } });';

  // do actions
  eval(appearer);  
  
}

function Click(elt) {
  // get other info
  pos = Event.element(elt).parentNode.bxPos;
  // get parent div id
  elt = Event.element(elt).parentNode.id;
  /*
  alt - Anpassungen BX Jan 2009
  
  // stop carousel
  clearInterval(b);
  
  // get clicked element positions
  elt_posx = parseFloat(Element.getStyle(elt, 'left'));
  elt_posy = parseFloat(Element.getStyle(elt, 'top'));
  
  // define actions
  
  var fader = 'new Effect.Parallel([';
  for(i=0;i < elements.length;i++) {
    if(elements[i]!=elt) { 
      fader += "new Effect.DropOut('"+elements[i]+"', {sync:true})";
    } else {
      fader += "new Effect.Morph('"+elt+"', {style: 'left: 0px; top: 0px;'})";
    }
    fader += ', ';
  }
  fader += 'new Effect.Appear(\'text\', {sync: true})], {duration:0.5, afterFinish : function() { textIt(\''+elt+'\') }, beforeStart : function() { requestText(\''+elt+'\') } });';

  // do actions
  eval(fader);
  */
  if(pos >= 0 && imgURL[pos] != null) window.location.href = imgURL[pos];
}

// request text
function requestText(elt) {
  var url = includes+'page.php';
  myText = new Ajax.Request( url, 
      { method: 'get', 
        parameters: {item: elt}, 
        onComplete: function(xhr) {
          if (xhr.status == 200) { $('text').innerHTML = xhr.responseText; } 
          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++
}
