
var pageOverlay = {
	overlay: null,
	initialized: false,
	
	init: function(){
		this.overlay = $('#overlay');
		this.overlay.click(function(){ pageOverlay.hide(); });
		this.initialized = true;	
	},
	show: function(){
		if(!this.initialized)
			this.init();
		/* overlay */
		this.setOverlaySize();
		$(this.overlay).css({ opacity: 0 }).show();	
		$(this.overlay).animate( { opacity: 0.7 }, 200 );	
		$(window).resize(pageOverlay.setOverlaySize);		
	},
	hide: function(){
		pageOverlay.overlay.fadeOut();
		$('#blog-window').hide();
		$(window).unbind('resize', pageOverlay.setOverlaySize);	 
	},
	setOverlaySize: function(){	
		var widthHeight = pageOverlay.viewport();
		$(pageOverlay.overlay).css({width:widthHeight[0],height:widthHeight[1]});
	},
	viewport: function() {			
		// ie
		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 getAjaxPage(url)
{
	if($('#blog-page').length > 0){
		$('#blog-page').remove();
	}
	$('#loading').show();
	$.get(url, function(data){
		//alert(data);
		$('#loading').hide();
		$('#ajax-content').replaceWith(data);
		if($('#blog-page').length > 0){
			$('#blog-page').insertAfter('#page-wrapper');	
		}
		initPage();
	});
}

function initPage(){
	Cufon.replace('#menu a',{hover: true,'fontFamily':'dinregular'});
	Cufon.replace('#plan-top h1 span.bold',{hover: true,'fontFamily':'dinbold'});
	Cufon.replace('div.title-links a, #submenu a, #campaign-top h1, #social-top h1, div.logo-group h2, div.social-list div.title h2, div.social-list div.title a, div.social-list div.content h3 a, div.social-list div.content h3 span.date,div.special-list div.title h2, div.special-list div.title a, div.special-list div.content h3 a, div.special-list div.content h3 span.date, #plan-top h1 span.text, #blog-window div.box h2.title, #blog-window a.title',{hover: true,'fontFamily':'dinlight'});
	
	$('#content-window a.close, #plan-window a.close').click(function(e){
		$('#content-tabs,#content-window,#plan-window,#content-arrow').hide();
		$('#menu a').removeClass('active').removeClass('active-pink').removeClass('active-blue').removeClass('active-orange');	
		Cufon.refresh();
		e.preventDefault();
	});
	
	if($('#blog-page').length > 0){
		
		if(!$('#overlay').is(':visible')){
			pageOverlay.show();
		} else {
			pageOverlay.overlay.css({'height':'1000px'});
			pageOverlay.setOverlaySize();	
		}
	}
	
	$('#blog-window a.close').click(function(e){
		pageOverlay.hide();
		$('#menu a').removeClass('active').removeClass('active-pink').removeClass('active-blue').removeClass('active-orange');
		Cufon.refresh();
		e.preventDefault();
	});
	
	loadPlan();
	
	$('#add_comment_submit').click(function(e){
		$('#loading').show();
		var form = $('#add_comment_form');
		
		var dataString = form.serialize();
		var url = form.attr('action');
		$.post(url, dataString,
		function(data){
			if($('#blog-page').length > 0){
				$('#blog-page').remove();
			}
			$('#ajax-content').replaceWith(data);
			$('#loading').hide();
			if($('#blog-page').length > 0){
				$('#blog-page').insertAfter('#page-wrapper');	
			}
			initPage();
		 });
		e.preventDefault();
	});
	
	/**
	 * linkide aktiveerimine 
	 */
	/* pealkirja lingid, alammenüü */
	$('div.title-links a, #submenu a').each(function(){
		$(this).click(function(e){
			getAjaxPage($(this).attr('href'));
			e.preventDefault();
		});
	});
	
	/* kampaaniad */
	$('a.campaign').each(function(){
		$(this).click(function(e){
			getAjaxPage($(this).attr('href'));
			e.preventDefault();
		});
	});
	
	/* eripakkumised */
	$('div.special-list a').each(function(){
		$(this).click(function(e){
			getAjaxPage($(this).attr('href'));
			e.preventDefault();
		});
	});
	
	/* poed */
	$('div.logo-group a').each(function(){
		$(this).click(function(e){
			getAjaxPage($(this).attr('href'));
			e.preventDefault();
		});
	});
	
	/* suhtleme */
	$('div.social-list a').each(function(){
		$(this).click(function(e){
			getAjaxPage($(this).attr('href'));
			e.preventDefault();
		});
	});
	
	/* uudised */
	$('#blog-window div.blog-post h2 a, #blog-archive-links a, div.pager a').each(function(){
		$(this).click(function(e){
			getAjaxPage($(this).attr('href'));
			e.preventDefault();
		});
	});
	
	if($('#dynamic_recaptcha').length > 0){
		showRecaptcha();
	}
	
	//galerii colorbox
	$("a[rel='gallery']").colorbox();
}

function showRecaptcha() {
  var element = $('#dynamic_recaptcha').get(0);
  Recaptcha.create("6LdLWQoAAAAAAOj4oLVx0xPc54RfCAIK5TSNvI3p", element, {
		theme: 'white',
	    lang: 'en',
	    custom_translations : { instructions_visual : "Kirjuta 2 sõna:" },
        callback: Recaptcha.focus_response_field
  });
  setTimeout(pageOverlay.setOverlaySize, 100);
  setTimeout(pageOverlay.setOverlaySize, 200);
  setTimeout(pageOverlay.setOverlaySize, 500);
}


$(function() {
	
	initPage();
	
	$('#menu a').each(function(){
		$(this).click(function(e){
			getAjaxPage($(this).attr('href'));
			e.preventDefault();
		});
	});

});