var pos = "info";
var ta = "klub";
/* Status codes for JSON */
var statusCode = new Array();
statusCode[200] = 'success';
statusCode[300] = 'notice';
statusCode[400] = 'error';
$(document).ready(function(){
	$(document).pngFix();
	$('.posta').attr("href","mailto:" + pos + "@" + ta + "-kbs.si");
	/* Enews */
	$('#subscribe').focus(function(){
		$(this).val('');
	}).blur(function(){
		if( $(this).val() == '' ){
			$(this).val('Prijava na E-novice');
		}
	});
	$('#go').click(function(){
		$.ajax({
			type: 'POST',
			url: '/script/subscribe.php',
			data: ({ mail : $('#subscribe').val() }),
			cache: false,
			dataType: 'json',
			success: function(data) {
				alert(data.msg);
				if( data.status == 200 )
				{
					$('#subscribe').val("Vnesite vašo E-pošto");
				}
			}
		});
	});
	/* Notices */
	$.Obvestilo = function(status, msg){
		var msgr = msg ? msg : 'Prišlo je do napake, prosim poskusite pozneje.';
		var statusc = status ? statusCode[status] : 'error';

		$('#obvestilo').empty().clearQueue()
			.prepend('<div class="' + statusc + '">' + unescape(msgr) + '</div>')
			.slideDown();
	}
	/* Form */
	$('.form').ajaxForm({success:showResponse,dataType: 'json'});
	function showResponse(responseText, statusText, xhr, $form){
		if( responseText.status ){
			$.Obvestilo(responseText.status, responseText.msg);
		} else {
			$.Obvestilo(responseText.status, "Nedefinirano");
		}
		var out = new Array();

		out["status"] = responseText.status;
		out["msg"] = responseText.msg;
		out["mark"] = responseText.mark;
		out["id"] = responseText.id;
		after_submit(out, statusText, xhr, $form);
	}
	function after_submit(responseText, statusText, xhr, $form){
		/* UnMark */
		$(":input, label", $form).removeClass("missing");
		
		if( responseText.status ){
			$.Obvestilo(responseText.status, responseText.msg);
		} else {
			$.Obvestilo(responseText.status, "Nedefinirano");
		}

		/* Submit successful */
		if( responseText.status == 200 ){
			/* Hide buttons */
			$(".shrani, .izbrisi", $form).attr("disabled", true).hide();
			$(":input", $form).val("");
			$form.hide();
			
			if( $form.attr('id') == 'comments' ){
				get_comments( responseText.id );
			}
		}
			
		/* Mark fields */
		if(responseText.mark){
			if( responseText.mark.length > 0 )
			{
				var mark_me = "";
				var polje = "";
				var vseh = responseText.mark.length;
				for(var i = 0; i <= vseh; i++ )
				{
					if(responseText.mark[i])
					{
						polje = responseText.mark[i];
						vejica = ( i < vseh ) ? "," : "";
						mark_me += "#" + polje + ",label[for=" + polje + "]" + vejica;
					}
				}
				$form.find( mark_me ).addClass("missing");
			}
		}
	}
	
	/* Update comments */
	function get_comments( news_id ){
		if(news_id){
			$.ajax({
				type: 'POST',
				url: '/script/comment.php',
				data: ({nid : news_id, read: 1}),
				dataType: 'html',
				success: function(data) {
					$('#comments_view').empty().html( data );
				}
			});
		}
	}
	/* Vote poll */
	$('#poll_vote').ajaxForm({
		target: '#poll',
		success: function(){$('#poll').fadeIn('slow');}
	});
	/* Facebook update UID */
	function update_userbox(uid, name, image, url, logout) {
	   //populate the data in #userbox and show it  
	  /* $('#userbox').html( "<a href='" +url+ "'>"  
						 + "<img alt='" +name+ "' src='" +image+ "' />"  
						 + "Prijavljeni ste kot " +name+ "</a> ").show();  */
	//					 + "(<a href='./index.php' onclick='" + logout + "'>logout</a>)" ).show();  
	   
	   //hide name input and service  
	   //login buttons  
	   //$('#userinfo').hide();  
	   
	   $('#uid').val(uid);
	   $('#unm, .unm').val(name);  
	   $('.fbusername').show();
	   //$('#url').val(url);  
	   //$('#image').val(image);  
	}
	/* Form reset handler */
	$('form').bind('reset', function(){
		$('input, label', $(this)).removeClass('missing');
		$('#obvestilo').empty();
	});
	/* Fonts */
	Cufon.replace(
		'.title_block h1,.post h2,.post h1', { fontFamily: 'Franklin Gothic Heavy' }
	);
	Cufon.replace(
		'.more,.less,.pg_next,.pg_previous,#main_nav span a,.imenu a,#foot_nav a', { fontFamily: 'Franklin Gothic Medium Cond', hover: true }
	);
});

