// 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-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",
		
	});
	$("#submit-author").click(function(){
		var valid = '';
		var isr = ' er kr&aelig;vet';
		var password = $("#password").val();
		var username = $("#username").val();
		var alias = $("#alias").val();
		var email = $("#email").val();
		if (username.length<1) {
			valid += '<br />Brugernavn'+isr;
		}
		if (password.length<1) {
			valid += '<br />Password'+isr;
		}
		if (alias.length<1) {
			valid += '<br />Forfatternavn'+isr;
		}
		if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += '<br />Email '+isr;
		}

		if (valid!="") {
			$("#response").fadeIn("slow");
			$("#response").html("<p><b>Hov du overså vist noget:</b>"+valid+"</p>");
		}
		else {
			var datastr ='eID=tg_articles-register-author&username=' + username + '&email=' + email + '&alias=' + alias + '&password=' + password;
			$("#response").css("display", "block");
			$("#response").html("Opretter dig... ");
//			$("#response").fadeIn("fast");
			setTimeout("send('"+datastr+"','"+username+"','"+password+"')",10);
		}
		return false;
	});
});
function send(datastr,username,password){
	$.ajax({
		type: "POST",
		url: "/index.php",
		data: datastr,
		cache: false,
		success: function(html){
			if(html == 'OK'){
				setTimeout('$("#response").html("Du er blevet oprettet, du kan nu skrive din første artikel!")',1000);
				$.post("/",
					   {user:username,pass:password,logintype:'login',pid:'12',redirect_url:''},
					   function(data){
						  window.location = "/tilfoej-artikler/";
					   }
				);
			}
			else {
				$("#response").html(html);
			}
/*			$.ajax({
				type: "POST",
				url: "/tilfoej-artikler/",
				data: 'user=' + username + '&pass=' + password + 'logintype=login',
				cache: false,
				success: location.href("/tilfoej-artikler/")
			}) */
		}
	});
}
