var progstimer;

$(document).ready(function(){
			jQuery.fn.mailto = function() {
	return this.each(function(){
		var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");
		$(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">Mail</a>').remove();
	});
};
		startUp();

});


//********* ******************************************************************************
//FUNCTIONS ******************************************************************************
//********* ******************************************************************************

function startUp(){
	var progid = $.url.param("progid");
	var ort = $.url.param("ort");
	
	$("body").css('overflow','hidden');
	$("body").css('background-image','url("/themes/kofmehl/images/bg.jpg")');
	$("#community").css('width','50%');
	$("#community").css('display','block');
	$("html").css('overflow','hidden');


	if(!get_cookie("commentboxopa")){
			$('#newestcomments').css('top',"300px");
			$('#newestcomments').css('left',"400px");
			$('#newestcomments').css('opacity',1);

	}else{
			$('#newestcomments').css('top',"-300px");
			$('#newestcomments').css('left',"-400px");
	}

	getnewestcomment();
	$('body').scrollTop = 0;

	initOtty();
	setDivHeights()
/*TIMED FUNCTIONS **************************************************************** */
	
	window.setInterval("getnewestcomment()",20000);
	//Check form Input
	progstimer = window.setInterval("setDivHeights()",300);

	$(window).resize(function(){
		progstimer = window.setInterval("setDivHeights()",600);
		ottytimer = window.setInterval("positionOtty()",600);
		
	});

	
/* PROGRAMM ******************************************************* */
	
	$('#resform .formbutton').click(function () {
		resFormHandler();
	});
	
	$('#anform .formbutton').click(function () {
		anFormHandler();
	});
	
	
	initPostIt();
	//menuHandler();

	

/*AJAX CONTAINER Sidebarentries*/			   

 	loadSidebar();
	
	
//Progsidebar *********************
		height = 0;
		path = "/ajaxrequests/events";
	
		$('#arrowprogdown').click(function(){
				loadnextProgEntry();

		});
		
		$('#arrowprogup').click(function(){
		//	sidebarentrytimer = window.setInterval("loadprevEntry()", 300);
			loadprevProgEntry();
		
		});
		
	$("#progsidebar").show();
		
	if ($("#programmuebersicht").css("display") != "none") {
				$("#progsidebar").hide();
				$("#arrowprogup").hide();
				$("#arrowprogdown").hide();
	}
		$("#progsidebar").append('<div class="progsidebarentry"></div>').children(".progsidebarentry").load(path, function(){
			loadprogsidebarentries();
		});

	var eventid = get_cookie("eventid");
	if(eventid != '' && progid == ""){
		loadProgEntryByIdFast(eventid);
	}else{
		if(progid == ""){
			callProgramm(ort);
		}else{
			loadProgEntryByIdFast(progid);
			
		}
	}	
	showElements();
	addCommunityFunctionality();	
	  
	
	  

}

	function reloadWebcam(){
		$("#webcam").attr('src','http://82.220.17.35:443/jpg/image.jpg?'+Math.random()*11111);
	}

	


function configformValidation(){
	/*FORMS **************************************************************** */
	$("#userForm").validate({
		rules: {
			password: {
				required: true,
				minlength: 5
			},
			confirm_password: {
				required: true,
				minlength: 6,
				equalTo: "#password"
			}
		},
		messages: {
			
			password: {
				required: "Gib ein Passwort an",
				minlength: "Dein Passwort muss mindestens 6 Buchstaben lang sein"
			},
			confirm_password: {
				required: "Gib ein Passwort an",
				minlength: "Dein Passwort muss mindestens 6 Buchstaben lang sein",
				equalTo: "Bitte das gleiche Passwort eingeben"
			}
		}
	});
	


	
	$("#userPWForm").validate({
		rules: {
			password: {
				required: false,
				minlength: 6
			},
			confirm_password: {
				required: false,
				minlength: 6,
				equalTo: "#password"
			}
		},
		messages: {
			
			password: {
				required: "Gib ein Passwort an",
				minlength: "Dein Passwort muss mindestens 6 Buchstaben lang sein"
			},
			confirm_password: {
				required: "Gib ein Passwort an",
				minlength: "Dein Passwort muss mindestens 6 Buchstaben lang sein",
				equalTo: "Bitte das gleiche Passwort eingeben"
			}
		}
	});
	

	
}



function setDivHeights(){
	var size = getSize();
	var progHeight = size[1] - 19 +"px";


	var half = size[0]/2;
	if(half > 440){
		var topNaviwidth=half;
	} else{
		var topNaviwidth=440;
	}
	if($(".subnav:first").length > 0){
		$("#sidebar").css('margin-top', '16px');
	   var contHeight = size[1] - 36 +"px";
	   $("#community").css('margin-top', '36px');
	}else{
			$('.subnav:first').hide();		
			$("#community").css('margin-top', '20px');
			$("#sidebar").css('margin-top', '0px');
	     	var contHeight = size[1] - 20 +"px";
	}

	  $("#programm").css("height", progHeight);
	 $("#community").css("height", contHeight);
	// $("#sidebar").css("height", contHeight);
	 
	 $(".specials").css("left", size[0] +size[2]- 100 +"px");
	 $("#arrowdown").css("top", size[1] - 22 +"px");
	 $("#arrowdown").css("left", size[0]  - 180 +"px");
	 $("#arrowup").css("top", size[1] - 44 +"px");
	 $("#arrowup").css("left", size[0] - 180 +"px");

	 $("#arrowprogdown").css("top", size[1] - 22 +"px");
	 $("#arrowprogdown").css("left", 10 +"px");
	 $("#arrowprogup").css("top", size[1] - 44 +"px");
	 $("#arrowprogup").css("left", 10  +"px");

	 $("#sidebar").css("left", size[0]  - 190 +"px");
	 $(".subnav").css("width", size[0]/2 -14   +"px");

     $("#programmuebersicht").css("width", size[0] /2- 40 +"px");

  	  $(".topnavi:first").css("width", topNaviwidth);



	//CONTAINER
	 
  	  $(".container").css("left", size[2]+"px");

		var progdetailleft = half-500 +"px";  
		var detailwidth = half-190 +"px";  
		  $("#programmdetaiimage").css("width", detailwidth);
		  $(".eventdetail").css("width", detailwidth);
		  $("#progdetail").css("width", detailwidth);
		  /*
			if($("#otty").css('background-image') == 'url(http://dev.kofmehl.net/themes/kofmehl/images/otty/otto_anim.gif)'){
				 		$("#otty").css("top",size[1] -175 +"px" );
			}else{
				$("#otty").css("top",size[1] -10 +"px" );

			}
				*/	
		entrypos();
		window.clearInterval(progstimer);
	

}


//****************************
//Login
//****************************
function loginFormHandler(){
	
	$("#community").append("<div class='loadingcontainer'/>");
	var password = $("#password").attr("username");
		var password = $("#password").attr("value");
				var username = $("#username").attr("value");
	alert(username+" "+password);
		$('.loadingcontainer').load("/admin?back=/login/userinfo/",{password:password,username:username},function(){
			
				

		});
			//		loadEntryByLink("/login/userinfo/");
			
}


function openIcs(){
		showProgDetailPattern();
		$('#progdetail').html("");
		$('#programm').load("/ajaxrequests/ics .pad",function(){
		$('#programm').fadeIn("slow");
		$('#programm').scrollTop =0;
	});
}

function openNewsletter(){
	showProgDetailPattern();


	$('#programm').load("/ajaxrequests/newsletter .pad",function(){
		$('#progdetail').fadeIn("slow");
		$('#programm').scrollTop =0;
		$("#programm_on").attr('id','programm_off');
		$("#halle_on").attr('id','halle_off');
		$("#raumbar_on").attr('id','raumbar_off');
		$("#newsletter_off").attr('id','newsletter_on');
	});
}
      


function showElements(){
	$(".content:first").show();
	$(".topnavi:first").show();
	$("#banner").show();
	$("#newsletterbutton").show();
	$("#icsbutton").show();
	$(".subnavi:first").show();
	$("#subnav:first").show();
	$("#newestcomments").show();
	var progid = $.url.param("progid");

	if(progid !=''){
		$("#kofmehl_logo").show();
		$("#progsidebar").show();
		$("#arrowprogup").show();
		$("#arrowprogdown").show();

	}

}




function entrypos(){
		var size = getSize();

		var entrywidth = size[0]/2 - 210 ;
		var entryformwidth = size[0]/2*0.8 - 240 +"px";
		$('.entry').css('width', entrywidth);
		$('.content:first').css('max-width', entrywidth+"px");
		$(".entry a img").each(function() {
					$(this).css('width','');
	           //Get the width of the image
	           var width = $(this).width();

	           //Max-width substitution (works for all browsers)
	           if (width > entrywidth) {
	             $(this).css("width", entrywidth+"px");
	           }

	         });

		$(".entry img").each(function() {

	           //Get the width of the image
	           var width = $(this).width();

	           //Max-width substitution (works for all browsers)
	           if (width > entrywidth) {
	             $(this).css("width", entrywidth+"px");
	           }

	         });
		
		

		
		$(".entry textarea").css('width', entryformwidth);
		$(".entry .commentblock input[type=text]").css('width', entryformwidth);

}




function scrollDown(id){
	$("#programm").scrollTo(id,600);
}



function handleLinks(){
	//Erweitert die entsprechenden Internen Links um die Ajaxfunktion loadEntryByLink
	
		$('#community a, .topnavi:first  a').each(function () {
			if (($(this).attr("class") != "extern") && ($(this).attr("class") != "email")) {
				var oldhref = $(this).attr("href");
				//test ob intern
				if (oldhref.indexOf('?progid=') <= 0) {
					//progid kommt vor
					if (oldhref.indexOf('kofmehl.net') > 0 ||
					(oldhref.indexOf('http://') <= 0 && oldhref.indexOf('www.') <= 0 &&
					oldhref != '' &&
					oldhref != '#' &&
					oldhref != 'javascript:void()')
					) {
						//Interner Link				
						$(this).attr("href", "#");
						$(this).click(function(){
							loadEntryByLink(oldhref)
						});
					//alert("oldhref: " + oldhref);
					}
					
					
				}
				else {

					$(this).attr("href", "javascript:void(0);");
					
					$(this).click(function(){
						var splittedHref = oldhref.split("progid=");
						loadProgEntryById(splittedHref[1]);
					});
					
				}
			}else{
				
				//alert("externerLink");
			}
	});
	
	
}




