/* -----------------------------------*/
/* --->>> onDOMReady Extension <<<----*/
/* -----------------------------------*/


Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady(); 
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});

/* ------------------------- sarahj ------------------------- */

Event.observe(window, 'load',
	function() {
		thumb_swap();
	}
);

Event.onDOMReady(
	function() {
		collection_thumbs();
		max_width();
	}
);

function thumb_swap() {
	if($('item_thumbs')) {
		$$('div[class~=item_thumb] img').each(
			function(img) {
				Event.observe(img, 'click', 
					function() {
						if($('item_thumbs').down('div[class~=selected]')) {
							$('item_thumbs').down('div[class~=selected]').removeClassName('selected');
						}
						img.parentNode.addClassName('selected');
						$('item_fullsize').innerHTML = '<img src="' + img.src + '" alt="' + img.alt + '" />';
					}
				);
			}
		);
	}
}

function collection_thumbs() {
	if($('sidebar')) {
		if($('sidebar').down('div[class~=related_products]')) {
			if($('sidebar').down('div[class~=related_products]').down('div[class~=collection_wrap]')) {
				var thumbs = $('sidebar').down('div[class~=related_products]').down('div[class~=collection_wrap]').getElementsBySelector('a');
				var prev = false;
				var current = false;
				var next = false;
				thumbs.each(
					function(thumb) {
						if(thumb.href == window.location) {
							current = thumb;
							if(thumb.previous('a')) {
								prev = thumb.previous('a');
								prev.addClassName('prev');
							}
							if(thumb.next('a')) {
								next = thumb.next('a');
								next.addClassName('next');
							}
						}
						thumb.toggle();
					}
				);
				thumbs.each(
					function(thumb) {
						if(thumb != prev && thumb != next) {
							thumb.remove();
						}
					}
				);
				if(prev != false) {
					prev.toggle();
				}
				if(next != false) {
					next.toggle();
				}
				if(prev == false && next == false) {
					$('sidebar').down('div[class~=related_products]').previous('h2').remove();
					$('sidebar').down('div[class~=related_products]').remove();
				}
			}
		}
	}
}

function max_width() {
	if(navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('7.0') == -1) {
		$$('img').each(
			function(img) {
				if(img.getStyle('maxWidth')) {
					var maxwidth = img.getStyle('maxWidth').replace(/px/,'');
					if(maxwidth != none && img.getWidth > maxwidth) {
						img.style.width = maxwidth + 'px';
					}
				}
			}
		);
	}
}
