
	IE6 = false;
	blockme = false;

	function getUdzbenici(R,S,P) {
		$.ajax({
			type: "GET",
			url: "ajax/udzbenici",
			data: "R=" + R + "&S=" + S + "&P=" + P,
			beforeSend: function(x) {
				$('#selector').empty().addClass('loading');
			},
			complete: function(x) {
				$('#selector').removeClass('loading');
			},
			success: function(data){
				$('.edit .filters .Naslov input').toggleMyText('Počnite upisivati naslov ovdje kako biste lakše pronašli udžbenik u popisu');
 				$('#selector').removeClass('loading').html(data);
 				$('#selector .udzbenik').unbind('hover').hover(
					function() { $(this).addClass('uhover'); },
					function() { $(this).removeClass('uhover'); }
				);
				$('#selector .udzbenik').css('-moz-border-radius','5px').css('-webkit-border-radius','5px');
				$('#selector .udzbenik').unbind('click').click(function() {
					if(!blockme) {
						blockme = true;
						var smj = $('select[name="T"]').val();
						var kid = $(this).attr('KID');
						var udz = $(this);
						$.ajax({
							type: "POST",
							url: "ajax/new",
							data: "smjer=" + smj + "&kid=" + kid,
							success: function(data){
								if(data.length > 0) {
									$('.book_list').prepend('<div class="book_list_item" style="height: 36px;"></div>');
									$(".book_list_item:first").html(data).css('height','auto'); 
									var nob = $(".book_list_item:first .nob").html();
									$(".book_list_item:first .nob").remove();
									$("p.number_of_books").html(nob);
									var xnob = nob.split('>')[1];
									xnob = xnob.split('<')[0];
									$(".og4 strong span").html(xnob);
									$('.book_list_item').removeClass('odd_bli');
									$('.book_list_item:odd').addClass('odd_bli');
									$(".book_list_item:first").effect('highlight',{ },1500);
									blockme = false;
								}
								else {
									udz.find('.ouvp').remove();
									udz.append('<strong class="ouvp">Ovaj udžbenik već postoji u vašim oglasima!</strong>');
									udz.find('.ouvp').animate({opacity:1},1500,function() { $(this).fadeOut(500); });
									blockme = false;
								}
							}
						});
					}
				});
			}
		});
	}

	$(function() {
			
		$("#acco").accordion({ autoHeight: false });
						
		$('.printme').click(function() { window.print(); return false; });
		
		$('.viseiz').click(function() {
			var thisa = $(this);
			thisa.parent('p').prev('.izs2').toggle('blind',{},300,function() {
				if($(this).is(':visible')) { thisa.text(thisa.text().replace('Više','Manje').replace('▼','▲')); } else { thisa.text(thisa.text().replace('Manje','Više').replace('▲','▼')); }
			});
			
			return false;
		});
				
		$('#scroller-thingy > div:first').clone().addClass('startOver').appendTo($('#scroller-thingy'));
		$('#scroller-thingy > div:gt(0)').hide();
		setInterval((function() {
	        $('#scroller-thingy > div:not(:last):visible').slideUp('slow').next('div').slideDown('slow',function() { if($(this).hasClass('startOver')) { $(this).hide(); $('#scroller-thingy > div:first').show(); } });
	    }), 5000);
	    
	    $('.obf').deObf();
								
		if($('#selector').length && $('#ogType').text() == 1) 
		{ 
			getUdzbenici(1,1,0); 
		
			$('.edit .filters select').change(function() {
				getUdzbenici($('select[name=R]').val(),$('select[name=S]').val(),$('select[name=P]').val());
			});
		}
		
		$('.edit .filters .Naslov input').keyup(function() {
			
			v = $(this).val().toLowerCase();
			
			if(v == '') {
				$('#selector .udzbenik').removeClass('hideme');
			}
			else {
				$('#selector .udzbenik').each(function() {
					vv = $(this).children('strong').text().toLowerCase();
					if(vv.match(v)) {
						$(this).removeClass('hideme');
					}
					else {
						$(this).addClass('hideme');
					}
				});
			}
		});
		
		$('.nap a').live('click',function() {
			$(this).parent('.nap').makeEditable();
			return false;
		});
		
		$('.xdropdown').xdropdown();
		
		//$('.search input[name=N]').toggleMyText('Naslov');
		//$('.search input[name=A]').toggleMyText('Autor');
		
		$('.mysearch input[name=L]').toggleMyText('Pretraži moje oglase');
		
		$('.mysearch button').click(function() { 
			if($('.mysearch input[name=L]').val() == 'Pretraži moje oglase') $('.mysearch input[name=L]').val('');
			return true;
		});
				
		$('select').each(function() {
			if($(this).val() == 0) {
				$(this).addClass('grey');
			}
			else {
				$(this).removeClass('grey');
			}
		});
		
		$('select').change(function() {
			if($(this).val() == 0) {
				$(this).addClass('grey');
			}
			else {
				$(this).removeClass('grey');
			}
		});
		
		$('#oglasi .search .button button').click(function() { 
			if($('.search input[name=N]').val() == 'Naslov') $('.search input[name=N]').val('');
			if($('.search input[name=A]').val() == 'Autor') $('.search input[name=A]').val('');
			return true;
		});
						
		$('.book_list_item:not(.matchuser):odd').addClass('odd_bli');
				
		if(IE6) {
			$('button').hover(
				function() { $(this).addClass('iehover'); },
				function() { $(this).removeClass('iehover'); }
			);
		}
		
		$('.withulink .book_list_item').hover(
			function() { $(this).addClass('uhover'); $(this).find('.ulink').show(); },
			function() { $(this).removeClass('uhover'); $(this).find('.ulink').hide(); }
		);
				
		$('.confirm').live('click',function() {
			var id = $(this).attr('rel');
			var bli = $(this).parents('.book_list_item');
			var ogType = $('#ogType').text();
			$.ajax({
				type: "POST",
				url: "ajax/kill",
				data: "kill=" + id + "&wtk=" + ogType,
				success: function(data){					
					bli.slideUp(250,function() { bli.remove(); $('.book_list_item').removeClass('odd_bli'); $('.book_list_item:odd').addClass('odd_bli'); });
					$('.iako').text(data);
					$('.og4 strong span').text(data);
				}
			});
			return false;
		});
		
		$('#forgot_password').css('-moz-border-radius','10px').css('-webkit-border-radius','10px');
		$('#selected').css('-moz-border-radius','5px').css('-webkit-border-radius','5px');
								
		$('#password_recovery').click(function() {
			$('#forgot_password').toggle('blind',{easing:'easeOutBounce'},500); return false;
		});
									
	});
	
	$.fn.makeEditable = function() {
		var n = $(this).find('span').text();
		var p = $(this);
		p.slideUp(200,function() { 
			$(this).html('<textarea name="n">' + n + '</textarea><a class="nspr" href="#">Spremi</a><a class="nodu" href="#">Odustani</a><div class="c"></div>').slideDown(200); 
			p.find('a:last').click(function() {
				if(n == '') {
					p.slideUp(200,function() { $(this).html('<a href="#">Dodaj napomenu</a>').slideDown(200).find('a').click(function() { $(this).parent('.nap').makeEditable(); return false; }); });
				}
				else {
					p.slideUp(200,function() { $(this).html('Napomena: <span>' + n + '</span> - <a href="#">Promijeni</a>').slideDown(200).find('a').click(function() { $(this).parent('.nap').makeEditable(); return false; }); });
				}
				return false;
			});
			p.find('a:first').click(function() {
				
				var t = p.find('textarea').val();
				
				$.ajax({
					type: "POST",
					url: "ajax/napomena",
					data: "k=" + p.attr('title') + "&t=" + t,
					success: function(data){
						if(data == '') {
							p.slideUp(200,function() { $(this).html('<a href="#">Dodaj napomenu</a>').slideDown(200).find('a').click(function() { $(this).parent('.nap').makeEditable(); return false; }); });
						}
						else {
							p.slideUp(200,function() { $(this).html('Napomena: <span>' + data + '</span> - <a href="#">Promijeni</a>').slideDown(200).find('a').click(function() { $(this).parent('.nap').makeEditable(); return false; }); });
						}
					}
				});
				
				return false;
			});
		}); 
		return $(this);
	}
	
	jQuery.fn.xdropdown = function() {
				
		$(this).each(function() {
					
			var inp = $(this);
			var lst = inp.next('ul');
			
			if(!lst.is('ul')) { return $(this); }		
			
			inp.attr('readonly','readonly');
			
			var xid = $.data($(this));
			var uid = 'xdropdown_' + xid;
			var val = inp.val();
						
			opt = '<ul>' + lst.html() + '</ul>';
			lst.remove();
			inp.prev('input').andSelf().wrapAll('<div class="xdropdown_parent"></div>');
			inp.attr('xdropdownid',xid);
			inp.after('<div class="xdropdown_wrapper" id="' + uid + '" />');
			$('#' + uid).html(opt);
			
			$('#' + uid).find('a').each(function() {
				if($(this).next('ul').is('ul')) $(this).addClass('xdropdownSub');
			});
			
			$('.xdropdown_wrapper ul').css('-moz-box-shadow','1px 2px 5px #aaa').css('-webkit-box-shadow','1px 2px 5px #aaa').css('-moz-border-radius','3px').css('-webkit-border-radius','3px');
					
			inp.click(function() { 
				$('.xdropdown_wrapper:not(#xdropdown_' + $(this).attr('xdropdownid') + ')').hide();
				var pos = inp.offset();
				$('#xdropdown_' + $(this).attr('xdropdownid')).css('top',-225).css('left',205).toggle('clip',250); return false; 
			});
						
			inp.outerClick(function() { 
				$('#xdropdown_' + $(this).attr('xdropdownid')).hide(); return false; 
			});
		
			$('#' + uid + ' ul li').unbind('hover').hover(
				function() { $(this).addClass('xdropdownHover'); },
				function() { $(this).removeClass('xdropdownHover'); }
			);
		
			$('#' + uid + ' ul li a').click(function() {
				if($(this).attr('rel') != '0') {
					inp.val($(this).text()); 
					inp.prev('input').val($(this).attr('rel')); 
					$('#' + uid).hide();
				}
				return false;
			});
		
		});
		
		return $(this);	
	}
	
	$.fn.toggleMyText = function(t) {
		$(this).unbind('focus').unbind('blur')
		if($(this).val() == '' || $(this).val() == t) $(this).addClass('grey').val(t);
		$(this).focus(function() { if($(this).val() == t) $(this).removeClass('grey').val(''); });
		$(this).blur(function() { if($(this).val() == '') $(this).addClass('grey').val(t); });
		return $(this);
	}
	
	$.fn.deObf = function() {
   		return this.each(function(){
     		var email = $(this).text().replace(/\s*\(.+\)\s*/,"@");
     		$(this).text(email).rot13();
   		});
	}; 
	
	$.fn.rot13 = function() {
        this.each(function() {
            $(this).text($(this).text().replace(/[a-z]/ig, function(chr) {
                var cc = chr.charCodeAt(0);
                if (cc >= 65 && cc <= 90) cc = 65 + ((cc - 52) % 26);
                else if (cc >= 97 && cc <= 122) cc = 97 + ((cc - 84) % 26);
                return String.fromCharCode(cc);
            }));
        });
        return this;
    };