var myConfig = new Object();
myConfig.defaultTextColor = '#FFFFFF';
myConfig.defaultTextColorHover = '#BBBBBB';

var circleObjs = new Object();


var globalVolume = 100;
var currentVolume = $.cookie("volume");
console.log('bef');
console.log(currentVolume);
if(currentVolume == undefined) {
	currentVolume = globalVolume;
}
console.log('init');
console.log(currentVolume);
var play = false;

function isOldBrowser()	{
	if($.browser.webkit) return true;
	
	if($.browser.mozilla && parseFloat($.browser.version < 4)) return true;
	
	if($.browser.msie && parseFloat($.browser.version < 9)) return true;
	
	return false;
}

function circleLinkEnter(c,e) {
	c.attr({stroke: myConfig.defaultTextColorHover});
	$(e).find('.circleLinkTable').css('color', myConfig.defaultTextColorHover);

	if(currentVolume > 0)	{
		var jingle = $(e).find('.parameters .jingle a').first().html(); 
		
		if(jingle != undefined && jingle.length > 0) {
			jwplayer('mediaplayer').load({file: jingle});
			jwplayer('mediaplayer').setVolume(currentVolume);
			jwplayer('mediaplayer').play();
		}
	}
}

function circleLinkLeave(c,e)	{

	c.attr({stroke: myConfig.defaultTextColor});
	$(e).find('.circleLinkTable').css('color', myConfig.defaultTextColor);
	if(jwplayer('mediaplayer') != null)	{
		if(typeof leaveFunction == 'function') {
			leaveFunction();
		}
		else {
			//jwplayer('mediaplayer').pause();
			//jwplayer('mediaplayer').setVolume(0);
		}
	}
}

