$(function() {
//	var GAID = '9580757-1',
//	GAURL = (location.protocol == 'http:' ? 'http://www.' : 'https://ssl.') + 'google-analytics.com/'+'ga.js';
//	jQuery.getScript(GAURL, function() {
//		pageTracker = _gat._getTracker(GAID);
////		pageTracker._initData();
//		pageTracker._trackPageview();
//	});
 
 

$('.entry .link').attr('target','_blank');
$('.entry .link').click(function(){
	id=$(this).parent().attr('id');
	id=id.replace('entry-','');
	
	$.post('/index.php?a=click',{id:id},function(){
		
	});
});
 
//Formularz dodawania strony
if($('.add-form').length>0) {
	var lengths= new Object();
	lengths["site-title"]			=new Array(4,60);
	lengths['site-description']		=new Array(250,1000);
	lengths['site-keywords']		=new Array(20,500);
	lengths['site-email']			=new Array(5,500);
	lengths['site-code']			=new Array(8,8);

	
	//Pokazuje formularz
	$('.add-form').css('display','block');
	
	
	function add_error(x) {
		$('div.add-form div.status div').fadeOut(100,function(){
			$('div.add-form div.status').removeClass('ok').removeClass('loader').addClass('error');
			$('div.add-form div.message').text(x);
			$('div.add-form div.status div').fadeIn();			
		});
	};
	
	function add_ok(x) {
		$('div.add-form div.status div').fadeOut(100,function(){
			$('div.add-form div.status').removeClass('error').removeClass('loader').addClass('ok');
			$('div.add-form div.message').text(x);
			$('div.add-form div.status div').fadeIn()			
		});
	};
	
	function add_clear() {
		$('div.add-form div.status div').fadeOut(100);
	};
	
	function add_loader() {
		$('div.add-form div.status div').fadeOut(10,function(){
			$('div.add-form div.status').removeClass('error').removeClass('ok').addClass('loader');
			$('div.add-form div.message').text('wczytywanie...');
			$('div.add-form div.status div').fadeIn();			
		});

	};
	



	
	//Sprawdza istnienie adresu
	function check_adress(){
		var site_adress=$('#site-adress').children('input').attr('value');
		var site_subcategory=$('#site-subcategory').attr('value');
		if (site_adress.length > 3) {
			add_loader();
			$.post('/index.php?a=add&m=site_check', {
				site_adress: site_adress,
				site_subcategory:site_subcategory
			}, function(data){	
				if(data['s']=='0') {

					$('#site-adress').children('[type="text"]').attr({'disabled':'disabled','value':'http://'+data.url});
					$('#site-submit').addClass('show-details');
					add_clear();				
					if(data['m'] !== undefined && data['m'].length>0) add_ok(data['m']);
					if(data['out'] !== undefined && data['out'].length>0) {
			    $('div.add-form fieldset .details').html(data['out']);
      
      		$('div.add-form fieldset .details').slideDown(1200,function(){
					//	pageTracker._trackPageview("/FORM");
					});          
          }
				} else if(data['s']=='1' && data['m'].length>0) {
					add_error(data['m']);
				} else if(data['s']=='2' && data['m'].length>0) {
					add_ok(data['m']);
				} else {
					add_error('Nieznany błąd (Error 01)');
				};
			},'json');	
			}
		else 
			add_error('Nieprawidłowy adres');
		}
	function add_site() {
		var add_errors=0;
		site_adress=$('#site-adress').children('.field');
		site_title=$('#site-title').children('input');
		site_description=$('#site-description').children('textarea');
		site_keywords=$('#site-keywords').children('textarea');
		site_email=$('#site-email').children('input');
		site_code=$('#site-code').children('input');	
		site_subcategory=$('#site-subcategory');	

	
		if(site_adress.val().length<11) { 
			site_adress.addClass('form-error');
			add_errors++;
		} else site_adress.removeClass('form-error');

		if(site_title.val().length<lengths["site-title"][0]) { 
			site_title.addClass('form-error');
			add_errors++;
		} else site_title.removeClass('form-error');
	

		if(site_description.val().length<lengths["site-description"][0]) { 
			site_description.addClass('form-error');
			add_errors++;
		} else site_description.removeClass('form-error');
		

		if(site_keywords.val().length<lengths["site-keywords"][0]) { 
			site_keywords.addClass('form-error');
			add_errors++;
		} else site_keywords.removeClass('form-error');

		if(site_email.val().length<lengths["site-email"][0]) { 
			site_email.addClass('form-error');
			add_errors++;
		} else site_email.removeClass('form-error');

		if(site_code.val().length<lengths["site-code"][0] ||site_code.val().length>lengths["site-code"][1]) { 
			site_code.addClass('form-error');
			add_errors++;
		} else site_code.removeClass('form-error');
	
	
		if(add_errors>0) {
			add_error('Pola oznaczone na czerwono nie są wypełnione poprawnie.');
			
		} else {
			add_loader();
			$.post('/index.php?a=add&m=site_add', {
				site_adress:site_adress.val(),
				site_title:site_title.val(),
				site_description:site_description.val(),
				site_keywords:site_keywords.val(),
				site_email:site_email.val(),
				site_code:site_code.val(),
				site_subcategory:site_subcategory.attr('value')
			},function(data){
				$('.add-form fieldset label input,.add-form fieldset label textarea').removeClass('form-error');
				if(data.s=='0') {
					$('#site-adress').children('[type="text"]').attr({'disabled':''});
					$('div.add-form fieldset .details').css('display','none');
					if(data['m'] !== undefined && data['m'].length>0) add_ok(data['m']);
					site_adress.val('');
					site_title.val('');
					site_description.val('');
					site_keywords.val('');
					site_email.val('');
					site_code.val('');
					pageTracker._trackPageview("/ADDED");
					
				} else if(data.s=='1') {
					if(data['m'] !== undefined && data['m'].length>0) add_error(data['m']);
					errors=data.errors;
					if(errors.length>0) {
						for(i=0;i<errors.length;i++) {
							$('#'+errors[i]+' .input').addClass('form-error');
						};
					};
					$('#site-code').children('input').val('');
				};
				
				
				
				
			},'json');
			
			
		}
	
	};
	
	
		
	
	$('.add-form fieldset label input,.add-form fieldset label textarea').live('keyup',function(){
		temp_id=$(this).parent().attr('id');
		
		if(temp_id=='site-title'|| temp_id=='site-description' || temp_id=='site-fulldescription' || temp_id=='site-keywords' || temp_id=='site-email' || temp_id=='site-code') {
			var f_text=$(this).val();
			f_length = f_text.length;
			if(f_length<lengths[temp_id][0] || f_length>lengths[temp_id][1]) {
				$('#'+temp_id+' .info em').css('color','red').text(f_length);
			} else {
				$('#'+temp_id+' .info em').css('color','green').text(f_length);
				$(this).removeClass('form-error');
			}
		}
	});
	

	$('#site-submit').click(function(){
		if(!$(this).hasClass('show-details')) check_adress();
		else add_site();
	});	

	$('#site-adress').keyup(function(e) {
		if(e.keyCode == 13) {
			check_adress();
		};
	});
	
	
};


function search() {
	s = $('#left .contents ul .search div input').val();
	if(s.length>2) {
			s=s.toLowerCase();
			window.location = '/tag,'+ s.replace(' ','-') +',0.html';
	};
	return FALSE;
};


$('#left .contents ul .search div a').click(function(){
	search();
});

$('#left .contents ul .search div input').keyup(function(e) {
	if(e.keyCode == 13) {
		search();
	};
});

});