﻿var map;
var gdir;
var geocoder = null;
var addressMarker;


function initializeMap() {
    if (GBrowserIsCompatible()) {

        map = new GMap2(document.getElementById("map_canvas"));
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);

        var geocoder = new GClientGeocoder();
        geocoder.getLatLng(getAddress(), function(point) {
            map.setCenter(point, 15);
            map.addOverlay(new GMarker(point, { title: "" }));
            map.setUIToDefault();
        });

        $('#route-submit').click(function() {
            setDirections(
				$('#route #route-from').val(),
				$('#route #route-to').val(),
				'pt_br'
			);
            return false;
        });

        $('#swap-address').click(function() {
            var mem = $('#route #route-from').val();
            $('#route #route-from').val($('#route #route-to').val());
            $('#route #route-to').val(mem);
            return false;
        });

        $('#route-errors').click(function() {
            $('#route-errors').animate({ top: -100 }, { queue: false, duration: 300, easing: 'linear', complete:
			function() {
			    $('#route-errors').hide();
			}
            });
        });
    }
}

function setDirections(fromAddress, toAddress, locale) {
    gdir.load("from: " + fromAddress + " to: " + toAddress,
			{ "locale": locale });
}

function getAddress() {
    return $('#photos input[type=hidden]').attr('value');
}



function handleErrors() {
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        triggerError("O endereço indicado não foi encontrado. Ele pode ser relativamente novo ou estar incorreto. Por favor, utilize o modelo: Logradouro, Número, Cidade e Estado, separados por vírgulas.");
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
        triggerError("O servidor dos mapas está fora do ar. Tente novamente mais tarde.");

    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
        triggerError("O endereço indicado não foi encontrado. Ele pode ser relativamente novo ou estar incorreto. Por favor, utilize o modelo: Logradouro, Número, Cidade e Estado, separados por vírgulas.");

    //   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
    //     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
        triggerError("Chave inválida para esse domínio.");

    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
        triggerError("Não foi possível traçar as direções. Preencha corretamente utilizando o modelo: Logradouro, Número, Cidade e Estado, separados por vírgulas.");

    else triggerError("O endereço indicado não foi encontrado. Ele pode ser relativamente novo ou estar incorreto. Por favor, utilize o modelo: Logradouro, Número, Cidade e Estado, separados por vírgulas.");

}



function triggerError(msg) {

    if (msg != "") {
        $('#route-errors').text(msg).show().animate({ top: 0 }, { queue: false, duration: 300, easing: 'linear' });
    } else {
        $('#route-errors').hide();
    }
}



function onGDirectionsLoad() {
    // Use this function to access information about the latest load()
    // results.

    // e.g.
    // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
    // and yada yada yada...
    $('#route-errors').hide();
}






var choose = {

    originalTitle: null,

    init: function()
    {
        choose.originalTitle = $('title').html().split('#')[0];
        
        var titleFlashText = $('.mainpanel .title h1').html();

        var divTitleFlash = $(document.createElement('div'));
        divTitleFlash.attr('id', 'divTitleFlash');
        $('.mainpanel .title h1').html('').append(divTitleFlash);

        swfobject.embedSWF("static/swf/shared/txt-rpl-greyscale-57pt.swf", 'divTitleFlash', "375", "88", "9", "static/swf/shared/expressInstall.swf", { $text: titleFlashText }, { "wmode": "transparent" }, { "wmode": "transparent" });

        photos.init();
        misc.init();
        home.init();

        $('a.where-is').live('click', function()
        {
            $('#photos > ul > li.maps > a').trigger('click');
            return false;
        });

        $('#photos ul li div a, div.mainpanel div.tourVirtualOptions dl dd a').live('click', function()
        {
            window.open(this, "tourWindow", "width=975,height=655");
            return false;
        });

        $('.analia-franco .nav-1-12 a').html('Não possui');
        $('.analia-franco .nav-1-12 a').attr('href', '#');
        $('.analia-franco .nav-1-12 a').css('background', '#505050');
        $('.analia-franco .nav-1-12 a').css('cursor', 'default');

        $('.analia-franco .nav-2-12 a').html('Não possui');
        $('.analia-franco .nav-2-12 a').attr('href', '#');
        $('.analia-franco .nav-2-12 a').css('background', '#505050');
        $('.analia-franco .nav-2-12 a').css('cursor', 'default');
    },

    lpsCall: function()
    {
        window.open("http://www.lopes.com.br/corretoronline/chat.asp?idc=4319&pre_empresa=1&vivocustom2=MAXCASA_MAXHAUS-PANAMBY&vivocustom3=3511&vivocustom4=MAXCASA_MAXHAUS-PANAMBY_CHAT_20090311", "attLopes", "width=200,height=200");
        void (0);
    }
};

