var jdx = {

	init : function(){

		this.socialshareprivacy();
		// this.resizeMainMenuItems();
		this.turnListElementsToHeaders();
		this.anchorlist();
		this.stellenausschreibungen();
		this.carousel();
		this.videobox();
		this.openExternalLinksInNewWindow();
		this.imageHover();

	}

	,openExternalLinksInNewWindow: function(){

		var uri = 'http://' + window.location.host;
		$('a[href^="http://"]:not([href^="'+uri+'"])').attr({target: "_blank"});

	}

	,resizeMainMenuItems: function(){

		$('#main-menu li').each(function(){
			var oldWidth = $(this).width();
			$(this).width(oldWidth + 2);
		})

	}

	,turnListElementsToHeaders: function(){

		$('.markAsH2 li').wrapInner('<h2>');
	}

	, anchorlist: function(){

		if (!$('#anchorlist').length) return;

		var $bottom		= $('#anchorlist .bottom'),
			$targets	= $('#main h2'),
			$top		= $('#main h1'),
			url = window.location.href.replace(/#.*/,'');
//			goToTopText = $('#goToTopText').html(),

		var topId = jdx._makeId();
		$top.attr('id', topId);

		$targets.each(function(index){

			var id = jdx._makeId();
			$(this).attr('id', id);

			$bottom.before('<a href="'+url+'#'+id+'">'+$(this).html()+'</a>');
//
//			if (index){
//				$(this).before('<a class="goToTop" href="'+url+'#'+topId+'">'+goToTopText+'</a>');
//			}

		})

		// letztes Element
//		$('#main').children().last().after('<a class="goToTop" href="'+url+'#'+topId+'">'+goToTopText+'</a>');

	}

	, stellenausschreibungen: function(){
		var active = document.location.hash;
		$('.accordion').accordion({
			active: $(active),
			collapsible: true
		});
	}

	, carousel : function(){
		$('#carousel').jcarousel({
			visible	: 1,
			scroll	: 1
		});

		$('#carousel img').each(function() {
		   marginTop = ($('#carousel').outerHeight(false) - $(this).outerHeight(false)) / 2;
		   if(marginTop > 0) {
			  $(this).css({'margin-top':marginTop});
		   }
		});


	}

	, videobox: function(){

//		if (!$('.overlay').length) return;

		if (typeof $f == 'undefined') return; // no video player

		// install flowplayer into flowplayer container
		var player = $f("player", "/assets/flowplayer/flowplayer-3.2.7-0.swf");

		// setup button action. it will fire our overlay
		$("a.start-video").overlay({

			// when overlay is opened, load our player
			onLoad: function() {
				player.load();
			},

			// when overlay is closed, unload our player
			onClose: function() {
				player.unload();
			},

			// some mask tweaks suitable for modal dialogs
			mask: {
				color: '#000',
				loadSpeed: 200,
				opacity: 0.8
			},

			closeOnClick: true

		});
	}


	, _makeId: function () {

		var text = "";
		var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

		for( var i=0; i < 5; i++ )
			text += possible.charAt(Math.floor(Math.random() * possible.length));

		return text;

	}

	,socialshareprivacy: function(d, s, id) {

		if($('#socialshareprivacy').length > 0){
		  $('#socialshareprivacy').socialSharePrivacy({
			    services : {
					facebook : {
					  'dummy_img': '/assets/socialshareprivacy/images/dummy_facebook.png',
					  'perma_option': 'off'
					},
					twitter : {
					  'dummy_img': '/assets/socialshareprivacy/images/dummy_twitter.png',
					   'perma_option': 'off'
					},
					gplus  : {
					  'dummy_img': '/assets/socialshareprivacy/images/dummy_gplus.png',
					   'perma_option': 'off',
					}

				},
				uri	: 'http://www.hansaberatung.de',
				css_path :	'/assets/socialshareprivacy/socialshareprivacy.css'
		  });
		}

	}
	
	, imageHover: function(){
		$('.img_active').load(function(){
			$(this).css('display', 'block');
		});
		var time = 100;
		$('.fader').hover(
			function(){
				$(this).find('.img_inactive').fadeOut(time);
			}, 
			function(){
				$(this).find('.img_inactive').fadeIn(time);
			}
		)
	}

}

// fallback for console.log
if (typeof console == 'undefined') console={log:function(){}};

$(document).ready(function(){

	jdx.init();

})

