/**
 * @author Estefan Antelo
 */
var selectedMenu = null;
var transitionSupported = false;


$(document).ready(function() {
	/**
	 * F�hre eine Function Detection durch um herauszufinden ob CSS3 Transitions verf�gbar sind.
	 */
	var div = document.createElement('div');
	var props = ['MozTransition', 'WebkitTransition', 'OTransition', 'MsTransition', 'KhtmlTransition'];

	for(var i = 0; i < props.length; i++) {
		if(props[i] in div.style) {
			transitionSupported = true;
			break;
		}
	}

	/*
	 * Erstelle die Scrollbars
	 */
	$('#newsContainer').tinyscrollbar();
	$('#angebot-box').tinyscrollbar();
	$('#ueber-box').tinyscrollbar();
	
	checkAnchor();
});

$(window).bind('hashchange', function() {
	checkAnchor();
});
function checkAnchor() {
	var anchor = $(location).attr('hash').split('-');
	var posX = 0;

	switch(anchor[0]) {
		case "#home"	:
			posX = 0;
			updateSelection(0);
			if(anchor.length>1){
				showFullContent(anchor[1])
			}else{
				hideFullContent();
			}
			break;
		case "#angebot"	:
			posX = -1024;
			updateSelection(1);
			var selection='velos'
			if(anchor.length>1){
				selection=anchor[1];
			}
			updateContent('angebot',selection);
			break;
		case "#ueber"	:
			posX = -2048;
			updateSelection(2);
			var selection='philosophie'
			if(anchor.length>1){
				selection=anchor[1];
			}
			updateContent('ueber',selection);
			break;
		case "#bilder"	:
			posX = -3072;
			updateSelection(3);
			var selection=''
			if(anchor.length>1){
				selection=anchor[1];
			}
			updateContent('bilder',selection);
			break;
		default			:
			posX = 0;
			updateSelection(0);
			break;
	}
	slidePane($('#page-content'), posX, 0);
}

function slidePane(pane, x, y) {
	if(transitionSupported) {
		pane.css('left', x + 'px');
		pane.css('top', y + 'px');
	} else {
		pane.animate({
			left : x + 'px',
			top : y + 'px',
		}, 1000);
	}
}

function updateSelection(i) {
	if(selectedMenu !== null)
		selectedMenu.removeClass('selected');
	else
		$('.menu ul li').first().removeClass('selected');
	selectedMenu = $('.menu ul li').eq(i);
	selectedMenu.addClass('selected');
}

function changeNews(item) {
	if(item.content == undefined)
		return;
	
	var actNews = item.content.id;
	var id = "#" + actNews + "-content";
	$('#newsEntry').html($(id).html());
	//$('#newsMoreLink').removeClass('hidden')
	$('#newsMoreLink').attr('href','#home-'+actNews);
	$('#newsContainer').tinyscrollbar_update();
}

function showFullContent(id) {
	$('#flowContainer').animate({
		height : '0px'
	}, 500);
	$('#newsContainer').animate({
		height : '520px',
		paddingTop : '20px'
	}, 500, function() {
		$('#newsContent').height('510px');

		$.ajax({
			url : "content/" + id + ".html",
			context : document.body,
			beforeSend : function() {
				$('#newsFull').html('<center><img src="contentflow/img/loader_white.gif"/></center>');
				$('#newsFull').removeClass('hidden');
			},
			success : function(data) {
				$('#newsFull').html(data);
				$('#newsFull a[target!="_blank"]').lightBox();
				$('#newsContainer').tinyscrollbar_update();
			}
		});
	});
	// Zur�ck Link anzeigen
	$('#newsMoreLink').addClass('hidden');
	$('#newsBackLink').removeClass('hidden');

}

function hideFullContent() {
	$('#newsFull').addClass('hidden');
	$('#flowContainer').animate({
		height : '420px'
	}, 500);
	$('#newsContainer').animate({
		height : '140px',
		paddingTop : '0'
	}, 500, function() {
		$('#newsContent').height('110px');

		/**
		 * AJAX Load content
		 */

		$(this).tinyscrollbar_update();
	});

	$('#newsMoreLink').removeClass('hidden');
	$('#newsBackLink').addClass('hidden');
}

function updateContent(mode,id) {
	if (id==''){
		id=$('#'+mode+'-list .active').attr('id');
	}else{
		$('#'+mode+'-list .active').removeClass('active');
	}
	$('#'+id).addClass('active');

	$.ajax({
		url : mode+"/" + id + ".html",
		context : document.body,
		beforeSend : function() {
			$('#'+mode+'-content').html('<center><img src="contentflow/img/loader_white.gif"/></center>');
		},
		success : function(data) {
			$('#'+mode+'-content').html(data);
			$('#'+mode+'-content').ready(function() {
				if (mode=='bilder'){
					$('.ad-gallery').adGallery({loader_image:'img/lightbox-ico-loading.gif'});
				}else{
					$('#'+mode+'-box').tinyscrollbar_update();
				}
				
			});
			
			$('form.contact').bind('submit',event, send);
		}
	});
	
}

function send(event){
	event.preventDefault();
	$.ajax({
    type: $(this).attr('method'),
    url: $(this).attr('action'),
    data: $(this).serialize(),
    success: function(msg){
        alert(msg);
        $(':input', 'form.contact').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
    }
});
}

