﻿$(document).ready(function() {
	$('#site-name').bind('click', function(){
		window.location.href = 'http://www.zanimivo.si';
	});
	
	$('.addCount').bind('click', function() {
		var id = $(this).parent().parent().parent().attr('id').substring(8);
		$.get(my_base + '/ogled.php?id=' + id, function(data) {
			if(data != 0 && data.length > 0) {
				$('#ogledov-' + id)[0].innerHTML = data; //.html()
			}
		});
	});
	
	$('.addCount2').bind('click', function() {
		var id = $(this).attr('rel');
		$.get(my_base + '/ogled.php?id=' + id, function(data) {
			if(data != 0 && data.length > 0) {
				$('#ogledov-' + id)[0].innerHTML = data; //.html()
			}
		});
	});
	
	$('.menealo').bind('click', function() {
		var id  = $(this).attr('id').substring(8);
		var md5 = $(this).children().attr('rel');
		$.get(my_base + '/menealo.php?id=' + id + '&md5=' + md5, function(data) {
			if(data != 0 && data.length > 0) {
				$('#mnms-' + id)[0].innerHTML = data;
				$('#mnmlink-' + id)[0].innerHTML = '<span>glasovano<\/span>';
			}
		});
	});
	
	$('.g_tell').bind('click', function() {
		var id = $(this).parent().parent().parent().parent().attr('id').substring(8);
		$('#emailto-' + id)[0].innerHTML = 'Nalagam... Prosim počakaj!';
		if($('#emailto-' + id)[0].innerHTML.length < 35) {
			$.get(my_base + '/recommend.php?draw=small&linkid=' + id, function(data) {
				$('#emailto-' + id)[0].innerHTML = data;
			});
		}
		$('#emailto-' + id).slideToggle('slow');
	});
	
	$('.dremove_lnk').click(function(){
		$('#'+$(this).attr('rel')).toggle();
		return false;
	});
	
	
	emailto = function(_this) {
		var id = $(_this).parent().parent().parent().parent().parent().attr('id').substring(8);
		var emails = '';
		for(var i = 1; i <= 3; i++) {
			if($('#email_address_' + i)[0].value.length > 0) {
				emails += $('#email_address_' + i)[0].value + ', ';
			}
		}
		message = $(_this).children().filter('textarea')[0].value;
		$.post(my_base + '/recommend.php', {linkid: id, email_address: emails, email_message: message, email_to_submit: true}, function(data) {
			$('#emailto-' + id)[0].innerHTML = data;
		});
	};
	
	prevod = function(_this) {
		var id = $(_this).parent().parent().parent().parent().parent().attr('id').substring(8);
		
		besedilo = $(_this).children().filter('textarea')[0].value;
		$.post(my_base + '/prevedi.php', {linkid: id, txt: besedilo, translate: true}, function(data) {
			$('#foreign-' + id)[0].innerHTML = data;
		});
	};
	
	getCheckedRadioValue = function(name) {
		radio = document.getElementsByName(name);
		for(var i = 0; i < radio.length; i++) {
			if(radio[i].checked) {
				return radio[i].value;
			}
		}
	};
	
	checkPrevodi = function(_this, act) {
		var ids = '';
		checkbox = document.getElementsByName('ids_ok[]');
		for(var i = 0; i < checkbox.length; i++) {
			if(checkbox[i].checked) {
				ids += '&ids[]=' + escape(checkbox[i].value);
			}
		}
		
		$.ajax({type: 'POST',
				url: my_base + '/admin_predlogi.php',
				data: 'moderiraj=' + act + ids,
				success: function(data) {
					location.href = window.location.pathname;
				}
		});
	}
	
	$('ul.column-list').bind('change', function() {
		if(getCheckedRadioValue('link_field1') == 'slo') {
			$('#tuj-opis').hide('slow');
		} else {
			$('#tuj-opis').show('slow');
		}
	});
	
	$('.foreign').bind('click', function() {
		var id = $(this).parent().parent().parent().parent().attr('id').substring(8) || $(this).parent().parent().parent().attr('id').substring(8);
		var action = $(this).attr('rel');
		
		$('#foreign-' + id)[0].innerHTML = 'Nalagam... Prosim počakaj!';
		if($('#foreign-' + id)[0].innerHTML.length < 35) {
			$.get(my_base + '/prevedi.php?draw=' + action + '&linkid=' + id, function(data) {
				$('#foreign-' + id)[0].innerHTML = data;
			});
		}
		
		$('#foreign-' + id).toggle('slow');
	});
	
	$('#predlogi a').bind('click', function() {
		$.get(my_base + '/admin_predlogi.php?get=all', function(data) {
			$('#predlogi')[0].innerHTML = data;
		});
	});
	
	$('.thumb').bind('focus', function() {
		$('input:radio[@name="slika_tip"][@value="' + $(this).attr('type') + '"]')[0].checked = true;
	});
	
	$('input:text[@name="thumb_url"]').bind('keyup', function() {
		var img = new Image();
		img.src = $('input:text[@name="thumb_url"]').attr('value');
		img.onload = function()	{
			$('img#img_thumb').attr('src', $('input:text[@name="thumb_url"]').attr('value'));
		};
		img.onerror = function() {
			$('img#img_thumb').attr('src', my_base + '/img/nophoto.gif');
		};
	});
	
	if(getCheckedRadioValue('link_field1') == 'slo') {
		$('#tuj-opis').hide('slow');
	} else {
		$('#tuj-opis').show('slow');
	}
});