
/**
 * DatePicker
 */
$.dpText = {
	TEXT_PREV_YEAR		:	'Letztes Jahr',
	TEXT_PREV_MONTH		:	'Letzter Monat',
	TEXT_NEXT_YEAR		:	'Nächstes Jahr',
	TEXT_NEXT_MONTH		:	'Nächster Monat',
	TEXT_CLOSE			:	'Schliessen',
	TEXT_CHOOSE_DATE	:	'Datum auswählen'
}
Date.firstDayOfWeek = 1;
Date.format = 'yyyy-mm-dd';
$(function(){
	$('.date-pick').datePicker({startDate:'2009-01-01'});
});

/**
 * Audio-Player Close
 */
function ap_stopAll(player_id) {
	$('.audio_flash').each(function(){
		if($(this).attr('id') != player_id)
		{
			$(this).find('object')[0].SetVariable("closePlayer", 1);
		}
		else 
		{
			$(this).find('object')[0].SetVariable("closePlayer", 0);
		}
	});
}

$().ready(function() {
	
	if (window.location.pathname.match(/admin/)) {
		var url = '../';
	} else {
		var url = '';
	}
	
	/**
	 * TextLinks
	 */
	$('div.primar div.text div.innertext.activetext').html($('div.textlinks > div.active > div.text').html());
	$('div.textlinks > div').click(function(){
		var text = $(this).find('div.text').html();
		var inpt = $('div.primar div.text div.innertext');
		if (inpt.html() != text) {
			inpt.fadeOut(250, function(){
				$(this).html(text).fadeIn(500);
			});
		}
	});
	
	/**
	 * Audio-Player
	 */
	$('.audio').each(function() {
		audio_file = $(this).attr('href'); 
		audio_title = $('<a class="title" href="' + audio_file + '">' + $(".title", this).text() + '</a><div class="description">' + $(".description", this).text() + '</div>');
		audio_id = $(this).attr('id');
 
		div = $('<div class="audio_flash" id="' + audio_id + '"></div>');		
		$(this).after(div);
		$(this).after(audio_title);
		$(this).remove();
		div.flash({
			swf :		'inc/audio-player/player.swf',
			params : 
			{
				wmode :			'transparent'
			},
			flashvars :
			{
				soundFile :		audio_file,
				playerID :		"'" + audio_id + "'",
				quality :		'high',
				lefticon :		'0x158FBD',
				righticon :		'0x158FBD',
				righticonhover :'0xD20014',
				leftbg :		'0x030341',
				rightbg :		'0x030341',
				rightbghover :	'0x030341',
				wmode :			'transparent'
			},
			height : 	24,
			width :		300
		});
	});
	
	/**
	 * TinyMCE
	 */
	$('textarea.tinymce').tinymce({
		
		// Location of TinyMCE script
		script_url : url+"inc/tiny_mce/tiny_mce.js",

		// General options
		theme : "advanced",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,forecolor,backcolor,separator,bullist,numlist,undo,redo,separator,link,unlink,pasteword,code",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : false,
		max_chars : 800,

		content_css : url+"css/geschichtendock.css,"+url+"css/fonts.css"
	});
	
	/**
	 * Animation von den Votings
	 */
	$("div.vote").animate({
		"width" : "100%"
	}, 1000);
	
	/**
	 * Durch drücken des TR Voting auswählen
	 */
	$("div.voting table tr.vote").click(function(){
		if ($(this).find("input").is("[type=radio]")) {
			$(this).parent().find("input[type=radio]:checked").attr("checked", false).next("div.fakeinput").attr("class", "fakeinput imgbtn remove");
			$(this).find("input[type=radio]").attr("checked", true).next("div.fakeinput").attr("class", "fakeinput imgbtn accept");
		}
	})
	
	/**
	 * Toggle Gruppe von BuchDerGeschichten
	 */
	$("ul.allgstorys li ul.allstorys li.active").parent().css({"display" : "block"});
	$("ul.allgstorys li div.title").click(function() {
		$(this).parent().find("ul.allstorys").slideToggle("normal");
	});
	
	/**
	 * Addieren und subtrahieren per Button
	 */
	$('input[type=button].intplus').click(function(){
		var inp = $(this).parent().children('input');
		inp.val(parseInt(inp.val()) + 1);
	});
	$('input[type=button].intminus').click(function(){
		var inp = $(this).parent().children('input');
		inp.val(parseInt(inp.val()) - 1);
	});

	/**
	 * Uploadify
	 */
	$("#uploadify").uploadify({
		'uploader'	: url+'inc/uploadify/uploadify.swf',
		'script'	: url+'ajax/upload.php',
		'cancelImg'	: url+'inc/uploadify/cancel.png',
		/*'buttonImg'	: url+'img/btn_w100_search.png',
		'width'		: '100',
		'height'	: '24',*/
		'buttonText': 'Suche..',
		'wmode'		: 'transparent',
		'folder'	: 'uploads',
		'scriptData': {
			'cid'	: $("#uploadify").parent().find("input[name=id]").val(),
			'sess'	: $("#uploadify").parent().find("input[name=sess]").val()
		},
		'queueID'	: 'fileQueue',
		'fileDesc'	: 'PDF und MP3',
		'fileExt'	: '*.pdf;*.mp3',
		/*'scriptAccess' : 'always',*/
		'auto'		: true,
		'multi'		: true,
		'onAllComplete' : 
			function () {
				var rq = $.ajax({
					url		: url+"ajax/get_admin_explore_data.php",
					async	: false,
					type	: 'POST',
					dataType: 'html',
					data	: ({
						'id' : $("#explore_data").parent().parent().find("input[name=id]").val()
					})
				});
				$("#explore_data").html(rq.responseText);
			}
	});
	
	/**
	 * FancyBox
	 */
	$("a.youtube").fancybox({
		titlePosition		: 'inside',
	    'autoScale'         : false,
	    'autoDimensions'    : false,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'frameWidth'        : 854,
		'frameHeight'       : 'auto',
		'overlayShow'		: true,
		'hideOnContentClick': false
	});
	
	/**
	 * FileInput rebrush
	 */
	$("input[type=file].rebrush").rebrushfileupload({
		button_text: "Suche...",
		class_container: "fileupload",
		class_field: "txt",
		class_button: "button btnw60"
	});
	$("input[type=file].rebrush").each(function() {
		var pr = $(this).parent();
		if (!pr.hasClass("rebrush-container")) 
			return false;
		pr.find("input.rebrush-field").val(pr.parent().find("div.before").html());
		return true;
	});
	
	/**
	 * Google Analytics
	 */
	$.qGoogleAnalytics('UA-12349616-1');
	
	/**
	 * Abstimmung hinzufügen und entfernen
	 */
	var inewvote = 0;
	$("tr.voting input[type=button].add").click(function() {
		$(this).before('<div class="vote votenew"id="newvote_'+inewvote
				+'"><input type="text" class="text" name="newvote[]" /><input type="button" class="imgbtn remove" onclick="removeNewVote('+inewvote+')" /></div>');
		inewvote++;
	});
	$("div.vote input[type=button].remove").click(function() {
		$(this).parent().css("display","none");
		$(this).parent().find("input").attr("name","remove" + $(this).parent().find("input").attr("name"));
	});
});
function removeNewVote (id) {
	$("#newvote_"+id).remove();
}