var slider_page_search=1;
var slider_total_search=0;
var prev_select_search=null;
var step = 0;
$(document).ready(function() {
	//slider_total_search = Math.ceil($('#custom_filters_holder div.refine-label').size());
	slider_total_search = Math.ceil($('#custom_filters_holder ul.refine-filters').size());
	slider_page_search=slider_total_search;

	$("#btn-next").click(function() {
		if ((slider_page_search-6) <0) return;
		slider_page_search -= 6;
		step++;
		var p = step*986*(-1);
		$('#custom_filters_holder').animate({'margin-left':p+'px'}, 800);
	});

	$("#btn-prev").click(function() {
		if ((slider_page_search+6) > slider_total_search) return;
		slider_page_search += 6;
		step--;
		var p = step*986*(-1);//alert(p);
		$('#custom_filters_holder').animate({'margin-left':p+'px'}, 800);
	});

	imagePreview();
});

function check_uncheck(the) {
	for (var i=0;i<the.elements.length;i++) {
		var e=the.elements[i];
		if (e.name!='check_all' && e.name!='dummy' && e.type=='checkbox') {
			e.checked=the.check_all.checked;
		}
	}
}


function allow_contact(the) {
	are_checked=false;//alert(the.elements.length);
	for (var i=0;i<the.elements.length;i++) {
		var e=the.elements[i];
		if (e.name!='check_all' && e.name!='dummy' && e.name!='dummy2' && e.type=='checkbox' && e.checked==true) {
			are_checked=true;
		}
	}
	if (are_checked) {
		return true;
	} else {
		alert('Bifati cel putin unul din rezultate pentru a contacta');
		return false;
	}
}

function get_ids(the,what)
{
	str='';
	for (var i=0;i<the.elements.length;i++)
	{
		var e=the.elements[i];
		if (e.name!='check_all' && e.name!='dummy' && e.name!='dummy2' && e.type=='checkbox' && e.checked==true)
		{
			if(e.name.indexOf(what)===0)
			{
				str+=e.name.replace(/[^0-9]/g,'')+',';
			}
		}
	}
	//alert(str);
	return str;
}

function get_sale_ids(the)
{
	return get_ids(the,'sales');
}
function get_company_ids(the)
{
	return get_ids(the,'comps');
}
function get_buys_ids(the)
{
	return get_ids(the,'buys');
}


