(function($, window) {

function fixLayout()
{
	var container = $('#container'),
		diff = $(window).height() - container.outerHeight();

	container.css({
	    'position': 'relative',
	    'margin-top':  Math.max(0, diff) + 'px'
	});
			
	var main = $('#main').css('margin-right', 'auto').css('margin-left', 'auto'), 
		width = main.outerWidth(), 
		left = main.position().left,
		docwidth = $(document).width(),
		diff = docwidth - (width + left);
		
	if (diff < 150) {
	    if ((width + 150) < docwidth) {
		    main.css('margin-right', '150px');
		}
	}
}

$(window).load(fixLayout).resize(fixLayout);

/*
$(function() {
	setTimeout(function() {
		fixLayout();
	}, 10);
});

var resizeTimer;
$(window).resize(function() {
	if (resizeTimer) {
		clearTimeout(resizeTimer);
	}
	
	resizeTimer = setTimeout(function() {
		fixLayout();
	}, 10);
});*/

/**********/
var lastBackgrounds;
window.background = function(images) {
    function compare(first, second) {
	    if (!first || !second || first.length !== second.length) {
		    return false;
		}
		
		for (var i = 0; i < first.length; i++) {
			if ($.inArray(first[i], second) < 0) {
			    return false;
			}
		}
		
		return true;
	}

    if (compare(lastBackgrounds, images)) {
	    return;
	}
	lastBackgrounds = images;

    function doBackground(images) {
	    var  imgs = $();
        $.each(images, function(i, img) {
            //imgs = imgs.add($('<a href="' + img + '" title="proevent background"/>' + (i + i) + '</a>'));
            imgs = imgs.add($('<img src="' + img + '" title="proevent background"/>'));
        });

        var options = {
            randomtransition: 0, // 0-none, 1-use random transition (0-7)
            transition: 0,
            slideshow: 0 // 0-none, 1-autostart slideshow
        };

        if (imgs.size() > 1) {
            $.extend(options, {
                randomtransition: 0, // 0-none, 1-use random transition (0-7)
                transition: 1,
                slideshow: 1, // 0-none, 1-autostart slideshow
                slide_interval: 5000, // interval for the slideshow
                randomimage: 1, // 0-none, 1-random image
                speed: 2000 // animation speed
            });
        }

        jQuery.fn.stopSlideShow();
		$('#superbgimage').remove();
		var el = $('<div id="superbgimage"/>').append(imgs).hide();
		$('body').append(el);

		return el.superbgimage(options);
	};

	if ($.isReady) {
		doBackground(images).superbgLoad();
	} else {
		$(function() {
			doBackground(images);
		});
	}
}

window.getgrayscale = function() {
    $(function(){
		//grayscale
		$('.people-person li div').hide();
	
		$(".people-person li a").hover(
		    function () {
				$(this).find('div').show();
				$(this).css('opacity','1');
		    },
		    function () {
				$(this).css('opacity','0.6');
				$('.people-person li div').hide();
		    }
		);
    });
}


$('.comment-body a').livequery(function() {
    $(this).attr("target", "_blank");
});

//scroll
$('.scroll').livequery(function() {
    $(this).jScrollPane({showArrows: true, hijackInternalLinks: true, autoReinitialise: true});
});

// start-boxes
$('.start-boxes li').livequery(function() {
    $(this).click(function() {
        window.location.href = $(this).find("a").attr("href");
    });
});

// cat-boxes
/*$('.cat-boxes li.is_link)').livequery(function() {
    $(this).click(function() {
        window.location.href = $(this).find("a").attr("href");
    });
});*/

//galerie
$(".wp-gallery").livequery(function() {
    $(this).jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: 1
    });
});

//small galerie 
$('.wp-gallery-small').livequery(function() {
        $(this).jCarouselLite({
            btnNext: $(this).children()[1],
            btnPrev: $(this).children()[0],
            visible: 3
        });
});

$(".jwplayer").livequery(function() {
    var $this = $(this);
	jwplayer(this).setup({
		'flashplayer': $this.attr('data-jwplayer'),
		'file': $this.attr('href'),
                'image': $this.attr('data-jwplayer-image'),
		'controlbar': 'over',
		'stretching': 'fill',
		'dock': 'false',
		'width': $this.css('width'),
		'height': $this.css('height')
    });
});

$(".popup, .popup-link a").livequery(function() {
    $(this).colorbox({iframe:true, innerWidth:800, innerHeight:640});
});

$(".galpopup").livequery(function() {
    $(this).colorbox();
});

$('.tabs').livequery(function() {
    $(this).each(function() {
	    var tabs = $(), contents = $();
		$(this).find('a').each(function() {
		    var $this = $(this), hash = $this.attr('href');
		    tabs = tabs.add(this);
			contents = contents.add($(hash).hide());
			
			$this.click(function () {
			    tabs.removeClass('selected');
				$this.addClass('selected');
				contents.hide().filter(hash).show().find('.scroll').jScrollPane({showArrows: true, hijackInternalLinks: true});
				return false;
			});
		}).filter(':first').trigger('click');
    });
});

$(function() {
	var socialCache = {};
	function socialTip(el) {
	    var el  = $(el),
		    id  = el.attr('id'), 
		    tip = $('<span>Lade...</span>');

		
		if (socialCache[id]) {
		    return tip.html(socialCache[id]);
		}
		
		if (id === 'stwitter') {
			$.getJSON('http://twitter.com/status/user_timeline/proevent.json?count=1&callback=?', function(data) {
				tip.text(socialCache[id] = data[0].text);
			});
		}
		
		if (id === 'syoutube') {
			$.getJSON('http://gdata.youtube.com/feeds/api/videos?q=proeventHeidelberg&alt=json-in-script&max-results=1&start-index=1&callback=?', function(data) {
				tip.html(socialCache[id] = '<strong>' + data.feed.entry[0].title['$t'] + '</strong><br/>' + data.feed.entry[0].content['$t']);
			});
		}

		if (id === 'sfacebook') {
		    var access_token = '256515084360100|cbd3d6a76e6bbe419ae4ff05.1-1420646029|bWkXt_IEhOf0zGLxV6EH6u37hFs',
			    client_id = 'proevent.livecommunication';

		    $.getJSON('https://graph.facebook.com/' + client_id + '/feed?access_token=' + access_token + '&limit=1&callback=?', function(data) {
				tip.html(socialCache[id] = data.data[0].message);
			});
		}
		
		if (id === 'sfeed') {
			$.get(el.attr('href'), function(data) {
				tip.text(socialCache[id] = $(data).find('item:eq(0) title').text());
			},
			null,
			'xml');
		}
		
		return tip;
	}

    //social popup
	$('.head-social a').tipsy({
	    title: function() {
			return socialTip(this);
		},
		gravity: 'ne',
		offset: 10
	});	
});

})(jQuery, window);

