var toolTips = {
	init: function(){
		
		$('#pageTop ul.social li').each(function(){
			var rel = $(this).attr('rel');
			
			if(rel && rel.length>0){
				$(this).mouseover(function(){
					toolTips.show(rel,this);
				}).mouseout(function(){
					toolTips.hide(rel);
				});	
			}
			
		});
	
	},
	show: function(tipId,elem){
		var offset = $(elem).offset();
		var left = offset.left;
		var top = offset.top + $(elem).height() + 3;
			
		$('#'+tipId).css({'left':left+'px','top':top+'px'}).show();
	},
	hide: function(tipId){
		$('#'+tipId).hide();	
	}
};

var quotePanel = {
	window: null,
	pageContainer: null,
	initialized: false,
	pages: new Array(),
	current: 0,
	
	init: function(){
		quotePanel.window = $('#quotePanel');
		quotePanel.pageContainer = $('#quoteContainer');
		quotePanel.initialized = true;	
		
		$('a.showPrev',quotePanel.window).click(function(e){
			quotePanel.prevPage();
			e.preventDefault();
		});
		$('a.showNext',quotePanel.window).click(function(e){
			quotePanel.nextPage();
			e.preventDefault();
		});
		
		var width = 0;
		$('div.quote',quotePanel.pageContainer).each(function(i){
			quotePanel.pages.push($(this));
			width = width + 445;
			$(this).click(function(e){
				window.location = $(this).attr('rel');
				e.preventDefault();
			});
			$(this).mouseenter(function(e){
				$(this).addClass('quoteHover');
			}).mouseleave(function(e){
				$(this).removeClass('quoteHover');
			});
		});
		quotePanel.pageContainer.css({'width':width+'px'});
	},
	showPage: function(i){
		if(!quotePanel.initialized)
			quotePanel.init();

		if(i != quotePanel.current){
			var margin = i*445*-1;
			//liigutame õige lehe juurde
			$(quotePanel.pageContainer).stop(true).animate({'marginLeft':margin+'px'});
		}
		quotePanel.current = i;
	},
	prevPage: function(){
		if(quotePanel.current==0){
			quotePanel.showPage(quotePanel.pages.length-1);
		} else {
			quotePanel.showPage(quotePanel.current-1);
		}
	},
	nextPage: function(){
		if(!quotePanel.pages[quotePanel.current+1]){
			quotePanel.showPage(0);
		} else {
			quotePanel.showPage(quotePanel.current+1);
		}
	}

};

var modalNormalWindow = {
	overlay: null,
	window: null,
	pageContainer: null,
	w: null,
	pages: new Array(),
	circles: new Array(),
	current: 0,
	currentSms: null,
	initialized: false,
	
	init: function(){
		this.overlay = $('#modalOverlay');
		this.overlay.click(function(){ modalNormalWindow.hide(); });
		this.w = $($('html').get(0));
		this.window = $('#modalNormalWindow');
		this.pageContainer = $('#modalNormalPageWrap');
		this.initialized = true;	
		
		$('.modalClose',modalNormalWindow.window).click(function(e){
			modalNormalWindow.hide();
			e.preventDefault();
		});
		
		var circlePager = $('#circlePager');
		$('div.page',this.window).each(function(i){
			modalNormalWindow.pages.push($(this));
		});
	},
	
	show: function(page){
		if(!modalNormalWindow.initialized)
			modalNormalWindow.init();
		
		if(!page){
			var page = 0;	
		}
		this.current = page;
		
		/* overlay */
		this.setOverlaySize();
		$(this.overlay).css({ opacity: 0 }).show();	
		$(this.overlay).animate( { opacity: 0.7 }, 200 );	
		$(window).resize(modalNormalWindow.setOverlaySize);
		
		/* window */	
		this.window.show();
		var pageHeight = $(modalNormalWindow.pages[this.current]).height();
		$(modalNormalWindow.pageContainer).css({'height':pageHeight+'px'});
		
		this.showPage(this.current);
		
	},
	hide: function(){
		
		modalNormalWindow.window.hide();
		modalNormalWindow.overlay.fadeOut();
		$(window).unbind('resize', modalNormalWindow.setOverlaySize);	
		
		$(modalNormalWindow.pages[modalNormalWindow.current]).hide();
	},
	setOverlaySize: function(){	
		var widthHeight = modalNormalWindow.viewport();						
		$(modalNormalWindow.overlay).css({width:widthHeight[0],height:widthHeight[1]});
	},
	showPage: function(i){
			
		if(i != modalNormalWindow.current){
			//peidame eelmise lehe
			$(modalNormalWindow.pages[modalNormalWindow.current]).hide();
			
			//arvutame uue lehekülje kõrguse
			//var pageHeight = $(modalNormalWindow.pages[i]).height();
			//$(modalNormalWindow.pageContainer).stop(true).animate({'height':pageHeight+'px'});
		}
		modalNormalWindow.current = i;
		
		//kuvame lehekülje sisu
		$(modalNormalWindow.pages[i]).fadeIn();
		modalNormalWindow.setOverlaySize();
	},
	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()];
	
	} 

};


//onload
$(function(){
	
	$('#quotePanel a.showPrev').mouseover(function(){
		if($(this).hasClass('prevDisabled')){
		} else {
			$(this).removeClass('prevNormal').addClass('prevHover');	
		}
	});
	$('#quotePanel a.showPrev').mouseout(function(){
		$(this).removeClass('prevHover').addClass('prevNormal');	
	});
	$('#quotePanel a.showNext').mouseover(function(){
		if($(this).hasClass('nextDisabled')){
		} else {
			$(this).removeClass('nextNormal').addClass('nextHover');	
		}
	});
	$('#quotePanel a.showNext').mouseout(function(){
		$(this).removeClass('nextHover').addClass('nextNormal');	
	});
	
	quotePanel.init();
	toolTips.init();
	
	$('#search a').click(function(e){
		$('#searchForm').submit();						  
		e.preventDefault();
	});
	
	//galerii colorbox
	$("a[rel='gallery']").colorbox({
		onLoad: function(){
			$('#flashMovie').css({'visibility':'hidden'});
		},
		onCleanup: function(){
			$('#flashMovie').css({'visibility':'visible'});
		}
	});
	
	/*$('.showAwards').click(function(e){
		modalNormalWindow.show();
	});
	$('.showWinners').click(function(e){
		modalNormalWindow.show(1);
	});
	$('.showRules').click(function(e){
		modalNormalWindow.show(2);
	});*/
	
	//toolTips.init();
	//$(window).resize(toolTips.fixPositions);
	
});