/* 
   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="_css/clear.gif" 
var drag = false;
var slowdown = false;;
var previousSpeed = 0;
var direction = 0;

var dragMax = 0;

var mouseX = 0;
var prev_mouseX = 0;

var moveCarousel = false;

// During page on load
window.onload = function() { 

//Set control actions for drag control text (so can't highlight)
//document.getElementById("dragcontrol").onselectstart = function(e){if(e){e.preventDefault();}return false;}
//document.getElementById("dragcontrol").onfocus = function(e){if(e){e.preventDefault();}return false;}
//document.getElementById("dragcontrol").onmouseover = function(e){if(e){e.preventDefault();}return false;}

  // create each element
  images.each(function(photo) {
      id_div = photo.split(".");
      var id = id_div[0];
      var div = new Element('div', { 'id': id, 'class': 'image', 'styles': { 'position': 'absolute', 'opacity': '0' } }).inject('container');
      if(clickit!=false) { 
		div.addEvent('click', Click);
		div.onfocus = function(e){if(e){e.preventDefault();}return false;}
        div.setStyle('cursor', 'pointer');
		div.onmouseover = function(e){if(e){e.preventDefault();}return false;}
		div.onselectstart = function(e){;e.preventDefault();return false;}
	
      }
	 // var anchor = new Element('a', { 'href': 'carousel/_images/hutton.jpg' }).inject(div);
	 // anchor.setAttribute("rel", "thumbnail");
      var img = new Element('img', { 'src': 'carousel/reflect.php?img='+path+photo+'&height='+height+'&start_fade='+opacity }).inject(div);
	  //img.setAttribute("name", "Image26");
	  //img.setAttribute("border", "0");
	 // <img class="imageLink" src="Guitars/Hutton/Thumbs/soundhole-sm.jpg" name="Image28" border="0" alt="image">
	  elements.include(id);
    });
  
  if(Browser.Engine.trident4) { 
    elements.each(function(element) {
      var el = $(element).getFirst();
      el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";
      el.src = clear;
    });
  }
    
  open();

};

// DropOut Effect
DropOut = function(element) {
  element = $(element);
  var oldtop = element.getStyle('top').toInt();  
  
  var DropOutEffect = new Fx.Morph(element, {duration: 500, transition: Fx.Transitions.Sine.easeOut, onComplete: function() { element.setStyle('top', oldtop) } });
  DropOutEffect.start({'top': [oldtop+'px', oldtop+100+'px'], 'opacity': [1, 0]});
}

// create each element
function open() { 

  // appear each element
  elements.each(function(element) {
  	var appear_effect = new Fx.Morph(element, {duration: 800, transition: Fx.Transitions.Sine.easeOut });
    appear_effect.start({ 'opacity': [0, 1] });  
  });
  
  // start carousel   
  //b = setInterval('carousel()',40);
  moveCarousel = true;
  carousel();
  moveCarousel = false;
  b = setInterval('carousel()',40);
 
  // set mouse events
  function position(e) 
  {
	if(drag)
	{
	    if (navigator.appName.substring(0,3) == "Net") 
		{
			tempX = e.pageX;
			if(prev_mouseX>tempX) tempX = prev_mouseX;
			
			
			mouseX = e.pageX;
			if(mouseX>prev_mouseX) direction = 0;
			else direction = 1;
			prev_mouseX = mouseX;
	    } 
		else 
		{
			tempX = event.x+document.body.scrollLeft;
	    }
	 	move();
	}
  }
  
  document.getElementById("dragcontrol").onmousemove = position;
  document.getElementById("dragcontrol").onmouseout = out; 
  document.getElementById("dragcontrol").onmousedown = function(){drag=true;};
  document.getElementById("dragcontrol").onmouseup = function(){drag=false;out();};
 
  
}


/*function AllowDrag()
{
	drag = true;
}

function DisallowDrag()
{
	grad = false;
}*/

// on mouse move on main container
function move() 
{	
	//alert(document.getElementById("container").offsetWidth-centerx);
	bob = document.getElementById("dragcontrol").offsetWidth-centerx;
	//alert("TempX:"+tempX+"-CenterX:"+centerx+"="+(tempX - centerx));
	//alert(bob);
	
	
	
	
	moveCarousel=true;
	
	//if(!direction) speed = (tempX - centerx) / 2500;
	
	if(dragMax<tempX)
	{
		dragMax = tempX;
		if(!direction) speed = 0.6;
		else
		{
			slowdown = true;
		}
		moveCarousel=true;
	}
	else
	{
		if(!direction) speed = 0.6;
		else
		{
			moveCarousel=false;
		}
		slowdown = false;
	}
	
	
	
	
	
	//speed = 0.1;
	
	//if(direction==1) speed = speed * -1;
	
	
	//if(speed>previousSpeed) direction = 0;
	//else direction = 1;
	
	//previousSpeed = speed;
	//base = 0.05;	
}

