var aa = {

	initHomepageSlideshow: function(TI, IMG) {
	
		var results = $("home");
		if(results) {
			var totalImages = TI;
			//singleImg (IMG) is the number of the image used if totalImages (TI) == 1
			var singleImg = IMG;
			
			var container = new Element('div');
			$(container).addClassName('slideshow-container');
			
			var link = new Element('a');
			link.setAttribute('href', 'http://accessoryartists.com/index.php/collections/art-meets-fashion.html');
			$(container).appendChild(link);
//			alert(container);
			
			if(totalImages == 1){
				if(typeof singleImg == 'undefined'){
					console.log(singleImg);
				} else {
					var image = new Image(935, 354);
					image.src = '/skin/frontend/default/accessoryartists/images/homepage-slideshow/new-' + singleImg + '.jpg';
					$(link).appendChild(image);
					$('home').appendChild(container);
				}
			
			}
			else{
				for(var c=1; c<=totalImages; c++) {
					var image = new Image(935,347);
					image.src = '/skin/frontend/default/accessoryartists/images/homepage-slideshow/new-' + c + '.jpg';
					image.click = 'alert();';
				
					$(link).appendChild(image);
				}
				$("home").appendChild(container);
				var slideshow = new iSlideShow({ autostart:true,start:1,wait:3000,slides: $$("#home .slideshow-container img") });
				//slideshow.startSlideShow();
			}
		}
	},
	
	initNavigation: function() {		
		$("navigation").observe('mousemove',function(event) {
			if($(event.findElement('li'))) {
				//hide sub nav
				$$('ul.subnavigation').each(function(item) { item.hide(); });	
				$$('#navigation a, #navigation li').each(function(item) { item.removeClassName('navlinkon'); });	
							
				var className 	= $(event.findElement('li')).classNames();
				className 		= className.toArray().pop();
				$$('ul.sub-nav-' + className).each(function(item) { item.show(); });
				
				
				$(event.findElement('li')).addClassName('navlinkon');
				$(Selector.findChildElements(event.findElement('li'),['a'])[0]).addClassName('navlinkon');
			}
				
		})
		
	},
	
    initCarousel: function() {
        //see if the carousel exists
        if($$('.carousel').length == 0) {
            return;
        }
        
        //go through all product uls and find their width,
        //then set the width of the 
        var uls     = $$('.carousel .products-grid');
        var width   = 0;
        for(var index = 0; index < uls.length; index ++) {
            width += Element.getWidth(uls[index]) + 10;
        }
        
        if(Prototype.Browser.IE) {
            width += 150;
        }
        
        //set width of inner div to hold all images in one row
        $$('.carousel')[0].down().setStyle({width: width + 'px'});
        
        //inject next and previous buttons
        var prevButton = new Element('a', { 'class': 'btn btn-previous', 'href': '#'});
        prevButton.className = 'btn btn-previous';
        $$('.carousel')[0].insert({before:prevButton});
        
        var nextButton = new Element('a', { 'class': 'btn btn-next', 'href': '#'});
        nextButton.className = 'btn btn-next';
        $$('.carousel')[0].insert({after:nextButton});
        
        var scrollValue = 0;
        var maxScroll   = width - $$('.carousel')[0].getWidth();
        
       
        
        //drop in scrolling actions
        var scrollDirection = function(positiveOrNegativeOne) {

            scrollValue += 150 * positiveOrNegativeOne;
            
            if(scrollValue < 0) {
                scrollValue = 0;
            } else if(scrollValue > maxScroll) {
                scrollValue = maxScroll;
            }
            
            new Effect.Scroll($$('.carousel')[0],{x: scrollValue, duration: 0.5 });
        }
        
        prevButton.observe('click',function(event) {
            scrollDirection(-1);
            Event.stop(event);
        });
        
        nextButton.observe('click',function(event) {
            scrollDirection(1);
            Event.stop(event);
        });
    }
};

$ready = Element.observe.curry(document, 'dom:loaded');

jQuery(document).ready(function(){
    //check for carousel
    aa.initCarousel();
    
    //initialize slideshow / nav
    aa.initHomepageSlideshow(1,1);
    aa.initNavigation();

	

Effect.Scroll = Class.create();
Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'absolute'
    } , arguments[1] || {}  );
    this.start(options);
  },
  setup: function() {
    if (this.options.continuous && !this.element._ext ) {
      this.element.cleanWhitespace();
      this.element._ext=true;
      this.element.appendChild(this.element.firstChild);
    }

    this.originalLeft=this.element.scrollLeft;
    this.originalTop=this.element.scrollTop;

    if(this.options.mode == 'absolute') {
      this.options.x -= this.originalLeft;
      this.options.y -= this.originalTop;
    } else {

    }
  },
  update: function(position) {   
    this.element.scrollLeft = this.options.x * position + this.originalLeft;
    this.element.scrollTop  = this.options.y * position + this.originalTop;
  }
});
});

