
var ie6;
var searchval;

$(document).ready(function(){

searchval = $('#search-term').attr("value"); 

/* Muestra un valor por defecto en el campo de busqueda */
if ( searchval == "") {
      $('#search-term').addClass("value"); 
      $('#search-term').click(function () { 
            $(this).removeClass("value");
      })
}

$('#search-term').click(function () { 
if ( searchval == "") {  $(this).removeClass("value");}
})

$('#search-term').blur(function(){
searchval = $('#search-term').attr("value");
if ( searchval == "") { $('#search-term').addClass("value"); }
})


/* Esconde mensajes */
//$('#msgRef').hide();
$('p.alert, p.success').not('#msgRef').fadeTo(5000,1).slideUp("slow"); 

      
/* IE6 only */       
      if (ie6 == true) {
            /* PNGFix */   
            DD_belatedPNG.fix('.png, #main-content .intro, #content h2, #welcome, #pagination span, #pagination a, .box a, #contact h3, #direccion, #marco, #detalle h2 strong, #top a'); /* Add class="png" to the png images in the HTML*/
            $('#homepage .browser-support').fadeTo(9000,1).slideUp("slow"); 
            $('#homepage #wrapper').animate( { marginTop:"30" }, 9000 ).animate( { marginTop:"0" }, "slow" ); 
      }


$('#slideshow').after('<div id="marco" style="background:url('+OneCommon.url+'assets/images/style/detalle-marco.png) no-repeat 0 0; width:358px; height:309px; position:absolute; top:0; left:0; z-index:500;"></div>')


$('#slideshow').after('<ul id="photosnav" class="jcarousel-skin-tango">').cycle({ 
    fx:     'fade', 
    speed:  'fast', 
    timeout: 0, 
    pager:  '#photosnav', 
     
    // callback fn that creates a thumbnail to use as pager anchor 
    pagerAnchorBuilder: function(idx, slide) { 
        return '<li><a href="#"><img src="' + slide.src + '" width="64" height="64" /></a></li>'; 
    } 
});
                 

$('#photosnav').jcarousel();             
      $('.jcarousel-prev-horizontal').html('<div class="hbp"></div>'); 
      $('.jcarousel-next-horizontal').html('<div class="hbn"></div>');  

if(typeof($('#lnkEnviarAmigo').attr('id')) != 'undefined') OneCommon.openSendToFriend();

//eventos para las acciones
	$('#fichapdf a').bind('mousedown', function(){
		$(this).children().eq(0).fadeOut();
	}).bind('mouseleave', function(){$(this).children().eq(0).fadeIn();});
	
	$('#enviar-amigo a').bind('mousedown', function(){
		$(this).children().eq(0).fadeOut();
	}).bind('mouseleave', function(){$(this).children().eq(0).fadeIn();});

}); 

$('#photosnav:last-child').css({border:"1px solid red"});


var OneCommon = {
	url: 'http://www.trobalia.com/',
	
	doSearch: function(current){
		var objForm = $('#frmSearch');
		
		var intGestionTipo = parseInt($('select[name="gestion"]').val());
		
		switch(intGestionTipo){
			case 1:
				if(intGestionTipo != current){
					objForm[0].action = OneCommon.url + 'comprar';
					objForm[0].submit();
					return;
				}
				break;
			case 2:
				if(intGestionTipo != current){
					objForm[0].action = OneCommon.url + 'alquilar';
					objForm[0].submit();
					return;
				}
				break;
			case 3:
				if(intGestionTipo != current){
					objForm[0].action = OneCommon.url + 'obra-nueva';
					objForm[0].submit();
					return;
				}
				break;
		}
		
		jQuery.ajax({
			type: "POST",
			url: objForm[0].action,
			data: objForm.serialize(),
			dataType: 'html',
			cache: false,
			success: function(strData){
				$('#resultados').html(strData);
				var position = $('#resultados').position();
				$.scrollTo(position.top-15, {duration:1000});
			}
		 });
	},
	
	pager: function(intPage){
		var objForm = $('#frmResults');
		
		jQuery.ajax({
			type: "POST",
			url: objForm[0].action + '/' + intPage,
			data: objForm.serialize(),
			dataType: 'html',
			cache: false,
			success: function(strData){
				$('#resultados').html(strData);
				var position = $('#resultados').position();
				$.scrollTo(position.top-15, {duration:1000});
				
				if (ie6 == true) {
				    /* PNGFix */   
				    DD_belatedPNG.fix('.png, #content h2, #pagination span, #pagination a, .box a, #direccion, #marco, #detalle h2 strong, #top a'); /* Add class="png" to the png images in the HTML*/
				}
			}
		 });
	},
	
	addToBookmarks: function(intInmuebleId){
		jQuery.ajax({
			type: "POST",
			url: OneCommon.url + 'inmueble/addtobookmarks/' + intInmuebleId,
			dataType: 'text',
			cache: false,
			success: function(strData){
				$('#addToBookmarks').children().eq(0).fadeOut(1000);
			}
		 });
	},
	
	openSendToFriend: function(){
		$('#lnkEnviarAmigo').nyroModal({
			resizable: false,
			width: 470,
			height: 240,
			minWidth: 470, // Minimum width
 			minHeight: 240, // Minimum height
 			autoSizable: false,

			css: {
				wrapper: { 
					border: 0,
					padding: 0,
					background: '#94A01C'
				}
			}
			
		});
	},
	
	gotoByRef: function(){
		var objInput = $('#search-term');
		var intRef = new Number(objInput.attr('value')); 
		if((objInput.attr('value') == '') || isNaN(intRef) || intRef == 0){
			$('#msgRef').slideDown().pause(5000).slideUp("slow"); 
			return false;
		}
		
		var objForm = $('#frmSearchRef');
		objForm.attr('action', objForm.attr('action') + '/' + intRef);
		objForm.submit();
	}
};