var slowdownFactor = 1;
var noOfSlows = 0;

function Slow()
{
	if(slowdown)
	{
		//alert("HERE");
		if(slowdownFactor<0.01)
		{
		//alert(speed);
			//speed = 0.01;
			//clearInterval(slowdown);
			slowdown=false;
			moveCarousel = false;
			slowdownFactor = 1;
			counter = counter-1;
		}
		else
		{
			counter = counter -1 + slowdownFactor;
			slowdownFactor -= 0.01
		}
	}
}

// on mouse out from main container
function out() 
{	
	//speed =0.01;
	//slowdown = setInterval("Slow()", 1000);
	slowdown = true;
	dragMax = 0;
	//if(speed<0)alert(speed);
	//speed = 0.05;
	//speed = 0.01;
	//speed = speed;	
}




// Change image event to back
function textIt(elt) {
  elt = $(elt);
  elt.removeEvent('click', Click);
  elt.addEvent('click', Back);
}

// Change image event to click
function untextIt(elt) {
  elt = $(elt);
  elt.removeEvent('click', Back);
  elt.addEvent('click', Click);
}

// Back to carousel
function Back(elt) {

  if(typeof elt == 'object') { elt = this.id; }
  
  document.getElementById("dragcontroltext").style.display = "";
	document.getElementById("dragcontrol").style.backgroundImage = "";
  // define actions
  var appearer = '';
  elements.each(function(photo) 
  {
    if(photo!=elt) { 
      appearer += "var AppearEffect = new Fx.Morph($('"+photo+"'), {duration: 500, transition: Fx.Transitions.Sine.easeOut }); AppearEffect.start({visibility: ['hidden', 'visible'], 'opacity': [0, 1]});";
    } else {
     appearer += "$('"+photo+"').morph({left: "+elt_posx+", top: "+elt_posy+"});";
   }
  });
  appearer += "var TextEffect = new Fx.Morph($('guitarinfo'), {duration: 500, transition: Fx.Transitions.Sine.easeOut, onComplete: function() { untextIt('"+elt+"'); $('guitarinfo').innerHTML = ''; b = setInterval('carousel()',40); } }); TextEffect.start({visibility: ['visible', 'hidden'], 'opacity': [1, 0]});";

  // do actions
  eval(appearer);  
  
}

function Click() {

  elt = this.id;
  // stop carousel
  $clear(b);
  
  // get clicked element positions
  elt_posx = this.getStyle('left').toInt();
  elt_posy = this.getStyle('top').toInt();
  
  // define actions
 var fader= '';
 elements.each(function(photo) 
 {
    if(photo!=elt) 
	{ 
      fader += "DropOut('"+photo+"');";
    } 
	else 
	{
      fader += "$('"+photo+"').morph({left: 50, top: 70});";
    }
}
  );
 
  fader += "TextEffect = new Fx.Morph($('guitarinfo'), {duration: 500, transition: Fx.Transitions.Sine.easeOut, onStart: function() { requestText('"+elt+"') }, onComplete: function() { textIt('"+elt+"') } }); TextEffect.start({visibility: ['hidden', 'visible'], 'opacity': [0, 1]});";

  // do actions
  eval(fader); 
  
}

// request text
function requestText(elt) 
{
	//var url = includes+'page.php'; 
	//alert('Guitars/'+elt+'/index.php');
	document.getElementById("dragcontroltext").style.display = "none";
	document.getElementById("dragcontrol").style.backgroundImage = "none";
	
  //var url =  '/Guitars/'+elt+'/index.php'; 
  var url =  '/Guitars/'+elt+'/index.php'; 
  new Request.HTML({url:url, 
    onSuccess: function(html) 
	{
      $('guitarinfo').adopt(html);
    },
    onFailure: function() 
	{
      $('guitarinfo').set('html', 'An error occurred. Please re-try.');
    }
  }).get({'item': elt});
 
  
}

// carousel 
function carousel() 
{
	if(moveCarousel)
	{
		Slow();

		var indexer = 0;
		elements.each(function(photo, i) 
		{
			//if(direction==1) i = elements.length-indexer+1;
		
			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;
			
			$(photo).setStyles({ left: posx+'px', top: posy+'px', zIndex: Math.round(posy/3)+100 });
			
			
			
				bob = tempX;
				$(photo).setAttribute("direction", direction);
			
			//angle += speed;
			indexer++;		
		});	
		counter++
	}
}



function disabletext(e){
return false
}

function reEnable(){
return true
}

//if the browser is IE4+
document.onselectstart=new Function ("return false")

//if the browser is NS6
if (window.sidebar){
document.onmousedown=disabletext
document.onclick=reEnable
}
