/* JQ Funktionsdefinition */
jQuery.fn.center = function (centerCallback) {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    if(centerCallback != undefined){
	centerCallback(this);
	return this;
    }
}

$(document).ready(function(){
    initLightbox();
    /* Navigation */
    $('#siteNavi').accordion({
	icons: false,
	active: false,
	header: "a.naviHeader",
	navigation: true,
	autoHeight: false,
	collapsible: true,
	animated: "easeslide"
    });

    /* Banner */
    $('#colBanner').css('display','block');
    $('#colBanner').nivoSlider({
	effect:'random',
	slices:10,
	animSpeed:500,
	pauseTime:7000,
	startSlide:0,
	directionNav:false,
	controlNav:false,
	keyboardNav:false,
	pauseOnHover:false,
	manualAdvance:false,
	captionOpacity:0
    });
    var naviLoader = setInterval(reloadNavigation,10000);
    prepareLinks();

    

});


function prepareLinks(){
    $('a[rel="external"], a[rel="nofollow"]').attr('target','_blank');
    $('a[rel="content"]').unbind('click').click(function(){	
        var url = clearUrl($(this).attr('href'));
	$.ajax({
	    url: '/js'+url,
	    beforeSend: function(){$('#siteContent').html('<div style="text-align: center; height:'+$('#siteContent').innerHeight()+'px;"><img src="/images/loader.gif" alt="loader"/></div>');},
	    success: function(data) {
		htmlContent = data;
	    },
	    error: function() { alert("error"); },
	    complete: function() {
		$('#siteContent').html(htmlContent);
		carouselStop();
		prepareLinks();
	    }
	});
	return false;
    });

    $('a[rel="lightboxImage"]').unbind('click').click(function(){
	var link = this.href;
	var img = new Image();
	$('#lightboxBack').fadeTo(100,0.8,function(){
	    $(img).load(function(){
		$(this).hide();
		$('#lightboxWrapper').append(this).show();
		$(this).show().center();
	    }).attr({src: link}).addClass('lightboxImage').click(function(){hideBackground();});
	});
	return false;
    });

    $('a[rel="lightboxContent"]').unbind('click').click(function(){
	var contentUrl = $(this).attr('href');
	var urlPattern = /([0-9]+)\/([0-9]+)\.jpg$/;
	var destinationUrl = contentUrl.match(urlPattern);
	destinationUrl = '/js/gallery/pics/show/'+destinationUrl[1]+'_'+destinationUrl[2]+'/';
	$('#lightboxBack').fadeTo(100,0.8,function(){
	    //$('#lightboxContent').html('<div id="galleryLightboxContent">'+destinationUrl+'</div>').show().center();
	    $.ajax({
		url: destinationUrl,
		success: function(data) {
		    htmlContent = data;
		},
		error: function() { alert("error"); },
		complete: function() {
		    $('#lightboxContent').html(htmlContent).show().center();
		    prepareLinks();
		}
	    });
	});

	return false;
    });

    $('#naviMainCommunity a[rel="navigation"]').unbind('click').click(function(){
	var url = clearUrl($(this).attr('href'));
	$.ajax({
	    url: '/js'+url,
	    success: function(data) {
		htmlContent = data;
	    },
	    error: function() { alert("error"); },
	    complete: function() {
		$('#naviMainCommunity').html(htmlContent);
		prepareLinks();
	    }
	});
	return false;
    });

    prepareForms();
    $('#naviHome, #naviNews, #naviEvents, #naviBusshuttle, #naviU18').click(function(){
	collapseAll();
    });
}

function prepareNavigationLinks(){

    $('#navigationSubContent a[rel="content"]').unbind('click').click(function(){
	var url = clearUrl($(this).attr('href'));
	$.ajax({
	    url: '/js'+url,
	    beforeSend: function(){$('#siteContent').html('<div style="text-align: center; height:'+$('#siteContent').innerHeight()+'px;"><img src="/images/loader.gif" alt="loader"/></div>');},
	    success: function(data) {
		htmlContent = data;
	    },
	    error: function() { alert("error"); },
	    complete: function() {
		$('#siteContent').html(htmlContent);
		prepareLinks();
	    }
	});
	return false;
    });
}

function initLightbox(){
    $('body').prepend('<div id="lightboxWrapper"><div id="lightboxBack" style="width:'+$('body').innerWidth()+'px; height:'+$('body').innerHeight()+'px;"></div><div id="lightboxContent"></div></div>');
    $(window).resize(
	function(){
	    $('#lightboxBack').css({'height' : $('body').innerHeight()+'px','width' : $('body').innerWidth()+'px'});
	}
    )
    $(window).scroll(
	function(){
	    $('#lightboxBack').center();
	}
    )
    $('#lightboxBack').click(function(){
	hideBackground();
    });
}

