/* Tabber */

function showTab(ref,set){
	$('#'+set+' > LI > A').each(function(i,a){
		$(a).removeClass('active');
		$($(a).attr('href')).addClass('hidden');
	});
	$(ref).addClass('active');
	$(ref).addClass('active');
	$($(ref).attr('href')).removeClass('hidden');
	return false;
}

$(document).ready(function(){

	$("#currency-switch-link").click(function(){

		$("#currency-switch").toggleClass("hide-drop");

	});

	_sort()
});

function _sort(){

	var href = window.location.href;
	var sorted = false;

	var re = new RegExp('[&|?]sort=*([a-z]*)([A|D]+)');
	var m = re.exec(href);
	if (m != null) {
		/*var s = "Match at position " + m.index + ":\n";
		for (i = 0; i < m.length; i++) {
			s = s + i+" "+ m[i] + "\n";
		}
		alert(s);
*/
		sorted = m[1];
		var order = m[2];
	}

	href = href.replace(/[&|?]page=*\d*/gi, '');//start from 1st page
	//href = href.replace(/[&|?]sort=*\[a-Z]*/gi, '');
	//alert("href"+href);

	var s = $("#sort_th > th > a");
	for (var n = 0; n < s.length; n++){

		if(sorted && s[n].id==sorted){
			if(order=="A"){
				var re = new RegExp("sort="+sorted+order);
				href2 = href.replace(re, "sort="+sorted+"D");
				s[n].href = href2;
				s[n].className = "down";
			}
			else {
				//alert("sorted "+sorted + " A");
				var re = new RegExp("sort="+sorted+order);
				href2 = href.replace(re, "sort="+sorted+"A");
				s[n].href = href2;
				s[n].className = "up";
			}
		}else if (sorted){
			var order2 = (n==0) ? "D" : "A";//kui esimene e. vaikimisi sorteering
			
			href2 = href.replace(/[&|?]sort=*([a-z]*)([A|D]+)/gi, '');
			var arr = href2.split('?');
			var symbol = (arr.length>1) ?'&' :'?';			
			href2 = href2 + symbol + "sort="+ s[n].id + order2;
			s[n].href = href2;
			s[n].className = "";
		}else {
			var arr1 = href.split('best.php');//if best.php
			var order2 = (n==0) ? "D" : "A";//kui esimene e. vaikimisi sorteering			
			if (n==0 && arr1.length<2) s[n].className = "down";		
			var arr = href.split('?');
			var symbol = (arr.length>1) ?'&' :'?';			
			href2 = href + symbol + "sort="+ s[n].id + order2;
			s[n].href = href2;
		}



		//[&|?]sort=*([a-z]*)([A|D]+)
	}

}

function changePicture(s,t){
	try{
		var i=document.images['car_picture'];
		i.src=s;
		i.title=t;
		i.alt=t;

		return false;
	}catch(e){}
}

function _change_currency(obj){

	var selected_currency = obj.value;
	var s = $("#price_section > span");

	var patt=/([price_|price_old_])(\d)/m;

	for (var i = 0; i < s.length; i++){
		//alert( "span innerHtml "+s[i].innerHTML);
		var test_id = s[i].id;
		if (test_id) {
		var result=patt.exec(test_id);

		if(result && result[2] == selected_currency){
			$("#"+test_id).show();
		}
		else{
			$("#"+test_id).hide();
		}
		            }
	}
}

function _location(obj){
	var $location = obj.value;

	$body_types = $("#body_type_icons > li > a");

	try{
		$("#location").val($location);
		$.get("session.php", { location: $location } );

		for (var i = 0; i < $body_types.length; i++){
			$body_types[i].href = $body_types[i].href + "&location="+$location
		}

		return false;
	}catch(e){}

}

function _adv_search($action){
	var quik_search_form = document.getElementById('quik_search');
	quik_search_form.action = $action;
	quik_search_form.submit();
}

// check required field on form
// form.elements.NAME
// if exist hiden "form.elements.NAME_req",
// then on empty element alert uses form.elements.NAME_req.value
function checkRequired(myform)
{
	var req = false;
	var required = 'required:\n';
	var chek;
	var reqvalue;

	for (var i = 0; i<myform.elements.length; i++) {
		chek = myform.elements[i].name+'_req';
		if (document.getElementById(chek)){
			if (  Trim(myform.elements[i].value) < '1' ){
				req = true;
				required += document.getElementById(chek).value+'\n';
			}
		}
	}

	if(req){
		alert(required);
		return false;
	}


	return true;
}


function Trim(STRING){
	STRING = LTrim(STRING);
	return RTrim(STRING);
}


function RTrim(STRING){
	while(STRING.charAt((STRING.length -1))==" "){
		STRING = STRING.substring(0,STRING.length-1);
	}
	return STRING;
}


