// folosita pentru redirectionarea link-urilor din sortari si filtre
function redirectProduse(domain, relativePath) 
{
	window.location = domain + relativePath;
}

$(document).ready(function() {
	
	$('.expand_prod_list_detalii').click( function () {
		var id = $(this).attr('id');
		if( id == 'all' && !$(this).hasClass('activ') ) {
			$('.expand_prod_list_detalii').addClass('activ');
			//$('.detalii_produs_tabelare').hide();
			$('.detalii_produs_tabelare').css({'height':'auto'});
		} else if( id == 'all' && $(this).hasClass('activ') ) {
			$('.expand_prod_list_detalii').removeClass('activ');
			//$('.detalii_produs_tabelare').show();
			$('.detalii_produs_tabelare').css({'height':'1px'});
		} else {
			if( $('.' + id).height() == '1') {
				$('.' + id).css('height','auto');
			} else {
				$('.' + id).css('height','1px');
			}
			
			if( !$(this).hasClass('activ') ){
				$(this).addClass('activ');
			} else {
				$(this).removeClass('activ');
			}
		} 
		
		// calcul height pt elementele din SEO de sub listing
		var paramCateg = $(".paramCateg").val();
		var hgt = $(".total_hgt").height();
		
		if(paramCateg == "credite" || paramCateg == "leasing" || paramCateg == "carduri"){
			var abs_top = hgt + 100;
		} else {
			var abs_top = hgt + 260;
		}
		
		
		$(".instit_random").attr("style","top: "+abs_top+"px;");
		// end calcul
		
	});
	
	$('.sortare').mouseover(function () {
		$('.srt').addClass('activ_select');
		$('.srt').removeClass('inactiv_select');
	});
	$('.sortare').mouseout(function () {
		$('.srt').removeClass('activ_select');
		$('.srt').addClass('inactiv_select');
	});
	
	$('.sortare_institutie').mouseover(function () {
		$('.srt_int').removeClass('inactiv_select');
		$('.srt_inst').addClass('activ_select');
	});
	$('.sortare_institutie').mouseout(function () {
		$('.srt_inst').removeClass('activ_select');
		$('.srt_inst').addClass('inactiv_select');
	});
	
	/*
	 * Resetare nr item-uri pe pagina
	 * 
	 */
	$("#rowsPerPage").change(function () {
		var pathname = window.location.href;

		var newAdditionalURL = "";
		var tempArray = pathname.split("?");
		var baseURL = tempArray[0];
		var aditionalURL = tempArray[1]; 

		var temp = "";
		if(aditionalURL) {
			var tempArray = aditionalURL.split("&");
			for ( var i in tempArray ){
		        if(tempArray[i].indexOf("rowsPerPage") == -1){
		        	newAdditionalURL += temp + tempArray[i];
		            temp = "&";
		        }
			}
		}
		var finalURL =  "?" + newAdditionalURL + "&rowsPerPage=" + $(this).val();
		window.location = finalURL;
	});
	
	$("li.rowsPerPage").hover( function () {
		$(".colmiddle_560").css('position', 'relative');
		$(".colmiddle_560").append("<div id='rowsPerPageTip' style='position:absolute; width: 225px; top:0px; left:350px; background-color: rgb(250, 250, 227); font-size: 11px; display: block; border: 1px solid black; padding: 2px; text-align:center;'>" +
					"Selecteaza numarul de produse in pagina." +
				"</div>");
	  },
	  function (){
		$("#rowsPerPageTip").remove();
	});	
	/*
	 *	END rowPerPage tooltip 
	 */
	
	$(".ptypes").hide();
	var ptypeid = $.getURLParam("ptype");
	
	
	if(ptypeid  == null) {
		$(".ptypes:first").show();
	} else {
		$("#ptypeid-" + ptypeid ).show();
	}

	$("#select_type").change(function() {
		var ptypeid = $(this).val();
		$(".ptypes").hide();
		$("#ptypeid-" + ptypeid).show();
	});
	
	$("#ptype").change(function() {
		if ($(this).val().indexOf("asigurari")!=-1) {
			$("#product_filter").attr("action", 'http://asigurari.efin.ro/' + $(this).val());	
			$("#product_filter").submit();
		} else {	
			window.location = $(this).val();
		}
		//window.location = "/produse?ptype=" + $(this).val();
	});
   //add_compare(0, $("#ptype").val());
	
	/**************************
	 * Tooltip listing tabelar
	 **************************/
	$('.listing td').hover(
			function () {
				if ($(this).attr('title')) {
					$(".tooltip").remove();
					
					titleVar = this.title;
					$(this).attr("temp", titleVar);
					$(this).attr("title", '');
					tdPos = $(this).offset();
					var text = "<p>" + titleVar + "</p>";

					if ($(this).hasClass("dae")) 
						focusId = "perioada_maxima";
					else
						focusId = "suma_maxima";
					
					var linkInput = "<p>Pentru alte valori modifica la <a onClick=\"focusOn('"+focusId+"');return false;\" href=\"#product_filter\">FILTRE PRODUS</a>.</p>";
					$("body").append("<div id='tooltip' class='tooltip'>" + text + linkInput + "</div>")
					$("#tooltip").css({"left": tdPos.left - 230, "top": tdPos.top + 50});
				}
			},
			function() { 
				if($(this).attr("temp") != ''){
					$(this).attr('title', $(this).attr("temp"));
					$(this).attr('temp', '');
				}
			}
		);

	$('.listing th').hover(
			function () {
				if($(this).attr('title')) {
					$(".tooltip").remove();
					
					titleVar = this.title;
					$(this).attr("temp", titleVar);
					$(this).attr("title", '');
					tdPos = $(this).offset();
					var text = "<p>" + titleVar + "</p>";
					$("body").append("<div id='tooltip_th' class='tooltip'>" + text + "</div>")
					$("#tooltip_th").css({"left": tdPos.left - 230, "top": tdPos.top - 45});
				}
			},
			function() {
				if ($(this).attr("temp") != '') {
					$(this).attr('title', $(this).attr("temp"));
					$(this).attr('temp', '');
				}
			}
		);
	$("body").click( function () { 
		if ($(".tooltip").is(':visible')) {
			$(".tooltip").remove();
		}
	});
	/*****************************
	 * END Tooltip listing tabelar
	 *****************************/
	
	$(".info_categorie").click( function () {
		html = "<div id='info_categorie'>" + $("#info_categorie").html() + "</div>";
		if($("div#info_categorie:visible").size() > 0) {
			$("#info_categorie").slideUp();
		} else {
			$("#info_categorie").remove();
			$(".col_listing").prepend(html);
			$("#info_categorie").slideDown();
		}
	});
});