$(document).ready(function() {	
	//$('base').remove();
	
	/*
	var x = $('<div id="meinkreis">huhuh</div>');
	$('body').append(x);
	var paper = Raphael(document.getElementById('meinkreis'), 100,100);
	var circle2 = paper.circle(20, 20, 20);
	circle2.attr({fill: 'url(http://62.75.138.85/banane.png)'});
	*/

	$('body').prepend('<div style="position: absolute;" id="mediplayerwrapper"><!-- --></div>');
	if(currentVolume == 0) {
		$('#mute').css('background-image', 'url("/fileadmin/templates/img/sound_mute2.png")');
	}

	// standard sound player
	if($('#mute').length) {
		var x = $('<div class="mediaplayer" id="mediaplayer"><!-- --></div>');
		$(x).appendTo('#myContent');
		jwplayer('mediaplayer').setup({
				'flashplayer': 'fileadmin/templates/js/mediaplayer/player.swf',
				'id': 'playerID',
				'width': '1',
				'height': '1',
				'repeat': false,
				'volume': currentVolume
			  });
		jwplayer('mediaplayer').pause();
	}
	
	$('#mute').live('click', function() {
		if(currentVolume == 0) {
			currentVolume = globalVolume;
			//jwplayer('mediaplayer').play();
			$(this).css('background-image', 'url("/fileadmin/templates/img/sound_high.png")');
		}
		else {
			currentVolume = 0;
			//jwplayer('mediaplayer').pause();
			$(this).css('background-image', 'url("/fileadmin/templates/img/sound_mute2.png")');
		}
		$.cookie("volume", currentVolume, { expires: 365,  path: '/'  });
		jwplayer('mediaplayer').setVolume(currentVolume);
	});
	
	$.each($('.csc-textpic-image a[target=thePicture]'), function(index, element) {
		$(element).removeAttr('onclick');
	});

	$('.csc-textpic-image a[target=thePicture]').live('mouseenter', function() {
		$(this).fancybox({
			'overlayColor'	: '#fff',
			'overlayOpacity'	: 0.1,
			'overlayShow'		: true,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'type'			: 'ajax',
			'autoDimensions'	: true
		}); 
		return false;
	});
	$('.csc-textpic-image a[target=thePicture]').live('click', function() {
		return false;
	});

	if($('#contentScroll').length > 0) {
		var cont = $('#content').html();
		var contnew = '<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>' + '<div class="viewport"><div class="overview">' +cont + '</div></div>';
		$('#contentScroll').html(contnew );
		$('#contentScroll').show();
		$('#contentScroll').tinyscrollbar();
		$('#content').hide();
		$('#contentScroll').show();
	}
	
	
	$.each($('.circleLink'), function(index, element) {
		//var basehref = $('base').first().attr('href');
		//$('base').remove();
		
		var id = $(element).find('.parameters').first().find('.id').first().html();
		if(id.length == 0) {
			var mydate = new Date();
			var x = Math.random() + '';
			id = 'circleLink' + mydate.getTime()+ x.substring(2);
			$(element).find('.parameters').first().find('.id').first().html(id);
		}

		if(id.length > 0) {
			var left = parseInt($(element).find('.parameters').first().find('.left').first().html());
			var top = parseInt($(element).find('.parameters').first().find('.top').first().html());
			$(element).css('left', left);
			$(element).css('top', top);
			
			var target = $(element).find('.circleLinkMarker').first();
			target.attr('id', id);
			
			var size = $(element).find('.parameters').first().find('.size').first().html();
			
			if(size.length == 0) {
				size = 82;
			}
			else {
				size = parseInt(size);
			}
			var paper = Raphael(document.getElementById(id), size,size);
			$(element).css('width', size);
			$(element).css('height', size);
			
			// Creates circle at x = 50, y = 40, with radius 10
			var strokewidth = $(element).find('.parameters').first().find('.strokewidth').first().html();

			if(strokewidth == undefined || strokewidth.length == 0) {
				strokewidth = 2.5;
			}
			else {
				strokewidth = parseFloat(strokewidth);
			}
			
			var radius = parseInt(Math.round(size/2-strokewidth), 0);
			var middle = radius + strokewidth;

			//
			var basehref = $('base').first().attr('href');
			$('base').remove();
			
			var x = $('<div id="over_'+id+'" style="position: absolute;left: 0px;top:0px;z-index:99999"></div>');
			var newElement = $(x).appendTo($(element));
			
			
				
			var circle = paper.circle(middle, middle, radius);
			var paper2 = Raphael(document.getElementById('over_'+id), size,size);
			var circle2 = paper2.circle(middle, middle, radius);
			
			// Sets the fill attribute of the circle to red (#f00)
			var backgroundcolor = $(element).find('.parameters').first().find('.backgroundcolor').first().html();
			var backgroundimage = $(element).find('.parameters').first().find('.backgroundimage img').first().attr('src');

			if(backgroundimage != undefined && backgroundimage.length > 0) {
				
				circle.attr("fill", 'url(/'+backgroundimage+')');
				//circle.attr("fill", 'url(http://62.75.138.85/banane.png)');
				//return false;
			}
			else {
				if(backgroundcolor.length > 0) {
					circle.attr("fill", backgroundcolor);
				}
				else {
					circle.attr("fill", '#CCC');
				}
			}
			
				
			var source = $(element).find('.parameters').first().find('.source').first().html();
			if(source!=undefined && source != null) {
				if(source.length > 0)	{
					var sourceImgUrl= $('#'+source).attr('src');
					if(sourceImgUrl.length > 0) {
						circle.attr("fill", 'url(/'+sourceImgUrl+')');
					}
				}
			}
			
			var strokecolor = $(element).find('.parameters').first().find('.strokecolor').first().html();
			if(strokecolor.length == 0) {
				strokecolor = '#FFFFFF';
			}
			var strokedasharray = $(element).find('.parameters').first().find('.strokedasharray').first().html();
			if(strokedasharray.length == 0) {
				strokedasharray = '.';
			}
			
			
			circle.attr("stroke", strokecolor);
			circle.attr("stroke-width", strokewidth);
			circle.attr("stroke-dasharray", strokedasharray);
			
			/*if($.browser.msie && parseFloat($.browser.version) < 9)	{
				circle2.attr('fill', '#ff0000');
				circle2.attr("stroke", '#FFFFFF');
				circle2.attr("stroke-width", 4);
				circle2.attr("stroke-dasharray", strokedasharray);
				$(element).find('.circleLinkMarker').hide();
			}
			else {*/
				circle2.attr("fill", 'url(/fileadmin/templates/img/spacer.gif)');
				//circle2.attr({fill: 'url(http://62.75.138.85/banane.png)'});
				circle2.attr("stroke-width", 0);
			//}
			//
			
			
			if($.browser.webkit) {
				circle2.attr("stroke", "rgba(0, 0, 0, 0)");
			}
			
			var text = $(element).find('.parameters').first().find('.text').first().html();
			var title = $(element).find('.parameters').first().find('.title').first().html();
			//circle2.attr('title', title);
			if(text == null || text.length == 0) {
				//text = title;
			}
			
			$(element).find('.circleLinkText').first().html(text);

			var href = $(element).find('.parameters').first().find('.ahref').first().find('a').attr('href');
			
			var area = $(element).find('area').first();
			
			
			var linktarget = $(element).find('.parameters').first().find('.ahref').first().find('a').attr('target');
			if(typeof linktarget != "undefined" && linktarget.length > 0) {
				$('#over_'+id).find('circle').attr('onclick', "window.open('"+href+"');");
				$('#over_'+id).find('circle').css('cursor', 'pointer');
			}
			else {
				circle2.attr('href', href);
			}
			
			area.attr('title', title);
			area.attr('alt', title);
			area.attr('href', href);
			var half = parseInt(size/2);
			area.attr('coords', half + ',' + half + ',' + half);
			
			var jingle = $(element).find('.parameters .jingle a').first().html(); 

			if(jingle != undefined && jingle.length > 0) {
				var y = $('<img style="display: none" src="'+jingle+'"/>');
				var newElement2 = $(y).appendTo($(element));
			}
			
			var tipleft = parseInt(size) + 5;
			var tip = $('<div class="tip" style="left: '+tipleft+'px;">'+title+'</div>');
			var newElement3 = $(tip).appendTo($(element));
			
			circle2.mouseover(function () {
				circleLinkEnter(circle, element);
				$(element).find('.tip').css('display', 'block');
				$(element).find('.tip').show();
				
			});
			circle2.mouseout(function () {
				circleLinkLeave(circle, element);
				$(element).find('.tip').hide();
			});
			
			var base = $('<base href="' + basehref + '" />');
			$('head').append(base);
		}
		
	});
	
	
});

