/**
* Copyright ladybug.co.rs
* author: agvozden@gmail.com 
*/
$().ready(function() {
	/**
	* autocomplete
	*/
	$("#search_autocomplete")
	.bind( "keydown", function( event ) {
		if ( event.keyCode == 13){
			$("#form1").submit();
			event.keyCode = 0;
		}
	})
	.autocomplete(url+"index.php", {
		//mustMatch: false,
		//matchContains: false,
		//matchSubset: false,
		minChars: 2,
		delay: 40,
		max: 50,
		multiple: true,
		//multipleSeparator: ', ',
		extraParams: {ajax:'autocomplete', t:'keyword'},
		parse: function(data) {
			return $.map(eval(data), function(row) {
				return {
					data: row,
					value: row.text,
					result: row.text
				};
			});
		},
		formatItem: function(item) {
			return item.text;
		}
	})
	.result(function(event, item) {
	})
	;

	$("#destinacija").autocomplete(url+"index.php", {
		minChars: 2,
		delay: 40,
		max: 50,
		extraParams: {ajax:'autocomplete', t:'keyword', t2:'cat'},
		parse: function(data) {
			return $.map(eval(data), function(row) {
				return {
					data: row,
					value: row.text,
					result: row.text
				};
			});
		},
		formatItem: function(item) {
			return item.text;
		}
	}).result(function(event, item) {
	});
	
	/**
	* autocomplete za registraciju
	*/
	$("#reg_grad").autocomplete("index.php", {
		mustMatch: false,
		matchContains: false,
		minChars: 2,
		delay: 40,
		max: 50,
		extraParams: {ajax:'autocomplete', t:'grad'},
		parse: function(data) {
			return $.map(eval(data), function(row) {
				return {
					data: row,
					value: row.text,
					result: row.text
				};
			});
		},
		formatItem: function(item) {
			return item.text;
		}
	}).result(function(event, item) {
	});
	
   	$("a").focus(function(){
		$(this).blur();
	});
	$('input:text').hint();
	
	// $("#stavka1 ul li:nth-child(2)").hide();
	// $("#stavka1 ul li:nth-child(4)").hide();
	//$("#stavka3").hide();
});
/*
$(function(){
	$('.izbordatuma1').datePicker()
		.bind(
			'dateSelected', function(e, selectedDate, $td){
				var datum=new Date(selectedDate);
				var selDatum = datum.getDate()+'.'+(datum.getMonth()+1)+'.'+datum.getFullYear()+'.';
				$("#search_autocomplete").val($(this).val()+' '+selDatum);
			}
		);

});*/
$(function(){
	$('.izbordatuma2').datePicker();
});


/**
 * jquery.hint.js
 */
jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
	  
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title
	  

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

/*
 * Ladybug
*/
$(function(){
	// hide register, show login
	$("#show_login").live('click', function() {
		$("#signup").hide();
		$("#recovery").hide();
		
		
		$('#login').show();
		$('#login').html($('#login').html());
		$('input:text').hint();
		
		$("#show_register").show();
		$("#show_recovery").show();
		$(this).hide();
		return false;
	});
	
	$("#show_register").live('click', function() {
		$("#login").hide();
		$("#recovery").hide();
		
		$("#signup").show();
		$('#signup').html($('#signup').html());
		$('input:text').hint();
		
		$("#show_login").show();
		$("#show_recovery").show();
		$(this).hide();
		return false;
	});
	
	$("#show_recovery").live('click', function() {
		$("#signup").hide();
		$("#login").hide();
		
		$("#recovery").show();
		$("#recovery").html($("#recovery").html());
		$('input:text').hint();
		
		$("#show_login").show();
		$("#show_register").show();
		$(this).hide();
		return false;
	});
});


$(function(){

	$("#password_mask").live('focus', function() {
		$('#password_mask').hide();
		$('#password').show();
		
		
		
		setTimeout(function() { $('#password').focus(); }, 10);
	});
	

	$("#password").live('blur', function() {
		if($('#password').val() == '') {
			$('#password_mask').show();
			$('#password').hide();
		}
	});
	
	
	$("#password").live('focus', function() {
		//alert('focus');
	});
});


/*
checkForm = function(className){
	var status = true;
	var msg = 'Unesite sva potrebna polja <br />\n<br />\n';
	$("#"+className+" .required").each(function(){
		var elem = $(this);
		//msg = msg + elem.attr('name') + elem.val() + '\n';;
		if (elem.val().length==0 || elem.val()==0 || elem.val()===elem.attr('title')) {
			status = false;
			msg = msg + elem.attr('title') + '<br />\n';
		}				
	});
	if (!status) info(msg, 2000);
	return status;
};
*/
/**
 * krpljenje za id za ie
 */
if (/msie/i.test (navigator.userAgent)){ //only override IE
	document.nativeGetElementById = document.getElementById; 
	document.getElementById = function(id){
		var elem = document.nativeGetElementById(id);
		if(elem){
			//make sure that it is a valid match on id
			if(elem.attributes['id'].value == id){
				return elem;
			}else{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++){
					if(document.all[id][i].attributes['id'].value == id){
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	}
}

