﻿// ---------------------------------------------------------------------
// Description:		SearchDetailDialog.js
//					ClientSide Script for SearchDetailDialog.cs Control
// 
// Autor:			Herbert Granofszky
// Date:			23.10.2007
// 
// (c) by team ModulAcht. All rights reserved
// ---------------------------------------------------------------------

var SearchDetailDialog =
{
    CONTACT_PANEL_HEIGHT: 138,

    dialogCtrlId: null,
    ajaxLoaderUrl: null,
    offerId: null,
    contactDisplayed: false,
    contactPanelState: "closed",
    affiliWindow: null,

    initialize: function()
    {
        var dialog = document['dlg_' + this.dialogCtrlId];
        if (dialog == null)
        {
            setTimeout(SearchDetailDialog.initialize, 100);
            return;
        }
        dialog.addDemandArgument('dialogtype', 'SearchDetail');
        dialog.forceReload = true;

        this._isLegacyIE = (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7);

        eval(this.dialogCtrlId + ".registerCallback(" + this.dialogCtrlId + ".EVENTID_DEMANDEND, SearchDetailDialog._onDemandEnd.bind(SearchDetailDialog));");
    },

    _onHomepageLinkClick: function()
    {
        var ajaxRequest = new SmartAjax();
        ajaxRequest.serviceId = "SmabusService";
        ajaxRequest.url = this.ajaxLoaderUrl;
        ajaxRequest.appendRequest("command", "TrackHomepageLinkClick");
        ajaxRequest.appendRequest("offerId", this.offerId);
        ajaxRequest.sendRequest();
    },

    _onDemandEnd: function()
    {
        this.contactDisplayed = false;
        this.contactPanelState = "closed";
        this.contactDivTop = $('searchDetailContactDiv').positionedOffset().top;
        Event.observe('detailActionBt', 'click', this._onActionButtonClick.bind(this));
        Event.observe('detailActionBt', 'mouseover', this._onShowContactMouseOver.bind(this));
        Event.observe('detailActionBt', 'mouseout', this._onShowContactMouseOut.bind(this));
        Event.observe('detailAddToWatchlistBt', 'click', this._onAddToWatchListClick.bind(this));
        Event.observe('detailAddToWatchlistBt', 'mouseover', this._onAddToWatchListMouseOver.bind(this));
        Event.observe('detailAddToWatchlistBt', 'mouseout', this._onAddToWatchListMouseOut.bind(this));
        Event.observe('searchresult_show_contact_link', 'click', this._onActionButtonClick.bind(this));
    },

    _onShowContactMouseOver: function()
    {
        if (this._isLegacyIE)
        {
            $('detailActionBt').className = "detailActionBtBg_hover"
        }
    },

    _onAddToWatchListMouseOver: function()
    {
        $('detailAddToWatchlistBtV').className = "detailActionBtImg_hover";
        if (this._isLegacyIE)
        {
            $('detailAddToWatchlistBt').className = "detailAddToWatchListBtBg_hover"
        }
    },

    _onShowContactMouseOut: function()
    {
        if (this._isLegacyIE)
        {
            $('detailActionBt').className = "detailActionBtBg"
        }
    },

    _onAddToWatchListMouseOut: function()
    {
        $('detailAddToWatchlistBtV').className = "detailActionBtImg";
        if (this._isLegacyIE)
        {
            $('detailAddToWatchlistBt').className = "detailAddToWatchListBtBg"
        }
    },

    _onActionButtonClick: function()
    {
        if ($('actionType').value == "ShowContact")
        {

            if (this.contactPanelState == "opening" || this.contactPanelState == "loading" || this.contactPanelState == "closing")
            {
                return;
            }

            var buttonPos = $('detailActionBt').positionedOffset();


            if (!this.contactDisplayed)
            {
                this.contactDivTop = $('searchDetailContactDiv').positionedOffset().top;
                this._requestContact();
            }
            else
            {
                if (this.contactPanelState == "closed")
                {
                    this.contactDivTop = $('searchDetailContactDiv').positionedOffset().top;
                    this._openContactPanel();
                }
                else if (this.contactPanelState == "open")
                {
                    this._closeContactPanel();
                }
            }
        }
        else if ($('actionType').value == "AffiliRedirect")
        {
            if (this.affiliWindow != null && !this.affiliWindow.closed)
            {
                this.affiliWindow.close();
            }
            this.affiliWindow = window.open("about:blank", 'affiliWindow', "hotkeys=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes");
            var ajaxRequest = new SmartAjax();
            ajaxRequest.serviceId = "SmabusService";
            ajaxRequest.url = this.ajaxLoaderUrl;
            ajaxRequest.appendRequest("command", "GetAffiliRedirectUrl");
            ajaxRequest.appendRequest("offerId", this.offerId);
            ajaxRequest.onResponse = this._onRequestAffiliRedirectUrlResponse.bind(this);
            ajaxRequest.sendRequest();
        }
    },

    _onRequestAffiliRedirectUrlResponse: function(ajaxObj)
    {
        if (ajaxObj.error == null && this.affiliWindow != null)
        {
            this.affiliWindow.location.href = ajaxObj.response;
        }
        else
        {
            alert("SearchDetailDialog._onRequestAffiliRedirectUrlResponse:" + ajaxObj.error);
        }
    },

    _onAddToWatchListClick: function()
    {
        var ajaxRequest = new SmartAjax();
        ajaxRequest.serviceId = "SmabusService";
        ajaxRequest.url = this.ajaxLoaderUrl;
        ajaxRequest.appendRequest("command", "TogglePartOfferWatchList");
        ajaxRequest.appendRequest("offerId", this.offerId);
        ajaxRequest.onResponse = this._onAddToWatchListResponse.bind(this);
        ajaxRequest.sendRequest();
    },

    _onAddToWatchListResponse: function(argAjaxObj)
    {
        if (argAjaxObj.response != null)
        {
            var dialog = document['dlg_' + this.dialogCtrlId];
            dialog.hideDialog();
            window.location.reload();
        }
    },

    _requestContact: function()
    {
        this.contactPanelState = "loading";
        var ajaxRequest = new SmartAjax();
        ajaxRequest.serviceId = "SmabusService";
        ajaxRequest.url = this.ajaxLoaderUrl;
        ajaxRequest.appendRequest("command", "GetContactInfo");
        ajaxRequest.appendRequest("offerId", this.offerId);
        ajaxRequest.onResponse = this._onRequestContactResponse.bind(this);
        ajaxRequest.sendRequest();
    },

    _onRequestContactResponse: function(argAjaxObj)
    {
        if (argAjaxObj.error == null)
        {
            $('searchDetailContactContent').update(argAjaxObj.response);
            this.contactDisplayed = true;
            this._openContactPanel();
        }
        else
        {
            this.contactPanelState = "closed";
            alert("SearchDetailDialog._onRequestContactResponse:" + argAjaxObj.error);
        }
    },

    _openContactPanel: function()
    {
        this.contactPanelState = "opening";
        $('searchDetailContactDiv').style.visibility = "visible";
        $('searchDetailContactDiv').style.height = "130px";
        Effect.SlideDown('searchDetailContactDiv', {
            duration: 0.4,
            afterFinish: this._onContactDivOpenFinish.bind(this)
        });
    },

    _closeContactPanel: function()
    {
        this.contactPanelState = "closing";
        Effect.SlideUp('searchDetailContactDiv', {
            duration: 0.4,
            afterFinish: this._onContactDivCloseFinish.bind(this)
        });
    },

    _onContactDivOpenFinish: function(argEffect)
    {
        this.contactPanelState = "open";
    },

    _onContactDivCloseFinish: function(argEffect)
    {
        this.contactPanelState = "closed";
    },

    _showImage: function(argImage)
    {
        $('__theImg').src = argImage;
    }
};
