window.onresize = resize;
var binder = null;

function detectIE6(){
  var browser = navigator.appName;
  if (browser == "Microsoft Internet Explorer"){
    var b_version = navigator.appVersion;
    var re = /\MSIE\s+(\d\.\d\b)/;
    var res = b_version.match(re);
    if (res[1] <= 6){
      return true;
    }
  }
  return false;
}
	
function resize() {
	ns4 = (document.layers)? true:false
	ie4 = (document.all)? true:false
	winW = (ns4)? window.innerWidth : document.body.offsetWidth
	winH = (ns4)? window.innerHeight : document.body.offsetHeight
	$('photoFeedContainer').setStyle('width', winW-280);
}


		
function galleryRequest(arg){
	var target = $('photoFeedContainer').getElement('div.highslide-gallery ul');
	var reqImg = new Request.HTML({url:'data.php'+arg, 
		onSuccess: function(html) {	
			var galleryWidth = 0;
			var w = 0;
			target.empty();
			target.adopt(html);
			target.getElements('img').each(function(img){ 
				w = parseInt(img.getProperty('width'));
				galleryWidth += w;
				img.getParent('li').setStyle('width', w+'px')
				img.setStyle('opacity', .4);
				img.addEvents({
				    'mouseenter': function(){
				        this.fade(1);
				        window.status='';
				    },
				    'mouseleave': function(){
				        this.fade(.4);
				    }
				});
			});
			target.getParent().setStyle('width', parseInt(galleryWidth)+'px');
			$('waiting').setStyle('display','none');
			$('photoFeedContainer').fade(0,1);
			$('photoFeedContainer').scrollTo(0,0);
		},
		onFailure: function() {
			target.empty();
			target.set('text', 'The request failed.');
		}
	});
	reqImg.send();
}
	
function menuInit(){
	var target = $('menu').getElements('img');
	target.each(function(img){ 
		img.set('class', 'hand');
		img.set('width', '69');
		img.set('height', '16');
		img.set('vspace', '5');
		img.set('border', '0');
	});
	target[0].addEvents({
		'mouseenter': function(){ this.set('src', 'img/menu_72157627852192585_.gif'); },
	    'mouseleave': function(){ this.set('src', 'img/menu_72157627852192585.gif'); },
	    'click': function(){ menuClick(target, '72157627852192585'); }
	});
	target[1].addEvents({
		'mouseenter': function(){ this.set('src', 'img/menu_72157628009559996_.gif'); },
	    'mouseleave': function(){ this.set('src', 'img/menu_72157628009559996.gif'); },
	    'click': function(){ menuClick(target, '72157628009559996'); }
	});
	target[2].addEvents({
		'mouseenter': function(){ this.set('src', 'img/menu_72157627884924153_.gif'); },
	    'mouseleave': function(){ this.set('src', 'img/menu_72157627884924153.gif'); },
	    'click': function(){ menuClick(target, '72157627884924153'); }
	});
	target[3].addEvents({
		'mouseenter': function(){ this.set('src', 'img/menu_72157628009538836_.gif'); },
	    'mouseleave': function(){ this.set('src', 'img/menu_72157628009538836.gif'); },
	    'click': function(){ menuClick(target, '72157628009538836'); }
	});
	target[4].addEvents({
		'mouseenter': function(){ this.set('src', 'img/menu_72157628009581862_.gif'); },
	    'mouseleave': function(){ this.set('src', 'img/menu_72157628009581862.gif'); },
	    'click': function(){ menuClick(target, '72157628009581862'); }
	});
}

function menuClick(target, i){
	menuTrim(target); 
	target.each(function(img){ 
		img.removeEvents(); 
	});
	menuInit();
	
	$(i).removeEvents();
	$(i).set('class', '');
	$(i).set('src', 'img/menu_'+i+'_.gif');
	
	hs.close();
	var myFx = new Fx.Tween('photoFeedContainer');
	myFx.start('opacity', 1, 0).chain(function(){
		$('waiting').setStyle('display','block');
		galleryRequest('?g='+i)
	});
	if (!binder) {
		var myScroller = new Scroller('photoFeedContainer', {area: 300, velocity:.2});
		binder = $('photoFeedContainer').addEvent('mouseover', myScroller.start.bind(myScroller));
	}		
	
}
function menuTrim(target){
	/*
target.each(function(img){ 
		img.set('src', img.get('src').substr(0,10)+'.gif');
		
	});
*/
	target.each(function(img){ 
		img.set('src', img.get('src').replace(/_\.gif/, '\.gif'));
	});
}
