var tabPanel = {
	contents: null,
	current: null,
	tabs: null,
	init: function(){
		$('div.tabPanel ul').removeClass('hidden');
		this.contents = $('div.tabPanel div.tabContents div.tabContent');
		this.tabs = $('div.tabPanel ul.tabs li');
		
		this.tabs.each(function(i){
			if(!$(this).hasClass('active')){
				$(tabPanel.contents.get(i)).hide();
			} else {
				tabPanel.current = i;
			}
			$(this).click(function(e){
				tabPanel.showTab(i);
				e.preventDefault();
			});
		});
		if(this.current == null){
			this.showTab(0);
		}
	},
	showTab: function(i){
		if(i>=0 && i<tabPanel.contents.length){
			if(tabPanel.current != null){
				tabPanel.tabs.removeClass('activeleft').removeClass('activeright');
				$(tabPanel.tabs.get(tabPanel.current)).removeClass('active').removeClass('tableft').removeClass('tabright');
				$(tabPanel.contents.get(tabPanel.current)).hide();
			}
			tabPanel.current = i;
			$(tabPanel.tabs.get(tabPanel.current)).addClass('active');
			if(i>0){
				$(tabPanel.tabs.get(tabPanel.current)).addClass('tableft');
				$(tabPanel.tabs.get(tabPanel.current-1)).addClass('activeright');
			}
			if(i<tabPanel.contents.length-1){
				$(tabPanel.tabs.get(tabPanel.current)).addClass('tabright');
				$(tabPanel.tabs.get(tabPanel.current+1)).addClass('activeleft');
			}
			$(tabPanel.contents.get(tabPanel.current)).show();
		}
	}
};

$.fn.modal3d = function() {
	
	var overlay = $('#overlay'),
	    w = $($('html').get(0)),
		win = $('#modalWindow'),
		lists = {},
		currentList = null,
		currentNr = 0,
		currentListWrapper = null;
		
	overlay.click(function(){ hide(); });
	$('a.prev', this).click(function(e){
		showPrevPage();
		e.preventDefault();
	});
	$('a.next', this).click(function(e){
		showNextPage();
		e.preventDefault();
	});
	$('a.close', this).click(function(e){
		hide();
		e.preventDefault();
	});
	$('.modalImageList',this).each(function(i){
		lists[$(this).attr('id')] = $(this);
	});

	function show(galleryId){		
		currentList = $('#' + galleryId + ' .image');
		currentListWrapper = $('#' + galleryId).css('visibility','visible');
		currentList.removeClass('current').hide();
		$(currentList.get(0)).addClass('current');
		currentNr = 0;
		
		/* overlay */
		setOverlaySize();
		$(overlay).css({ opacity: 0 }).show();	
		$(overlay).animate( { opacity: 0.7 }, 200 );	
		$(window).resize(setOverlaySize);
		
		/* window */
		win.show();	
		showPage(currentNr);
	}
	
	function showPage(i){
		var prev = currentNr;
		currentNr = i;
		if(i > 0){
			$('a.prev', win).css('visibility','visible');	
		} else {
			$('a.prev', win).css('visibility','hidden');		
		}
		if(i < (currentList.length-1)){
			$('a.next', win).css('visibility','visible');	
		} else {
			$('a.next', win).css('visibility','hidden');
		}
		
		$(currentList.get(prev)).hide();
		$(currentList.get(i)).show();
		
		var imgSrc = $($('img', currentList.get(i)).get(0)).attr('src');
		$($('img', currentList.get(i)).get(0)).hide();
		  $(currentList.get(i)).iviewer(
		  {
			  src: imgSrc,
			  zoom: 50,
			  zoom_max: 250,
			  zoom_min: 20,
			  initCallback: function()
			  {
				iviewer = this;
			  }
		  });
	}
	
	function showNextPage(){
		showPage(currentNr+1);
	}
	
	function showPrevPage(){
		showPage(currentNr-1);
	}
	function hide(){		
		win.hide();
		overlay.fadeOut();
		$(window).unbind('resize', setOverlaySize);
		currentListWrapper.css('visibility','hidden');
	}
	
	function setOverlaySize(){	
		var widthHeight = viewport();						
		$(overlay).css({width:widthHeight[0],height:widthHeight[1]});
	}
	
	function viewport() {
			
		// the horror case
		if ($.browser.msie) {
			
			// if there are no scrollbars then use window.height
			var d = $(document).height(), w = $(window).height();
			
			return [
				window.innerWidth || 								// ie7+
				document.documentElement.clientWidth || 	// ie6  
				document.body.clientWidth, 						// ie6 quirks mode
				d - w < 20 ? w : d
			];
		} 
		
		// other well behaving browsers
		return [$(window).width(), $(document).height()];
	
	}
	
	return {
		show: function(galleryId){
			if(typeof lists[galleryId] != 'undefined'){
				show(galleryId);
				return true;
			} else {
				return false;
			}
		},
		hide: function(){
		}
	};
	
};

$(function(){
		   
	tabPanel.init();
	
	
	var backgrounds = $('#frontBg .bg');
	backgrounds.hide();
	var bgs = backgrounds.length;
	$(backgrounds.get(0)).show().css('z-index','5');
	var selectedBg = 0;
	
	function selectNextBg(){
		
		var currentId = selectedBg;
		if((selectedBg+1) == bgs){
			var nextId = 0;
		} else {
			var nextId = selectedBg+1;
		}
		
		var currentElem = $(backgrounds.get(currentId));
		var nextElem = $(backgrounds.get(nextId));
		currentElem.css('z-index','5');
		nextElem.css('z-index','4').show();
		
		currentElem.fadeOut(1000);
		nextElem.fadeIn(1000);
		
		selectedBg = nextId;
		
		setTimeout(selectNextBg,6000);
	
	}
	
	$(backgrounds.get(selectedBg)).css('z-index','5').show();
	setTimeout(selectNextBg,6000);
	
	/* modal images */
	var modal3dapi = $('.modal3d').modal3d();
	$('a.modal3d').click(function(e){
		modal3dapi.show($(this).attr('rel'));
		e.preventDefault();
	});
	
});
