/*
	Rotates content by fading.
*/
var rotator = {
	elements : Array(),
	current : 0,
	seconds : 5,
	interval : null,
	
	init : function(selector, seconds) {
		rotator.elements = $(selector);
		rotator.seconds = seconds*1000;
	},
	
	start : function() {
		rotator.stop(); /* stop it just in case already playing */
		rotator.interval = setInterval("rotator.next()", rotator.seconds);
	},
	
	stop : function() {
		clearInterval(rotator.interval);
	},
	
	next : function() {		
		var show = (rotator.current < rotator.elements.length-1) ? rotator.current+1 : 0;
		rotator.swap(show);
	},	

	prev : function() {
		var show = (rotator.current == 0) ? rotator.elements.length-1 : rotator.current-1;
		rotator.swap(show);		
	},	
	
	swap : function(indexToShow, anchor) {
		if(rotator.interval){ rotator.start();}
		$(rotator.elements[rotator.current]).fadeOut(200, function(){
			$(rotator.elements[indexToShow]).fadeIn();
			rotator.current = indexToShow;
		});
		if(anchor){
			$(anchor).siblings('a').each(function(){
				$(this).removeClass('active');
			})
			$(anchor).addClass('active');
		}
	}
}

var anchor = {
	get : function() {
		var url = document.location.toString();
		if (url.match('#')) { 
			return url.split('#')[1];
		} else {
			return false;
		}		
	},
	set : function(val) {
		//redirect.go('#'+val);
	}
}

var d = document;
var dsS = d.styleSheets;
if (dsS[0].insertRule) dsS[0].insertRule("html { overflow-x: hidden; }", dsS[0].cssRules.length); // W3C DOM2 Style
else if (dsS[0].addRule) dsS[0].addRule("html", "overflow-x: hidden"); // Microsoft


var gallery = {
	items : Array(),
	currentIndex : 0,
	holder : null,
	step : 3,
	indicator : null,
	init : function() {
		var startIndex = parseInt(anchor.get());
		gallery.holder=$('div#gallery');
		gallery.indicator=$('div#gallery-header .h2-label strong');
		var itemsArray = gallery.holder.find('div.work');
		for(var i=0;i<itemsArray.length;i++){
			$(itemsArray[i]).bind('mouseover', i, function(e){
				for(var j=0;j<itemsArray.length;j++){
					if(j != e.data){
						$(itemsArray[j]).find('img.workimg').stop().fadeTo(200, 0.50);
					}else{
						$(itemsArray[j]).find('h1.work-header').show();
					}
				}
			});
			$(itemsArray[i]).bind('mouseout', i, function(e){
				for(var j=0;j<itemsArray.length;j++){
					$(itemsArray[j]).find('img.workimg').stop().fadeTo(200, 1);
					$(itemsArray[j]).find('h1.work-header').hide();
				}
			});
			gallery.items.push({div:itemsArray[i], pos:($(itemsArray[i]).attr('rel')*-1)})
		}
		if(startIndex){ gallery.goToIndex(startIndex, true) }
	},
	next : function() {
		if(!gallery.items[gallery.currentIndex+gallery.step]){ gallery.goToIndex(0); return false;}
		gallery.goToIndex(gallery.currentIndex+gallery.step);
		
	},
	prev : function() {
		if(!gallery.items[gallery.currentIndex-gallery.step]){ gallery.goToIndex(0); return false; }
		gallery.goToIndex(gallery.currentIndex-gallery.step);

	},
	goToIndex : function(newIndex, jump) {	    
		var newPos = gallery.items[newIndex].pos;
		gallery.currentIndex = newIndex;
		
		var remainingGalleryWidth = (gallery.items.length)*325 + newPos;
		if(remainingGalleryWidth<$('body').width())
		    newPos = newPos+($('body').width()-remainingGalleryWidth);
		
		var indicatorStart = gallery.currentIndex+1;
		var indicatorEnd = indicatorStart+2;
		while(indicatorEnd > gallery.items.length){ indicatorEnd--; }
		gallery.indicator.text(indicatorStart+' - '+indicatorEnd);
		
		
		if(jump){ gallery.jump(newPos); }
		else{    gallery.slide(newPos); }
		
		anchor.set(newIndex);

		if(gallery.currentIndex==0){ 
			$('a#gallery-prev').removeClass('active');
		}else{
			$('a#gallery-prev').addClass('active');
		}
		
		if(!gallery.items[gallery.currentIndex+gallery.step]){ 
			$('a#gallery-next').removeClass('active');
		}else{
			$('a#gallery-next').addClass('active');
		}
	},
	slide : function (pos) {
		gallery.holder.animate({ left: pos }, 900 );
	},
	jump : function (pos) {
		gallery.holder.css('left', pos);
	}
	
}