var modalProject = {
	overlay: null,
	window: null,
	gallery: null,
	w: null,
	elements: null,
	images: new Array(),
	pages: new Array(),
	page: 0,
	current: 0,
	currentText: 0,
	
	init: function(elements){
		this.overlay = $('#overlay');
		this.overlay.click(function(){ modalProject.hide(); });
		this.gallery = $('div.gallery');
		this.w = $($('html').get(0));
		this.window = $('#modalProject');	
		this.initialized = true;	
		
		$('#modalClose').click(function(e){
			modalProject.hide();
			e.preventDefault();
		});
		$('#modalPrev').click(function(e){
			modalProject.prevPage();
			e.preventDefault();
		});
		$('#modalNext').click(function(e){
			modalProject.nextPage();
			e.preventDefault();
		});
		
		this.elements = elements;
		var totalElements = elements.length;
		var pager = (totalElements>24)?true:false;
		var totalElements2 = totalElements;
		var counter = 0;
		var page = 0;
		if(!pager){
			this.pages[0] = new Array(0,(totalElements-1));	
		} else {
			this.pages[0] = new Array(0,22);
		}
		for(var i=0;i<totalElements; i++){
			
			var img = new Image();
			img.src = '/thumbnail/type/projectsmall/id/'+ elements[i][0];
			this.images[i] = img;
			
			if(pager){
			  counter++;
			  totalElements2--;
			  
			  if (page != 0 && counter == 22) {
			  	if(totalElements2>2){
					page++;		    
					counter = 0;
					this.pages[page-1] = new Array((this.pages[page-2][1]+1),i);
				} 
			  } else if(counter == 23) {
			  	page++; 
				counter = 0;
				if(page != 1){
					this.pages[page-1] = new Array((this.pages[page-2][1]+1),i);
				}
			  }
			  
			}			
		}
		if(counter != 0){
			page++; 
			counter = 0;
			this.pages[page-1] = new Array((this.pages[page-2][1]+1),i);
		}
		
		this.showGalleryPage(0);
	},
	
	showGalleryPage: function(page){
		
		var slots = $('div.image',modalProject.gallery);
		var start = modalProject.pages[page][0];
		var end = modalProject.pages[page][1];
		var prevPage = (page>0)?true:false;
		var nextPage = (modalProject.pages.length>(page+1))?true:false;
		modalProject.page = page;
		
		for(var i=0;i<slots.length;i++){
			if(prevPage && i==8){ //prev pager
				$(slots.get(i)).html('<a href="javascript:void(0)"><img id="galleryPrev" src="/gfx/gallery-prev.png" width="94" height="94" alt="" /></a>');
				$('a',slots.get(i)).click(function(){ modalProject.showGalleryPage(page-1); });
			} else if(nextPage && i==15) {
				$(slots.get(i)).html('<a href="javascript:void(0)"><img id="galleryNext" src="/gfx/gallery-next.png" width="94" height="94" alt="" /></a>');
				$('a',slots.get(i)).click(function(){ modalProject.showGalleryPage(page+1); });
			} else {
				if(start<=end){
					$(slots.get(i)).html('<a href="javascript:void(0)"></a>');
					$('a',slots.get(i)).html(modalProject.images[start]);
					$('a',slots.get(i)).data('id',start).click(function(){ modalProject.show($(this).data('id')); });
					start++;	
				} else {
					$(slots.get(i)).html('');
				}
			}
			
		}
		/*
		kuvame õigesse auku õige pildi
		*/
		
	},
	
	
	
	
	
	
	show: function(page){		
		$('#modalImage').attr('src','/gfx/blank.gif');
		
		/* overlay */
		this.setOverlaySize();
		$(this.overlay).css({ opacity: 0 }).show();	
		$(this.overlay).animate( { opacity: 0.7 }, 200 );	
		$(window).resize(modalProject.setOverlaySize);
		
		/* window */	
		this.window.show();
		$('html, body').animate({scrollTop:0}, 'slow');
		//$('html, body').scrollTop(0);		
		
		this.showPage(page);
		
		return false;
		
	},
	hide: function(){
		/* modal bottom*/
		//$('#pk-modal-bottom').animate( { top: "-68px" }, 300 );
		
		modalProject.window.hide();
		modalProject.overlay.fadeOut();
		$(window).unbind('resize', modalProject.setOverlaySize);	
	},
	setOverlaySize: function(){	
		var widthHeight = modalProject.viewport();						
		$(modalProject.overlay).css({width:widthHeight[0],height:widthHeight[1]});
	},
	showPage: function(i){
		
		/* uue lehe valimine (modal juba nähtav)*/	
		if(modalProject.elements[i][1] != modalProject.elements[modalProject.current][1]){
			//peidame eelmise lehe
			$('#projectText'+modalProject.elements[modalProject.current][1]).hide();
		}
		modalProject.current = i;
		var url = "/thumbnail/type/projectbig/id/"+modalProject.elements[modalProject.current][0];
		$('#modalImage').attr('src',url);
		
		$('#projectText'+modalProject.elements[modalProject.current][1]).show();
		
		if(typeof modalProject.elements[modalProject.current-1]  != 'undefined'){
			$('#modalPrev').show();
		} else {
			$('#modalPrev').hide();
		}
		
		if(typeof modalProject.elements[modalProject.current+1] != 'undefined'){
			$('#modalNext').show();
		} else {
			$('#modalNext').hide();
		}
		
		
	},
	prevPage: function(){
		if(modalProject.current == 0)
			return;
		modalProject.showPage(modalProject.current-1);
	},
	nextPage: function(){
		if(!modalProject.elements[modalProject.current+1]){
			return;
		} else {
			modalProject.showPage(modalProject.current+1);
		}
	},
	viewport: function() {
			
		// 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()];
	
	} 

};

