Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

/* define a loading div to see it when ajax is running*/
var loaderdiv = $('<div>').attr({'id':'loading'}).css({display:'none'});
loaderdiv.appendTo($('body'));

 $(document).ajaxStart(function(){ 
 	if($("#loading").html().length == 0 ) {
 		$("#loading").css({width: $(window).width() + 'px', height: $(window).height() + 'px', top:0, left:0, position:'absolute', background: 'none repeat scroll 0 0 #000000', opacity:'0.5'});
 	 	$("#loading").html('<div style="left: 50%; line-height: 1px; position: absolute; top: 50%; margin-top: -16px; margin-left: -16px;"><img src="/images/colorbox/images/loading.gif" width="32" height="32" alt=""/></div>');
 	}
   $("#loading").show(); 
 }).ajaxStop(function(){ 
   $("#loading").hide(); 
 });
 

function auto_delete(make_id, model_id){
	var service_url = cfg.root_www+'/autojson?table=mod_auto_makemodeljx&limit=999&callback=?';
	
	$.getJSON(service_url, function(data){auto_get_makes_callback(data, make_id, model_id)});
}

function featured_listing(id) {
	var service_url = cfg.root_www+'/autojson?op=featured&id=' + id + '&callback=?';

	$.getJSON(service_url, function(data){
		if(data.successfull) {
			$('#record_' + id + ' a.featured_listing').html(data.featured=='y'?auto_lang['Yes']:auto_lang['No']);
		}else {
			alert(data.error);
		}
	});

}

function urgent_listing(id) {
	var service_url = cfg.root_www+'/autojson?op=urgent&id=' + id + '&callback=?';

	$.getJSON(service_url, function(data){
		if(data.successfull) {
			$('#record_' + id + ' a.urgent_listing').html(data.urgent=='y'?auto_lang['Yes']:auto_lang['No']);
		}else {
			alert(data.error);
		}
	});

}

function delete_listing(id) {
	var service_url = cfg.root_www+'/autojson?op=delete_listing&id=' + id + '&callback=?';
	if(confirm(auto_lang['are_you_sure_you_want_to_delete_this'])) {
		$.getJSON(service_url, function(data){
				if(data.successfull) {
					$('#record_' + id).hide('slow');
				}else {
					alert(data.error);
				}
		});
	}
}

function auto_get_makes(make_id, model_id){
	var service_url = cfg.root_www+'/autojson?table=mod_auto_makemodeljx&limit=999&callback=?';
	
	$.getJSON(service_url, function(data){auto_get_makes_callback(data, make_id, model_id)});
}

function auto_get_makes_callback(data, make_id, model_id){
	var make_id_select = document.getElementById('make_id');
	var has_rss_make_id_select = false;
	if(document.getElementById('rss_make_id') != undefined) {
		has_rss_make_id_select = true;
	}
	
	if(typeof(make_id)!='number'){
		make_id = 0;
	}
	
	if(typeof(data.items)=='object'){
		for(var i in data.items){
			data.items[i].id = parseInt(data.items[i].id);
			var temp_option = document.createElement('option');
			temp_option.value = data.items[i].id;
			temp_option.innerHTML = data.items[i].cname;
			
			if(make_id==data.items[i].id){
				temp_option.selected = true;
			}
			
			make_id_select.appendChild(temp_option);
			
			if(has_rss_make_id_select) {
				var temp_option2 = document.createElement('option');
				temp_option2.value = data.items[i].id;
				temp_option2.innerHTML = data.items[i].cname;
				document.getElementById('rss_make_id').appendChild(temp_option2);
			}
		}
	}
	
	if(make_id){
		auto_get_models(model_id);
	}
}

function auto_get_models(model_id){
	var make_id_select = document.getElementById('make_id');
	var make_id = $(make_id_select).val();
	if(make_id>0){
		var service_url = cfg.root_www+'/autojson?table=mod_auto_makemodeljx&limit=999&id_parent='+make_id+'&callback=?';
		$('#model_id').attr("disabled", true);

		$.getJSON(service_url, function(data){auto_get_models_callback(data, model_id)});
	}
	else{
		auto_get_models_callback(false);
	}
}