function doRafinareJudet(value, text) {
	$('#state').val(value);
	$('#state_text').val(text);
	doRafinare(document.getElementById('form-rafinare'));//'form-rafinare');
}
function doRafinareDomeniu(value, text) {
	$('#domenii').val(value);
	$('#domenii_text').val(text);
	doRafinare(document.getElementById('form-rafinare'));//'form-rafinare');
}
function doRafinare(theForm)
{

	var targetURL='http://'+location.hostname;

	if(theForm.magic.value==1) targetURL+='/magic';

	var realm=theForm.realm.value;
	var state=theForm.state.value;//options[theForm.state.selectedIndex];
	if(theForm.domenii) var domeniu=theForm.domenii.value;
	if(theForm.browse.value=='1')
	{
		switch(realm)
		{
			case '1': targetURL+='/catalog-produse/';break;
			case '2': targetURL+='/catalog-cumparare/';break;
			case '3': targetURL+='/catalog-firme/';break;
		}
	}
	else
	{
		switch(realm)
		{
			case '1': targetURL+='/produse/';break;
			case '2': targetURL+='/cumpar/';break;
			case '3': targetURL+='/firme/';break;
		}
	}
	if(theForm.keywords.value!='') targetURL+=escape(theForm.keywords.value.replace(/ /g,'-'))+'/';
	else if(theForm.magic.value==1) targetURL+='-/'



	if(theForm.ind_name.value!='') targetURL+=theForm.ind_name.value+'-cat-'+theForm.ind_id.value+'/';
	if(state!=0) targetURL+=theForm.state_text.value+'-judet-'+state+'/';
	if(theForm.domenii) {if(domeniu!=0) targetURL+=escape(theForm.domenii_text.value.replace(/ /g,'+'))+'-domeniu-'+domeniu+'/';}
	else if(theForm.domeniu.value!='') targetURL+=escape(theForm.domenii_text.value.replace(/ /g,'+'))+'-domeniu='+domeniu+'/';
	if(theForm.litera.value!='') targetURL+='litera-'+theForm.litera.value+'/';
	targetURL+='start-0/10/';


	//get the custom fields/prices part
	var pieces=document.location.href.split(/start\-[0-9]+\/[0-9]+\//g);
	if(pieces[1]) targetURL+=pieces[1];
//alert(targetURL);
	document.location=targetURL;


}

function toggleCustomField(el)
{
	var currentClass = $(el).attr("class");
	var ul = el.parentNode.parentNode;
	//for(i=0;i<ul.childNodes.length;i++)
	//alert(ul.childNodes[i])
	if (currentClass == 'link-more') {
		el.childNodes[0].innerHTML = 'Mai putine';
		$(ul).find('.hidden-li').slideDown(200);
		$(el).removeClass('link-more').addClass('link-less');
	} else {
		el.childNodes[0].innerHTML = 'Mai multe';
		$(ul).find('.hidden-li').slideUp(200);
		$(el).removeClass('link-less').addClass('link-more');
	}


	/*
	if(mustExpand)
	{
		//document.getElementById('custom_filters_ul').style.position='static';
		//document.getElementById('custom_filters_placeholder').style.position='static';

		el.childNodes[1].innerHTML='MAI PUTINE';
		el.parentNode.parentNode.nextSibling.style.display='';

		document.getElementById('custom_filters_ul').style.height=(document.getElementById('custom_filters_ul').offsetHeight + el.parentNode.parentNode.nextSibling.offsetHeight + 20)+ 'px';
		document.getElementById('custom_filters_placeholder').style.height=(document.getElementById('custom_filters_placeholder').offsetHeight + el.parentNode.parentNode.nextSibling.offsetHeight + 20)+ 'px';
		//document.getElementById('custom_filters_ul').style.overflowY='visible';
		//document.getElementById('custom_filters_placeholder').style.overflowY='visible';

	}
	else
	{
		document.getElementById('custom_filters_ul').style.height=(document.getElementById('custom_filters_ul').offsetHeight - el.parentNode.parentNode.nextSibling.offsetHeight - 20)+ 'px';
		document.getElementById('custom_filters_placeholder').style.height=(document.getElementById('custom_filters_placeholder').offsetHeight - el.parentNode.parentNode.nextSibling.offsetHeight - 20)+ 'px';

		el.childNodes[1].innerHTML='MAI MULTE';
		el.parentNode.parentNode.nextSibling.style.display='none';
	}
*/
}
var globalTplUrl='';

function sendIndividualMessage(id) {
	$('#contact-form input:checkbox').attr('checked', false);
	$('#'+id).attr('checked', true);
	$('#contact-form').submit();

}


this.imagePreview = function(){
	/* CONFIG */

		xOffset = 10;
		yOffset = 30;

		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result

	/* END CONFIG */
	$("a.img_preview").hover(function(e){
		this.t = this.title;
		var v = this.title;
		if (v != "" && v.length > 50) this.t = v.substr(0, 50)+"...";
		this.title = "";
		if (this.t != undefined) {
			var c = (this.t != "") ? "<div style='clear:both;margin: 5px 0 5px 5px; padding: 5px 0 0 0;'>" + this.t + "</div>" : "";
		} else {
			var c = "";
		}
		this.id = "current_img_preview";
		var src = $("#current_img_preview > img").attr('src');//"http://www.bizoo.ro/test/1.jpg";
		//alert(src);
		$("body").append("<div id='img_preview'><img src='"+ src +"' alt='"+this.t+"' />"+c+"</div>");
		$("#img_preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("normal");
		this.id = "";
    },
	function(){
		this.title = this.t;
		$("#img_preview").remove();
    });
	$("a.img_preview").mousemove(function(e){
		$("#img_preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};