function hideBackground(){
    $('#lightboxContent').hide().html('');
    $('#lightboxBack').fadeTo(200,0.1,function(){
	$(this).hide();
    });
    $('.lightboxImage').fadeTo(200,0.1,function(){
	$(this).detach();
    });
}

function getContent(e){
    var url = clearUrl($(this).attr('href'));
    $.ajax({
	    url: '/js'+url,
	beforeSend: function(){$('#siteContent').html('<div style="text-align: center; height:'+$('#siteContent').innerHeight()+'px;"><img src="/images/loader.gif" alt="loader"/></div>');},
	success: function(data) {
	    htmlContent = data;
	},
	error: function() { alert("error"); },
	complete: function() {
	    $('#siteContent').html(htmlContent);
	}
    });
    return false;
}

function reloadNavigation(){
    $.ajax({
	url: '/js/community/navigation_subcontent/',
	success: function(data) {
	    htmlContent = data;
	},
	error: function() { alert("error"); },
	complete: function() {
	    $('#navigationSubContent').html(htmlContent);
	    //prepareNavigationLinks();
	    prepareLinks();
	}
    });
}

function loadMyProfile(){
    hideBackground();
    $.ajax({
	url: '/js/community/myprofile/',
	success: function(data) {
	    htmlContent = data;
	},
	error: function() { alert("error"); },
	complete: function() {
	    $('#siteContent').html(htmlContent);
	}
    });
    return false;
}

function loadCommunityStart(){
    hideBackground();
    $.ajax({
	url: '/js/community/',
	success: function(data) {
	    htmlContent = data;
	},
	error: function() { alert("error"); },
	complete: function() {
	    $('#siteContent').html(htmlContent);
	}
    });
    return false;
}

function hideUpload(){
    hideBackground();
    $.ajax({
	url: '/js/community/edit/profile/',
	success: function(data) {
	    htmlContent = data;
	},
	error: function() { alert("error"); },
	complete: function() {
	    $('#siteContent').html(htmlContent);
	}
    });
    return false;
}

function openIFrame(url){

    $('#lightboxBack').fadeTo(100,0.8,function(){

	$('#lightboxContent').show().center();
    });

    return false;
}

function snShare(url){
    var winWidth = 640;
    var winHeight = 400;
    var winTop = (window.screen.height-winHeight)/2;
    var winLeft = (window.screen.width-winWidth)/2;
    window.open(url,'Seite weiterempfehlen','width='+winWidth+',height='+winHeight+',top='+winTop+',left='+winLeft+'');
    return false;
}


function d7carousel(element,duration){
    if(typeof(cTimer)!='undefined'){
	clearInterval(cTimer);
	if(carouselClear()){
	    carouselInit(element,duration);
	}
    }else{
	carouselInit(element,duration);
    }
}
function carouselInit(element,duration){
    cElement = element;
    cC = $(cElement);
    cCE = $(cElement+' li');
    eMargin = parseInt($(cCE[0]).css('margin-right'));
    eWidth = $(cCE[0]).outerWidth()+eMargin;
    cWidth = (eWidth*cCE.length);
    $(cC).css({'position':'relative','width':cWidth,'left':'-'+eWidth});
    $(cCE).css({'display':'inline-block'});

    $(cC).unbind('hover').hover(function(){
	carouselStop();
    },
    function(){
	carouselInit(element,duration);
    });
    cTimer = setInterval(function(){carouselAnimation(duration)},duration);
}
function carouselAnimation(duration){
    var slideDuration = duration*0.75;
    $(cC).animate({left: '-='+eWidth},slideDuration,function(){
	$(cC).animate({left: '+='+eWidth},0);
	$(cC).append($(cElement+' li:first'));
    });
}

function carouselStop(){
    if(typeof(cTimer)!='undefined'){
	clearInterval(cTimer);
	cTimer = null;
    }
    return false;
}

function carouselClear(){
    cElement = null;
    cC = null;
    cCE = null;
    eMargin = null;
    eWidth = null;
    cWidth = null;
    clearInterval(cTimer);
    cTimer = null;
    if(delete(cTimer)){
	return true;
    }else{
	return false;
    }
}


function clearUrl(url){
    var localHostname_1 = 'http://'+location.hostname;
    var localHostname_2 = location.hostname;
    var regEx_1 = new RegExp(localHostname_1,'i');
    var regEx_2 = new RegExp(localHostname_2,'i');
    url = url.replace(regEx_1, "");
    url = url.replace(regEx_2, "");
    return url;
}

function prepareForms(){
    $('form').attr('action', function(){
	var thisAction = $(this).attr('action');
	var jsPattern = /^\/js/;
	if(!thisAction.match(jsPattern)){
	    thisAction = '/js'+$(this).attr('action');
	}
	return thisAction;
    }).prepend('<input type="hidden" name="submitForm" />');
    $('input[type="submit"]').removeAttr('name');
}

function collapseAll() {
    $("#siteNavi").accordion({active: false, collapsible: true});
}
