$('document').ready(function() {

	$('#prev_new, #next_new, #next_used, #prev_used').click( function() {

		var ident = $(this).attr('id').substr(5);

		var p = $('#page_'+ident).val();
		var pages = $('#pages_'+ident).val();
		var active = $('#active').val();
		var brand = $('#brand').val();

		if($(this).attr('id') == 'prev_'+ident){
			p = parseInt(p) - 1;
		}else if($(this).attr('id') == 'next_'+ident){
			p = parseInt(p) + 1;
		}

		var categoryID = 70;
		var newcar = 0;

		if(ident == 'new'){
			var categoryID = 18;
			var newcar = 1;
		}

		if(p <= pages && p >= 1){
			$.get("xmlhttp/getPage.php", { p: p, active: active, newcar: newcar, categoryID: categoryID, brand: brand }, function(data){
				$('#list_'+ident).html(data);
			});

			if(p > 1){
				$('#prev_'+ident).removeClass('inactive');
			}else{
				$('#prev_'+ident).addClass('inactive');
			}

			if(p == pages){
				$('#next_'+ident).addClass('inactive');
			}else{
				$('#next_'+ident).removeClass('inactive');
			}

			$('#page_'+ident).attr('value', p);
		}

	});
	
	
	$('.personal-city-navigator-link').click( function() {
		var destElementId = 'personal_' + $(this).attr('destinationid');
		$.scrollTo('#'+destElementId, 1500);
	});
});

function showFinans(){
	$('#finans').toggle();

	if($('#finans').attr('style') == 'display: none;'){
		$("input[@name='finans']").attr('value', 0);
	}else{
		$("input[@name='finans']").attr('value', 1);
	}

	countCars();
}

function selectMonth(nr){
	$('#months').attr('value', nr);

	$('.FinansMonth td').removeClass('on');
	$('.FinansMonth td#'+nr).addClass('on');

	countCars();
}

function selectCar ( id ) {

	$.get('xmlhttp/getCar.php', { carID: id }, function(data){
		$('#product').html(data);
		$('#product').show();

		goto_top();
	});
}

function selectWeekCar ( id, cityID) {

	$.get('xmlhttp/getWeekCar.php', { carID: id, cityID: cityID }, function(data){
		$('#product').html(data);
		$('#product').show();
		goto_top();
	});
}

function setScrollVals(min, max){
	$('#monthFrom').attr('value', min);
	$('#monthTo').attr('value', max);

	countCars();
}

function popup (url, name, width, height, status, menu, resizable) {
	var time = new Date();
	var name = Math.floor((Math.random() * Math.round(time.getTime())));

	var prop = 'toolbar=no,location=no,directories=no,scrollbars=yes,copyhistory=no,';
	prop += 'status='+ (status ? 'yes' : 'no') +',';
	prop += 'resizable='+ (resizable ? resizable : 'yes') + ',';
	prop += 'menu='+ (menu ? 'yes' : 'no') +',';
	prop += 'width='+ (width ? width : 500) +',';
	prop += 'height='+ (height ? height : 400);
	
	var win = window.open(typeof(url) == 'string' ? url : url.href, name, prop);
	win.focus();
	
	return false;
}

function nemo (namn,foretag){
	location.href= 'mailto:'+namn+'@'+foretag;
	return false;
}



var goto_top_type = -1;
var goto_top_itv = 0;

function goto_top_timer() {
	var y = goto_top_type == 1 ? document.documentElement.scrollTop : document.body.scrollTop;
	//alert(y);
	var moveby = 15;

	y -= Math.ceil(y * moveby / 100);
	if (y < 0)
		y = 0;

	if (goto_top_type == 1)
		document.documentElement.scrollTop = y;
	else
		document.body.scrollTop = y;

	if (y == 0) {
		clearInterval(goto_top_itv);
		goto_top_itv = 0;
	}
}

function goto_top() {

	if (goto_top_itv == 0) {

		if (document.documentElement && document.documentElement.scrollTop){
			goto_top_type = 1;
		}else if (document.body && document.body.scrollTop){
			goto_top_type = 2;
		}else{
			goto_top_type = 0;
		}

		if (goto_top_type > 0)
			goto_top_itv = setInterval('goto_top_timer()', 25);
	}
}

function goToAnchor(nameAnchor) {

	var offset = $('div#sellers').offset();

	document.getElementById('sellers').scrollTop = $('div#sellers').offsetTop;
}

var scroller={
	getScrollX:function(){ return(document.all)?document.body.scrollLeft:window.pageXOffset; },
	getScrollY:function(){ return(document.all)?document.body.scrollTop:window.pageYOffset; },
	getAnchorPos:function(name,c){
		var arr=document.getElementsByTagName("a"); var elm=null;
		for(a=0;a<arr.length;a++){ if(arr[a].name==name){ elm=arr[a] }}
		if(!elm) return(0); 
		var x=y=0;
		while(elm.offsetParent){ x+=elm.offsetLeft; y+=elm.offsetTop; elm=elm.offsetParent; }
		return((c=="x")?x:y);
	},
	scroll:function(x,y){
		var scrollx=this.getScrollX(); var scrolly=this.getScrollY(); 
		if(Math.abs(scrollx-x)<=1&&Math.abs(scrolly-y)<=1){
			window.scrollTo(x,y);
		} else {
			window.scrollTo(parseInt(scrollx+(x-scrollx)/2),parseInt(scrolly+(y-scrolly)/2));
			if(scrollx!=this.getScrollX()||scrolly!=this.getScrollY()){
				this.timer=setTimeout("scroller.scroll("+x+","+y+");",50);
			} else {
				window.scrollTo(x,y);
			}
		}
	},
	scrollTo:function(obj){
		var name=obj.href.substring(obj.href.indexOf("#")+1);
		this.scroll(0,this.getAnchorPos(name));
		return false;
	}
}