$(function(){

	Cufon.replace('#languages a',{hover: true,'fontFamily':'VistaSansAltLight'});
	Cufon.replace('#mainMenu a',{hover: true,'fontFamily':'VistaSansAltLight'});
	Cufon.replace('#subMenu a',{hover: true,'fontFamily':'VistaSansAltLight'});
	Cufon.replace('#contentText p',{hover: true,'fontFamily':'VistaSansAltLight'});
	Cufon.replace('ul.contentLinks li',{hover: true,'fontFamily':'VistaSansAltLight'});
	Cufon.replace('#footer h2',{'fontFamily':'VistaSansAltLight'});
	Cufon.replace('#footer table td',{hover: true,'fontFamily':'VistaSansAltLight'});
	Cufon.replace('div.peopleData h2',{'fontFamily':'VistaSansAltLight'});
	Cufon.replace('div.peopleData table td',{hover: true,'fontFamily':'VistaSansAltLight'});
	
	Cufon.replace('#modalClose',{hover: true,'fontFamily':'VistaSansAltLight'});
	Cufon.replace('#modalProject h2',{'fontFamily':'VistaSansAltLight'});
	Cufon.replace('#modalProject p',{hover: true,'fontFamily':'VistaSansAltLight'});
	
	Cufon.replace('h1.name, div.position',{'fontFamily':'VistaSansAltLight'});
	//Cufon.replace('*',{hover: true,'fontFamily':'VistaSansAltLight'});
	
	if(typeof pimg != 'undefined'){
		
		modalProject.init(pimg);
		
		/*var pager = true;
		if(pimg.length<24){
			pager = false;	
		}
		function imagesLoaded(i,img){
			$(img).css({'display':'none','width':'94px','height':'94px'});
			$('#galLink'+i).append($(img));
			$(img).fadeIn();
		}
		var gallery = new Array();
		for(var i=0;i<pimg.length; i++){
			if(pager && i==15){
				var imgHtml = '<div class="image">' +
			              '<a id="galPageNext" href="#"><img src="/gfx/gallery-next.png" width="94" height="94" alt="" /></a>' +
						  '</div>';
				$('div.gallery').append(imgHtml);
			}
			if(pager && i==23){
				break;
			}
			if(i==24){
				break;	
			}
			var imgHtml = '<div class="image">' +
			              '<a id="galLink'+ i +'" href="#" rel="'+ pimg[i] +'"></a>' +
						  '</div>';
			$('div.gallery').append(imgHtml);

			var img = new Image();
			img.onLoad = imagesLoaded(i,img);
			img.src = '/thumbnail/type/projectsmall/id/'+ pimg[i];
			
		}
		$('div.gallery').append('<div class="clear"></div>');
		*/
	}
	
	/*$('div.gallery div.image a').click(function(e){
		var rel = $(this).attr('rel');
		if(rel == 'next'){
		} else if(rel == 'prev'){
		} else {
			modalProject.showPage(rel);
		}
		
		e.preventDefault();
	}); */
	
});