var heerenHillewaere = {
    map: null,
    
    init : function() {
        
        //SimpleMaps
        this.bindOfficesSimpleMap();
        this.bindEstatesSimpleMap();
        this.bindOfficeSimpleMap();
        this.bindEstateSimpleMap();
        
        this.bindHintOnTextfieldsWithTitle();
        this.bindPopupOnReferenceLink();
        this.bindTargetBlankOnExternalLinks();
        this.bindLinkOnSubmitOfficeLink();
        this.bindEstatePictures();
        this.bindTab();
        this.bindCalculateMort();
        this.bindCalculateMortOnLOad();
        this.bindTwitterFeed();
        this.bindjqTransformOnOfficesSelect();
        this.bindOfficePeople();
        this.bindLinkOnSearchContact();
        this.bindjqTransformOnYearSelect();
        this.bindCycleSlideshowOnCycleDiv();
    },
    
    bindOfficesSimpleMap : function() {
        if($('div.kantorenMap').length && $('div#map').length) {
            this.map = new simpleMap({
                adres:"/kantoren/kantorenjson"
            });
        }
    },
    
    bindEstatesSimpleMap : function() {
        if($('div.estatesMap').length && $('div.map').length) {
            this.map = new simpleMap({
                adres:window.location.href.replace("viewmode=map", "viewmode=json"),
                clustering: true,
                zoom: 9
            });
        }
    },
    
    bindOfficeSimpleMap : function() {
        if($('div.kantoormap').length && $('div#map').length) {
            var self = this;
            self.map = new simpleMap({
                adres: '/kantoren/kantorenjson/locatie/' + $('span.kantoorid').html(),
                clustering: false,
                directions: true,
                zoom: 17
            });
            
            $('form').submit(function(e) {
                e.preventDefault();

                var ua = $('input[name="address"]').val()+" "+$('input[name="place"]').val();

                self.map.showDirections({
                    from:ua,
                    onSuccess:function() {
                        $('#printLink, #hideDirections').show();
                    }
                });
            });
            
            $('#hideDirections').click(function(e) {
                e.preventDefault();
                self.map.hideDirections();
                $('#printLink, #hideDirections').hide();
            });
            
            $('#printLink').click(function() {
                window.open('/printMap.html', 'Heeren &amp; Hillewaere', 'status=1,toolbar=0,width=600');
                return false;
            });
        }
    },
    
    bindEstateSimpleMap : function() {
        if($('div#regioplan').length && $('div#map').length) {
            var self = this;
            self.map = new simpleMap({
                adres: '/vastgoed/detailjson/locatie/' + $('span.estateid').html(),
                clustering: false,
                directions: true,
                zoom: 16
            });

            $('form').submit(function(e) {
                e.preventDefault();

                var ua = $('input[name="address"]').val()+" "+$('input[name="place"]').val();

                self.map.showDirections({
                    from:ua,
                    onSuccess:function() {
                        $('#printLink, #hideDirections').show();
                    }
                });
            });

            $('#hideDirections').click(function(e) {
                e.preventDefault();
                self.map.hideDirections();
                $('#printLink, #hideDirections').hide();
            });

            $('#printLink').click(function() {
                window.open('/printMap.html', 'Heeren &amp; Hillewaere', 'status=1,toolbar=0,width=600');
                return false;
            });
        }
    },

    bindHintOnTextfieldsWithTitle : function() {
        $('input[title!=""]').hint();
    },
    
    bindPopupOnReferenceLink : function() {
        if($('a.reference').length) {
            $('a.reference').click(function() {
                $('div.referencebox').slideToggle();
            });
            
            $('div.closePopup').click(function() {
                $('div.referencebox').slideToggle();
            });
            
            $('.submit').click(function() {
                //window.location = '/vastgoed/' + encodeURIComponent($('input#referenceNumber').val()) + '/referentie/';
            });
        }
    },
    
    bindTargetBlankOnExternalLinks : function() {
        /*
         * If the href of the link begins with http or https and has an external hostname, open new window.
         */
        $('a[href^=http], a[href^=https]').each(
            function(){
              if(this.href.indexOf(location.hostname) == -1) {
                $(this).attr('target', '_blank');
              }
            }
        )
    },
    
    bindLinkOnSubmitOfficeLink : function() {
        if($('a.submitOffice').length) {
            $('a.submitOffice').click(function() {
                window.location = $('select[name="office"]').val();
            })
        }
    },
    
    bindEstatePictures : function () {
        if($('.pikawrapitall').length) {
            $("#pikame").PikaChoose({autoPlay:true,transition:[1]});
            //$("#pikame").PikaChoose({autoPlay:true,/*speed:200,animationSpeed:0,carousel:true*/});
            $(".pikawrapitall").removeClass("DN");
			$('.pikawrapitall').clone().appendTo('#pict4print');
			$('#pict4print ul#pikame').attr('id','pikaPrint');
			$('#pikaPrint li div.clip img').removeAttr('style');
			$('#pikaPrint li div.clip').attr('style','text-align:center;');
			$('#pikaPrint li div.clip img').attr('style','height:100%;');
			
        }
    },

    bindTab : function() {
        if($("div#galFilterInside div.tab a").length) {
            var self = this;
            $("div#galFilterInside div.tab a").click(function(e){
                e.preventDefault();
                $("div#galFilterInside div.active").removeClass("active");
                $(this).parent().addClass("active");
                $("#variContent>div").addClass("DNIMP");
                $("div"+ $(this).attr('href') +"").removeClass("DNIMP").fadeIn();
                if ($(this).attr("title") == "regioplan") {
                    self.map.refresh();
                }
            });
        }
    },
    
    bindCalculateMort : function() {
        if($('div#financieel').length) {
            $('input#buttonLening').click(function() {
                
                var capital = $('input#amount').val();
                var interest = $('input#interest').val();
                var plazo = $('select#years').val();
                var ownMoney = $('input#ownMoney').val();
                var check = parseInt(plazo);
                
                if (!isNaN(check)){
                    $('select#years').val(check);
                } else {
                    alert("Aantal jaar: enkel cijfers.");
                    return false;
                }
                if (isNaN(capital)){
                    alert("Te lenen bedrag: enkel cijfers.");
                    return false;
                }
                if(interest <= 0) {
                    alert('Interest: vul een cijfer in dit groter is dan 0.0');
                    return false;
                }
                if (isNaN(interest)){
                    alert("Interest: enkel cijfers. Voor decimale waardes een punt gebruiken ipv een komma (bv: 4.3 ipv 4,3)");
                    return false;
                }
                if(isNaN(ownMoney)) {
                    alert('Eigen inbreng: enkel cijfers.');
                    return false;
                }
                var monthly = capital * (interest / 100) / 12;
                interest = interest / 12;
                plazo = plazo * 12 * (-1);
                var payment = ((capital - ownMoney) * interest)/(100 * (1 - Math.pow(1 + (interest/100),plazo)));
                payment = parseInt(payment);

                $('span#payment').html("&euro; "+payment);
                return true;
            })
        }
    },
    
    bindCalculateMortOnLOad : function() {
        if($('div#financieel').length) {
            $('input#buttonLening').click();
        }
    },
    
    bindTwitterFeed : function() {
        if (typeof $("testelement").tweet == "function") {
            $(".tweet").tweet({
                username: "HHvastgoed",
                join_text: null,
                avatar_size: 50,
                count: 2,
                auto_join_text_default: "deelden we ",
                auto_join_text_ed: "we ",
                auto_join_text_ing: "waren we ",
                auto_join_text_reply: "antwoorden we aan ",
                auto_join_text_url: "bekeken we ",
                loading_text: "tweets laden..."
            });
        }
    },
    
    bindjqTransformOnOfficesSelect : function() {
        if($('form#submitOffice').length) {
            $('form#submitOffice').jqTransform({imgPath:'jqtransformplugin/img/'});
        }
    },
    
    bindOfficePeople : function() {
        if($('.humanwrapper').length) {
            $('.humanwrapper').children().each(function(index, child) {
                if(child.className == 'human') {
                    var persoonid = $('span#persoonid', child).html();
                    $(child).colorbox({width:900, inline:true, href:"#persoon_" + persoonid});
                }
            });
        }
    },
    
    bindLinkOnSearchContact : function() {
        if($('.hoogte').length) {
            $('.hoogte').css('cursor', 'pointer');
            $('.hoogte').click(function() {
                window.location = $('.hoogtelink', '.hoogte').attr('href');
            });
        }
    },
    
    bindjqTransformOnYearSelect : function() {
        if($('.yearSelect').length) {
            $('#financialForm').jqTransform({imgPath:'jqtransformplugin/img/'});
        }
    },
    
    bindCycleSlideshowOnCycleDiv : function() {
        if($('div.cycle').length) {
            $('div.cycle').cycle({
                fx: 'fade'
            });
        }
    }
}
