var upload = {
	
	_max_files: 10,
	_default_display: '&#160;',
	
	load: function() {
		
		upload.add();
		
	},
	add: function() {
		$('.add').click(function() {
			
			var len = $('.row').length;
			var c = len - 1;
			var current = '.row:eq('+c+')'; 
			var add = '.row:eq('+c+') .add';
			var count = upload._max_files - c;
			
			if ( upload._max_files === len ) {
				$(add).remove();
				$(current).append('<span class="counter">'+count+'</span>');
				upload.hShow('display', 'Možete poslati najviše '+ upload._max_files +' fajlova istovremeno');
				return false;
			}
			
			$(current).clone().fadeIn().insertAfter(current);
			
			var n = '[name^="file"]:eq('+len+')';
			$(n).val('');
			
			var n = '[type="text"]:eq('+len+')';
			$(n).val('http://');
			
			$(n).click(function() {
				$(this).val('');
			});
			
			$(add).remove();
			$(current).append('<span class="counter">'+count+'</span>');
			
			upload.add();
			
			return false;
			
		});
	},
	stopTypingCustom: function(e) {
		var element = '#'+e;
		var l = $('#custom').val().length;
		$(element).attr('maxlength', l);
	},
	continueTypingCustom: function(e) {
		var element = '#'+e;
		$(element).attr('maxlength', form._custom_max);
	},
	focus: function(element) {
		var id = '#'+element;
		$(id).focus();
	},
	fShow: function(e, v) {
		var element = '#'+e;
		$(element).val(v);
	},
	hShow: function(e, v) {
		var element = '#'+e;
		$(element).html(v);
	},
	showLoaderIcon: function() {
		var html = '<img src="/images/design/loader.gif" alt="Loading..." />';
		form.hShow('short', html);
	},
	disable: function(e) {
		var element = '#'+e;
		$(element).attr('disabled', 'disabled');
	},
	enable: function(e) {
		var element = '#'+e;
		$(element).removeAttr('disabled');
	},
	readonly: function(e) {
		var element = '#'+e;
		$(element).attr('readonly', 'readonly');
	},
	unreadonly: function(e) {
		var element = '#'+e;
		$(element).removeAttr('readonly');
	},
	markError: function(e) {
		var element = '#'+e;
		$(element).css('color', '#f00');
	},
	markOK: function(e) {
		var element = '#'+e;
		$(element).css('color', '#666');
	}
	
};


var page = {
	
	_type: 1,
	_publish_online: 'Objavi online fotke',
	_publish_local: 'Objavi lokalne fotke',
	
	load: function() {
		
		page.initialValues();
	
		$('[rel="page"]').click(function() {
			
			var id = $(this).attr('href').replace('/#', '');
			page.show(id);
			
			if ( id == 'home' ) {
				page.initialValues();
				upload.hShow('display', '');
			}
			
			page.showSystemPopular();
			page.showSystemNew();
			
		});
		
		
		page.initShow();
		page.type();
		
	},
	showSystemNew: function() {
		if ( $('#page_new').is(':visible') ) {
			var u = '/api.php?system=_get_new';
			$('#page_new').html('');
			$('#page_new').append('<h5>Nove:</h5><br />');
			$.getJSON(u, function(data) {
				$.each(data, function(i) {
					var thumb = 'http://'+window.location.hostname+'/'+data[i].short+'/thumb';
					var href = 'http://'+window.location.hostname+'/'+data[i].short;
					var a = '<a class="preview" href="'+href+'" title="'+href+'" target="_blank"><img src="'+thumb+'" alt="'+href+'" /></a>\n';
					$('#page_new').append(a);
				});
			});
			
		}
	},
	showSystemPopular: function() {
		if ( $('#page_popular').is(':visible') ) {
			var u = '/api.php?system=_get_popular';
			$('#page_popular').html('');
			$('#page_popular').append('<h5>Popularne:</h5><br />');
			$.getJSON(u, function(data) {
				$.each(data, function(i) {
					var thumb = 'http://'+window.location.hostname+'/'+data[i].short+'/thumb';
					var href = 'http://'+window.location.hostname+'/'+data[i].short;
					var c = data[i].clicked;
					var clicked = c +' '+ page.grammarClick(c);
					var a = '<a class="preview" href="'+href+'" title="'+clicked+'" target="_blank"><img src="'+thumb+'" alt="'+href+'" /></a>';
					
					$('#page_popular').append(a);
				});
			});
			
		}
	},
	initShow: function() {
		var u = window.location.href;
		
		if ( /\#online$/.test(u) ) {
			page._type = 0;
			page.show('home');
			page.showOnline();
		}
		else if ( /\#local$/.test(u) ) {
			page._type = 1;
			page.show('home');
			page.showLocal();
		}
		else if ( /\#popular$/.test(u) ) {
			page.show('popular');
			page.showSystemPopular();
		}
		else if ( /\#new$/.test(u) ) {
			page.show('new');
			page.showSystemNew();
		}
		else if ( /\#/.test(u) ) {
			var p = /#[a-zA-Z]+/;
			var q = String(u.match(p)).replace('#', '');
			page.show(q);
		}
		else if ( !/\$/.test(u) ) {
			page.show('home');
		}
	},
	initialValues: function() {
		page._type = 1;
		page.showLocal();
		upload.hShow('type', page._publish_online);
		
		var rows = $('.row').length-1;
		var e = '.row:lt('+rows+')';
		$(e).hide(500).remove();
		
	},
	show: function(id) {
		page.hideAll();
		
		var i = '#page_'+id;
		$(i).css('display', 'block');
	},
	hideAll: function() {
		$('[id^="page_"]').css('display', 'none');
	},
	grammarClick: function(items) {
		if ( /(0|5|6|7|8|9|10|11|12|13|14)$/.test(items) )
			links = 'klikova';
		else if ( /(2|3|4)$/.test(items) )
			links = 'klika';
		else if ( /(1)$/.test(items) )
			links = 'klik';
		return links;
	},
	type: function() {
		$('#type').click(function() {
			if ( page._type % 2 ) {
				page.showOnline();
			} 
			else {
				page.showLocal();
			}
			page._type += 1;
		});
	},
	showLocal: function() {
		$('#type').html(page._publish_online);
		$('#type').attr('href', '#local');
		$('[name^="file"]').each(function(i) {
			var input = '<input name="file[]" size="30" type="file" />';
			var r = '.row:eq('+i+')';
			$(this).remove();
			$(r).prepend(input);
		});
	},
	showOnline: function() {
		$('#type').html(page._publish_local);
		$('#type').attr('href', '#online');
		$('[name^="file"]').each(function(i) {
			var input = '<input name="file[]" type="text" size="41" value="http://" />';
			var r = '.row:eq('+i+')';
			$(this).remove();
			$(r).prepend(input);
			$('[name^="file"]').click(function() {
				$(this).val('');
			});
		});
	}
	
};



$(document).ready(function() {
	
	upload.load();
	
	page.load();
	
});
