$oldLink="";
function dynamics() {
	$(".link").click(
		// menu item clicked
		function () {
			if ($(this).is("img")){
				if ($oldLink!=""){
					$($oldLink).removeClass("clicked");
					if ($($oldLink).is("img")){
						old_img = $($oldLink).attr("src").replace("_highlight.jpg",".jpg");
						$($oldLink).attr("src",old_img);
					}
				}
			
			
			
				img = $(this).attr("src");
				if (img.indexOf('_highlight')<=0) new_img = img.replace(".jpg","_highlight.jpg");
				$(this).attr("src",new_img);
				
				$(this).addClass("clicked");
				$oldLink=$(this);
				
			}
			
			siteID=$(this).attr("id").substr(4);
			$.post("sites.php", {siteID:siteID},
			function(data){
				$('#BigBox').html(data.text);
				$('#BigBox').css("background-image","url(images/sites/"+lang+"/"+data.image+")");
				$('#SmallBox').html(data.smallText);
				
				dynamics();
			},"json");
		}
	).removeClass("link").addClass("link_old")
}

function hover(){
	
	$(".video").hover(
		// menu hover effect
		function () {
			if ($(this).attr("src").indexOf('_active.gif')<0){
				img = $(this).attr("src");
				new_img = img.replace(".gif","_active.gif");
				$(this).attr("src",new_img);
				$(this).addClass("videoHover");
			}
			
		}, 
		function () {
			if ($(this).attr("src").indexOf('_active.gif')>0 && $(this).hasClass("videoHover") ){
				img = $(this).attr("src");
				new_img = img.replace("_active.gif",".gif");
				$(this).attr("src",new_img);
				$(this).removeClass("videoHover");
			}
		}
		);
}


function fromVideo(id) {
	siteID=id;
	$.post("sites.php", {siteID:siteID},
		function(data){
			$('#BigBox').html(data.text);
			$('#BigBox').css("background-image","url(images/sites/"+lang+"/"+data.image+")");
			$('#SmallBox').html(data.smallText);
			
			dynamics();
		},"json");
}

function clickVideo() {
	$(".video").click(
		function () {
			
			if ($oldVButton!="") {
				// revert old button
				$(".oldButton").addClass("video");
				img = $(".oldButton").attr("src");
				new_img = img.replace("_active.gif",".gif");
				$(".oldButton").attr("src",new_img);
				$(".oldButton").removeClass("oldButton");
				hover();
				
			}
			$oldVButton=$(this);
			img = $(this).attr("src");
			$oldID=$(this).attr("id");
			$parent=$(this).parent();
			$(this).remove();
			$parent.append('<img src="'+img+'" id="'+$oldID+'" class="video oldButton"/>');
			clickVideo();
			$("#helpbox").remove();
		}
	)
}


function setButton(old,id){
	$oldButton=$("#videoImage"+id);
	if (old>=0) {
		//alert(old);
		
		img = $("#videoImage"+old).attr("src");
		new_img = img.replace("_active.gif",".gif");
		//alert(new_img);
		$("#videoImage"+old).removeClass("oldButton");
		$("#videoImage"+old).attr("src",new_img);
		hover();
	}
	img = $("#videoImage"+id).attr("src");
	new_img = img.replace(".gif","_active.gif");
	$("#videoImage"+id).attr("src",new_img);
	$("#videoImage"+id).addClass("oldButton");
	
}


function removeButton(id){
	
	
}

function linkHover(){
	
	$(".link").hover(
		// menu hover effect
		function () {
			
			if ($(this).hasClass("clicked")==false){
				img = $(this).attr("src");
				new_img = img.replace(".jpg","_highlight.jpg");
				$(this).attr("src",new_img);
			}
		}, 
		function () {
			if ($(this).hasClass("clicked")==false){
				img = $(this).attr("src");
				new_img = img.replace("_highlight.jpg",".jpg");
				$(this).attr("src",new_img);
			}
		}
	);
}


var $oldButton="";

$(function() {

	linkHover();
	dynamics();
	hover();
	clickVideo();
	

	$("#news").click(
		// news item clicked
		function () {
			$.post("news.php",{newsID:0},
			function(data){
				var len=data.length;
				var str="";
				for (i=0;i<len;i++){
					str+="<span id=newsheadline>" + data[i].title + "</span> <span id=newsdate>"+ data[i].date+ "</span><br/>"+data[i].smallText+"<br/><a href='#' class='newsBig' id='news"+data[i].newsID+"'>mehr ...</a><hr/><br/>";
				}
				$('#SmallBox').html(str);
				$('#BigBox').html(data[0].text);
				$('#BigBox').css("background-image","url(images/news/"+lang+"/"+data[0].image+")");
							
				$(".newsBig").click(
					function () {
						newsID=$(this).attr("id").substr(4);
						$.post("news.php",{newsID:newsID},
						function(data){
							
							$('#BigBox').html(data[0].text);
							$('#BigBox').css("background-image","url(images/news/"+lang+"/"+data[0].image+")");
						},"json");
					
					}
				)
			},"json");
		}
	)	
	
	// [IMPRESSUM_6]
	// ---------------------  hardcoded impressum link ---------------------
	$("#impressum").click(	
		function () {
			if (lang=="de") siteID="6";
			if (lang=="en") siteID="3";
			$.post("sites.php", {siteID:siteID},
			function(data){
				$('#BigBox').html(data.text);
				$('#BigBox').css("background-image","url(images/sites/"+lang+"/"+data.image+")");
				$('#SmallBox').html(data.smallText);
			},"json");
		}
	)
	// ---------------------------------------------------------------------
	// [/IMPRESSUM_6]
		
		
	$("#news").click();
});