function LTrim(STRING){
	while(STRING.charAt(0)==" "){
		STRING = STRING.replace(STRING.charAt(0),"");
	}
	return STRING;
}

function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;


	for (i = 0; i < sText.length && IsNumber == true; i++)
	{
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			IsNumber = false;
		}
	}
	return IsNumber;

}


function _update_mark_options(obj){

	var $type = obj.value;
	var list = document.getElementById('mark');
	var $mark_selected = $("#mark").val();

	$.getJSON("system.php", { op: "mark_options", type: $type },
	function(data){

		list.options.length = 0; //clear select options
		$.each(data, function(i,dat){
			createOpt(dat.value,dat.text,list)
		});

		$("#mark").val($mark_selected);
	}
	);

}

function _reload(obj){
	var href = window.location.href;

	href = href.replace(/[&|?]page=*\d*/gi, '');
	//alert("href"+href);

	var arr = href.split('?');
	var symbol = (arr.length>1) ?'&' :'?';
	window.location.href = href + symbol + obj.name +"="+ obj.value;
}


function createOpt(strVal,strText,objTargetSelect) {
	var opt = document.createElement('OPTION');
	opt.setAttribute('value',strVal);
	var text = document.createTextNode(strText);
	opt.appendChild(text);
	objTargetSelect.appendChild(opt);
}

var best_of_position = 0;
var best_of_interval = 5
var best_of_width = 645;
/*
#front-car DIV.back A { background-position: 0 0; }
#front-car DIV.back A:hover { background-position: -23px 0; }
#front-car DIV.next A { background-position: -82px 0; }
#front-car DIV.next A:hover { background-position: -59px 0; }
*/

function _best_of_back(){

	var scroll = 0;

	var next_mov = best_of_position + best_of_width;
	if( next_mov > 0 ){
		scroll = parseInt(best_of_position);
	}else{
		var scroll = best_of_width;
	}
	$("#best_of").animate({"left": "+="+scroll+"px"}, 800);

	return false;
	$("#best_of > li").hide();

	best_of_position = best_of_position - best_of_interval;


	$("#best_of > li").each(function (index, domEle) {
		if(index>=best_of_position & index<=(best_of_position+best_of_interval) )
		$(domEle).show();

		//if ($(this).is("#stop")) {
		//	$("span").text("Stopped at div index #" + index);
		//	return false;

	});
	return false;
}

function _best_of_next(){

	$("#best_of").animate({"left": "-=645px"}, 800);
	//$("#best_of > li").hide();
	/*
	best_of_position = best_of_position + best_of_interval;


	$("#best_of > li").each(function (index, domEle) {
	if(index>=best_of_position & index<=(best_of_position+best_of_interval) )
	$(domEle).show('slow');

	//if ($(this).is("#stop")) {
	//	$("span").text("Stopped at div index #" + index);
	//	return false;

	});*/
	return false;
}


/*
var TINY = {};


TINY.setwidth = {

init : function(e) {
var m = e.getElementsByTagName("ul"), i = 0, w = 0;
this.l = m.length;

//alert(this.l);

for (i; i < this.l; i++) {
w += parseInt(m[i].offsetWidth) + 1;
//alert(m[i].offsetWidth+" kokku: "+w);
}

e.style.width = w + 'px';
}
};

TINY.scroll = function() {
return {
init : function(e, d, s) {
var p = e.style.left || TINY.style.val(e, 'left');
e.style.left = p;
var l = d == 1 ? parseInt(e.offsetWidth) - parseInt(e.parentNode.offsetWidth) : 0;
e.si = setInterval(function() {
TINY.scroll.mv(e, l, d, s)
}, 20)
},
mv : function(e, l, d, s) {
var c = parseInt(e.style.left);
if (c == l) {
TINY.scroll.cl(e)
} else {
var i = Math.abs(l + c);
i = i < s ? i : s;
var n = c - i * d;
e.style.left = n + 'px'
}
},
cl : function(e) {
//e = typeof e == 'object' ? e : $(e);
clearInterval(e.si)
}
}
}();

TINY.style = function() {
return {
val : function(e, p) {
//e = typeof e == 'object' ? e : $(e);
return e.currentStyle ? e.currentStyle[p] : document.defaultView.getComputedStyle(e, null).getPropertyValue(p)
}
}
}();


$(document).ready(function() {

var $location = document.getElementById("front-car-cont");

TINY.setwidth.init( $location );

$("#scroll_left").hover(function () {
TINY.scroll.init( $location ,-1,2);
},function () {
TINY.scroll.cl($location);
}
);

$("#scroll_right").hover(function () {
TINY.scroll.init( $location ,1,2);
},function () {
TINY.scroll.cl($location);
}
);
});
*/
