function showCategory(id) {
	$('.categoryContent').hide();
	id = '#'+id;
	$(id).show();
}

function reloadStat() {
	updateIVW();
	if(typeof reloadGAStats=='function')reloadGAStats();
}

function closeWindow() {
	parent.modalWindow.close();
}

var modalWindow = {
	parent:"body",
	windowId:null,
	content:null,
	width:null,
	height:null,
	close:function()  {
		$(".modal-window").remove();
		$(".modal-overlay").remove();
	},
	open:function()  {
		var modal = "";
		modal += "<div class=\"modal-overlay\"></div>";
		modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
		modal += this.content;
		modal += "</div>";

		$(this.parent).append(modal);
		$(".modal-window").append("<a class=\"close-window\"></a>");
		$(".close-window").click(function(){modalWindow.close();});
		 $(".modal-overlay").click(function(){modalWindow.close();});
	}
};


var openContentOverlay = function(source)  {
	modalWindow.windowId = "myModal";
	modalWindow.width = 510;
	modalWindow.height = 720;
	modalWindow.content = "<iframe width='600' height='720' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'></iframe>";
	//reloadStat();
	modalWindow.open();
};

$(window).load(function(){
	//var scrollable = $(".scrollable").scrollable();
	if ($(".scrollable").length) var scrollable = $(".scrollable").scrollable().navigator();
});

function scrollGo() {
	height = $(window).height();
	objHeight = $('#carousel').height();

	var status = $('#carousel').css('display') ;
	if ( status == 'block') $('#overLay').height(height+objHeight);
	else $('#overLay').height(600);
    $('html,body').animate({scrollTop: $("#navigation").offset().top},'slow');
    //$('html,body').animate({ scrollBottom: $(window).height() }, "slow");

}

/* - - - - - - - - - - - - - - - - - - - -- - - - - - - - - -  IMAGE PREVIEW SHOW CAPTION  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
var defHeight = false;
function scaleLayer(oId) {
	id = '#'+oId;
	aHeight = $(id).height();

	if (!defHeight) {
		defHeight = aHeight;
	}
	iHeight = $(id+'-inner').height()-20;
	maxPercent = Math.round((100/defHeight)*iHeight);
	minPercent = Math.round((100/maxPercent)*defHeight);
	if (aHeight < defHeight+5) {
		$('#moreLink').hide();
		$(id).height(iHeight);
	} else {
		$('#moreLink').show(aHeight);
	}
}

/*
 * ------------------------------------------------------------------------------------------------------------
 * Cookie Funktionen
 * ------------------------------------------------------------------------------------------------------------
 */

function saveRef() {
	SetCookie('saveRef', true, null, '/');
}

function saveBacklink() {
	SetCookie('saveBacklink', true, null, '/');
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	if (!navigator.cookieEnabled || !document.cookie) {return 'noc'}
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return 'noc';
}

function SetCookie (name, value) {
	if (!navigator.cookieEnabled) {return null;}
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name+ "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

/**
 * Ruft die URL url auf und schreibt das Ergebnis in das Element
 * mit der ID targetId. Enthaelt das Ergebnis ein Element mit der
 * targetId enthaelt wird das komplette bisherige Element ersetzt (replace(...))
 * Ansonsten wird der Inhalt in das Element geschrieben (innerHTML=...).
 */
function updateElement(target, url, parameters, callbackFunc, indicator, method) {
	var targetId = target.id || target;
	if (url.href) url = url.href;
	parameters = parameters ? parameters+'&ajax=1' : 'ajax=1';
	callbackFunc = callbackFunc || writeResponse;
	method = method || 'get';
	indicator = indicator || $(targetId+'_indicator');
	var indicatorImg = $(indicator);

	$.ajax({
	  url: url,
	  data: parameters,
	  type: method = method || 'GET',
	  success: function(data) {
	    $('#'+target).html(data);
	  }
	});

}

/**
 * Schreibt das HTML des Ajax Requests in ein HTML Node mit der ID
 * die im Header X-TargetId steht
 */
function writeResponse(originalRequest, json, skipDelay) {
	json = json || new Object();
	delay = (json && json.delay) ? json.delay : 0;
	if (delay < 0) return;
	if (delay && !skipDelay) {
		setTimeout(function () {writeResponse(originalRequest, json, true);}, delay);
		return;
	}
	if (json.hReplace) {
		$H(json.hReplace).keys().each(
			function(k) {
				var element = $(k);
				if (element) element.innerHTML = json.hReplace[k];
			}
		);
	}
	var targetId = getTargetId(originalRequest); //originalRequest.getResponseHeader('X-TargetId');
	var response = originalRequest.responseText.replace("/'/", "''");
	if (!targetId) return false;
	if (originalRequest.responseText.indexOf(' id="'+targetId+'"') >= 0) {
		$(targetId).replace(response);
	} else {
		$(targetId).innerHTML = response;
	}
}

function updateImageLi(id) {
	$('#gImages > li').removeClass('hi');
	$('#img_'+id).addClass(' hi ');
}

function toggleCredits() {
	if ($('#creditsLink').hasClass('lo')) {
		$('#creditsLink').removeClass('lo');
		$('#creditsLink').addClass('hi');
		$('#creditsDetails').show();
	} else {
		$('#creditsLink').removeClass('hi');
		$('#creditsLink').addClass('lo');
		$('#creditsDetails').hide();
	}
}

function checkQuizForm() {
	var ok = true;
	if ($('#freetext_o').attr('checked') == true) {
		if ($('#freetext_t').val() == '') {
			ok = false;
			$('#freetext_t').css('border-color','#be0027');
		}
	}
	if (ok) $('#votingForm').submit();
}

function quizFormReset () {
	$('#freetext_t').css('border-color','#DFD7B4');
	$('#freetext_t').val('');
}

function showQuizResult(link,category) {
	var url = link + '?ajax=1&do=votingresult&category='+category;
	updateElement('votingResult',url);
}