function recalcSumaMaxima(valoare, avans) {
	if( (/^\d+$/.test(valoare) && valoare > 0 && valoare < 9999999999) && 
			(/^\d+$/.test(avans) && avans > 0 && avans < 95) && valoare != '' ) {
		$("#suma_maxima").val(valoare * (100 - avans)/100);
	} else if(avans ==0 ) {
		$("#suma_maxima").val(valoare);
	} else {
		$("#suma_maxima").val("");
	}
}

function add_compare(product_id, ptype_slug)
{
	if (typeof(ptype_slug) != "undefined")
	{
		arr_slug = ptype_slug.split(/\//);
		ptype_slug = arr_slug[1];
	}
	
	data = "product_id/"+product_id;
	if (typeof(ptype_slug) != "undefined" && ptype_slug!='') { data += '/ptype_slug/'+ptype_slug; }
	
	var time = new Date();
	data += '/t/'+time.getTime();

	//alert($('#compara_'+product_id + ':checked').length);
	if ( $('#compara_'+product_id + ':checked').length > 0) {
		data += '/act/add';
	} else {
		data += '/act/del';
	}
	
	// verific sa nu depasesc 3 produse pt comparatie
	if( $('.produs_comparat').length == 3 && data.search('/act/add') != -1 ){
		alert("Puteti alege maxim 3 produse pt comparatie!"); 
		return false;
	}
	
	$.ajax({
		   type: "GET",
		   url: "/produse/ajaxCompara/"+data,
		   dataType: "html",
		   success: function(html){
				$("#fixmetoo").remove();
 			    $("body").append(html);
			}
	});
}

function uncheckCheckbox(id){
	$("#compara_" + id).parent().css( {'font-weight':'normal', 'color':'#BAB9BA'} );
	$("#compara_" + id).attr("checked", false);
}

function desproyQtip() {
	$(document).ready(function(suma, curr, per) {
		val = "";
		$('.listing td.dae').qtip('disabled');
	});
}

function getQtip() {
	
	var suma = $("#d_suma").attr('value');
	var curr = $("#d_curr").attr('value');
	var per  = $("#d_per").attr('value');
	
	val = '<a href="#" onclick=' + focus("#suma_maxima") + ' title="Modifica filtru" style="color: #FFB021; font-weight: bold;">'+ suma + '&nbsp;' + curr + '</a> pe <a href="#" onclick='+focus("#perioada_maxima")+' title="Modifica filtru" style="color: #FFB021; font-weight: bold;">' + per + ' ani</a>';
	dbd = "Pentru mai multe detalii vezi explicatia in <a href='/credite/glosar_economic_D/dobanda.html' style='color: #FFB021; font-weight: bold;' target='_blank'>glosar</a>.";
	thDae = "Pentru mai multe detalii vezi explicatia in <a href='/credite/glosar_economic_D/dobanda_anuala_efectiva_dae.html' style='color: #FFB021; font-weight: bold;' target='_blank'>glosar</a>.";
	
	$(document).ready(function() {

		$('.listing th.dbd, th.thDae').each(function(){
			$(this).qtip({
			   content: $(this).hasClass('dbd') ? dbd : thDae,
			   hide: {
		           fixed: true // Make it fixed so it can be hovered over
		       },
			   style: {
			        padding: '5px 15px', // Give it some extra padding
			        name: 'dark'
			   },
		       position: {
		 	      corner: {
		 	         target: 'topLeft',
		 	         tooltip: 'bottomLeft'
		 	      }
		 	   }
			});
		})

		$('.listing td.dae, .listing td.rata_lunara').qtip({
		   content: 'Calculat pentru '+ val,
		   hide: {
	           fixed: true // Make it fixed so it can be hovered over
	       },
		   style: {
		        padding: '5px 15px', // Give it some extra padding
		        name: 'dark'
		   },
	       position: {
	 	      corner: {
	 	         target: 'bottomLeft',
	 	         tooltip: 'bottomLeft'
	 	      }
	 	   }
		});

	});
}