var misc = {

    init: function() {
        $('div.mainpanel dl.contactChannels dd.node-2 a').neoHover({ inverted: true });
       
        $('.neighborhood-select span.up, .neighborhood-select span.down').hide();
        $('.neighborhood-select ul li a').neoHover();
        //$('.neighborhood-select .holder').height(($('.neighborhood-select ul li a').height() + 1) * $('.neighborhood-select ul li a').length);

        $('div.mainpanel fieldset.propertyInfoForm dl dd.node-6 a').neoHover();
        $('div.pnlSuccess a').neoHover();
    },

    hideSuccessPanel: function(successDivId, formDivId) {
        $("#" + successDivId).hide()
        $("#" + formDivId).show();
    }
}

var photos = {


    init: function() {
        //----------------------------------------------------------
        // adding classes
        //----------------------------------------------------------
        var level_0 = $('#photos > ul > li');

        $.each(level_0, function(i, el_0) {
            var level_1 = $('ul > li', el_0);
            var classAdded_1 = $(el_0).attr('class');

            $(el_0).attr('class', '');
            $(el_0).addClass('nav-' + i);
            $(el_0).addClass(classAdded_1);

            $.each(level_1, function(j, el_1) {
                var level_2 = $('ol > li', el_1);
                var classAdded_2 = $(el_1).attr('class');

                $(el_1).attr('class', '');
                $(el_1).addClass('nav-' + i + '-' + j);
                $(el_1).addClass(classAdded_2);

                $.each(level_2, function(k, el_2) {
                    $(el_2).addClass('nav-' + i + '-' + j + '-' + k);
                    $('a', el_2).text(k + 1);
                });
            });
        });

        $('#photos .title').animate({ opacity: 0 }, { queue: false, duration: 10, easing: 'easeOutQuad', complete: function() { } });
        $('#photos ol').animate({ opacity: 0 }, { queue: false, duration: 10, easing: 'easeOutQuad', complete: function() { } });

        this.active();
        this.pagination();
        this.bevavior();
    },
    active: function() {
        //----------------------------------------------------------
        // setting active
        //----------------------------------------------------------

        var active = [];
        var myHash;

        if (location.hash) {
            myHash = location.hash.split('=')[1];
            active = myHash.split(',');
        }
        else {
            myHash = "nav=0,0,0";
            myHash = myHash.split('=')[1];
            active = myHash.split(',');
        }

        var level_0_items = $('#photos > ul > li');
        var level_0 = $('#photos > ul > li:eq(' + active[0] + ')');

        var level_1_items = $('#photos > ul > li:eq(' + active[0] + ') > ul > li');
        var level_1 = $('#photos > ul > li:eq(' + active[0] + ') > ul > li:eq(' + active[1] + ')');

        var level_2_items = $('#photos > ul > li:eq(' + active[0] + ') > ul > li:eq(' + active[1] + ') > ol > li');
        var level_2 = $('#photos > ul > li:eq(' + active[0] + ') > ul > li:eq(' + active[1] + ') > ol > li:eq(' + active[2] + ')');

        //----------------------------------------------------------
        // loading img
        //----------------------------------------------------------

        var imgUrl = $('a', level_2).attr('href');

        var viewUrl = imgUrl.replace(/(.*)(\/large\/)(.*)/g, '$1/small/$3');


        var imgPreloader = new Image();
        var preventOverload = true;

        imgPreloader.onload = function() {
            /* Aqui ele tava dando um loop infinito */
            if (preventOverload == false) return;
            preventOverload = false;

            var html = '';
            html += '<ul class="images">';

            $.each(level_2_items, function(i, el) {

                if (i == active[2]) {
                    html += '<li class="img-' + i + '">';

                    if (!$(level_0).hasClass('maps')) {
                        html += '<img src="' + imgPreloader.src + '" alt="' + $('a', el).attr('title') + '" />';
                    }

                    var level_2_rel = $(level_2).attr('rel') == undefined ? "" : $(level_2).attr('rel');
                    var level_2_rel_arr = level_2_rel.split("|");

                    for (var i = 0; i < level_2_rel_arr.length; i++) {
                        if (level_2_rel_arr[i] == 'has-observer') {
                            html += '<div class="observer-view"><img src="' + viewUrl + '" alt="" /></div>';
                        }

                        if (level_2_rel_arr[i] == 'has-tour') {
                            /*here*/
                            html += '<div class="tour-view"><a href="' + level_2.find('span input').val() + '" target="_blank">&nbsp</a></div>';
                        }
                    }

                    html += '</li>';

                    var caption = $('a', el).attr('title');
                    var maxLength = 120;

                    $('#photos .title').text(caption);
                    $('#photos .title').css('font-size', caption.length > maxLength ? '9px' : '11px');


                } else {
                    html += '<li class="img-' + i + '">' + $('a', el).attr('title') + '</li>';
                }
            });

            html += '</ul>';

            $('#photos .holder:eq(0)').html(html);

            //----------------------------------------------------------
            // arrows - next - prev
            //----------------------------------------------------------

            $('#photos a.arrow-next').live('click', function() {
                if ($.support.cssFloat) {
                    $('#photos ol li.active').next().next().find('a').trigger('click');
                } else {
                    $('#photos ol li.active').next().find('a').trigger('click');
                }


                return false;
            });

            $('#photos a.arrow-prev').live('click', function() {
                if ($.support.cssFloat) {
                    $('#photos ol li.active').prev().prev().find('a').trigger('click');
                } else {
                    $('#photos ol li.active').prev().find('a').trigger('click'); ;
                }


                return false;
            });

            // maps

            if ($(level_0).hasClass('maps')) {
                $('#route').show();
                $('#route #route-to').val($('#photos input[type=hidden]').attr('value'));
                initializeMap();
            } else {
                $('#route').hide();
            }

            if ($(level_0).hasClass('diff')) {
                $('#photos .holder').addClass('diff');
            } else {
                $('#photos .holder').removeClass('diff');
            }

            $('#photos .observer-view').live('mouseover', function() {
                $(this).addClass('hover');
                $('.arrow-next').hide();
            });

            $('#photos .observer-view').live('mouseout', function() {
                $(this).removeClass('hover');
                $('.arrow-next').show();
            });

            $('#photos li').removeClass('active');

            $('#photos ul, #photos ol').css('display', 'none');
            $('#photos ul.images li').css('display', 'none');

            // level_0
            $('#photos ul.photos-nav').css('display', 'block');
            $('#photos ul.photos-nav > li:eq(' + (active[0] || 0) + ')').addClass('active');

            // level_1
            $('#photos ul.photos-nav > li.active > ul').css('display', 'block');
            $('#photos ul.photos-nav > li.active > ul > li:eq(' + (active[1] || 0) + ')').addClass('active');
            $('#photos ul.photos-nav > li.active > ul > li > a').animate({ marginLeft: 0, opacity: 1 }, { queue: false, duration: 300, easing: 'easeOutQuad', complete: function() { } });

            // pagination
            $('#photos ul.photos-nav > li.active > ul > li.active > ol').css('display', 'block');

            var lis = $('#photos ul.photos-nav > li.active > ul > li.active > ol li');

            $.each(lis, function(i, el) {
                var li = $(el);
                li.css('right', (23 * (lis.length - (i + 1))) + 'px')
            })

            $('#photos ul.photos-nav > li.active > ul > li.active > ol li:eq(' + (active[2] || 0) + ')').addClass('active');

            // images
            $('#photos .holder ul').css('display', 'block');
            $('#photos .holder ul li:eq(' + (active[2] || 0) + ')').css('display', 'block');
            $('#photos .holder ul').animate({ height: '491px' }, { queue: false, duration: 300, easing: 'easeOutQuad', complete: function() { } });

            if (!$(level_0).hasClass('maps')) {
                $('#photos .holder ul li').fadeTo(0, 0);
            }

            $('#photos .holder ul li:eq(' + (active[2] || 0) + ')').addClass('active');

            if (!$(level_0).hasClass('maps')) {
                $('#photos .holder ul li.active').animate({ opacity: 1 }, { queue: false, duration: 300, easing: 'easeOutQuad', complete: function() { } });
            }

            // title
            $('#photos .title').animate({ opacity: 1 }, { queue: false, duration: 300, easing: 'easeOutQuad', complete: function() { } });
            $('#photos ol').animate({ opacity: 1 }, { queue: false, duration: 300, easing: 'easeOutQuad', complete: function() { } });

            //----------------------------------------------------------
            // arrows - next - prev
            //----------------------------------------------------------

            var arrows = "<a href='#' class='arrow-prev'>prev</a>";
            arrows += "<a href='#' class='arrow-next'>next</a>";
            $('#photos .holder:eq(0)').prepend(arrows);

            $('#photos a.arrow-next').live('click', function() {
                if ($.support.cssFloat) {
                    $('#photos ol li.active').next().next().find('a').trigger('click');
                } else {
                    $('#photos ol li.active').next().find('a').trigger('click');
                }

                return false;
            });

            $('#photos a.arrow-prev').live('click', function() {
                if ($.support.cssFloat) {
                    $('#photos ol li.active').prev().prev().find('a').trigger('click');
                } else {
                    $('#photos ol li.active').prev().find('a').trigger('click');
                }

                return false;
            });

            if ($(level_2_items).eq(0).hasClass('active')) {
                $('#photos a.arrow-prev').hide();
            } else if (level_2_items.length < 2) {
                $('#photos a.arrow-prev').hide();
            } else {
                $('#photos a.arrow-prev').show();
            }

            if ($(level_2_items).eq(level_2_items.length - 1).hasClass('active')) {
                $('#photos a.arrow-next').hide();
            } else if (level_2_items.length < 2) {
                $('#photos a.arrow-next').hide();
            } else {
                $('#photos a.arrow-next').show();
            }

            if ($('#photos .nav-1').hasClass('active') || $('#photos .nav-2').hasClass('active')) {
                $('#photos a.arrow-prev').addClass('diff');
                $('#photos a.arrow-next').addClass('diff');
            } else {
                $('#photos a.arrow-prev').removeClass('diff');
                $('#photos a.arrow-next').removeClass('diff');
            }

            if ($('#photos .maps').hasClass('active')) {
                $('#photos a.arrow-prev').hide();
                $('#photos a.arrow-next').hide();
            }
        };

        imgPreloader.src = imgUrl;
    },

    pagination: function() {
        $('#photos ol li a').bind('click', function() {
            if (!$(this).parent().hasClass('active')) {
                var el = $(this);
                var pos = $(this).parent().attr('class').split('-')[3];

                var imgUrl = $(this).attr('href');

                var tour_url = $(this).parent().find('span input').val();
                /*here*/

                var viewUrl = imgUrl.replace(/(.*)(\/large\/)(.*)/g, '$1/small/$3');

                var has_observer = false;
                var has_tour = false;

                var this_rel = $(this).parent().attr('rel') == undefined ? "" : $(this).parent().attr('rel');
                var this_rel_arr = this_rel.split("|");

                for (var i = 0; i < this_rel_arr.length; i++) {
                    if (this_rel_arr[i] == 'has-observer') has_observer = true;
                    if (this_rel_arr[i] == 'has-tour') has_tour = true;
                }

                var imgPreloader = new Image();

                imgPreloader.onload = function() {
                    var caption = $(el).attr('title');
                    var maxLength = 120;

                    $('#photos .title').text(caption);
                    $('#photos .title').css('font-size', caption.length > maxLength ? '9px' : '11px');

                    var newhtml = "";

                    if (has_observer) {
                        newhtml = '<div class="observer-view"><img src="' + viewUrl + '" alt="" /></div>';
                    }

                    if (has_tour) {
                        newhtml = newhtml + '<div class="tour-view"><a href="' + tour_url + '" target="_blank">&nbsp</a></div>';
                    }

                    $('#photos .holder li.img-' + pos).html('<img src="' + imgPreloader.src + '" alt="' + $(el).attr('title') + '" />' + newhtml);
                    $('#photos .holder li').animate({ opacity: 0 }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function() {

                        $(this).css('display', 'none');
                        $('#photos .holder li.img-' + pos).css('display', 'block');
                    }
                    });

                    $('#photos .holder li.img-' + pos).animate({ opacity: 1 }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function() {
                        $('#photos ol li').removeClass('active');
                        $('#photos .holder li').removeClass('active');
                        $(el).parent().addClass('active');
                        $(this).addClass('active');
                    }
                    });

                    $('#photos .title').animate({ opacity: 1 }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function() { } });
                    $('#photos ol').animate({ opacity: 1 }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function() { } });

                    if ($.support.cssFloat) {
                        if ($(el).parent().next().next().attr('class')) {
                            $('#photos a.arrow-next').show();
                        } else {
                            $('#photos a.arrow-next').hide();
                        }

                        if ($(el).parent().prev().prev().attr('class')) {
                            $('#photos a.arrow-prev').show();
                        } else {
                            $('#photos a.arrow-prev').hide();
                        }
                    } else {
                        if ($(el).parent().next().attr('class')) {
                            $('#photos a.arrow-next').show();
                        } else {
                            $('#photos a.arrow-next').hide();
                        }

                        if ($(el).parent().prev().attr('class')) {
                            $('#photos a.arrow-prev').show();
                        } else {
                            $('#photos a.arrow-prev').hide();
                        }
                    }

                    imgPreloader.onload = null;
                    imgPreloader = null;

                };

                imgPreloader.src = imgUrl;

                var select = $(el).parent().attr('class').split(' ')[0].split('-');

                location.hash = 'nav=' + select[1] + ',' + select[2] + ',' + select[3];
                select = [];
            }

            return false;

        });
    },
    bevavior: function() {
        $('#photos > ul > li > a').live('click', function() {

            var select = $(this).parent().attr('class').split(' ')[0].split('-');

            location.hash = 'nav=' + select[1] + ',0,0';
            select = [];

            $('#photos .title').animate({ opacity: 0 }, { queue: false, duration: 300, easing: 'easeOutQuad', complete: function() { } });
            $('#photos ol').animate({ opacity: 0 }, { queue: false, duration: 300, easing: 'easeOutQuad', complete: function() { } });

            $('#photos > ul > li > ul > li > a').animate({ marginLeft: '200px', opacity: 0 }, { queue: false, duration: 300, easing: 'easeOutQuad', complete: function() { } });

            if ($(this).parent().hasClass('maps')) {
                $('#photos .holder ul').animate({ height: 0 }, { queue: false, duration: 300, easing: 'easeOutQuad', complete: function() {
                    photos.active();
                }
                });
            } else {
                $('#photos .holder ul').animate({ height: 0, opacity: 0 }, { queue: false, duration: 300, easing: 'easeOutQuad', complete: function() {
                    photos.active();
                }
                });
            }

            return false;
        });

        $('#photos > ul > li > ul > li > a').live('click', function() {

            var select = $(this).parent().attr('class').split(' ')[0].split('-');

            location.hash = 'nav=' + select[1] + ',' + select[2] + ',0';
            select = [];

            $('#photos .title').animate({ opacity: 0 }, { queue: false, duration: 200, easing: 'easeOutQuad', complete: function() { } });
            $('#photos ol').animate({ opacity: 0 }, { queue: false, duration: 200, easing: 'easeOutQuad', complete: function() { } });

            photos.active();

            return false;
        });
    }
};


