// old browsers: Internet Explorer <= 7, Firefox <= 3.5, Opera <= 10.5, Safari <= 3
var $buoop = {vs:{i:7,f:3.5,o:10.5,s:3,n:9}};

// jQuery no conflict mode, because other parts of the website are using MooTools
jQuery.noConflict();

jQuery(document).ready(function() {
	// browser update hint
	var e = document.createElement("script"); 
	e.setAttribute("type", "text/javascript"); 
	e.setAttribute("src", "http://browser-update.org/update.js"); 
	document.body.appendChild(e); 	
	
	animateTabs(jQuery);
	
	if (location.hostname.indexOf('beta.wbgym.de') != -1) {
		showBetaHint();
	}
	
	showMoreText(jQuery); // for the news
});

function animateTabs($) {
	if (!$('body').hasClass('schule')) {
		$('#tab_schule').css('opacity', 0.85).hover(function(e) {
				$(this).stop().animate({opacity:1},'fast');
			}, function(e) {
				$(this).stop().animate({opacity:0.85},'fast');
			}
		);
	}
	if (!$('body').hasClass('schueler')) {
		$('#tab_schueler').css('opacity', 0.85).hover(function(e) {
				$(this).stop().animate({opacity:1},'fast');
			}, function(e) {
				$(this).stop().animate({opacity:0.85},'fast');
			}
		);
	}
	
	$('.static-link').hover(
		function(e) {
			$(this).stop().animate({color:'#f3c63c'},'fast');
		},
		function(e) {
			$(this).stop().animate({color:'#fff'},'fast');
		}
	);
}

function showBetaHint() {
	var bShowHint = getCookie('betaUser', 0) == '0';
	if (bShowHint) {
		var sQuestion = "Dies ist eine Testseite, deren Inhalt unvollständig oder veraltet sein kann.\n"
			+"Möchten Sie nun auf die offizielle Website des Weinberg-Gymnasiums Kleinmachnow (wbgym.de) weitergeleitet werden?";
		if (confirm(sQuestion)) {
			location.href = 'http://wbgym.de';
		} else {
			setCookie('betaUser', 1, 60);
		}
	}
}

function showMoreText($) {
	$('.show-more').click(function(e) {
		e.preventDefault();
		var oArticle = $(this).parent().parent();
		var oImage = oArticle.find('.post-image');
		$(this).parent().remove(); // remove "Weiterlesen ..."
		if (oImage.length) {
			oArticle.append(oImage);
			oImage.css({"float":"none"}).find('img').animate({"width":500},'fast');	// move image to the end, center and resize it
		}
		oArticle.find('.hide').show('fast');	// make more text visible
	});
}

/* Cookie helper functions */
function setCookie(sName, mValue, iDaysUntilExpire) {
	var sExpires = "";
	if (iDaysUntilExpire) {
		var date = new Date();
		date.setTime(date.getTime()+(iDaysUntilExpire*86400*1000));
		sExpires = "; expires="+date.toGMTString();
	}
	document.cookie = sName+"="+mValue+sExpires+"; path=/";
}

function getCookie(sName, mDefaultValue) {
	var sNameEQ = sName + "=";
	var aCookies = document.cookie.split(';');
	for(var i=0; i < aCookies.length; i++) {
		var c = aCookies[i];
		while (c.charAt(0)==' ') {
			c = c.substring(1,c.length);
		}
		if (c.indexOf(sNameEQ) == 0) {
			return c.substring(sNameEQ.length, c.length);
		}
	}
	return mDefaultValue;
}

function deleteCookie(sName) {
	setCookie(sName, "", -1);
}
