/*
Class: Land Detail Box
Used for displaying the Land Details for My Selection.
Thankyou Slimbox for the inspiraion for this little Diddy - http://www.digitalia.be/software/slimbox
Note:
The Detail Box requires an XHTML doctype.
Arguments:
element 	- the knob container
options 	- see Options below
data		- Contains the content to be displayed in the detail box
Options:
mode 		- either 'browse' or 'detail'. Defaults to "detail"
id			- The Holders ID name. Default is "detailHolder"
width		- Modal Box Width
height		- Modal Box Height
className	- Class name.
dataType	- 'Json'.
*/

var apartmentDetail = new Class({
    options: {
        mode: 'details',
        id: 'detailHolder',
        width: 250,
        height: 250,
        className: null,
        dataType: Object,
        container: null,
        google: false,
        fixed: false,
        fixTop: 0,
        fixLeft: 0
    },
    initialize: function(el, options, data) {
        this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
        this.eventPosition = this.positions.bind(this);

        this.setOptions(options);

        this.options.container = $(el);

        if (!this.options.fixed) marginLeft = -(this.options.width / 2);
        else marginLeft = this.options.fixLeft;

        this.browseStyle = { 'width': this.options.width, 'height': this.options.height, 'marginLeft': marginLeft };

        if ($(this.options.id + '_content')) $(this.options.id).empty();

        this.holder = new Element('div', { 'id': this.options.id + '_content' }).setHTML(data).injectInside(this.options.container);

        this.header = new Element('div', { 'id': 'header' }).injectInside(this.holder);
        this.closeButt = new Element('a', { 'class': 'closeButton' }).setHTML('close').injectInside(this.header);
        this.header_ul = new Element('ul').injectInside(this.header);
        this.header_li1 = new Element('li').injectInside(this.header_ul);
        this.lotName = new Element('h4').injectInside(this.header_li1);
        this.price = new Element('li').addClass('price').injectInside(this.header_ul);
        this.header_li3 = new Element('li').addClass('controls').injectInside(this.header_ul);
        this.interest = new Element('a').addClass('interest').setHTML('Register Interest').injectInside(this.header_li3);
        // this.share		= new Element('a', {'title':'Share with a friend'}).addClass('share').setHTML('Share Details').injectInside(this.header_li3);
        this.Save = new Element('a', { 'id': 'actionButton' }).setHTML('Save to MyList').injectInside(this.header_li3);

        this.middle = new Element('div', { 'id': 'middle' }).injectInside(this.holder);
        this.middleHold = new Element('div', { 'id': 'middleHolder' }).injectInside(this.middle);

        this.extras = new Element('p').addClass('extras').injectInside(this.middleHold);
        this.Status = new Element('span').addClass('status').setHTML('Property Status:&nbsp;&nbsp;').injectInside(this.extras);
        this.statMark = new Element('img').injectInside(this.Status)
        this.StatusText = new Element('span').injectInside(this.Status);
        this.bedCount = new Element('span').addClass('bed').injectInside(this.extras);
        this.bathCount = new Element('span').addClass('bath').injectInside(this.extras);
        this.levels = new Element('span').addClass('levels').injectInside(this.extras);
        this.carSpace = new Element('span').addClass('car').injectInside(this.extras);
        this.Size = new Element('span').addClass('size').injectInside(this.extras);
        this.envelope = new Element('span').addClass('envelope').injectInside(this.extras);

        this.detailMenu = new Element('div', { 'id': 'detailMenu' }).injectInside(this.middleHold);
        this.menu = new Element('ul').injectInside(this.detailMenu);

        this.overview = new Element('li').addClass('current').injectInside(this.menu);
        this.overview_link = new Element('a').setHTML('Overview').injectInside(this.overview);
        this.overview_link.onclick = this.viewChange.bind(this, [this.overview, 'overview']);
        this.overviewMarker = new Element('img', { 'src': config['baseURI'] + 'SiteAssets/images/content/g-detailMenu_marker.gif' }).injectInside(this.overview_link);

        //		this.facades				= new Element('li').injectInside(this.menu);
        //		this.facades_link			= new Element('a').setHTML('Facades').injectInside(this.facades);
        //		this.facades_link.onclick 	= this.viewChange.bind(this, [this.facades, 'facades']);
        //
        //		this.facadesMarker			= this.overviewMarker.clone().injectInside(this.facades_link);

        this.floorplan = new Element('li').injectInside(this.menu);
        this.floorplan_link = new Element('a').setHTML('Floorplan').injectInside(this.floorplan);
        this.floorplan_link.onclick = this.viewChange.bind(this, [this.floorplan, 'floorplan']);
        this.floorplanMarker = this.overviewMarker.clone().injectInside(this.floorplan_link);

        this.floorplate = new Element('li').injectInside(this.menu);
        this.floorplate_link = new Element('a').setHTML('Floorplate').injectInside(this.floorplate);
        this.floorplate_link.onclick = this.viewChange.bind(this, [this.floorplate, 'floorplate']);
        this.floorplateMarker = this.overviewMarker.clone().injectInside(this.floorplate_link);

        this.inclusions = new Element('li').injectInside(this.menu);
        this.inclusions_link = new Element('a').setHTML('Inclusions').injectInside(this.inclusions);
        this.inclusions_link.onclick = this.viewChange.bind(this, [this.inclusions, 'inclusions']);
        this.inclusionsMarker = this.overviewMarker.clone().injectInside(this.inclusions_link);

        this.specifications = new Element('li').injectInside(this.menu);
        this.specifications_link = new Element('a').setHTML('Specifications').injectInside(this.specifications);
        this.specifications_link.onclick = this.viewChange.bind(this, [this.specifications, 'specifications']);
        this.specificationsMarker = this.overviewMarker.clone().injectInside(this.specifications_link);

        this.galllery = new Element('li').injectInside(this.menu);
        this.galllery_link = new Element('a').setHTML('Gallery').injectInside(this.galllery);
        this.galllery_link.onclick = this.viewChange.bind(this, [this.galllery, 'gallery']);
        this.gallleryMarker = this.overviewMarker.clone().injectInside(this.galllery_link);

        this.detailCont = new Element('div', { 'id': 'detailContent' }).injectAfter(this.detailMenu);
        this.contentHold = new Element('div').addClass('contentPanel').injectInside(this.detailCont);

        this.homeTitle = new Element('h4').setHTML('Beautiful styled property perfect for view').injectInside(this.contentHold);
        this.descript = new Element('p', { 'id': 'descript' }).injectInside(this.contentHold);

        this.imgPanel = new Element('div', { 'id': 'imagePanel' }).injectBefore(this.contentHold);

        this.dvImage = new Element('div', { 'id': 'dvImage' }).injectInside(this.imgPanel);
        this.imgFPlan = new Element('img', { 'id': 'imgFPlan' }).injectInside(this.dvImage);

        //this.dvFPlate 	 = new Element('div', {'id':'dvFloorPlan'}).setStyle('display','none').injectInside(this.dvImage);
        this.imgFPlate = new Element('img', { 'id': 'imgFPlate' }).setStyle('display', 'none').injectInside(this.dvImage);

        this.showEstate = new Element('p').addClass('designs').setHTML('Estate: ').injectInside(this.imgPanel);
        this.estateTxt = new Element('strong').injectInside(this.showEstate);
        this.showComplex = new Element('p').addClass('designs').setHTML('Complex: ').injectInside(this.imgPanel);
        this.complexTxt = new Element('strong').injectInside(this.showComplex);

        //actions Panel
        this.actions = new Element('div').addClass('actionsPanel').injectAfter(this.middleHold);
        this.actPan1 = new Element('div').addClass('actPan1').setHTML('&nbsp;').injectInside(this.actions);
        this.actPan3 = new Element('div').addClass('actPan3').injectInside(this.actions);
        //this.actPan3 	= new Element('div').addClass('').injectBefore(this.imgPanel);
        this.actPan2 = new Element('div').addClass('actPan2').setHTML('&nbsp;').injectInside(this.actions);
        this.pdf_file = new Element('a', { 'href': '#' }).addClass('downloadPDF').setHTML('Download PDF').injectInside(this.actPan2);
        this.control = new Element('span').setHTML('Images:&nbsp;&nbsp;').injectInside(this.actPan3);
        this.viewFPlan = new Element('a', { 'href': 'javascript:void(0);' }).addClass('viewFPlan').setHTML('Floor Plan').injectInside(this.actPan3);
        this.viewFPlan.onclick = this.viewChange.bind(this, [this.overview, 'overview']);
        this.viewFPlate = new Element('a', { 'href': 'javascript:void(0);' }).addClass('viewFPlat').setHTML('Floor Plate').injectInside(this.actPan3);
        this.viewFPlate.onclick = this.viewChange.bind(this, [this.overview, 'overview']);
        //		this.viewFPlan.addEvent('click', function(){
        //		 		if($('imgFPlan').getStyle('display') == 'none') $('imgFPlan').setStyle('display', 'block');
        //		 		$('imgFPlate').setStyle('display', 'none');
        //		 	});
        //		this.viewFPlate.addEvent('click', function(){
        //		 		if($('imgFPlate').getStyle('display') == 'none') $('imgFPlate').setStyle('display', 'block');
        //		 		$('imgFPlan').setStyle('display', 'none');
        //		 	});

        //Detail Panel Footer
        this.footer = new Element('div', { 'id': 'footer' }).injectAfter(this.middle);
        //this.fits = new Element('p').addClass('fits').setHTML('&nbsp;').injectInside(this.footer);

        //Overlay used in browse mode
        this.overlay = new Element('div', { 'id': this.options.id + '_overlay' }).injectInside(document.body);

        this.fx = {
            overlay: this.overlay.effect('opacity', { duration: 500 }).hide(),
            holder: this.holder.effect('opacity', { duration: 500 }).hide(),
            // fullMap: this.mapView.effect('height', {duration: 500, transition: Fx.Transitions.quartInOut}).hide(),
            control: this.imgPanel.effect('opacity', { duration: 500 }).set(0)
        };

        if (this.options.mode == 'detail' || this.options.mode == 'browse') {
            // this.map.setStyle('display','block');
            if (this.options.mode == 'detail') $('itemNav').removeClass('none');
            this.fx.control.set(1);
        }
        if (this.options.mode == 'browse') {
            this.holder.setStyles(this.browseStyle);
            // this.fx.holder.hide();
        }

    },
    updateDetail: function(obj, key) {
        // $(this.options.id).empty();
        Apartment.currentItem = key;

        //bind Event to Links
        this.overview_link.removeEvents('click').addEvent('click', function() { this.switchContent('overview', obj) } .bind(this));
        this.floorplan_link.removeEvents('click').addEvent('click', function() { this.switchContent('plan', obj) } .bind(this));
        this.floorplate_link.removeEvents('click').addEvent('click', function() { this.switchContent('plate', obj) } .bind(this));
        this.viewFPlan.removeEvents('click').addEvent('click', function() { this.switchContent('viewFPlan', obj) } .bind(this));
        this.viewFPlate.removeEvents('click').addEvent('click', function() { this.switchContent('viewFPlate', obj) } .bind(this));
        if ($chk(obj.tblGallary)) {
            this.galllery.setStyle('display', 'block');
            this.galllery_link.removeEvents('click').addEvent('click', function() { this.switchContent('gallery', obj) } .bind(this));
        } else {
            this.galllery.setStyle('display', 'none');
        }
        this.specifications_link.removeEvents('click').addEvent('click', function() { this.switchContent('specifications', obj) } .bind(this));
        //this.specifications_link.setProperty('href','../PDF/createPDF.aspx?param=1,' + obj.APPT_nId.toString());


        if ($chk(obj.tblInclusion)) {
            this.inclusions.setStyle('display', 'block');
            this.inclusions_link.removeEvents('click').addEvent('click', function() { this.switchContent('inclusions', obj) } .bind(this));
        } else {
            this.inclusions.setStyle('display', 'none');
        }
        if (Site.sUser.uDetails()) {
            if (obj.iRegistered.toInt() == 1) {
                this.interest.addClass('registered').setProperty('title', 'You have registered interest in ' + obj.APPT_strName).onclick = this.actionRegister.bind(this, true);
            } else {
                this.interest.removeClass('registered').setProperty('title', 'Register your interest in ' + obj.APPT_strName).onclick = this.actionRegister.bind(this, false);
            }
            $$('.detailLabel-tip').each(function(el) { el.remove(); });
        } else {
            this.interest.removeClass('registered').setProperty('title', 'Sign up or Sign In::to Register Interest');
            //Setting the toolTip for Register Interest.
            $$('.detailLabel-tip').each(function(el) { el.remove(); });
            var Tips1 = new Tips($$(this.interest), {
                offsets: { 'x': -27, 'y': -68 },
                className: 'detailLabel',
                fixed: true
            });
        }
        this.Save.setProperty('title', 'Save ' + obj.APPT_strName);
        this.lotName.setHTML(obj.APPT_strName);
        this.price.setHTML(obj.APPT_curPrice);

        this.bedCount.setHTML('Bed: ' + obj.APPT_nNumberOfBedrooms);
        this.bathCount.setHTML('Bath: ' + obj.APPT_nNumberOfBathrooms);
        //this.levels.setHTML('Level: ' + obj.APPT_nLocatedOnLevel);
         this.levels.setHTML('Level: ' + obj.APPT_strLevelName);
        this.carSpace.setHTML('Car: ' + obj.APPT_nParkingSpace);
        this.Size.setHTML('Size: ' + obj.APPT_nLivingArea + 'm&sup2;');

        this.estateTxt.setHTML(obj.EST_strName);
        this.complexTxt.setHTML(obj.COM_strComplexName);

        this.statMark.setProperty('src', config['baseURI'] + 'SiteAssets/images/content/g-' + obj.SLST_strName.toLowerCase() + 'Marker.gif')
        this.StatusText.addClass(obj.SLST_strName.toLowerCase()).setHTML('&nbsp;&nbsp;' + obj.SLST_strName);

        if (myList.list != null && myList.list.ApartmentIds.contains(obj.APPT_nId.toInt()) == true) {
            this.Save.setHTML('Saved').addClass('saved').onclick = function() { mySelection.Delete('AptIds', obj.LOT_nId, obj.LOT_strName); };
        } else {
            this.Save.setHTML('Save').removeClass('saved').onclick = function() { mySelection.save(obj.APPT_nId, obj.APPT_strName); };
        }
        //this.pdf_file.setProperty('href', '../pdf/createPDF.aspx?param=1,'+obj.APPT_nId);
        //this.pdf_file.setProperty('href', '../utilities/LauchAsset.aspx?pdf=1&param=1,'+  obj.APPT_nId);
        //Initialize pdf links
        downloadPDFRecover();
        this.pdf_file.removeEvents('click').addEvent('click', function() {
            if (this.hasClass('downloadPDF')) {
                window.location.href = '../pdf/createpdf.aspx?param=1,' + obj.APPT_nId;
                timeout = setTimeout("downloadPDFRecover()", 30000);
                //disable link here:
                this.removeClass('downloadPDF').addClass("downloadPDFdisabled").setText('Downloading PDF ...');
            }
        });

        //this.homeTitle.setHTML(obj.APPT_strOverviewTitle);
        //this.descript.setHTML(obj.APPT_strOverviewTrimmed);
        this.switchContent('overview', obj);
        this.viewChange.bind(this, [this.overview, 'overview']);

        $$('#detailMenu li').removeClass('current');
        this.overview.addClass('current');

        if (obj.FloorPlanLocation == null && obj.tblFloorPlate == null) { this.actPan3.setStyle("display", "none"); }

        if (obj.FloorPlanLocation != null) {
            this.imgFPlan.setProperty('src', config['baseURI'] + 'Utilities/GenerateImage.aspx?path=' + obj.FloorPlanLocation + '&w=300&h=240&mode=resizefit');
            if (config.AssetVersion == 1) {
                this.imgFPlan.removeEvents('click').addEvent('click', function() { Lightbox.show(config['baseURI'] + obj.FloorPlanLocation, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
            }
            else {
                this.imgFPlan.removeEvents('click').addEvent('click', function() { Lightbox.show(obj.FloorPlanLocation, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
            }
            this.viewFPlan.setStyle("display", "inline");
            this.floorplan.setStyle("display", "block");
        } else {
            this.viewFPlan.setStyle("display", "none");
            this.floorplan.setStyle("display", "none");
        }

        if (obj.tblFloorPlate != null) {
            var picFPlate
            if (config.AssetVersion == 1) {
                if (obj.tblFloorPlate.length) {
                    picFPlate = '../Assets/Appartments/' + obj.APPT_nId + '/' + obj.tblFloorPlate[0].AASS_strFilename;
                } else {
                    picFPlate = '../Assets/Appartments/' + obj.APPT_nId + '/' + obj.tblFloorPlate.AASS_strFilename;
                }
                this.imgFPlate.setProperty('src', config['baseURI'] + 'Utilities/GenerateImage.aspx?path=' + picFPlate + '&w=300&h=240&mode=resizefit');
                this.imgFPlate.removeEvents('click').addEvent('click', function() { Lightbox.show(config['baseURI'] + picFPlate, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
            } else {
                if (obj.tblFloorPlate.length) {
                    picFPlate = obj.tblFloorPlate[0].location;
                } else {
                    picFPlate = obj.tblFloorPlate.location;
                }
                this.imgFPlate.setProperty('src', config['baseURI'] + 'Utilities/GenerateImage.aspx?path=' + picFPlate + '&w=300&h=240&mode=resizefit');
                this.imgFPlate.removeEvents('click').addEvent('click', function() { Lightbox.show(picFPlate, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
            }
            this.viewFPlate.setStyle("display", "inline");
            this.floorplate.setStyle("display", "block");
        } else {
            this.viewFPlate.setStyle("display", "none");
            this.floorplate.setStyle("display", "none");
        }

        //this.getExtras(obj.APPT_nId.toInt(), 'fits');

        var registered = (obj.iRegistered.toInt() == 1) ? true : false;
        //sending View Data to the database for Viewed History
        this.addView(1, obj.APPT_nId.toInt(), obj.APPT_strName + ' ' + obj.COM_strComplexName, registered);

        if (this.options.mode == 'browse') {
            this.setup(true);
            this.positions();
            this.fx.overlay.start(0.15);
            this.closeButt.onclick = this.close.bind(this);
            this.overlay.onclick = this.close.bind(this);
        }
        this.holder.removeClass('none')
        this.fx.holder.start(1);


    },
    viewChange: function(el, view) {
        $$('#detailMenu li').removeClass('current');
        el.addClass('current');
    },
    //TODO later: Going to write a function that can resize the lightbox picture to suit the users window size.
    lightBoxSizes: function() {
        tmpHeight = window.getHeight() * 0.8;
        tmpWidth = window.getWidth() * 1;
    },
    changeMode: function(mode) {

        this.options.mode = mode;

        switch (mode) {
            case 'browse':
                if ($('pageNav_top').getStyle('display') == 'none') $('pageNav_top').setStyle('display', 'block');

                this.holder.setStyles(this.browseStyle)//.addClass('none');

                // this.mapView.setStyle('display','none');

                if (!$('itemNav').hasClass('none')) $('itemNav').addClass('none');

                this.holder.addClass('none');

                break;

            case 'detail':
                this.holder.setStyles({ 'width': 'auto', 'marginLeft': 0, 'height': 'auto' });
                //$('pageNav_top').setStyle('display', 'none');
                if ($('itemNav').hasClass('none')) $('itemNav').removeClass('none');
                break;
        }
    },
    getExtras: function(key, type) {
        switch (type) {
            case 'fits':
                var url = config['baseURI'] + config['GetLotsForHomes'] + '?HomeIds=' + key;
                var toFill = this.fits;
                break;
            case 'looked':
                var url = config['baseURI'] + config['viewedLots'] + '?ObjectId=3&PrimaryKey=' + key;
                var toFill = this.looked;
                break;
        }

        new Ajax(url, {
            method: 'post',
            update: toFill
        }).request();
    },
    addView: function(object, key, strName, registered) {
        //Sending home information to the SOXC panel
        if (Site.sUser) {
            if (!registered && Site.sUser.regProcess != 2) Site.sUser.updateRegister(object, key, strName, { estateName: EstateName });
            Site.sUser.showAgent(object, key);
        }
        var URI = config['baseURI'] + config['addHit'];
        var query = 'ObjectId=' + object + '&PrimaryKey=' + key;
        var myXHR = new XHR({ method: 'get' }).send(URI, query);
    },
    actionRegister: function(stopAction) {
        if (stopAction == true) {
            return;
        } else {
            if (Site.sUser.uDetails() && Site.sUser.regProcess == 1) {
                Site.sUser.actionRegInterest(1);
                if (this.options.mode == 'browse') this.close();
            }
        }
    },
    positions: function() {
        this.overlay.setStyles({ 'top': window.getScrollTop(), 'height': window.getHeight() });
        if (!this.options.fixed || window.getScrollTop() > this.options.fixTop) {
            this.top = (window.getHeight() / 2) - (this.options.height / 2) + window.getScrollTop();
        } else {
            this.top = this.options.fixTop;
        }
        this.holder.setStyles({ top: this.top });
    },
    setup: function(open) {
        var fn = open ? 'addEvent' : 'removeEvent';
        window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
        document[fn]('keydown', this.eventKeyDown);
    },
    keyboardListener: function(event) {
        switch (event.keyCode) {
            case 27: this.close(); break;
        }
    },
    close: function(args) {
        this.holder.addClass('none');
        this.fx.holder.start(0);
        this.fx.overlay.start(0);
        this.setup(false);
    },
    closeOverlay: function() {
        this.fx.overlay.start(0);
        return;
    },
    switchContent: function(sec, obj) {

        //Clear
        this.detailCont.firstChild.setStyle("display", "block");
		this.contentHold.addClass('noBackground');
        this.homeTitle.empty();
        this.descript.empty();

        //Dispaly
        switch (sec) {
            case 'overview':
				this.contentHold.removeClass('noBackground');
                this.homeTitle.setHTML(obj.APPT_strOverviewTitle);
                this.descript.setHTML(obj.APPT_strOverviewTrimmed);
                this.actPan3.setStyle("display", "block");
                break;
            case 'plan':
                this.detailCont.firstChild.setStyle("display", "none");
                this.homeTitle.setHTML('Floorplan');
                this.fpImage = new Element('img').setProperty('src', config['baseURI'] + 'Utilities/GenerateImage.aspx?path=' + obj.FloorPlanLocation + '&w=450&h=200&mode=resizefit').injectInside(this.descript);
                if (config.AssetVersion == 1) {
                    this.fpImage.removeEvents('click').addEvent('click', function() { Lightbox.show(config['baseURI'] + obj.FloorPlanLocation, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); });
                } else {
                    this.fpImage.removeEvents('click').addEvent('click', function() { Lightbox.show(obj.FloorPlanLocation, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); });
                }
                this.actPan3.setStyle("display", "none");
                break;
            case 'plate':
                this.detailCont.firstChild.setStyle("display", "none");
                this.homeTitle.setHTML('Floorplate');
                if (obj.tblFloorPlate) {
                    if (obj.tblFloorPlate.length) {
                        obj.tblFloorPlate.each(function(item, index) {
                            this.ImageContainer = new Element('div').setStyles({ 'width': 160, 'height': 150, 'float': 'left' }).injectInside(this.descript);
                            //To do in the future: work out webservice to show image
                            if (config.AssetVersion == 1) {
                                this.galleryImage = new Element('img').setProperty('src', '../Utilities/GenerateImage.aspx?path=../Assets/Appartments/' + obj.APPT_nId + '/' + item.AASS_strFilename + '&w=150&h=120&mode=resizefit').injectInside(this.ImageContainer);
                                this.galleryImage.removeEvents('click').addEvent('click', function() { Lightbox.show(config.baseURI + 'Assets/Appartments/' + obj.APPT_nId + '/' + item.AASS_strFilename, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                                new Element('br').injectInside(this.ImageContainer);
                                this.galleryTitle = new Element('a').setProperty('href', 'javascript:void(0)').setHTML(item.AASS_strTitle).injectInside(this.ImageContainer);
                                this.galleryTitle.removeEvents('click').addEvent('click', function() { Lightbox.show(config.baseURI + 'Assets/Appartments/' + obj.APPT_nId + '/' + item.AASS_strFilename, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                            } else {
                                this.galleryImage = new Element('img').setProperty('src', '../Utilities/GenerateImage.aspx?path=' + item.location + '&w=150&h=120&mode=resizefit').injectInside(this.ImageContainer);
                                this.galleryImage.removeEvents('click').addEvent('click', function() { Lightbox.show(item.location, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                                new Element('br').injectInside(this.ImageContainer);
                                this.galleryTitle = new Element('a').setProperty('href', 'javascript:void(0)').setHTML(item.title).injectInside(this.ImageContainer);
                                this.galleryTitle.removeEvents('click').addEvent('click', function() { Lightbox.show(item.location, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                            }
                        }, this);
                    } else {
                        this.ImageContainer = new Element('div').setStyles({ 'width': 160, 'height': 150, 'float': 'left' }).injectInside(this.descript);
                        //To do in the future: work out webservice to show image
                        if (config.AssetVersion == 1) {
                            this.galleryImage = new Element('img').setProperty('src', '../Utilities/GenerateImage.aspx?path=../Assets/Appartments/' + obj.APPT_nId + '/' + obj.tblFloorPlate.AASS_strFilename + '&w=150&h=120&mode=resizefit').injectInside(this.ImageContainer);
                            this.galleryImage.removeEvents('click').addEvent('click', function() { Lightbox.show(config.baseURI + 'Assets/Appartments/' + obj.APPT_nId + '/' + obj.tblFloorPlate.AASS_strFilename, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                            new Element('br').injectInside(this.ImageContainer);
                            this.galleryTitle = new Element('a').setProperty('href', 'javascript:void(0)').setHTML(obj.tblFloorPlate.AASS_strTitle).injectInside(this.ImageContainer);
                            this.galleryTitle.removeEvents('click').addEvent('click', function() { Lightbox.show(config.baseURI + 'Assets/Appartments/' + obj.APPT_nId + '/' + obj.tblFloorPlate.AASS_strFilename, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                        } else {
                            this.galleryImage = new Element('img').setProperty('src', '../Utilities/GenerateImage.aspx?path=' + obj.tblFloorPlate.location + '&w=150&h=120&mode=resizefit').injectInside(this.ImageContainer);
                            this.galleryImage.removeEvents('click').addEvent('click', function() { Lightbox.show(obj.tblFloorPlate.location, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                            new Element('br').injectInside(this.ImageContainer);
                            this.galleryTitle = new Element('a').setProperty('href', 'javascript:void(0)').setHTML(obj.tblFloorPlate.title).injectInside(this.ImageContainer);
                            this.galleryTitle.removeEvents('click').addEvent('click', function() { Lightbox.show(obj.tblFloorPlate.location, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                        }
                    }
                }
                this.actPan3.setStyle("display", "none");
                break;
            case 'viewFPlan':
                this.homeTitle.setHTML(obj.APPT_strOverviewTitle);
                this.descript.setHTML(obj.APPT_strOverviewTrimmed);
                if ($('imgFPlan').getStyle('display') == 'none') $('imgFPlan').setStyle('display', 'block');
                $('imgFPlate').setStyle('display', 'none');
                this.overview.addClass('current');
                this.actPan3.setStyle("display", "block");
                break;
            case 'viewFPlate':
                this.homeTitle.setHTML(obj.APPT_strOverviewTitle);
                this.descript.setHTML(obj.APPT_strOverviewTrimmed);
                if ($('imgFPlate').getStyle('display') == 'none') $('imgFPlate').setStyle('display', 'block');
                $('imgFPlan').setStyle('display', 'none');
                this.overview.addClass('current');
                this.actPan3.setStyle("display", "block");
                break;
            case 'inclusions':
                this.homeTitle.setHTML('Inclusions');
                this.detailCont.firstChild.setStyle("display", "none");
                if ($chk(obj.APPT_strInclusion)) new Element('p').setText(obj.APPT_strInclusion).injectInside(this.descript);
                if ($chk(obj.tblInclusion)) {
                    if ($chk(obj.APPT_strInclusion)) new Element('p').setText("Download:").injectInside(this.descript); ;
                    if (config.AssetVersion == 1) {
                        if (obj.tblInclusion.length) {
                            for (i = 0; i < obj.tblInclusion.length; i++) {
                                this.inclusionText = new Element('a').setProperty('href', '../Assets/Appartments/' + obj.tblInclusion[i].AASS_APPT_nId + '/' + obj.tblInclusion[i].AASS_strFilename).setProperty('target', '_blank').setHTML(obj.tblInclusion[i].AASS_strTitle).injectInside(this.descript);
                                this.bresizeBy = new Element('br').injectInside(this.descript);
                            }
                        } else {
                            this.inclusionText = new Element('a').setProperty('href', '../Assets/Appartments/' + obj.tblInclusion.AASS_APPT_nId + '/' + obj.tblInclusion.AASS_strFilename).setProperty('target', '_blank').setHTML(obj.tblInclusion.AASS_strTitle).injectInside(this.descript);
                        }
                    } else {
                        if (obj.tblInclusion.length) {
                            for (i = 0; i < obj.tblInclusion.length; i++) {
                                this.inclusionText = new Element('a').setProperty('href', obj.tblInclusion[i].location).setProperty('target', '_blank').setHTML(obj.tblInclusion[i].title).injectInside(this.descript);
                                this.bresizeBy = new Element('br').injectInside(this.descript);
                            }
                        } else {
                            this.inclusionText = new Element('a').setProperty('href', obj.tblInclusion.location).setProperty('target', '_blank').setHTML(obj.tblInclusion.title).injectInside(this.descript);
                        }
                    }
                }
                this.actPan3.setStyle("display", "none");
                break;
            case 'specifications':
                this.detailCont.firstChild.setStyle("display", "none");
                this.homeTitle.setHTML('Specifications');
                var SpecificationsHtml = '<p>Please click the link below to download specifications:</p>';
                //SpecificationsHtml += '<a href="../PDF/createPDF.aspx?param=1,' + obj.APPT_nId + '">Download</a>';
                //		        SpecificationsHtml += '<a href="javascript:void(0)">Download</a>';
                this.specifications = new Element('div').setHTML(SpecificationsHtml).injectInside(this.descript);
                var specificationLink = new Element('a').setText('Download').injectInside(this.specifications);
                //Initialize pdf links
                downloadPDFRecover2();
                specificationLink.removeEvents('click').addEvent('click', function() {
                    if (this.getText() == "Download") {
                        window.location.href = '../pdf/createpdf.aspx?param=1,' + obj.APPT_nId;
                    }
                    timeout = setTimeout("downloadPDFRecover2()", 30000);
                    //disable link here:
                    this.setText('Downloading...').setStyle('color', '#aaa');
                });
                this.actPan3.setStyle("display", "none");
                break;
            case 'gallery':
                if ($chk(obj.tblGallary)) {
                    this.detailCont.firstChild.setStyle("display", "none");
                    //this.homeTitle.setHTML('Gallery');
                    if (obj.tblGallary) {
                        if ($chk(obj.tblGallary.length)) {
                            obj.tblGallary.each(function(item, index) {
                                this.ImageContainer = new Element('div').setStyles({ 'width': 160, 'height': 120, 'float': 'left' }).injectInside(this.descript);
                                //To do in the future: work out webservice to show image
                                if (config.AssetVersion == 1) {
                                    this.galleryImage = new Element('img').setProperty('src', '../Utilities/GenerateImage.aspx?path=../Assets/Appartments/' + item.AASS_APPT_nId + '/' + item.AASS_strFilename + '&w=150&h=120&mode=resizefit').injectInside(this.ImageContainer);
                                    this.galleryImage.removeEvents('click').addEvent('click', function() { Lightbox.show(config.baseURI + 'Assets/Appartments/' + item.AASS_APPT_nId + '/' + item.AASS_strFilename, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                                    new Element('br').injectInside(this.ImageContainer);
                                    this.galleryTitle = new Element('a').setProperty('href', 'javascript:void(0)').setHTML(item.AASS_strTitle).injectInside(this.ImageContainer);
                                    this.galleryTitle.removeEvents('click').addEvent('click', function() { Lightbox.show(config.baseURI + 'Assets/Appartments/' + item.AASS_APPT_nId + '/' + item.AASS_strFilename, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                                } else {
                                    this.galleryImage = new Element('img').setProperty('src', '../Utilities/GenerateImage.aspx?path=' + item.location + '&w=150&h=120&mode=resizefit').injectInside(this.ImageContainer);
                                    this.galleryImage.removeEvents('click').addEvent('click', function() { Lightbox.show(item.location, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                                    new Element('br').injectInside(this.ImageContainer);
                                    this.galleryTitle = new Element('a').setProperty('href', 'javascript:void(0)').setHTML(item.title).injectInside(this.ImageContainer);
                                    this.galleryTitle.removeEvents('click').addEvent('click', function() { Lightbox.show(item.location, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                                }
                            }, this);
                        } else {
                            this.ImageContainer = new Element('div').setStyles({ 'width': 160, 'height': 120, 'float': 'left' }).injectInside(this.descript);
                            //To do in the future: work out webservice to show image
                            if (config.AssetVersion == 1) {
                                this.galleryImage = new Element('img').setProperty('src', '../Utilities/GenerateImage.aspx?path=../Assets/Appartments/' + obj.tblGallary.AASS_APPT_nId + '/' + obj.tblGallary.AASS_strFilename + '&w=150&h=120&mode=resizefit').injectInside(this.ImageContainer);
                                this.galleryImage.removeEvents('click').addEvent('click', function() { Lightbox.show(config.baseURI + 'Assets/Appartments/' + obj.tblGallary.AASS_APPT_nId + '/' + obj.tblGallary.AASS_strFilename, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                                new Element('br').injectInside(this.ImageContainer);
                                this.galleryTitle = new Element('a').setProperty('href', 'javascript:void(0)').setHTML(obj.tblGallary.AASS_strTitle).injectInside(this.ImageContainer);
                                this.galleryTitle.removeEvents('click').addEvent('click', function() { Lightbox.show(config.baseURI + 'Assets/Appartments/' + obj.tblGallary.AASS_APPT_nId + '/' + obj.tblGallary.AASS_strFilename, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                            } else {
                                this.galleryImage = new Element('img').setProperty('src', '../Utilities/GenerateImage.aspx?path=' + obj.tblGallary.location + '&w=150&h=120&mode=resizefit').injectInside(this.ImageContainer);
                                this.galleryImage.removeEvents('click').addEvent('click', function() { Lightbox.show(obj.tblGallary.location, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                                new Element('br').injectInside(this.ImageContainer);
                                this.galleryTitle = new Element('a').setProperty('href', 'javascript:void(0)').setHTML(obj.tblGallary.title).injectInside(this.ImageContainer);
                                this.galleryTitle.removeEvents('click').addEvent('click', function() { Lightbox.show(obj.tblGallary.location, obj.APPT_strName + ': ' + obj.COM_strComplexName + ' - ' + obj.EST_strName); })
                            }
                        }
                    }
                }
                this.actPan3.setStyle("display", "none");
                break;
            default:
                break;
        }
    }

});
apartmentDetail.implement(new Events);
apartmentDetail.implement(new Options);

function downloadPDFRecover() {
    var link = $('middle').childNodes[1].childNodes[2].childNodes[1];
    if (link) link.removeClass('downloadPDFdisabled').addClass("downloadPDF").setText('Download PDF');
}

function downloadPDFRecover2() {
    var descript = $('descript').childNodes[0].childNodes[1];
    if (descript) descript.setText('Download').setStyle('color', '#008BDD');
}
