﻿var Site = function () {

	var LastProps = {
		
		properties: $.jStorage.index(),
		limit: 5,
	
		insert: function ( o ) {
			
			if ( this.get().length >= this.limit && !$.jStorage.get( o.id ) ) {
			
				$.jStorage.deleteKey( this.get()[ this.limit - 1 ].id );
				
			}
			
			$.jStorage.set( o.id, $.toJSON( o ) );
			this.properties = $.jStorage.index();
			
		
		},
		
		get: function () {
		
			var props = [];
			for ( var i = -1, il = this.properties.length; ++i < il; ) {
				props.push( $.parseJSON( $.jStorage.get( this.properties[i] ) ) );
			}
			
			//console.log( props );
			
			return props.sort( function( a, b ) {
				if ( a.timestamp < b.timestamp ) return 1;
				if ( a.timestamp > b.timestamp ) return -1;
				return 0;
			});
			
		
		}
		
	};

	return {

		insertProperty: function ( p ) {
		
			LastProps.insert( p );
		
		},
		
		printLastProperties: function () {
		
		
			if ( LastProps.get().length > 0 ) {
		
				var $lp = $('#last-properties').show().find('ul');
				
				$.map( LastProps.get(), function ( p ) {
					
					$lp.append( $('<li />').html(
						'<a href="/index/details/id/' + p.id + '"><span class="img"><img src="' + p.thumb + '" alt="" /></span>' + 
						'<span class="description"><strong>' + p.type + '</strong><strong>' + p.address + '</strong></span></a>'
					) );
				
				});
				
			}
		
		},
		
		printLastPropertiesHome: function () {
		
		
			if ( LastProps.get().length > 0 ) {
		
				var $lp = $('#last-properties-home').show().find('ul');
				
				$.map( LastProps.get(), function ( p ) {
					
					$lp.append( $('<li />').html(
						'<a href="/index/details/id/' + p.id + '"><img src="' + p.thumb + '" alt="" />' + 
						'<span class="description"><strong>' + p.type + '</strong><strong>' + p.address + '</strong>' + p.details + '</span></a>'
					) );
				
				});
				
				window.onload = function () {
					$lp.find('a').equalHeight();
				}
				
			}
		
		},
	
		initCommon: function () {
			
			Plug.init();
			
			$('form')
				.find('input:text').placeholder().end()
				.find('select.selectbox').selectbox();

		},

		initHome: function () {
			
			$('#main')
				.find('ul.box')
					.each2(function () {
						this.children().equalHeight()
							.find('a').bigTarget({
								hoverClass: 'hover'
							});
					});
			
			$("#destaques-list").carouFredSel({
				items: 4,
				prev: {
					button: "#destaques-prev"
				},
				next: {
					button: "#destaques-next"
				},
				scroll: {
					easing: 'easeOutBack',
					duration: 600
				},
				auto: {
					play: false
				},
				height: 'auto',
				circular: false,
				infinite: false
			});
			
			$("#slider-images").carouFredSel({
				items: 1,
				height: 278,
				align: 'center',
				auto: {
					pauseOnHover: true
				},
				scroll: {
					fx: 'crossfade',
					duration: 800
				}
			});
			
		},

		initSearch: function () {
			
			$('#search-results').find('div.video a').fancybox();

		},

		initDetails: function () {
		
			$('#property-photo-items').find('a').fancybox({
				prevEffect		: 'elastic',
				nextEffect		: 'elastic',
				closeBtn		: false,
				helpers		: { 
					title	: { type : 'inside' },
					buttons	: {}
				}
			});
			$("#open-gallery").on('click', function (e) {
				$("#property-photo-items").find('a').first().click();
				e.preventDefault();
			});
			$('#youtube').fancybox();
			
			$("#details-plug").children().equalHeight();
			$("#details-paymodes").children().equalHeight();
			
			$("#carac").carouFredSel({
                items: 3,
                prev: {
                    button: "#carac_prev"
                },
                next: {
                    button: "#carac_next"
                },
                auto: {
                    play: false
                },
                height: 'auto',
                circular: false,
                infinite: false
            });
			
			var $form = $('#form-more-info');
			$form.find('input:text').setMask();
			$form.ajaxForm({
				error: function() {
					alert('Erro ao enviar o formulário. Por favor, tente novamente mais tarde.');
				},
				beforeSerialize: function () {
					$form.find("button.submit").prop("disabled", true).css("cursor", "wait");
				},
				success: function (data) {
					if (data == "OK") {
						$form.slideUp("medium", function () {
							$(this).html('<div class="alert-message success">Enviado com sucesso!</div>').slideDown();
						});
					} else {
						alert('Por favor, preencha os campos corretamente.');
					}
					$form.find("button.submit").prop("disabled", false).css("cursor", "default");
				}
			});
			
		}

	}

}();

function reloadCombo(combo, url, first_opt, second_opt) {
    $(combo).prop("disabled", true).parent().find(".styleSelect").removeClass("disabled").find("span").text("Carregando..");
    $.getJSON(encodeURI(url), function (response) {
        var options = '';
        options += '<option value="-1">' + first_opt + '</option>';
        options += '<option value="-1">' + second_opt + '</option>';
        for (var i = 0; i < response.length; i++) {
            options += '<option value="' + response[i].value + '">' + response[i].label + '</option>';
        }
        $(combo).html(options);
        $(combo).prop("disabled", false).next().removeClass("disabled").children().text(first_opt);
    });
}

function clearDisableCombo(combo, first_opt) {
    $(combo).prop("disabled", true).next().addClass("disabled").children().text(first_opt);
    $(combo).val(-1);
}
