﻿var Objects = function($) {
    var priv = {
        objectId: null,
        objectType: null,
        googleMap: null,
        googleMapDirections: null,
        photoCarousel: null,
        photoCarouselTab: null,

        getIcon: function() {
            var icon = new GIcon();
            icon.image = global.pathPrefix + "/images/googlemaps/marker-single.png";
            icon.iconSize = new GSize(25, 25);
            icon.iconAnchor = new GPoint(12, 25);
            icon.infoWindowAnchor = new GPoint(20, 12);

            return icon;
        },

        initGoogleMaps: function() {
            if ($('#googlemap').length) {
                if (GBrowserIsCompatible()) {
                    var zoom = 17;
                    if ($('#objectType').attr('name') == 'parc') {
                        zoom = 16;
                    }
                    var objectPoint = new GLatLng($('#hidLatitude').val(), $('#hidLongitude').val());
                    priv.googleMap = new GMap2($('#googlemap').get(0));
                    priv.googleMap.addControl(new GLargeMapControl());
                    priv.googleMap.addControl(new GMapTypeControl());
                    priv.googleMap.addControl(new GScaleControl());
                    priv.googleMap.addControl(new GOverviewMapControl());
                    priv.googleMap.setCenter(objectPoint, zoom);
                    priv.googleMap.enableDoubleClickZoom();
                    priv.googleMap.setMapType(G_HYBRID_MAP);

                    var marker = new GMarker(objectPoint, { "icon": priv.getIcon() });
                    //                    GEvent.addListener(marker, 'click', function() {
                    //                        priv.googleMap.openInfoWindowHtml(objectPoint, $('#addressdata').html());
                    //                    });
                    priv.googleMap.addOverlay(marker);

                    priv.googleMapDirections = new GDirections(priv.googleMap, $("#route").get(0));
                    GEvent.addListener(priv.googleMapDirections, "error", priv.googleMapsDirectionErrors);

                    var whiteLabelQuery = (($(".whiteLabelField").attr("value") != "") ? ("&WhiteLabel=" + $(".whiteLabelField").attr("value")) : "");

                    $('#btnPlanRoute').bind('click', priv.planRoute(objectPoint));

                    $('#btnPlanRouteShow').bind('click', function() {
                        if (priv.objectType == "accommodation") {
                            location.href = global.pathPrefix + "/routepageprint.aspx?type=acco&id=" + priv.objectId + "&country=" + encodeURI(encodeURI($('#txtCountry').val())) + "&city=" + encodeURI(encodeURI($('#txtCity').val())) + "&street=" + encodeURI(encodeURI($('#txtStreet').val())) + "&postal=" + encodeURI(encodeURI($('#txtPostal').val())) + whiteLabelQuery;
                        } else {
                            location.href = global.pathPrefix + "/routepageprint.aspx?type=parc&id=" + priv.objectId + "&country=" + encodeURI(encodeURI($('#txtCountry').val())) + "&city=" + encodeURI(encodeURI($('#txtCity').val())) + "&street=" + encodeURI(encodeURI($('#txtStreet').val())) + "&postal=" + encodeURI(encodeURI($('#txtPostal').val())) + whiteLabelQuery;
                        }
                    });
                }
            }
        },

        planRoute: function(objectPoint) {
            $('#route').html('');
            $('div.errors').html('');
            if (!($('#txtPostal').val() != '' || ($('#txtStreet').val() != '' && $('#txtCity').val() != ''))) {
                $('div.errors').html('Vul eerst de postcode of straat en plaats in waarvan u vertrekt.');
            } else {
                var fromAddress = "";
                if ($('#txtPostal').val() != '') {
                    fromAddress = $('#txtPostal').val() + ', ' + $('#txtCountry').val();
                } else {
                    if ($('#txtStreet').val() != '') {
                        fromAddress = $('#txtStreet').val();
                    }
                    if ($('#txtCity').val() != '') {
                        fromAddress += fromAddress == '' ? $('#txtCity').val() : ', ' + $('#txtCity').val();
                    }
                    fromAddress += ', ' + $('#txtCountry').val();
                }
                var toAddress = objectPoint.lat() + "," + objectPoint.lng();
                priv.googleMapDirections.load("from: " + fromAddress + " to: " + toAddress, { "locale": "nl_NL" });
                window.print();
            }
        },

        googleMapsDirectionErrors: function() {
            if (priv.googleMapDirections.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
                $('div.errors').html('De route kon niet worden bepaald, omdat het ingevoerde adres niet is gevonden.');
            }
            else {
                $('div.errors').html('Er is een fout opgetreden bij het bepalen van de route.');
            }
        },

        bindEvents: function() {

        }

    };

    return {

        printObject: function() {
            var whiteLabelQuery = (($(".whiteLabelField").attr("value") != "") ? ("&WhiteLabel=" + $(".whiteLabelField").attr("value")) : "");

            if (priv.objectType == "accommodation") {
                location.href = global.pathPrefix + "/accopageprint.aspx?id=" + priv.objectId + whiteLabelQuery;
            } else {
                location.href = global.pathPrefix + "/parcpageprint.aspx?id=" + priv.objectId + whiteLabelQuery;
            }
        },

        printRoute: function() {
            priv.planRoute(new GLatLng($('#hidLatitude').val(), $('#hidLongitude').val()));
        },

        saveObject: function() {
            var $button = $('.knop_vakantiemakelaar');
            var type = $button.hasClass('saved') ? '1' : '0';

            $.ajax({
                type: "POST",
                url: global.pathPrefix + '/js/ajax/saveobject.ashx',
                data: 'objectId=' + priv.objectId + '&type=' + type + '&option=detail',
                cached: false,
                async: true,

                success: function(result) {
                    if (result.indexOf('Redirect') != -1) {
                        document.location.href = global.pathPrefix + '/LoginPage.aspx';
                    } else
                        if (result.indexOf('Error') != -1) {
                        alert('Er is een fout opgetreden bij het opslaan.');
                    } else {
                        if (type == '0') {
                            $button.parent().remove();
                            alert('De woning is opgeslagen in uw bewaarde woningen.');
                        } 
                    }
                },
                error: function(result) {
                    alert('Er is een fout opgetreden bij het opslaan.');
                }
            });
        },

        saveSearchObject: function(id, type) {
            var $button = $('a[id=bewaar'+id+'] .knop_bewaar');
            var $link = $('a[id=bewaar'+id+']');
            var type = $button.hasClass('saved') ? '1' : '0';

            $.ajax({
                type: "POST",
                url: global.pathPrefix + '/js/ajax/saveobject.ashx',
                data: 'objectId=' + id + '&type=' + type + '&option=search',
                cached: false,
                async: true,

                success: function(result) {
                    if (result.indexOf('Redirect') != -1) {
                        document.location.href = global.pathPrefix + '/LoginPage.aspx';
                    } else
                        if (result.indexOf('Error') != -1) {
                        alert('Er is een fout opgetreden bij het opslaan.');
                    } else {
                    if (type == '0') {
                            $link.remove();     
                            alert('De woning is opgeslagen in uw bewaarde woningen.');
                        } 
                    }
                },
                error: function(result) {
                    alert('Er is een fout opgetreden bij het opslaan.');
                }
            });
        },

        selectPhotoPrev: function() {
            priv.photoCarousel.Prev();
        },

        selectPhotoNext: function() {
            priv.photoCarousel.Next();
        },
        
        selectPhotoPrevTab: function() {
            priv.photoCarouselTab.Prev();
        },

        selectPhotoNextTab: function() {
            priv.photoCarouselTab.Next();
        },

        initPhotoCarousel: function() {

            if ($('div.carousel-wrapper').length) {
                priv.photoCarousel = new PCarousel({ "speed": 500, "listItem": $("div.carousel-wrapper ul").get(0), "prevButton": $("div.carousel-wrapper .lnk-left").get(0), "nextButton": $("div.carousel-wrapper .lnk-right").get(0), "direction": "horizontal", "continues": true, "callback":
                    function() {
                        var currentItem = $(priv.photoCarousel.GetCurrentItem());
                        var data = currentItem.find('img').attr('title').split('|');
                        if (data[0] === 'Foto') {
                            $('#video').html('');
                            $('#multimedia-large #image img').attr('src', data[1]);
                            $('#image').css('display', 'block');
                            $('#video').css('display', 'none');
                            $('#photo360').css('display', 'none');
                            $('#photo360').html('');
                        }
                        if (data[0] === 'Filmpje') {
                            $('#video').html('');
                            $('#image').css('display', 'none');
                            $('#video').css('display', 'block');
                            $('#photo360').css('display', 'none');
                            $('#photo360').html('');
                            if(data[1].indexOf('.flv') != -1)
                            {
                                var videoObj = $('<a href=\"' + data[1] + '\" style=\"display:block;width:509px;height:327px;\" id=\"flvplayer\"></a>');
                                $('#video').html('').append(videoObj);
                                flowplayer("flvplayer", global.pathPrefix + "/flash/flowplayer-3.1.5.swf"); 
                            }
                            else
                            {
                                var videoObj = $('<object id=\"mediaPlayer\" width=\"509\" height=\"327\" classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\"><param name=\"url\" value=\"' + data[1] + '\" /><embed type=\"application/x-mplayer2\" autostart=\"true\" stretchtofit=\"true\" src=\"' + data[1] + '\" name=\"mediaPlayer\" width=\"509\" height=\"327\" /></object>');
                                $('#video').html('').append(videoObj);
                            }
                        }
                        if (data[0] === '360graden foto') {
                            $('#video').html('');
                            var photo360 = $("<div style=\"width:509px;height:327px;overflow:hidden;border:none;position:relative;\"><iframe src=\"" + data[1] + "\" width=\"509px\" height=\"327px\" style=\"border:none;overflow:hidden;scrolling:no;position:absolute;left:-2px;top:-2px;\"></iframe></div>");
                            $('#photo360').html('').append(photo360);
                            $('#image').css('display', 'none');
                            $('#video').css('display', 'none');
                            $('#photo360').css('display', 'block');
                        }
                    }
                });
                $("div.carousel-wrapper ul li").bind("click",
                    function(evt) {
                        priv.photoCarousel.To($(this).attr('rel'));
                    });
                    //do callback to force big image to load when carousel has too few items
                   priv.photoCarousel.TestCallback();
            }                        
        },
        
        initPhotoCarouselTab: function() {           
            
            if ($('div.carousel-wrapper-tab').length) {
                priv.photoCarouselTab = new PCarousel({ "speed": 500, "listItem": $("div.carousel-wrapper-tab ul").get(0), "prevButton": $("div.carousel-wrapper-tab .lnk-left-tab").get(0), "nextButton": $("div.carousel-wrapper-tab .lnk-right-tab").get(0), "direction": "horizontal", "continues": true, "callback":
                    function() {
                        var currentItem1 = $(priv.photoCarouselTab.GetCurrentItem());
                        var data1 = currentItem1.find('img').attr('title').split('|');
                        if (data1[0] === 'Foto') {
                            $('#video-tab').html('');
                            $('#multimedia-large-tab #image-tab img').attr('src', data1[1]);
                            $('#image-tab').css('display', 'block');
                            $('#video-tab').css('display', 'none');
                            $('#photo360-tab').css('display', 'none');
                            $('#photo360-tab').html('');
                        }
                        if (data1[0] === 'Filmpje') {
                            $('#video-tab').html('');
                            $('#image-tab').css('display', 'none');
                            $('#video-tab').css('display', 'block');
                            $('#photo360-tab').css('display', 'none');
                            $('#photo360-tab').html('');
                            if(data1[1].indexOf('.flv') != -1)
                            {
                                var videoObj1 = $('<a href=\"' + data1[1] + '\" style=\"display:block;width:509px;height:327px;\" id=\"flvplayer-tab\"></a>');
                                $('#video-tab').html('').append(videoObj1);
                                flowplayer("flvplayer-tab", global.pathPrefix + "/flash/flowplayer-3.1.5.swf"); 
                            }
                            else
                            {
                                var videoObj1 = $('<object id=\"mediaPlayer\" width=\"509\" height=\"327\" classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\"><param name=\"url\" value=\"' + data1[1] + '\" /><embed type=\"application/x-mplayer2\" autostart=\"true\" stretchtofit=\"true\" src=\"' + data1[1] + '\" name=\"mediaPlayer\" width=\"509\" height=\"327\" /></object>');
                                $('#video').html('').append(videoObj1);
                            }
                        }
                        if (data1[0] === '360graden foto') {
                            $('#video-tab').html('');
                            var photo3601 = $("<div style=\"width:509px;height:327px;overflow:hidden;border:none;position:relative;\"><iframe src=\"" + data1[1] + "\" width=\"509px\" height=\"327px\" style=\"border:none;overflow:hidden;scrolling:no;position:absolute;left:-2px;top:-2px;\"></iframe></div>");
                            $('#photo360-tab').html('').append(photo3601);
                            $('#image-tab').css('display', 'none');
                            $('#video-tab').css('display', 'none');
                            $('#photo360-tab').css('display', 'block');
                        }
                    }
                });
                $("div.carousel-wrapper-tab ul li").bind("click",
                    function(evt) {
                        priv.photoCarouselTab.To($(this).attr('rel'));
                    });
                    //do callback to force big image to load when carousel has too few items
                   priv.photoCarouselTab.TestCallback();
            }
        },

        initializeGoogleMaps: function() {
            priv.initGoogleMaps();
        },

        OnReady: function() {
            priv.objectId = $('#objectId').attr('name');
            priv.objectType = $('#objectType').attr('name');

            priv.bindEvents();
            priv.initGoogleMaps();

            if ($('div.detailmenu').length) {
                $('div.detailmenu ul li a').each(function(i) {
                    if (location.href.indexOf($(this).attr('href')) != -1) {
                        if (location.href.indexOf('view=') == -1 || $(this).attr('href').indexOf('view=') != -1) {
                            $(this).parent().addClass('orange');
                        }
                    }
                });
            }

            if (priv.objectType == "accommodation") {
                // save accommodation to viewed items
                PersonalItems.Load("alreadyviewed");
                PersonalItems.Add("alreadyviewed", priv.objectId);
            }
        }

    };
} (jQuery);
