var gf = new Class({

	initialize: function() {

		this.resizeTimeout = null;
		this.fadeDuration = 2100;
		this.initialDelay = 750;
		this.initialized = true;
		this.currentStyle = null;
		this.gf_scroller = null;
		this.imgDomPath = 'img.portfolio_img';
		this.initMainMenu();
		this.initExternalLinks();
		
		if ($('portfolio')) this.initGallery();

	},
	

	
	initMainMenu: function() {
		var menu = $('homepage_menu');
		if (menu) {
			menu.getChildren().getChildren('ul').each(function(el) {
				if (el.length > 0) {
					el.set('reveal', {duration: 'long', transition: 'bounce:in'});
					el.set('dissolve', {duration: 'long', transition: 'bounce:out'});
					el.getParent().getChildren('a').shift().addEvent('click', function(e) {
						e.stop;
						if (el.getStyle('display') == 'none') el.reveal();
						else el.dissolve();
						return false;
					});
				}
			});
		}
	},
	
	initGallery: function() {
		
		this.gf_scroller = new Scrollbar(
			$('portfolio_wrapper'), 
			'#portfolio_scroll_content', 
			'#portfolio_scrollbar', 
			'#portfolio_scroll_handle', 
			{	mode: 'horizontal'	}
		);
		
		this.gf_scroller.scrollbar.addEvent('mouseover', function(e) {
			this.gf_scroller.knobTop.removeClass('knobTop');
			this.gf_scroller.knobMiddle.removeClass('knobMiddle');
			this.gf_scroller.knobBottom.removeClass('knobBottom');
			this.gf_scroller.knobTop.addClass('knobTopOver');
			this.gf_scroller.knobMiddle.addClass('knobMiddleOver');
			this.gf_scroller.knobBottom.addClass('knobBottomOver');
		}.bind(this));
		
		this.gf_scroller.scrollbar.addEvent('mouseout', function(e) {
			this.gf_scroller.knobTop.removeClass('knobTopOver');
			this.gf_scroller.knobMiddle.removeClass('knobMiddleOver');
			this.gf_scroller.knobBottom.removeClass('knobBottomOver');
			this.gf_scroller.knobTop.addClass('knobTop');
			this.gf_scroller.knobMiddle.addClass('knobMiddle');
			this.gf_scroller.knobBottom.addClass('knobBottom');
		}.bind(this));
		
		
		$('portfolio_container').setStyle('height', '1px');
	},
	
	doResize: function() {
		if ($('portfolio')) {
			if(typeof this.gf_scroller != "undefined") {	
				this.gf_scroller.refresh();
			}
		}
	},

	prepareResize: function() {
		$clear(this.resizeTimeout);
		this.resizeTimeout = this.doResize.delay(500, this);
	},
	
	initExternalLinks: function() {
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
				anchor.target = "_blank";
			}
		}
	},
	
	suppressRightClick: function(el) {
		el.addEvent('contextmenu', function(e){
			var e = new Event(e).stop();
		});
	}
	


});


//////////////////////////////////////////////////////////////

var gfobj;
var iscroll;

//////////////////////////////////////////////////////////////

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {

	function iLoaded() {
		var body = $(document.body).set('styles', {
		    'height': '1000px',
		    'overflow': 'auto'
		});

		if ($('portfolio')) {
			$('portfolio_scrollbar').setStyle('display', 'none');
			$('portfolio_wrapper').set('styles', {
				'width': '94%',
				'margin': '0 auto',
				'height': '460px'
			});
			
			//window.addEventListener('onorientationchange' in window ? 'orientationchange' : 'resize', this, false);
			//document.addEventListener('touchmove', function(e){ e.preventDefault(); });
			myScroll = new iScroll('portfolio_scroll_content', {
				bounce: false,
				checkDOMChanges: true,
				topOnDOMChanges: false,
				hScrollbar: true,
				vScrollbar: true,
				desktopCompatibility: false,
				overflow: 'auto'
			});
		}
	}
	
	document.addEventListener('DOMContentLoaded', iLoaded, false);
	
} else {

	window.addEvent('domready', function() {
		gfobj = new gf();
	});

	window.addEvent('resize', function() {
		if (gfobj.initialized) {
			gfobj.prepareResize();
		}
	});
	
}
