$(document).ready(function(){
	$.fn.clearForm = function() {
	  return this.each(function() {
	 var type = this.type, tag = this.tagName.toLowerCase();
	 if (tag == 'form')
	   return $(':input',this).clearForm();
	 if (type == 'text' || type == 'password' || tag == 'textarea')
	   this.value = '';
	 else if (type == 'checkbox' || type == 'radio')
	   this.checked = false;
	 else if (tag == 'select')
	   this.selectedIndex = -1;
	  });
	};
	
	
	
	$('a[rel="external"]').each(function(){
		if($(this).attr('href') != '' && $(this).attr('href') != '#'){
			$(this).attr('target','_blank');
		}
	});
	
	//helper meniu
	$("ul.menu li").hover(
		function () {
			$(this).addClass('menu-hover');
			$(this).children('a').addClass('menu-hover-a');
		}, 
		function () {
			$(this).removeClass('menu-hover');
			$(this).children('a').removeClass('menu-hover-a');
		}
	);
	
	//helper table
	$("table#browse tr").hover(
		function () {
			$(this).children('td').css({'cursor':'pointer'});
			$(this).children('td').addClass('td-hover');
		}, 
		function () {
			$(this).children('td').removeClass('td-hover');
		}
	);
	$("table#browse tr").each(function(el){
		var $this = $(this);
		$this.children('td.click').click(function(e){
			var href = $this.attr('title');
			window.location = href;
		});
	});
	
	$('#check_all').click(function(el){
		$('table#browse tr input').each(function(e){
			if($(this).is(":checked")){
				$(this).attr('checked',false);
			}else{
				$(this).attr('checked',true);
			}
		});
	});
	
	//boxes
	var box_long = $('#models_box').height();
	var mini_box = $('#stats_box').height();
	var box_main_left = $('#box_main_left');
	var box_main_right = $('#box_main_right');
	
	if(box_long > mini_box){
		$('#stats_box').css({'height':box_long});
	}else if(mini_box > box_long){
		$('#models_box').css({'height':mini_box+2});
	}
	
	if(box_main_left.innerHeight() >= box_main_right.innerHeight()){
		$('#box_main_right').css({'height':box_main_left.innerHeight()});
	}else{
		box_main_left.css({'height':box_main_right.innerHeight()});
	}
	
	//send to compare
	$('#click_compare').click(function(e){
		$.ajax({
			url: APP_PATH+'anunt/add_compare',
			type: 'post',
			data: $('#browse-form').serialize()+'&is_ajax=1', 
			dataType: 'json',
			success: function(data){
		    	if(data.type == 'success'){
		    		window.location.href = data.redirect;
		    	}else if(data.type == 'error'){
		    		alert(data.msg);
		    	}
		    		
		    }
		});
		e.preventDefault();
	});
	
	//height footer
});


function alert_msg(loc){
	$ok = confirm('Esti sigur ca vrei sa stergi anuntul?');
	if ($ok){
		return true;
	}else{
		return false;
	}
}
