/**
 * @author Administrator
 */
$(document).ready(function()
{
	$('a.internal-link-new-window').nyroModal({
	bgColor: '#666666',
	autoSizable: true,
	minWidth:337,
	minHeight:423

	});
	
	$("img#vote_ja").click(function () 
	{
		$.ajax
		(
			{
				url:'/typo3conf/ext/wk_alt/php/save_vote.php',
				data:"vote_ja=1&vote_nein=0",
				dataType: 'text',
				success: function (data, status)
				{
					if (data==0)
					{
						alert("Du hast bereits eine Stimme fuer dieses Plakat abgegeben!");
					}
					else
					{
						alert("Danke fuer deine Stimme!");
						location.reload();
					}
					
				},
				error: function (data, status, e){}
			}
		)
	});
	
	
	$("img#vote_nein").click(function () 
	{
		$.ajax
		(
			{
				url:'/typo3conf/ext/wk_alt/php/save_vote.php',
				data:"vote_ja=0&vote_nein=1",
				dataType: 'text',
				success: function (data, status)
				{
					if (data==0)
					{
						alert("Du hast bereits eine Stimme fuer dieses Plakat abgegeben!");
					}
					else
					{
						alert("Danke fuer deine Stimme!");
						location.reload();
					}
					
				},
				error: function (data, status, e){}
			}
		)
	});
	
	
	$("#wertung_1").click(function(){
		$("#wertung_1").removeClass("inactiv");
		$("#wertung_1").addClass("activ");
		
		$("#wertung_2").removeClass("activ");
		$("#wertung_2").addClass("inactiv");
		
		$("#wertung_3").removeClass("activ");
		$("#wertung_3").addClass("inactiv");
		
		$("#wertung_4").removeClass("activ");
		$("#wertung_4").addClass("inactiv");
		
		$("#wertung_5").removeClass("activ");
		$("#wertung_5").addClass("inactiv");
	});

	$("#wertung_2").click(function(){
		$("#wertung_1").removeClass("inactiv");
		$("#wertung_1").addClass("activ");
		
		$("#wertung_2").removeClass("inactiv");
		$("#wertung_2").addClass("activ");
		
		$("#wertung_3").removeClass("activ");
		$("#wertung_3").addClass("inactiv");
		
		$("#wertung_4").removeClass("activ");
		$("#wertung_4").addClass("inactiv");
		
		$("#wertung_5").removeClass("activ");
		$("#wertung_5").addClass("inactiv");
	});
	
	$("#wertung_3").click(function(){
		$("#wertung_1").removeClass("inactiv");
		$("#wertung_1").addClass("activ");
		
		$("#wertung_2").removeClass("inactiv");
		$("#wertung_2").addClass("activ");
		
		$("#wertung_3").removeClass("inactiv");
		$("#wertung_3").addClass("activ");
		
		$("#wertung_4").removeClass("activ");
		$("#wertung_4").addClass("inactiv");
		
		$("#wertung_5").removeClass("activ");
		$("#wertung_5").addClass("inactiv");
	});
	
	$("#wertung_4").click(function(){
		$("#wertung_1").removeClass("inactiv");
		$("#wertung_1").addClass("activ");
		
		$("#wertung_2").removeClass("inactiv");
		$("#wertung_2").addClass("activ");
		
		$("#wertung_3").removeClass("inactiv");
		$("#wertung_3").addClass("activ");
		
		$("#wertung_4").removeClass("inactiv");
		$("#wertung_4").addClass("activ");
		
		$("#wertung_5").removeClass("activ");
		$("#wertung_5").addClass("inactiv");
	});
	
	$("#wertung_5").click(function(){
		$("#wertung_1").removeClass("inactiv");
		$("#wertung_1").addClass("activ");
		
		$("#wertung_2").removeClass("inactiv");
		$("#wertung_2").addClass("activ");
		
		$("#wertung_3").removeClass("inactiv");
		$("#wertung_3").addClass("activ");
		
		$("#wertung_4").removeClass("inactiv");
		$("#wertung_4").addClass("activ");
		
		$("#wertung_5").removeClass("inactiv");
		$("#wertung_5").addClass("activ");
	});
	
	$('#slider').jcarousel(
	{
		scroll: 1,
		vertical:true,
		animation:500,
		auto:2
	});
	
	$('.galerie').jcarousel(
	{
		scroll: 4,
		vertical:false,
		animation:500,
		buttonNextHTML: '<div class="next_items">&raquo; n&auml;chste</div>',
		buttonPrevHTML: '<div class="prev_items">&laquo; vorherige</div>'
	});
	
	/*
	 * Stimme abgeben
	 */
	$(".button_stimme_abgeben").click(function () 
	{
		var wertung=0;
		$(".voting .activ").each(function(i)
		{
		    wertung++;
		});
		if (wertung!=0 && wertung<=5)
		{
			$.ajax
			(
				{
					url:'/typo3conf/ext/wk_alt/php/save_wertung.php',
					data:"wertung="+wertung+"&uid="+$("#uid").val(),
					dataType: 'text',
					success: function (data, status)
					{
						if (data==0)
						{
							alert("Du hast bereits eine Stimme fuer dieses Plakat abgegeben!");
						}
						else
						{
							alert("Danke fuer deine Stimme!");
							location.reload();
						}
						
					},
					error: function (data, status, e){}
				}
			)
		}
		else
		{
			alert('Bitte gib eine Wertung an!');
		}
	});
	
	$("div.button_kommentieren").click(function () 
	{
		if ($('input#name').val()=="")
		{
			alert("Bitte gib deinen Namen an!");
		}
		else if($('input#email').val()=="")
		{
			alert("Bitte gib deine E-Mailadresse an!");
		}
		else if($('textarea#kommentar').val()=="")
		{
			alert("Bitte gib einen Kommentar ein!");
		}
		else
		{
			$.ajax
			(
				{
					url:'/typo3conf/ext/wk_alt/php/save_comment.php',
					data:"uid="+$("#uid").val()+"&name="+$('input#name').val()+"&email="+$('input#email').val()+"&kommentar="+$('textarea#kommentar').val(),
					dataType: 'text',
					success: function (data, status)
					{
						location.reload();
					},
					error: function (data, status, e){}
				}
			)
		}
	});
	
	$("div.button_abschicken").click(function () 
	{
		if ($('input#name').val()=="")
		{
			alert("Bitte gib deinen Namen an!");
		}
		else if($('input#email').val()=="")
		{
			alert("Bitte gib deine E-Mailadresse an!");
		}
		else if($('input#empfaenger').val()=="")
		{
			alert("Bitte gib die E-Mailadresse des Empfaengers an!");
		}
		else if($('input#code').val()=="")
		{
			alert("Bitte gib den abgebildeten Sicherheitscode ein!");
		}
		else
		{
			$.ajax
			(
				{
					url:'/typo3conf/ext/wk_alt/php/send_mail.php',
					data:$(".tipafriend form").serialize(),
					dataType: 'text',
					success: function (data, status)
					{
						if (data=="1")
						{
							reloadPic();
							$("input#code").attr("value","");
							$("input#name").attr("value","");
							$("input#email").attr("value","");
							$("input#empfaenger").attr("value","");
							$("textarea#nachricht").attr("value","");
							alert("Du hast diese Seite erfolgreich weiterempfohlen");
						}
						else if(data=="0")
						{
							reloadPic();
							$("input#code").attr("value","");
							alert("Der Sicherheitscode ist leider falsch. Versuch es noch einmal!");
						}
						else
						{
							alert("Der Versand ist leider fehlgeschlagen. Versuch es noch einmal!");
						}
					},
					error: function (data, status, e){}
				}
			)
		}
	});
	
	
	

function reloadPic(){
	var d = new Date();
	$.get("/typo3conf/ext/wk_alt/php/captcha.php");
	$('.captcha_img img').attr('src', '/typo3conf/ext/wk_alt/php/captcha.php?id='+ d.getTime());
}
	
	
	
	
    new AjaxUpload('upload_button', {
        action: '/typo3conf/ext/wk_alt/php/upload.php',
		onSubmit : function(file , ext){
			if (ext && /^(jpg|png|jpeg|gif)$/.test(ext))
			{
				$('#vorschau').html('<img src="/fileadmin/images/ajax-loader.gif" class="loader">');	
			} 
			else 
			{
				$('#vorschau').text('Nur Bilddateien sind erlaubt!');
				return false;				
			}
		},
		onComplete : function(file,response){
			$('#vorschau').text('Datei erfolgreich hochgeladen!');	
			$('#vorschau').html('<img src="'+response+'">');			
			$('#image').attr("value",response);
			
					
		}
    });
	
	$("span.button_beitrag_abschicken").click(function () 
	{ 
		if ($('input#name').val()=="")
		{
			alert("Bitte gib deinen Namen an!");
		}
		else if($('input#email').val()=="")
		{
			alert("Bitte gib deine E-Mailadresse an!");
		}
		else if($('input#titel').val()=="")
		{
			alert("Bitte gib den Titel deines Plakats an!");
		}
		else if($('input#image').val()=="")
		{
			alert("Bitte lad dein Plakat hoch!");
		}
		else if($('select#biermarke').val()=="0")
		{
			alert("Bitte waehle die gewuenschte Biermarke aus!");
		}
		else if(!$('#bedingung').attr("checked"))
		{
			alert("Bitte akzeptiere die Teilnahmebedingung!");
		}
		else
		{
			var form_data=$(".tx-wkalt-pi1 form").serialize();
			// AJAX function that sends the values of the inputs to the functions.php that will save the data in to the database
			$.ajax
			(
				{
					url:'/typo3conf/ext/wk_alt/php/save_entry.php',
					data:form_data,
					dataType: 'json',
					success: function (data, status)
					{
						if (data)
						{
							$('input#name').attr("value","");
						$('input#email').attr("value","");
						$('input#titel').attr("value","");
						$('#vorschau').html("Keine Datei ausgew&auml;hlt");
						$("#biermarke").selectOptions("0");
						$("#bedingung").attr('checked', false);

						alert("Danke fuer dein Plakat");
						}
						
					},
					error: function (data, status, e){}
				}
			)
		}

    });
});

function show_dialog(id)
	{
	    $("#" + id).dialog({
	        modal: true,
			bgiframe:true,
	        overlay: {
	            opacity: 0.5,
	            background: "black"
	        },
	        
	        buttons: {
	            "OK": function(){
	                $(this).dialog("close");
	            }
	        },
	        draggable: false,
	        resizable: false,
	        autoOpen: false
	    });
	    $("#" + id).dialog('open');
	}