function auto_get_models_callback(data, model_id){
	var models_id_select = document.getElementById('model_id');
	models_id_select.innerHTML = '';
	$('#model_id').attr("disabled", false);
	if(typeof(model_id)!='number'){
		model_id = 0;
	}
	
	var temp_option = document.createElement('option');
	temp_option.value = 0;
	temp_option.innerHTML = lang_auto_search.any;
	
	models_id_select.appendChild(temp_option);
		
	if(typeof(data.items)=='object'){
		for(var i in data.items){
			var temp_option = document.createElement('option');
			temp_option.value = data.items[i].id;
			temp_option.innerHTML = data.items[i].cname;
			
			if(model_id==data.items[i].id){
				temp_option.selected = true;
			}
			
			models_id_select.appendChild(temp_option);
		}
	}
}

function set_url_param($param, $value, $link, $unset){
	if (!$link){
		$link = window.location.href;
	}
	
	var patern = new RegExp("([\&\?])"+$param+"=[^\&]*(\&|$)");
	$link = $link.replace(patern, "$1");
	$last_char = $link.substr(-1);
	
	if (!$unset){
		if ($last_char!='&' && $last_char!='?'){
			if ($link.indexOf('?')!=-1){
				$link += '&';
			}
			else {
				$link += '?';
			}
		}
		$link += $param+"="+encodeURIComponent($value);
	}
	
	return $link;
}

function setCookie(name,value,expires,path,domain,secure) {
	if(!expires){
		expires = 315360000000; // 10 years
	}
	
	var today = new Date();
	var expires_date = new Date( today.getTime() + (expires) );
	
    var cookieString = name + "=" +escape(value) + 
       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : "");
    document.cookie = cookieString;
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function add_car2favorites(car_id){
	var fav_cars = getCookie('fav_cars');
	var fav_cars_arr = new Array();
	if(fav_cars.length > 0) {
		fav_cars_arr = fav_cars.split('|');
	}
	
	if(!fav_cars_arr.in_array(car_id)){
		fav_cars_arr.push(car_id);
		fav_cars = fav_cars_arr.join('|');
		
		setCookie('fav_cars',fav_cars,null,'/');
		alert(auto_lang['car_added_to_favorites']);
		window.location.reload();
	}
	else{
		alert(auto_lang['car_already_added_to_favorites']);
	}
}

function remove_car_from_favorites(car_id){
	var fav_cars = getCookie('fav_cars');
	fav_cars = fav_cars.split('|');
	
	if(fav_cars.in_array(car_id)){
		var new_fav_cars = Array();
		for(var i in fav_cars){
			if(fav_cars[i]!=car_id && typeof(fav_cars[i])!='function'){
				new_fav_cars.push(fav_cars[i]);
			}
		}
		
		fav_cars = new_fav_cars.join('|');
		
		setCookie('fav_cars',fav_cars,null,'/');
	}
	
	window.location.href = window.location.href;
	return false;
}

function get_favorites_count() {
	var fav_cars = getCookie('fav_cars');
	if('' == fav_cars) {
		return 0;
	}else {
		fav_cars = fav_cars.split('|');
		return fav_cars.length
	}
}

var def_sort_order = {mileage:'ASC',price:'ASC',year:'DESC'};
function set_sort_order(order_criteria){
/*	var old_criteria = getCookie('car_sort_field');
	
	if(old_criteria==order_criteria){
		order_direction = def_sort_order[order_criteria]=='ASC'?'DESC':'ASC';
	}
	else{
		order_direction = def_sort_order[order_criteria];
	}*/
	
	var order_direction = $('input[name=sort_order]:checked').val();
	
	setCookie('car_sort_field',order_criteria);
	setCookie('car_sort_order',order_direction);
}
