var ColorboxTools = {

	ColorBoxMode : '',

	iframeSettings: {
		'opacity':				0.6,
		'scrolling':			false,
		'title':				false,
		'next':                 'nächstes Bild',
		'previous':             'vorheriges Bild',
		'current':              'Bild {current} von {total}',
		'close':                'schliessen'
	},
	webcamSettings: {
		'opacity':				0.6,
		'scrolling':			false,
		'title':				false,
		'next':                 'nächstes Bild',
		'previous':             'vorheriges Bild',
		'current':              'Bild {current} von {total}',
		'close':                'schliessen'
	},

	init:function() {

		if (typeof sColorBoxMode != 'undefined') {
			this.ColorBoxMode = sColorBoxMode;
		}

		if (typeof jQuery().colorbox == 'function')
		{
			if (TemplateMode != 'iframe')
			{
				jQuery('.ce_project_gallery a[rel*="lightbox"]').colorbox(this.iframeSettings);
				jQuery('.ce_news_item_gallery a[rel="newsGallery"]').colorbox(this.iframeSettings);
				jQuery("a[href*='webcam.hauserpartner.de']").colorbox(this.webcamSettings);
			}
		}
	}
}


/**
 * http://briancray.com/2009/10/06/scroll-to-top-link-jquery-css/
 */
var DynamicTopLink = {

	scroll_timer : null,
	displayed    : false,
	message      : null,
	window       : null,
	top          : null,

	init: function() {
		jQuery('body').append('<div id="message"><a href="#toplink">nach oben Scrollen</a></div>');

		this.message = jQuery('#message a');
		this.window  = jQuery(window);
		this.top     = jQuery(document.body).children(0).position().top;

		jQuery(this.message).bind('click.moveWindow', function(event){
			event.preventDefault();
			jQuery(window).scrollTo(0, 500);
		});

		this.window.scroll(this.onScroll);
		this.onScroll();
	},

	onScroll: function()
	{
		DynamicTopLink.window.scroll(function () {
		window.clearTimeout(DynamicTopLink.scroll_timer);
		DynamicTopLink.scroll_timer = window.setTimeout(function () { // use a timer for performance
			if(DynamicTopLink.window.scrollTop() <= DynamicTopLink.top) // hide if at the top of the page
			{
				DynamicTopLink.displayed = false;
				DynamicTopLink.message.fadeOut(500);
			}
			else if(DynamicTopLink.displayed == false) // show if scrolling down
			{
				DynamicTopLink.displayed = true;
				DynamicTopLink.message.stop(true, true).fadeIn(250).click(function () { DynamicTopLink.message.stop(true, true).fadeOut(500); });
			}
		}, 100);
	});
	}
}



var UIOptimisations = {

	init: function() {
		this.hideOldBrowserInIframe();
		this.addJavascriptLinks();
		this.initClickRedirects();
		this.initTagCloud();
		this.initSaturationBoxes();
	},

	hideOldBrowserInIframe : function() {
		if ( (window.parent.frames.length > 0) && (TemplateMode == 'iframe') )
		{
			jQuery('.noscriptHint').hide();
		}
	},

	initClickRedirects:function() {
		jQuery('.ce_box, .ce_home_box_big, .ce_project_highlight').bind('click.translocate', function() {
			window.location.href = jQuery(this).find('a').attr('href');

		});
		jQuery('.ce_box, .ce_home_box_big, .ce_project_highlight').css('cursor', 'pointer');
	},

	addJavascriptLinks:function() {
		jQuery('.ce_project_gallery .linkBox').append('<a href="javascript:void(0);" class="allImages">Alle Bilder</a>');
		jQuery('.ce_project_gallery .linkBox .allImages').bind('click.showGallery', function(){
			jQuery('.ce_project_gallery a.cboxElement:first').trigger('click');
		});

		jQuery('#header').append('<div class="metaInfoButton"><a href="javascript:void(0);" class="metaInfoButton">Über uns</a></div>');
		jQuery('#header .metaInfoButton').bind('click.toggleBox', function() {
			var button = jQuery(this);
			if (button.hasClass('open'))
			{
				button.removeClass('open');
				jQuery('#header .ce_header_metainfo').slideUp();
			}
			else
			{
				button.addClass('open');
				jQuery('#header .ce_header_metainfo').slideDown();
			}
			return false;
		});
	},

	initTagCloud:function() {
		jQuery('.ce_tagcloud .header a').bind('click.toggleTab', function() {
			var currentTab = jQuery(this).attr('rel');
			jQuery('.ce_tagcloud .header a').removeClass('active');
			jQuery('.ce_tagcloud .header a.tabLink'+currentTab).addClass('active');
			jQuery('.ce_tagcloud .tab').hide();
			jQuery('.ce_tagcloud .tab'+currentTab).show();

			return false;
		});
	},

	initSaturationBoxes:function()
	{
		jQuery('#middle .ce_box .imagecontainer').hover(
			function(){
				jQuery(this).find('.overlay').stop(true,true).fadeOut(250);
			},
			function() {
				jQuery(this).find('.overlay').stop(true,true).fadeIn(250);
			}
		)
	}

}



function initPage() {
	UIOptimisations.init();
	ColorboxTools.init();
	DynamicTopLink.init();
}
