var wallpaperIndex = new Array();
var windowHeight;
var windowWidth;
var selectedSlide = 0;
var lastFade = 0;


function getApp(appName)
{
	if (navigator.appName.indexOf ("Microsoft") !=-1)
	{ 
		return window[appName];  
	} else { 
		return document[appName];
	}
}


$(function() { 
	ajaxArticleResize();
	$('#overLay').css('opacity', 0.65);

	$(".ajaxContent").bind('click', function(e){
		$(window).unbind('resize', ajaxArticleResize);

		$.get('getContent.php', {id: $(this).attr('id')}, function(data){
			$('#ajaxArticle').html(data);
			updateAjaxContainer();
		});										 
	});
	
	$('.ajaxContent').bind('mouseover', function(e) {
		$(this).addClass('ajaxContentHover');
	});
	$('.ajaxContent').bind('mouseout', function(e) {
			$(this).removeClass('ajaxContentHover');
	});

	
	if ( $("#ajaxArticle").html() !== "" ) {
		updateAjaxContainer();
	}
});


function updateAjaxContainer()
{
	$('#ajaxArticlBg').css('height', (windowHeight-54));

	$('#diashow img').each(function(e) {
		jQuery.preloadImages($(this).attr('src').replace(/\/s_/, '/'));
	});

	// diashow navigation
	$('#diashow').cycle({ 
	    prev:   '#diashowPrev', 
	    next:   '#diashowNext', 
	    after:   onAfter, 
	    timeout: 0 
	});

	$('#diashow img').click(function(e) {
		$('#diashowBig').show();
		$('#diashowImagesBig').html($('#diashowBigTemplate').html());
		$(".closeAjaxArticle, #overLay").unbind("click", closeAjax);
		$(".closeAjaxArticle, #overLay").click(closeDiashowBig);

		$('#diashowImagesBig').cycle({ 
		    prev:   '#diashowPrevBig', 
		    next:   '#diashowNextBig', 
		    startingSlide: selectedSlide,
		    before: onBeforeBig,
		    after:   onAfterBig, 
		    timeout: 0 
		});
	});
	
	$('#overLay').fadeIn(800, function () { 
		$('#ajaxArticle').show();
		ajaxArticleResize();
	});

	
	$(window).bind('resize', ajaxArticleResize);
	
	// text ein und ausblenden
	$('.more').bind('click', function(e) {
		$(this).parent().next().show();
	});

	$('.close').bind('click', function(e) {
		$(this).parent().parent().hide();
		$('#scrollarea').scrollTo( $(this).parent().parent().prev(), 800 );
		
	});

	$(".closeAjaxArticle, #overLay").click(closeAjax);
	
	$(".newsletterSubmit").closest("form").bind("submit", function(e) { 
		e.preventDefault();
		e.stopPropagation();
		
		$.get('newsletter.php', {email: $('.newsletterInput').attr('value')}, function(data){
			$('#newsletterSuccess')[ data == "false" ? 'hide' : 'show' ]();
			$('#newsletterFailure')[ data == "false" ? 'show' : 'hide' ]();
		});															   
		
		return false;
	});
	
	$(".newsletterSubmit").bind('click', function(e){
		$.get('newsletter.php', {email: $('.newsletterInput').attr('value')}, function(data){
			$('#newsletterSuccess')[ data == "false" ? 'hide' : 'show' ]();
			$('#newsletterFailure')[ data == "false" ? 'show' : 'hide' ]();
		});
	});

}


function closeDiashowBig()
{
	$('#diashowBig').hide();
	$(".closeAjaxArticle, #overLay").unbind("click", closeDiashowBig);
	$(".closeAjaxArticle, #overLay").click(closeAjax);
}


function closeAjax()
{
	$('#ajaxArticle').hide();
//	$('#overLay').fadeOut(500, function (e) {   changeWallpaper(100);  }); 
	
	 $('#overLay').fadeOut(500);
	 changeWallpaper(100);

	$(".closeAjaxArticle, #overLay").unbind("click", closeAjax);
}


function changeWallpaper(propability)
{
	var timestamp = new Date().getTime();
	var rand = Math.ceil(Math.random() * 100);
	var obj = getApp('wallpaperFlv');	
	var index;
	
	if ( rand > propability || timestamp - lastFade < 10000)  {
		return;
	}
	
	if ( wallpaperIndex.length == bgImages.length )
		wallpaperIndex = new Array();
	
	for ( var i = 0 ; true ; i++ ) {
		index = Math.floor(Math.random() * bgImages.length);
		if ( !wallpaperIndex.inArray(index))
			break;
			
		if ( i > 100 )
			break;
	}

	
	if ( typeof obj['loadImage'] == "undefined" ) {
		return;
	}
	
	lastFade = timestamp;
	wallpaperIndex.push(index);
	obj.loadImage(bgImages[index]['src']);
}


function onAfter(curr, next, opts) {
    	var index = opts.currSlide;
		selectedSlide = index;
	$('#diashowPrev')[index == 0 ? 'addClass' : 'removeClass']('versteckt');
	$('#diashowNext')[index == opts.slideCount - 1 ? 'addClass' : 'removeClass']('versteckt');

	$('#diaInnerText').html(this.alt);
}



function onAfterBig(curr, next, opts) {
    	var index = opts.currSlide;
	
	$('#diashowPrevBig')[index == 0 ? 'addClass' : 'removeClass']('versteckt');
	$('#diashowNextBig')[index == opts.slideCount - 1 ? 'addClass' : 'removeClass']('versteckt');
	$('#diaInnerTextBig').html($(this).attr("alt"));
}

function onBeforeBig(curr,next,opts) {
    var $slide = $(next);
    var w = $slide.outerWidth();
    var h = $slide.outerHeight();
    var marginTop = ($('#ajaxArticle').height() - h) / 2;
    var marginLeft = (940 - w) / 2;

    $slide.css({
//        marginTop: marginTop,
        "marginLeft": marginLeft
    });
};



function scrollAreaResize () {
	var offset = $('#scrollContainer').offset();
	$('#scrollContainer').css('height', (windowHeight-27-offset.top));
}


function ajaxArticleResize()
{
	windowHeight = $(window).height();
	windowWidth = $(window).width();

	$('#ajaxArticle').css('left', ((windowWidth-700) / 2));
	$('#ajaxArticle').css('height', (windowHeight-50));

	$('#ajaxArticlBg').css('left', ((windowWidth-700) / 2));
	$('#ajaxArticlBg').css('height', (windowHeight-54));

	$('#diashowBig').css('left', ((windowWidth-1000) / 2));
	$('#diashowBig').css('height', (windowHeight-50));
	
	scrollAreaResize();
}

if (Array.prototype.inArray == null) {
	Array.prototype.inArray = function(value) { 
		for ( var i = 0 ; i < this.length; i++ ) {
			if ( this[i] == value )
				return true;
		}
		
		return false;
	}
}

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preloadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
