/*Change History
-------------------------------------------------------------------------
Task ID       : #262
Date          : 2010/12/27
Developer     : Manish Sain (IRIS)
Description   : Register Bid Tooltip UserControl (Passing parameters to UC) 
Changes In    : placeBidTooltipBeforeShow()
-------------------------------------------------------------------------*/

Site.Pages.Artist = Class.create
(
    Site.Page,
    {
        constructor: function($super) {
            $super();
        },

        initialize: function($super) {
            $super();

            this.addBlocks("placeBidTooltipBeforeShow", "tracklotTooltipBeforeShow", "saveInterestTooltipBeforeShow");

        },

        domOnLoad: function($super, event) {
            $super(event);

            this.addObservers("lotLinkOnClick", "show_more_info", "hide_more_info");

            this.bioTruncator = new Site.Controllers.Truncator($('artistBioContent'));

            // setup the lot links widget
            if ($('lots-scroller-items')) {
                this.lotLinks = $('lots-scroller-items').select("a");
                this.lotListItems = $('lots-scroller-items').select("li");
                this.lotDetailsPanels = $('sidebar').select("div.lot-details");
                this.lotListImgs = $('lots-scroller-items').select("img");
                this.lotDetailsImgs = $('lot_details_container').select("img");

                this.lotListImgs.each
				(
					function(img) {
					    this.adjustImageSize(img, 60, 60);
					},
					this
				);

                this.lotDetailsImgs.each
				(
					function(img) {
					    //console.log(img.id);
					    this.adjustImageSize(img, 259, 259);
					},
					this
				);

                this.lotLinks.each
                (
                    function(link) {
                        addEvent(link, "click", this.observers.lotLinkOnClick);
                    },
                    this
                );

                this.lotDetailsPanels.each
                (
                    function(div) {
                        if (!div.hasClassName("lot-details-active"))
                            div.hide();
                        else
                            this.activeLotDetailsPanel = div;
                    },
                    this
                );
            }

            this.interestsTooltip = new Site.Widgets.Tooltip
            (
                $('tooltip-save-to-my-interests'),
                $('trigger-save-to-my-interests'),
                {
                    anchor: "left"
                }
            )

            if ($('tooltip-save-interest') && $('bt-save-to-my-interests')) {
                this.tooltipSaveInterest = new Site.Widgets.Tooltip
				(
					$('tooltip-save-interest'),
					$('bt-save-to-my-interests'),
					{
					    showOnClick: true,
					    beforeShow: this.blocks.saveInterestTooltipBeforeShow
					}
				);
            }

            if ($('more_info_div')) {
                addEvent($('more_info_link'), "click", this.observers.show_more_info);
                addEvent($('less_info_link'), "click", this.observers.hide_more_info);
            };

            this.setupRolloverController();
        },

        destroy: function($super) {
            //this.interestsTooltip.destroy();

            if ($('lots-scroller-items')) {
                this.scroller.destroy();

                this.lotLinks.each
                (
                    function(link) {
                        removeEvent(link, "click", this.observers.lotLinkOnClick);
                    },
                    this
                );
            };

            $super();
        },

        windowOnLoad: function($super, event) {
            // calculate the inner Width of the Exceptions prices scroller

            if ($('lots-scroller-items')) {
                var panelWidth = $('lots-scroller-items').select("li").inject(0, function(acc, item) { return acc + item.offsetWidth + 3 }) - 4;
                this.lotListImgs = $('lots-scroller-items').select("img");
                this.lotDetailsImgs = $('lot_details_container').select("img");

                this.lotListImgs.each
				(
					function(img) {
					    this.adjustImageSize(img, 60, 60);
					},
					this
				);

                this.lotDetailsImgs.each
				(
					function(img) {
					    //console.log(img.id);
					    this.adjustImageSize(img, 259, 259);
					},
					this
				);

                // setup the scroller for the lot link widget
                this.scroller = new Site.Widgets.Scroller
                (
                    $('lots-scroller'),
                    {
                        direction: "horizontal",
                        panelWidth: panelWidth,
                        increment: 260,
                        method: "click",
                        scrollDuration: 0.5
                    }
                );

            }

        },

        lotLinkOnClick: function(event) {
            var element = Event.element(event);
            //alert(element.id); //Repeater_PastLotsThumb_ctl02_img_PastLotsThumb
            var a = element.matchUp("a");
            var li = a.up("li");
            //alert(li.id); //Repeater_PastLotsThumb_ctl02_li_PastLotThumbNail

            if (li && a) {
                if (!li.hasClassName("active")) {
                    if (this.activeLotDetailsPanel)
                        this.activeLotDetailsPanel.hide();

                    //Repeater_PastLotsDetails_ctl00_lot_details
                    //Repeater_PastLotsDetails_ctl00_img_PastLotsMain
                    //alert(a.id);
                    //alert(a.id.replace("link", "details"));
                    var activeLotID = a.id.replace("link", "details");
                    activeLotID = activeLotID.replace("PastLotsThumb", "PastLotsDetails");
                    //alert(activeLotID);
                    this.activeLotDetailsPanel = $(activeLotID);

                    if (this.activeLotDetailsPanel)
                        this.activeLotDetailsPanel.show();

                    this.lotListItems.invoke("removeClassName", "active");
                    li.addClassName("active");
                }

            }

            if (element.blur)
                element.blur();


            Event.stop(event);
        },


        show_more_info: function(event) {
            //alert('test');
            var element = Event.element(event).id;
            $('more_info_div').show();
            //$('ellipsis').hide();
            $('show_more_p').hide();
            Event.stop(event);
        },

        hide_more_info: function(event) {
            //alert('test');
            var element = Event.element(event).id;
            element = element.sub('_close', '');
            $('more_info_div').hide();
            //$('ellipsis').show();
            $('show_more_p').show();
            Event.stop(event);
        },

        placeBidTooltipBeforeShow: function(trigger) {
            var params = trigger.href.toQueryParams();
            //TaskID#262 - START
            if (params.bob) {
            //Method Available on Bill Tooltip usercontrol aspx
                FillBidTooltipInputs(params.bob);
            }
            return;
            /*if (params) {
                //alert(params.objectid);
                if (params.objectid.length == 0) {
                    //$('absentee-bid').hide();
                    $('bt-leave-absentee-bid').hide();
                    $('bt-leave-absentee-bid-div').show();
                    $('p_AbsenteeBidMessage').innerHTML = 'Online absentee bidding for this sale has closed. Please contact the <a href="' + securerootpath + '/features/guides/buying/bid-departments.aspx">Bid Department</a> for assistance.';
                } else {
                    //if(params.saleid.length == 0){
                    //    document.location = securerootpath + "/LotFinder/AbsenteeBidding/Absentee_Bid.aspx?action=absenteebid&ObjectID=" + params.objectid;
                    //}
                    //$('absentee-bid').show();
                    $('bt-leave-absentee-bid').show();
                    $('bt-leave-absentee-bid-div').hide();
                    $('bt-leave-absentee-bid').href = securerootpath + "/LotFinder/AbsenteeBidding/Absentee_Bid.aspx?action=absenteebid&ObjectID=" + params.objectid;
                    //alert($('bt-leave-absentee-bid').href);
                }
                if (params.saleid.length == 0) {
                    //$('bid-live').hide();
                    $('bt-bid-live').hide();
                    $('bt-bid-live-div').show();
                    if (params.closeddate.length == 0) {
                        $('bt-bid-live-div').innerHTML = "<span>Bid Live</span>"
                    } else {
                        $('bt-bid-live-div').innerHTML = "<span>Bid Live <span>Register by " + params.closeddate.replace("|||", " ") + "</span></span>";
                    }
                    $('p_LiveBidMessage').innerHTML = 'Online registration for this sale has closed. Please contact the <a href="' + securerootpath + '/features/guides/buying/bid-departments.aspx">Bid Department</a> for assistance.';
                } else {
                    //if(params.objectid.length == 0){
                    //    document.location = securerootpath + "/LiveBidding/SaleRegistration.aspx?SaleID=" + params.saleid;
                    //}
                    //$('bid-live').show();
                    $('bt-bid-live').show();
                    $('bt-bid-live-div').hide();
                    $('bt-bid-live').href = securerootpath + "/LiveBidding/SaleRegistration.aspx?SaleID=" + params.saleid;
                    if (params.closeddate.length == 0) {
                        $('bt-bid-live').innerHTML = "<span>Bid Live <span></span></span>";
                    }
                    else {
                        $('bt-bid-live').innerHTML = "<span>Bid Live <span>Register by " + params.closeddate.replace("|||", " ") + "</span></span>";
                    }
                    //alert($('bt-bid-live').href);
                }
            }*/
            //TaskID#262 - END
        },

        //added 0319
        setupRolloverController: function() {
            //alert('test');
            // CHANGED: Added "hold element" to prevent rollovers from closing when place bid tooltip is moused over (08 Feb 2008)
            //this.rolloverController = new Site.Controllers.Rollover(this.grid, 'li.grid-trigger', 'div.rollover', { holdElement: $('tooltip-place-bid') } );       

            if ($("upcoming-lots")) {
                // setup place bid tooltip on place bid buttons
                this.placeBidTooltipController = new Site.Controllers.Tooltips($("upcoming-lots").select("a.bt-bid"), { showOnClick: true, width: 250, beforeShow: this.blocks.placeBidTooltipBeforeShow });
                // setup track lot tooltip on track lot buttons
                this.tracklotTooltipController = new Site.Controllers.Tooltips($('upcoming-lots').select("a.bt-track"), { showOnClick: true, beforeShow: this.blocks.tracklotTooltipBeforeShow });
            }

        },

        tracklotTooltipBeforeShow: function(trigger) {
            var params = trigger.href.toQueryParams();

            //alert('params.objectid = ' + params.objectid);
            //alert(params.saleid);
            //alert('params.elemid = ' + params.elemid);
            //alert('innerHTML = ' + $(params.elemid).innerHTML);

            //var track_objectid = params.objectid

            //if($('tracklot_' + track_objectid).innerHTML == 'Tracked Lot'){
            if ($(params.elemid).innerHTML == 'Tracked Lot') {
                document.location = securerootpath + "/MyChristies/my_tracked_lots_bids.aspx";
            }
        },

        saveInterestTooltipBeforeShow: function(trigger) {
            var params = trigger.href.toQueryParams();
            document.forms["tooltip_saveinterest"].elements["interest_name"].value = params.interest_name;
            //alert('2: ' + this.tooltipSaveInterest.triggerOnClick);
            //alert('cancelling trigger');
            //this.tooltipSaveInterest.cancelTrigger = true;

            //alert($("bt-save-to-my-interests").innerHTML);
            if ($("bt-save-to-my-interests").innerHTML.indexOf('Saved Interest') > -1) {
                document.location = securerootpath + "/MyChristies/my_interests.aspx";
            }
        },

        richSelectOnChange: function(select) {
            // would load the specified page here
            //window.location.href = "?p=" + select.value;
            window.location.href = select.value;
        },

        adjustImageSize: function(img, limitsizeW, limitsizeH) {
            var currentW = img.width;
            var currentH = img.height;
            var destW = limitsizeW;
            var destH = limitsizeH;
            var multiplier = 0;
            var layout = 'landscape';

            //determine if it's Portrait or Landscape
            if (currentH > currentW) layout = 'portrait';

            if (layout == 'portrait') {
                if (currentH <= destH) return;
                if (destH > destW) {
                    multiplier = destW / currentW;
                } else {
                    multiplier = destH / currentH;
                }
            } else {
                if (currentW <= destW) return;
                if (destH >= destW) {
                    multiplier = destW / currentW;
                } else {
                    multiplier = destH / currentH;
                }
            }
            //multiplier = destH/currentH;
            //alert('destH:' + destH + '/' + 'currentH:' + currentH + ' = ' + multiplier)

            //alert('currentW: ' + currentW);
            //alert('currentH: ' + currentH);
            //alert(layout);
            img.width = currentW * multiplier;
            img.height = currentH * multiplier;
            //alert('1.img.width: ' + img.width);
            //alert('1.img.height: ' + img.height);
            if (img.width > limitsizeW) {
                //alert('img:' + img.id + '.width = ' + img.width + '\nOld width: ' + currentW + '\nlimit width: ' + limitsizeW);
                img.width = limitsizeW;
            }
            if (img.height > limitsizeH) {
                img.heigth = limitsizeH;
            }
            //alert('2.img.width: ' + img.width);
            //alert('2.img.height: ' + img.height);
        }

    }
);


