// JavaScript Document

$(document).ready(function() {
		
	$('.cat-lvl1 ul').hide();
	
	$('.cat-lvl1 span').click(function() {
		$(this).parent().toggleClass('hide-cat');
		$(this).parent().find('ul').toggle(400);
    	return false;
  	});
	
	//hide latest articles after 5th
	$("#latest-articles-wrap div.latest-articles-article:gt(4)").hide();
	
	var Toggled = false;
	$(".latest-articles-morelink").click(function(){
		if(Toggled==false){
			$('.latest-articles-morelink').children('p').html('Færre artikler'); 
			Toggled=true;
		}
		else{
			$('.latest-articles-morelink').children('p').html('Flere artikler');
			Toggled=false;
		}
		$(".latest-articles-article:gt(4)").toggle(400);
//        $("#contenu").slideToggle(400);
        return false;

	});

	$('#q').autocomplete({
		serviceUrl:'http://www.fag-artikler.dk/fileadmin/fag-artikler/tag_json.php',
		minChars:2,
		delimiter: /(,|;)\s*/, // regex or character
		maxHeight:400,
		width:300,
		deferRequestBy: 30, //miliseconds
		params: { country:'Yes' } //aditional parameters
		// callback function:
//		onSelect: function(value, data){ alert('You selected: ' + value + ', ' + data); },
		// local autosugest options:
//		lookup: ['January', 'February', 'March', 'April', 'May'] //local lookup values 
	});
	
	$("#create-new-user").validate({
//		debug: true,
		rules: {
			"tx_tgarticles_pi1[email]":{
				required: true,
				email: true,			
			},
			"tx_tgarticles_pi1[password]":{
				required: true,
				minlength: 7,
			},
			"tx_tgarticles_pi1[username]":{
				required: true,				
				minlength: 7,				
			},
			"tx_tgarticles_pi1[alias]":{
				required: true,
				minlength: 5,
			},
			"tx_tgarticles_pi1[conditions]":{
				required: true,
			}
		},
		messages: {
			"tx_tgarticles_pi1[email]":{
				required: "Du skal skrive din email adresse",
				email: "Hov, der er noget galt med din email!",
			},
			"tx_tgarticles_pi1[password]":{
				required: "Du glemte at vælge et kodeord!",
				minlength: jQuery.format("Minimum {0} tegn!"),
			},
			"tx_tgarticles_pi1[username]":{
				required: "Du skal skrive din email adresse",
				minlength: jQuery.format("Minimum {0} tegn!"),
			},
			"tx_tgarticles_pi1[alias]":{
				required: "Du glemte at vælge et forfatternavn!",
				minlength: jQuery.format("Minimum {0} tegn!"),
			},
			"tx_tgarticles_pi1[conditions]":{
				required: "Du skal acceptere betingelserne",
			},
		},
	});
	
	$("#create-new-article").validate({
		rules: {
			"tx_tgarticles_pi1[a_title]": {
				required: true,
				minlength: 25
			},
			"tx_tgarticles_pi1[a_author]": {
				required: true,
				minlength: 5
			},
			"tx_tgarticles_pi1[a_content]": {
				required: true,
				minlength: function () {
					var words = jQuery.trim($('#tx_tgarticles_pi1\\[a_content\\]').val()).split(" ").length;
					if (words < 250) {
						return "250";
					} else {
						return "0";
					}
				},
			},
			"q": {
				required: true,
				minlength: function () {
					var tags = $("#q").val().split(",");
					if(tags.length < 3){
						return "99999999";
					} else {
						return "0";
					}
				}
			},
			"tx_tgarticles_pi1[a_category]": {
				min: 1
			},
		},
		messages: {
			"tx_tgarticles_pi1[a_title]": {
				required: "Husk at skrive en titel til artiklen!",
				minlength: jQuery.format("Arh, lidt flere bogstaver! minimum {0} bogstaver!")
			},
			"tx_tgarticles_pi1[a_author]": {
				required: "Du skal skrive et forfatternavn!",
				minlength: jQuery.format("mindst {0} bogstaver!")
			},
			"tx_tgarticles_pi1[a_content]": {
				required: "Husk da lige at skrive artiklen hva!",
				minlength: jQuery.format("Liiiidt flere ord. Minimum {0} ord")
			},
			"q": {
				required: "Giv lige artiklen 3 emne-tags. Det er ikke så svært!",
				minlength: "Giv lige artiklen 3 emne-tags. Det er ikke så svært!",
			},
			"tx_tgarticles_pi1[a_category]": {
				min: "Husk at vælge en kategori!",
			},
		}									  
	});

	$('textarea.mcecontent').tinymce({
		// Location of TinyMCE script
		script_url : '/fileadmin/tinymce/tiny_mce.js',
 		// General options
		theme : "advanced",
		plugins : "safari,pagebreak,style,layer,iespell,inlinepopups,preview,contextmenu,paste,directionality,fullscreen,nonbreaking,xhtmlxtras,template",
		//plugins : "safari,pagebreak,style,layer,advlink,iespell,inlinepopups,preview,contextmenu,paste,directionality,fullscreen,nonbreaking,xhtmlxtras,template",
		// Theme options
		theme_advanced_buttons1 : "bold,italic,bullist,numlist,|,link,unlink,|,paste,pastetext,pasteword,undo,redo,preview,fullscreen",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		language: "da",
		// Example content CSS (should be your site CSS)
		content_css : "fileadmin/fag-artikler/style.css",
		
		//external_link_list_url : "fileadmin/fag-artikler/link_list.js",
		
		//Character count :: http://tinymce.moxiecode.com/punbb/viewtopic.php?id=1922
     	theme_advanced_path : false,
     	setup : function(ed) {
        	ed.onKeyUp.add(function(ed, e) {
				var content = tinyMCE.activeEditor.getContent();
            	var strip = content.replace(/(<([^>]+)>)/ig,"");
				var wordcount = strip.split(' ').length;
	        	var text = wordcount + " ord , minimum er 250 ord";
        		tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row'), text);
				$.article_c = {
					total: wordcount,
				}
			});
     	}

	});
	$('textarea.mcebio').tinymce({
		// Location of TinyMCE script
		script_url : '/fileadmin/tinymce/tiny_mce.js',
 		// General options
		theme : "advanced",
		plugins : "safari,pagebreak,style,layer,iespell,inlinepopups,preview,contextmenu,paste,directionality,fullscreen,nonbreaking,xhtmlxtras,template",
		//plugins : "safari,pagebreak,style,layer,advlink,iespell,inlinepopups,preview,contextmenu,paste,directionality,fullscreen,nonbreaking,xhtmlxtras,template",
		// Theme options
		theme_advanced_buttons1 : "bold,italic,bullist,numlist,|,link,unlink,|,paste,pastetext,pasteword,undo,redo,preview,fullscreen",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		language: "da",
		// Example content CSS (should be your site CSS)
		content_css : "fileadmin/fag-artikler/style.css",
		
	});
});