var topImageCounter = 0;
var topImages = new Array();
var nextMatchTimestamp = 0;
var serverTimeOffset = 0;

function initLoginLogoutButtons() {
	// replace login/logout button by .linkbutton and catch clicks
	jQuery('#rightcontentcolumn .login form .linkbutton.transform').attr('style', '');
	jQuery('#rightcontentcolumn .login input[type=submit]').css('display', 'none');
	jQuery('#rightcontentcolumn .login form .linkbutton.transform a').click(function(){
		//jQuery(this).parents('form').first().submit();
		//jQuery('#rightcontentcolumn .login form').submit();
    jQuery('#rightcontentcolumn .login form input[type=submit]').click();
		return false;
	});
}

function initFancyBox() {
    jQuery('a.viplightbox').each(function() {
    	var jThis = jQuery(this);
    	
    	if (jThis.attr('href').match(/speeddating$/)) {
		jQuery(this).fancybox({
		    'width'			: '6',
		    'height'		: '3',
		    'autoScale'		: false,
		    'modal'			: false,
		    'transitionIn'	: 'elastic',
		    'transitionOut'	: 'elastic',
		    'type'          : 'iframe'
		});
    	} else {
		jQuery(this).fancybox({
		    'width'			: '4',
		    'height'		: '5',
		    'autoScale'		: false,
		    'modal'			: false,
		    'transitionIn'	: 'elastic',
		    'transitionOut'	: 'elastic',
		    'type'          : 'iframe'
		});
	}
    });
    
    
    jQuery('a.iframe').fancybox({
        'width'			: '7',
        'height'		: '4',
        'autoScale'		: false,
        'modal'			: false,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic',
        'type'          : 'iframe'
    });
    
    jQuery('a.iframeSalesCall').fancybox({
        'width'			: '6',
        'height'		: '3',
        'autoScale'		: false,
        'modal'			: false,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic',
        'type'          : 'iframe'
    });
    
    jQuery('a.iframeSendMail').fancybox({
        'width'			: '7',
        'height'		: '6',
        'autoScale'		: false,
        'modal'			: false,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic',
        'type'          : 'iframe'
    });
    
    jQuery('a.iframeStandings').fancybox({
        'width'			: '5',
        'height'		: '5',
        'autoScale'		: false,
        'modal'			: false,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic',
        'type'          : 'iframe'
    });
}

function initStandingsShort() {
	var jReplace = jQuery('.replace_standingsShort');
	if (jReplace.length == 0) {
		// nothing to replace
		return;
	}
	
	jQuery.get('/optasports/standings.php', function(data) {
        var jContainer = jReplace.parent();
		jReplace.remove();
		jContainer.append(data);
	});
}

function initNextHomeMatch() {
    var jReplace = jQuery('.replace_nextHomeMatch');
    if (jReplace.length == 0) {
	// nothing to replace
	return;
    }
    
    jQuery.get('/optasports/nextHomeMatch.php', function(data) {
	var jContainer = jReplace.parent();
	jReplace.remove();
	jContainer.append(data);
	
	// initialize countdown
	var jRawDate = jContainer.find('span.rawdate');
	if (jRawDate.length > 0) { // only if we have a date
	    nextMatchTimestamp = jRawDate.text() * 1000;
	    var date = new Date();
	    var currentTime = Date.parse(date.toGMTString());
	    serverTimeOffset = currentTime - jContainer.find('span.serverdate').text() * 1000;
	    refreshHomeMatchCountdown();
	    jQuery('body').everyTime('5s', 'refreshHomeMatchCountdown', function() {
		refreshHomeMatchCountdown();
	    });
	}
    });
}

function initHerthaNews() {
	var jReplace = jQuery('.replace_herthaNews');
	if (jReplace.length == 0) {
		// nothing to replace
		return;
	}
	
	jQuery.get('/optasports/herthaflashnews.php', function(data) {
		var jContainer = jReplace.parent();
		jReplace.remove();
		jContainer.append(data);
	});
}