var home = {


	init: function()
	{
		var locale = $('span#current-locale input').val();
	
		swfobject.embedSWF( "static/swf/" + locale + "/default/banner-vila-leopoldina.swf", "banner-flash-content", "783", "396", "9" , "static/swf/shared/expressInstall.swf", {}, {"wmode": "transparent"}, {"wmode": "transparent"});
		
		this.slideShow('#banner', 800, 4000);
		
		/*if($('div#banner-flash-content')[0])
		{
			swfobject.embedSWF( "static/swf/pt-br/default/banner-vila-leopoldina.swf", "banner-flash-content", "783", "396", "9" , "static/swf/shared/expressInstall.swf");
		}
		else
		{
			this.slideShow('#banner', 800, 4000);
		}
		*/
		
		//this.sideNav('#neighborhood-select-24');
		//this.sideNav('#neighborhood-select-25');
		
		//$('#neighborhood-select-25').css('top', '190px');
		
		$('ul#highlights li:last').css('margin-right', '0');
		$('div#banner div#info').addClass('hover');
		$('div#banner div#info').hover(function() { $(this).addClass('hover') }, function() { $(this).removeClass('hover') });
		//swfobject.embedSWF( def.project.pathSwf + "shared/test.swf", "decorated-movie", "285", "200", libraries.swfObject.version , def.project.pathSwf + "shared/expressInstall.swf");
		this.lasts('div#lasts');

		$('ul#highlights li').neoHighlight({ hoverButton: true, duration: 400 });

		$('#lasts .controls a, #lasts .controls span.prev, #lasts .controls span.next, #testimonials a').neoHover();

		//this.sideNav('#neighborhood-select-25');
		
		//$('#neighborhood-select span.up, #neighborhood-select span.down').hide();
		this.hoverSideNavHome($("#state-select dd a"));

	},




	lasts: function(obj)
	{
		var duration = 400;
		var visible = 1;
		var step = 1;
		var id = $(obj).attr('id');
		var holder = $('#' + id + ' .holder');
		var el = $('#' + id + ' .holder ol');
		var items = $('#' + id + ' .holder ol li');
		var prev = $('#' + id + ' .holder .prev');
		var next = $('#' + id + ' .holder .next');
		var pos = $('#' + id + ' .holder .position');
		var readMore = $('#' + id + ' .holder .controls a');
		var amountItems = $(items).length;

		$('#' + id + ' ol').width($(items).width() * amountItems);

		var widthStep = $(items).width() * step;

		$(el).css('left', widthStep);

		$(items).hover(function()
		{
			$(this).addClass('hover');
		}, function()
		{
			$(this).removeClass('hover');
		});


		$(prev).bind('click', fcPrev);
		$(next).bind('click', fcNext);
		$(next).trigger('click');

		function fcPrev()
		{
			if (parseInt($(el).css('left')) < 0)
			{
				var posCurrent = -((parseInt($(el).css('left')) - widthStep) / widthStep) - 1;
				$(pos).html(posCurrent + " / " + amountItems);
				var linkCurrent = $(items).eq(posCurrent - 1).find('a').attr('href');
				$(items).eq(posCurrent - 1).attr('href', linkCurrent);
				$(readMore).attr('href', linkCurrent);
				$(items).eq(posCurrent - 1).click(function() { location.href = linkCurrent; });
				$(prev).unbind('click');
				$(el).animate({ left: parseInt($(el).css('left')) + widthStep }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function()
				{
					$(prev).bind('click', fcPrev);
				}
				});
			} else
			{
				$(pos).html(amountItems + " / " + amountItems);
				var linkCurrent = $(items).eq(amountItems - 1).find('a').attr('href');
				$(items).eq(amountItems - 1).attr('href', linkCurrent);
				$(readMore).attr('href', linkCurrent);
				$(items).eq(amountItems - 1).click(function() { location.href = linkCurrent; });
				$(prev).unbind('click');
				$(el).animate({ left: -(widthStep * (amountItems - 1)) }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function()
				{
					$(prev).bind('click', fcPrev);
				}
				});
			}
		}

		function fcNext()
		{
			if (-(parseInt($(el).css('left'))) < ($(el).width() - $(holder).width()))
			{
				var posCurrent = -((parseInt($(el).css('left')) - widthStep) / widthStep) + 1;
				$(pos).html(posCurrent + " / " + amountItems);
				var linkCurrent = $(items).eq(posCurrent - 1).find('a').attr('href');
				$(items).eq(posCurrent - 1).attr('href', linkCurrent);
				$(readMore).attr('href', linkCurrent);
				$(items).eq(posCurrent - 1).click(function() { location.href = linkCurrent; });
				$(next).unbind('click');
				$(el).animate({ left: parseInt($(el).css('left')) - widthStep }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function()
				{
					$(next).bind('click', fcNext);
				}
				});
			} else
			{
				$(pos).html(1 + " / " + amountItems);
				var linkCurrent = $(items).eq(0).find('a').attr('href');
				$(items).eq(0).attr('href', linkCurrent);
				$(readMore).attr('href', linkCurrent);
				$(items).eq(0).click(function() { location.href = linkCurrent; });
				$(next).unbind('click');
				$(el).animate({ left: 0 }, { queue: false, duration: 400, easing: 'easeOutQuad', complete: function()
				{
					$(next).bind('click', fcNext);
				}
				});
			}
		}
	},





	slideShow: function(obj, duration, delay)
	{

		var easing = 'easeOutQuad';
		var id = $(obj).attr('id');
		var holder = $('#' + id);
		var imgs = $('#' + id + ' ul li a img');
		var txt = $('#' + id + ' ul li a span');
		var txtPos = $(txt).eq(0).css('top');
		$(txt).fadeTo(0, 0);

		var zIndex = 1;

		function anime(index)
		{

			var direction = $(imgs).eq(index).attr('class');
			var topDiff = $(imgs).eq(index).height() - $(obj).height();
			var leftDiff = $(imgs).eq(index).width() - $(obj).width();

			$(imgs).eq(index).fadeTo(0, 0);
			$(imgs).eq(index).css('display', 'block');
			$(txt).eq(index).fadeTo(0, 0);
			$(txt).eq(index).css('display', 'block');

			var newIndex = ($(imgs).length != index + 1) ? index + 1 : 0;

			zIndex += 1;
			$(imgs).eq(index).css('z-index', zIndex);

			$(txt).eq(index).css('z-index', zIndex + 10);
			$(txt).eq(index).css('top', (parseInt(txtPos) + $(txt).eq(index).height()) + 'px');

			switch (direction)
			{


				case 'top':
					$(imgs).eq(index).css({ 'top': 0, 'left': 0 });
					$(imgs).eq(index)
					//.animate({ opacity: 1 }, duration / 2, easing)
						.animate({ opacity: 1 }, { queue: false, duration: duration, easing: easing, complete: function()
						{
							$(txt).eq(index).animate({ opacity: .8, top: txtPos }, 300, easing);
						}
						})
						.queue(function()
						{
							setTimeout(function()
							{
								$(imgs).eq(index).dequeue();
								$(imgs).eq(index).animate({ opacity: 0 }, { queue: false, duration: duration + 2000, easing: easing, complete: function() { $(this).css({ 'display': 'none' }) } });
								$(txt).eq(index).animate({ opacity: 0, top: (parseInt(txtPos) + $(txt).eq(index).height()) + 'px' }, { queue: false, duration: 200, easing: easing, complete: function() { anime(newIndex); } });
							}, delay);
						});

					break;


				/*case 'top':
				$(imgs).eq(index).css({ 'top': 0, 'left': 0 });
				$(imgs).eq(index)
				.animate({ opacity: 1 }, duration / 2, easing)
				.animate({ top: -topDiff, left: 0 }, { queue: false, duration: duration, easing: easing, complete: function()
				{
				$(txt).eq(index).animate({ opacity: .8, top: txtPos }, 300, easing);
				}
				})
				.queue(function()
				{
				setTimeout(function()
				{
				$(imgs).eq(index).dequeue();
				$(txt).eq(index).animate({ opacity: 0, top: (parseInt(txtPos) + $(txt).eq(index).height()) + 'px' }, { queue: false, duration: 200, easing: easing, complete: function() { anime(newIndex); } });
				}, delay);
				});

					break;*/ 

				case 'bottom':
					$(imgs).eq(index).css({ 'top': -topDiff, 'left': 0 });
					$(imgs).eq(index)
						.animate({ opacity: 1 }, duration / 2, easing)
						.animate({ top: 0, left: 0 }, { queue: false, duration: duration, easing: easing, complete: function()
						{
							$(txt).eq(index).animate({ opacity: .8, top: txtPos }, 300, easing);
						}
						})
						.queue(function()
						{
							setTimeout(function()
							{
								$(imgs).eq(index).dequeue();
								$(txt).eq(index).animate({ opacity: 0, top: (parseInt(txtPos) + $(txt).eq(index).height()) + 'px' }, { queue: false, duration: 200, easing: easing, complete: function() { anime(newIndex); } });
							}, delay);
						});
					break;

				case 'left':
					$(imgs).eq(index).css({ 'top': 0, 'left': 0 });

					$(imgs).eq(index)
						.animate({ opacity: 1 }, duration / 2, easing)
						.animate({ top: 0, left: -leftDiff }, { queue: false, duration: duration, easing: easing, complete: function()
						{
							$(txt).eq(index).animate({ opacity: .8, top: txtPos }, 300, easing);
						}
						})
						.queue(function()
						{
							setTimeout(function()
							{
								$(imgs).eq(index).dequeue();
								$(txt).eq(index).animate({ opacity: 0, top: (parseInt(txtPos) + $(txt).eq(index).height()) + 'px' }, { queue: false, duration: 200, easing: easing, complete: function() { anime(newIndex); } });
							}, delay);
						});
					break;

				case 'right':
					$(imgs).eq(index).css({ 'top': 0, 'left': -leftDiff });

					$(imgs).eq(index)
						.animate({ opacity: 1 }, duration / 2, easing)
						.animate({ top: 0, left: 0 }, { queue: false, duration: duration, easing: easing, complete: function()
						{
							$(txt).eq(index).animate({ opacity: .8, top: txtPos }, 300, easing);
						}
						})
						.queue(function()
						{
							setTimeout(function()
							{
								$(imgs).eq(index).dequeue();
								$(txt).eq(index).animate({ opacity: 0, top: (parseInt(txtPos) + $(txt).eq(index).height()) + 'px' }, { queue: false, duration: 200, easing: easing, complete: function() { anime(newIndex); } });
							}, delay);
						});
					break;

				default:
					$(imgs).eq(index).css({ 'top': 0, 'left': 0 });
					$(imgs).eq(index).fadeTo(1, 1);
					break;
			}
		}
		anime(0);
	},

	sideNav: function(obj)
	{
		var visible = 4;
		var step = 1;
		var id = $(obj).attr('id');
		var holder = $('#' + id + ' .holder');
		var el = $('#' + id + ' .holder ul');
		var items = $('#' + id + ' .holder ul li');
		var links = $('#' + id + ' .holder ul li a');
		var up = $('#' + id + ' .holder .up');
		var down = $('#' + id + ' .holder .down');
		var amountItems = $(items).length;
		var heightStep = $(items).height() * step + 1;

		//$(links).neoHover();
		this.hoverSideNavHome($(links));
		
		$(".neighborhood-select .holder").height($(".neighborhood-select ul").height());
		
		//$(holder).height((visible + 1) * (heightStep - .1));
		$(down).hover(function() { $(this).addClass('hover') }, function() { $(this).removeClass('hover'); });
		$(up).hover(function() { $(this).addClass('hover') }, function() { $(this).removeClass('hover'); });
		$(up).hide();
		$(up).bind('click', fcUp);
		$(down).bind('click', fcDown);
		
		if(items.length < visible){
			$(down).hide();
		}
		
		function fcUp() {
		    
			if (parseInt($(el).css('top')) < 0)
			{
				$(up).unbind('click');
				$(el).animate({ top: parseInt($(el).css('top')) + heightStep }, { queue: false, duration: 250, easing: 'easeOutQuad', complete: function()
				{
					$(up).bind('click', fcUp);
					foo();
				}
				});
			}
		}

		function fcDown() {

			if (-(parseInt($(el).css('top'))) <= ($(el).height() - $(holder).height()))
			{
				$(down).unbind('click');
				$(el).animate({ top: parseInt($(el).css('top')) - heightStep }, { queue: false, duration: 250, easing: 'easeOutQuad', complete: function()
				{
					$(down).bind('click', fcDown);
					foo();
				}
				});
			}
		}

		function foo()
		{
			if (parseInt($(el).css('top')) < 0)
			{
				$(up).fadeIn(60);
			} else
			{
				$(up).fadeOut(60);
			}
			if (-(parseInt($(el).css('top'))) <= ($(el).height() - $(holder).height()))
			{
				$(down).fadeIn(60);
				$(down).removeClass('disabled');
			} else
			{
				$(down).addClass('disabled');
			}
		}

	},
	
	hoverSideNavHome: function(obj){

	    $.each(obj, function(i, n)
	    {
	        $(n).append("<span>" + $(n).text() + "</span>");
	        $(n).find("span").css("opacity", "0");
        });
		
		$(".holder ul li a span, #state-select dd a span").mouseover(function(){
		    if (!$(this).parent().parent().hasClass('current'))
		    {
			    $(this).animate({
				    opacity: 1
			    }, {
    			    duration: 200,
    	            queue: false
			    } );
			}
		}).mouseout(function(){
	        $(this).animate({
	            opacity: 0
			    }, {
    			    duration: 200,
    	            queue: false
			    } );
        });
	}
};
