var Guillon = Class.create();
Guillon.prototype = {

	initialize: function()
	{
		if ($('navBottom')) {
			$$('#navBottom .top').invoke('observe', 'mouseover', this.showTopSubMenu.bindAsEventListener(this));
			$$('#navBottom .top').invoke('observe', 'mouseout', this.hideTopSubMenu.bindAsEventListener(this));
		}
		
		// bubbles
		$$('#right-block .glass').invoke('observe', 'mouseover', this.showBubble.bindAsEventListener(this));
		$$('#right-block .glass').invoke('observe', 'mouseout', this.waitHideBubble.bindAsEventListener(this));

		$$('#right-block .view-glass').invoke('observe', 'click', this.sendViewGlass.bindAsEventListener(this));

		$$('#single-thumb .single-thumb').invoke('observe', 'click', this.switchSunglassImg.bindAsEventListener(this));
		$$('#single-thumb .single-thumb').invoke('observe', 'mouseover', this.sgMouseOver.bindAsEventListener(this));

		if ($('sg-main-thumb')) {
			var thumbs = $('sg-main-thumb').childElements();
			thumbs.invoke('observe', 'mouseover', this.startZoomSunglass.bindAsEventListener(this));
			thumbs.invoke('observe', 'mouseout', this.endZoomSunglass.bindAsEventListener(this));
		}

		$$('#customer_comment .comment_nav').invoke('observe', 'mouseover', this.commentNavMouseOver.bindAsEventListener(this));
		$$('#customer_comment .comment_nav').invoke('observe', 'mouseout', this.commentNavMouseOut.bindAsEventListener(this));

		this.timerId = 0;

		if ($('press_less')) {
			$('press_less').observe('mouseover', this.pressScrollRight.bindAsEventListener(this));
			$('press_less').observe('mouseout', this.pressScrollStop.bindAsEventListener(this));
		}
		if ($('press_more')) {
			$('press_more').observe('mouseover', this.pressScrollLeft.bindAsEventListener(this));
			$('press_more').observe('mouseout', this.pressScrollStop.bindAsEventListener(this));
		}
		if ($('press_frame')) {
			window['press'].scrollTo(0, 0);
		}
		$$('.press_image_link').invoke('observe', 'click', this.pressShowImage.bindAsEventListener(this));

		// home page scrolling text
		if ($('scrolling-text-home')) {
			this.doTextScrolling($('scrolling-text-home'));
		}
		// about page scrolling text
		if ($('scrolling')) {
            this.doTextScrolling($('scrolling'));
		}

		if ($('fontPlus')) {
			$('fontPlus').observe('mouseover', this.fontPlusMouseOver.bindAsEventListener(this));
			$('fontPlus').observe('mouseout', this.fontPlusMouseOut.bindAsEventListener(this));
		}
		if ($('fontMinus')) {
			$('fontMinus').observe('mouseover', this.fontMinusMouseOver.bindAsEventListener(this));
			$('fontMinus').observe('mouseout', this.fontMinusMouseOut.bindAsEventListener(this));
		}
		if ($('fontPlus')) {
			$('fontPlus').observe('click', this.increaseFont.bindAsEventListener(this));
		}
		if ($('fontMinus')) {
			$('fontMinus').observe('click', this.decreaseFont.bindAsEventListener(this));
		}
		
		$$('#menu .submenu-head').invoke('observe', 'mouseover', this.showSubMenu.bindAsEventListener(this));
		$$('#menu .submenu-list').each(function(elm) {
				Event.observe(elm, 'mouseout', function(e) {
						e.stop();
						var ul = elm.down('ul.subsubmenu');
						if (ul) {
							var t = typeof e.relatedTarget != 'undefined' ? e.relatedTarget : e.toElement;
							if (!t.descendantOf(elm)) {
								ul.setStyle({left : '-100em'});
							}
						}
					});
			});
	},

	showSubMenu : function(e)
	{
		e.stop();
		var t = e.element();
		if (!t) return;

		var ul = t.next('ul.subsubmenu');
		if (ul) {
			ul.setStyle({left : 'auto'});
		}
	},

	increaseFont : function(e)
	{
		$$('#main-block .slurp').each(function(s) {
				var size = s.getStyle('font-size');
				if (size.lastIndexOf('px') != -1) {
					size = Number(size.replace('px', ''));
					if (size > 20) return;
					size++;
					s.setStyle({fontSize : size + 'px'});
				}
				else if (size.lastIndexOf('pt') != -1) {
					size = Number(size.replace('pt', ''));
					if (size > 15) return;
					size++;
					s.setStyle({fontSize : size + 'pt'});
				}
			});

		if ($('text-block')) {
			var s = $('text-block');
			var size = s.getStyle('font-size');
			if (size.lastIndexOf('px') != -1) {
				size = Number(size.replace('px', ''));
				if (size > 20) return;
				size++;
				s.setStyle({fontSize : size + 'px'});
			}
			else if (size.lastIndexOf('pt') != -1) {
				size = Number(size.replace('pt', ''));
				if (size > 15) return;
				size++;
				s.setStyle({fontSize : size + 'pt'});
			}			
		}
	},

	decreaseFont : function(e)
	{
		$$('#main-block .slurp').each(function(s) {
				var size = s.getStyle('font-size');
				if (size.lastIndexOf('px') != -1) {
					size = Number(size.replace('px', ''));
					if (size < 10) return;
					size--;
					s.setStyle({fontSize : size + 'px'});
				}
				else if (size.lastIndexOf('pt') != -1) {
					size = Number(size.replace('pt', ''));
					if (size < 7) return;
					size--;
					s.setStyle({fontSize : size + 'pt'});
				}
			});

		if ($('text-block')) {
			var s = $('text-block');
			var size = s.getStyle('font-size');
			if (size.lastIndexOf('px') != -1) {
				size = Number(size.replace('px', ''));
				if (size < 10) return;
				size--;
				s.setStyle({fontSize : size + 'px'});
			}
			else if (size.lastIndexOf('pt') != -1) {
				size = Number(size.replace('pt', ''));
				if (size < 7) return;
				size--;
				s.setStyle({fontSize : size + 'pt'});
			}
		}
	},

	fontPlusMouseOver : function(e)
	{
		var t = e.element();
		if (!t) return;

		t.setStyle({cursor : 'pointer'});
	},

	fontPlusMouseOut : function(e)
	{
		var t = e.element();
		if (!t) return;

		t.setStyle({cursor : 'default'});
	},

	fontMinusMouseOver : function(e)
	{
		var t = e.element();
		if (!t) return;

		t.setStyle({cursor : 'pointer'});
	},

	fontMinusMouseOut : function(e)
	{
		var t = e.element();
		if (!t) return;

		t.setStyle({cursor : 'default'});
	},


	doTextScrolling : function(elem)
	{
		var s = elem.innerHTML;
		s = s.replace(/&nbsp;/, '_');
		var s_array = s.toArray();
		var c = s_array.shift();
		s_array.push(c);
		var str = s_array.join('');
		elem.innerHTML = str.replace(/_/g, '&nbsp;');

		if (this.timerId != 0)
        {
            clearTimeout(this.timerId);
            this.timerId = 0;
        }
		var self = this;
        this.timerId = setTimeout(function() {self.doTextScrolling(elem);}, 333);
	},

	pressShowImage : function(e)
	{
		e.stop();
		var t = e.element();
		if (t.nodeName == 'IMG') {
			t = $(t.parentNode);
		}
		if (t.nodeName != 'A') return;

		window.open(t.href,'','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1');
	},

	pressScrollRight : function(e)
	{
		if (this.timerId != 0)
        {
            clearTimeout(this.timerId);
            this.timerId = 0;
        }
        window['press'].scrollBy(-25, 0);
		var self = this;
        this.timerId = setTimeout(function() {self.pressScrollRight();}, 100);
	},

	pressScrollLeft : function(e)
	{
		if (this.timerId != 0)
        {
            clearTimeout(this.timerId);
            this.timerId = 0;
        }
        window['press'].scrollBy(25, 0);
		var self = this;
        this.timerId = setTimeout(function() {self.pressScrollLeft();}, 100);
				
	},

	pressScrollStop : function(e)
	{
		if (this.timerId != 0)
        {
            clearTimeout(this.timerId);
            this.timerId = 0;
        }		
	},

	commentNavMouseOver : function(e)
	{
		var t = e.element();
		if (!t || t.nodeName != 'INPUT') return;
		t.setStyle({color:'#eb008b'});
		t.setStyle({cursor : 'pointer'});
	},

	commentNavMouseOut : function(e)
	{
		var t = e.element();
		if (!t || t.nodeName != 'INPUT') return;
		t.setStyle({color:'#77787b'});
		t.setStyle({cursor : 'default'});
	},

	startZoomSunglass : function(e)
	{
		var t = e.element();
		if (!t || t.nodeName != 'IMG') return;

		t.setStyle({cursor : 'crosshair'});
		t.observe('mousemove', this.zoomSunglass.bindAsEventListener(this));
	},

	endZoomSunglass : function(e)
	{
		var t = e.element();
		if (!t || t.nodeName != 'IMG') return;
		t.setStyle({cursor : 'default'});
		t.stopObserving('mousemove', this.zoomSunglass.bindAsEventListener(this));

		var zoomImg = null;
		if ($('sg-main-img')) {
			var imgs = $('sg-main-img').descendants();
			imgs.each(function(i) {
					if(i.visible()) {
						zoomImg = i;
					}
				});
		}
		if (zoomImg) {
			zoomImg.setStyle({marginTop: '30px'});
			zoomImg.setStyle({marginLeft: '-110px'});
		}
	},
	
	zoomSunglass : function(e)
	{
		var t = e.element();
		if (!t || t.nodeName != 'IMG') return;

		var zoomImg = null;
		if ($('sg-main-img')) {
			var imgs = $('sg-main-img').descendants();
			imgs.each(function(i) {
					if(i.visible()) {
						zoomImg = i;
					}
				});
		}
		if (zoomImg) {

			var scrollOffset = this.getScrollXY();
			var yOffset = scrollOffset[1];
			var xOffset = scrollOffset[0];

			var top = Number(e.pointerY() - (t.viewportOffset().top + yOffset));
			var left = Number(e.pointerX() - (t.viewportOffset().left + xOffset));

			var containerCenter = Number((zoomImg.parentNode.getWidth() / 2));
			var containerMiddle = Number((zoomImg.parentNode.getHeight() / 2));
			var leftPos = Number(containerCenter - (left * 4));
			var topPos = Number((containerMiddle - (top * 4)) - 15);

			zoomImg.setStyle({marginTop: topPos + 'px'});
			zoomImg.setStyle({marginLeft: leftPos  + 'px'});
		}	
	},
	
	getScrollXY : function() 
	{
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return [ scrOfX, scrOfY ];
	},

	sgMouseOver : function(e)
	{
		var t = e.element();
		if (!t || t.nodeName != 'IMG') return;
		t.setStyle({cursor: 'pointer'});
	},

	switchSunglassImg : function(e)
	{
		var t = e.element();
		if (!t || t.nodeName != 'IMG') return;
		
		if ($('sg-main-thumb')) {
			var imgs = $('sg-main-thumb').childElements();
			if (imgs) {
				imgs.each(function(s) {
						if (s.className.match('front') && t.alt.match('front')) {
							s.show();
						}
						else if (s.className.match('angle') && t.alt.match('angle')) {
							s.show();
						}
						else if (s.className.match('side') && t.alt.match('side')) {
							s.show();
						}
						else if (s.nodeName == 'A') {
							s.hide();
						}
					});
			}
		}

		if ($('sg-main-img-' + t.alt)) {
			if ($('sg-main-img-'+t.alt)) $('sg-main-img-'+t.alt).show();
		}
		var sides = ['front','angle','side'];
		sides.each(function(i) {
				if ($('sg-main-img-'+i) && i != t.alt) {
					$('sg-main-img-'+i).hide();
				}
			});
	},

	sendViewGlass : function(e)
	{
		e.stop();
		var t = e.element();
		if (!t || t.nodeName != 'IMG') return;
		var link = $(t.parentNode);
		if (link.nodeName != 'A') return;

		// hide the bubble before navigating to single glass view
		var bubble = $(t.parentNode).next();
		if (bubble) bubble.hide();
		
		var search = window.location.search.empty() ? '' : '&' +  window.location.search.replace('?', '');
		window.location.href = link.href + search;
	},

	getQueryParam : function(name, query)
	{
		if (query == null || query.empty()) {
			query = window.location.href;
		}
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec(query);
		if( results == null )
			return "";
		else
			return results[1];
	},

	showTopSubMenu : function(e)
	{
		var t = e.element();
		if (!$(t).hasClassName('top')) {
			t = $(t).up('.top');
		}

		$(t).setStyle({borderTop : '1px solid #eb008b',
					   borderRight: '1px solid #eb008b',
					   borderLeft: '1px solid #eb008b'});

		$(t).select('ul.sub').invoke('show');
		var pos = $(t).positionedOffset();
		var h = $(t).getHeight();
		pos.top += h;
		$(t).select('ul.sub').each(function(s) {
				s.setStyle({left:pos.left + "px",
							top:pos.top + "px"});
			});
	},


	hideTopSubMenu : function(e)
	{
		$$('#navBottom .top').each(function(s) {
				
				s.setStyle({borderTop : '1px solid #fff',
							borderRight: '1px solid #fff',
							borderLeft: '1px solid #fff'});
			});

		$$('#navBottom .sub').invoke('hide');
	},

	showBubble : function(e)
	{
		var t = e.element();
		if (!t) return;
		if (t.nodeName != 'IMG') return;
		var pos = $(t).positionedOffset();
		var bubble = $(t.parentNode).next();
		var left_add = 0;
		var top_add = 0;

		if ($(bubble).className == 'bubble-round') {
			left_add = ($(t).getWidth() - 50);
			top_add = 10;
		}
		else if ($(bubble).className == 'bubble-square') {
			left_add = ($(t).getWidth() - 50);
			top_add = ($(t).getHeight() / 2) + 10;
		}

		var top = pos.top + top_add;
		var left = pos.left + left_add;

		$(bubble).setStyle({top:top+'px',left:left+'px'});
		//$(bubble).appear({duration:1});		
		$(bubble).show();
	},

	waitHideBubble : function(e)
	{
		setTimeout(function() {
				var t = e.element();
				if (!t || t.nodeName != 'IMG') return;
				var bubble = $(t.parentNode).next();
				$(bubble).hide();
			}, 500);
	}
}
function init() {
	new Guillon();
}
Event.observe(window, 'load', init);