function initHeaderImages() {
    var allImages = jQuery('#header img');
    var headerspeed = new Array();
    var headerfrom = new Array();
    var headerto = new Array();
    
    
    headerspeed[1] = jQuery('#headerspeed0').text();
    headerfrom[1] = jQuery('#headerfrom0').text();
    headerto[1] = jQuery('#headerto0').text();
    
    headerspeed[2] = jQuery('#headerspeed1').text();
    headerfrom[2] = jQuery('#headerfrom1').text();
    headerto[2] = jQuery('#headerto1').text();
    
    headerspeed[3] = jQuery('#headerspeed2').text();
    headerfrom[3] = jQuery('#headerfrom2').text();
    headerto[3] = jQuery('#headerto2').text();
    
    headerspeed[4] = jQuery('#headerspeed3').text();
    headerfrom[4] = jQuery('#headerfrom3').text();
    headerto[4] = jQuery('#headerto3').text();
    
    
    //alert(headerspeed);
    //alert(headerfrom[0]);
    //alert(headerfrom[1]);
    //alert(headerfrom[2]);
    //alert(headerfrom[3]);
    //alert(headerto);
    
    jQuery(allImages).each(function() {
		var jThis = jQuery(this);
        
		topImages[topImageCounter++] = {
		  src:  jThis.attr('src'),
		  from: headerfrom[topImageCounter],
		  to:   headerto[topImageCounter],
		  time: headerspeed[topImageCounter]
		};
        
		//alert(headerspeed);
		//alert(jThis.attr('src'));
		//alert(speed);
    });

   /*topImages[0] = {
	src: 'fileadmin/templates/images/topimages/stadion1.png'
   };

   topImages[1] = {
	src: 'fileadmin/templates/images/topimages/stadion_background.png'
   };

   topImages[2] = {
	src: 'fileadmin/templates/images/topimages/stadion1.png'
   };

   topImages[3] = {
	src: 'fileadmin/templates/images/topimages/stadion_background.png'
   };*/
    
    //jQuery('#topimages').prepend('<div id="topimages-animated"></div>');
    if ((topImages != null) && (topImages.length > 0)) {
	    jQuery('#topimages').crossSlide({
			fade: 5,
			variant: true,
			easing: 'easeInOutSine'
		      }
		      ,
		      topImages
		      /*, function(idx, img, idxOut, imgOut) {
			
			if (idxOut == undefined)
			{
			  // starting single image phase, put up caption
			  jQuery('div.caption').text(img.alt).animate({ opacity: .7 })
			}
			else
			{
			  // starting cross-fade phase, take out caption
			  jQuery('div.caption').fadeOut()
			}
			
		      }*/);
		}
}


function refreshHomeMatchCountdown() {
    if (nextMatchTimestamp > 0) {
		var currentDate = new Date();
		var currentTimestamp = Date.parse(currentDate.toGMTString());
		var jNumbers = jQuery('div.nexthomematch div.countdown div.numbers');
		
		var diffTimestamp = nextMatchTimestamp - currentTimestamp + serverTimeOffset;
		if (diffTimestamp > 0) {
			//var diffTime = new Date(diffTimestamp);
			
			diffTimestamp /= 1000; // milliseconds to seconds
			
			var days = Math.floor(diffTimestamp / (24 * 3600));
			var hours = Math.floor((diffTimestamp % (24 * 3600)) / 3600);
			var minutes = Math.floor((diffTimestamp % (24 * 3600) % 3600) / 60);
			
			jNumbers.find('.days').text(days);
			jNumbers.find('.hours').text(hours);
			jNumbers.find('.minutes').text(minutes);
		} else {
			// match is in the past...
			// FIXME: what should we show now?
			jNumbers.find('.days').text('0');
			jNumbers.find('.hours').text('0');
			jNumbers.find('.minutes').text('0');
		}
    }
}

function initPNGFix() {
	// applying the fix to document will change background-image on body
	// which will cause an impenetrable area of 750px width and full height
	// causing clicks to be caught by body which makes the whole site unusable
    //jQuery(document).pngFix();
    
    jQuery('body').pngFix();
}

function init() {
	// buttons first
	initLoginLogoutButtons();
	
	// login error close button
	jQuery('#loginerror a.close').click(function(){
		jQuery('#loginerror').fadeOut();
		return false;
	});
	
	// load current match data via AJAX
	initStandingsShort();
    initNextHomeMatch();
    initHerthaNews();
	
    
    //initialize Header Images
    initHeaderImages();
    
    initPNGFix();
    
	// initialize FancyBox last because it throws errors
	initFancyBox();
}

jQuery(document).ready(init);
