/*
    Behaviours for the Lot Details Page 
*/

Site.Pages.LotDetails = Class.create
(
    Site.Page,
    {
        initialize: function($super)
        {
            $super();
            
            //this.addObservers("btEnlargeZoomOnClick","btEnlargeZoomOnClick");
            this.addObservers("viewCRClick");
            this.addObservers("btPlaceBidOnClick");
            this.addObservers("btTrackLotOnClick");
        },
        
        domOnLoad: function($super, event)
        {
            $super(event);
            
            if ($('link-times'))
            {
                if ($('tooltip-times')){
                    this.tooltipTimes = new Site.Widgets.Tooltip($('tooltip-times'), $('link-times'), {offsetY: 0});
                }
            }
            
            if ($('condition-report-trigger'))
            {
                if ($('condition-report-tooltip')){
                    this.tooltipCR = new Site.Widgets.Tooltip($('condition-report-tooltip'), $('condition-report-trigger'), {offsetY: -500, showOnClick: true});
                }
            }

            if($('view_cr_layer'))
            {
                addEvent($('view_cr_layer'), "click", this.observers.viewCRClick);
            }
            
            if ($('tooltip-place-bid'))
            {
                this.placeBidTooltip = new Site.Widgets.Tooltip($('tooltip-place-bid'), $('bt-place-bid'), { offsetY: 10, offsetX: 4, showOnClick: true });
                
                addEvent($('bt-place-bid'), "click", this.observers.btPlaceBidOnClick);
                
            }
            
            if ($('tooltip-track-lot'))
            {
                if ($('bt-track-lot')) {
                    this.trackLotTooltip = new Site.Widgets.Tooltip($('tooltip-track-lot'), $('bt-track-lot'), { offsetY: 10, offsetX: 4, showOnClick: true });
                    addEvent($('bt-track-lot'), "click", this.observers.btTrackLotOnClick);
                } 
            }
            
            if($('tooltip-contact-info') && $('link-contact-info')){
                    this.tooltipContactInfo = new Site.Widgets.Tooltip($('tooltip-contact-info'), $('link-contact-info'),{ offsetY: 10, offsetX: 4, showOnClick: true });     
            }
            /*
            if ($('lotdetail_main_image'))
            {
                
                //$('lotdetail_main_image').setStyle({'visibility':'hidden'});
                
                //addEvent($('bt-enlarge-zoom'), "click", this.observers.btEnlargeZoomOnClick);
                //addEvent($('lot-image'), "click", this.observers.btEnlargeZoomOnClick);
                
            }
            */
        
        },
        
        windowOnLoad: function($super, event)
        {
            $super(event);
            
            if ($('lotdetail_main_image'))
            {
                
                var img_height  = $('lotdetail_main_image').getHeight();
                var img_width   = $('lotdetail_main_image').getWidth();
                
                var max_width 	= 340;
                
                if(img_width > 340){
                
                        var width_ratio 	= (img_width / max_width);
                        
                        var ratio = width_ratio;
                        
                        var new_width	= (img_width / ratio);
                        var new_height 	= (img_height / ratio);
                        
                        $('lotdetail_main_image').setStyle({
                                'width': new_width+'px',
                                'height': new_height+'px'
                        });
                }
                
                //$('lotdetail_main_image').setStyle({'visibility':'visible'});
                
                //addEvent($('bt-enlarge-zoom'), "click", this.observers.btEnlargeZoomOnClick);
                //addEvent($('lot-image'), "click", this.observers.btEnlargeZoomOnClick);
                if($('bt-place-bid')){
                    addEvent($('bt-place-bid'), "click", this.observers.btPlaceBidOnClick);
                }
                if($('bt-track-lot')) {
                    addEvent($('bt-track-lot'), "click", this.observers.btTrackLotOnClick);
                }
                
            }
            
            
        },
        
        viewCRClick: function(event)
        {
                $('important_cr_notice').hide();
                $('cr_agreed').show();
                Event.stop(event);
        },
        
        btPlaceBidOnClick: function(event)
        {
                $('tooltip-track-lot').hide();
                //$('tooltip-place-bid').show();
                Event.stop(event);
        },
        
        btTrackLotOnClick: function(event)
        {        
                if($('tracklotlink').innerHTML == 'Tracked Lot'){
                    document.location = "/MyChristies/my_tracked_lots_bids.aspx";
                }
                if($('tooltip-place-bid'))$('tooltip-place-bid').hide();
                //$('tooltip-track-lot').show();
                Event.stop(event);
        },
        
        
        destroy: function($super)
        {
            this.release(this.tooltipTimes);
            
            if ($('lotdetail_main_image'))
            {
                //removeEvent($('bt-enlarge-zoom'), "click", this.observers.btEnlargeZoomOnClick);
                //removeEvent($('lot-image'), "click", this.observers.btEnlargeZoomOnClick);
            }
            $super();
        },

              
        btEnlargeZoomOnClick: function(event)
        {
            var element = Event.element(event);
            
            var a = Element.matchUp(element, "a");
            
            if (a)
            {
                this.zoomPopup = window.open(a.href, "zoompopup", Site.processWindowOptions("width=650,height=550,left=c,top=c,status=no,menubar=no,scrollbars=yes,resizable=yes"));
                this.zoomPopup.focus();
            }
            
            Event.stop(event);
        },
              
        lotLinkOnClick: function(event)
        {
            
            /*
            var element = Event.element(event);
            
            if (!element.match("a"))
            {
                element = element.up("a");
            }
            
            if (id = element.href.toQueryParams().id)
            {
                var li = element.up("li");
                
                
                new Effect.Morph($('lots-overlay'), { style: { "top": (li.offsetTop - 4) + 'px' }, duration: 0.6 });
                
                new Effect.Fade
                (
                    $('lot-mock'), 
                    { 
                        duration: Site.Pages.LotDetails.DURATION_LOT_FADE,
                        afterFinish:
                            function() 
                            { 
                                $('lot-mock').removeClassName('lot-' + this.activeLotId);  
                                
                                this.activeLotId = id;
                                 
                                $('lot-mock').addClassName('lot-' + this.activeLotId); 
                                
                                $('lot-mock').hide();
                        
                                new Effect.Appear
                                (
                                    $('lot-mock'), 
                                    { 
                                        duration: Site.Pages.LotDetails.DURATION_LOT_FADE
                                    }
                                ); 
                            }.bind(this) 
                    }
                );
            }
            */
            
            Event.stop(event);
            
            element.blur(element);
        }
    }
);

    var xmlHttp;

    function GetXmlHttpObject(){ 
			    var objXMLHttp=null
			    if (window.XMLHttpRequest){
				    objXMLHttp=new XMLHttpRequest()
			    }
			    else if (window.ActiveXObject){
				    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
			    }
			    return objXMLHttp
		    }

    function conditionReport(objectid){
        var url="condition_report.aspx?ObjectID=" + objectid; 
		url += "&sid=" + Math.random() + "&SaleType=initial";;
        //alert(url);    
        xmlHttp=GetXmlHttpObject()
	    if (xmlHttp==null){
		    return
	    } 
	    xmlHttp.onreadystatechange=showConditionReport
	    xmlHttp.open("GET",url,true)
	    xmlHttp.send(null)
    }
	
	function conditionReportCustom(url){
        //var url="condition_report.aspx?ObjectID=" + objectid; 
		url += "&sid=" + Math.random();
        //alert(url);    
        xmlHttp=GetXmlHttpObject()
	    if (xmlHttp==null){
		    return
	    } 
	    xmlHttp.onreadystatechange=showConditionReport
	    xmlHttp.open("GET",url,true)
	    xmlHttp.send(null)
    }
	
	//This function is called from ConditionReportProcess() below
	function conditionReportSaleType(objectid, phonenum, countryCode, SaleType){
		//if(SaleType != 'Gold')SaleType = 'ViewCondition';
		//alert(SaleType);
		var url="condition_report.aspx?ObjectID=" + objectid; 
		url += '&confirm=true'
		url += '&SaleType=' + SaleType;
        url += '&phonenum=' + phonenum + '&countrycode=' + countryCode;
		url += '&sid=' + Math.random();
		//alert(url);
        xmlHttp=GetXmlHttpObject()
	    if (xmlHttp==null){
		    return
	    } 
	    xmlHttp.onreadystatechange=showConditionReport
	    xmlHttp.open("GET",url,true)
	    xmlHttp.send(null)
    }
	//This function is called from ConditionReportProcessCustom() below
	function conditionReportSaleTypeCustom(url, SaleType){
		//alert(SaleType);
		//alert(url);
        xmlHttp=GetXmlHttpObject()
	    if (xmlHttp==null){
		    return
	    } 
	    xmlHttp.onreadystatechange=showConditionReport
	    xmlHttp.open("GET", url, true)
	    xmlHttp.send(null)
    }
	
    function showConditionReport(){    
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){            
		    document.getElementById("condition_report").innerHTML = xmlHttp.responseText;
	     }
    }

    function conditionDisclaimer(){
        var disclaimerwin = window.open("condition_disclaimer.aspx", "ConditionDisclaimer", "width=600, height=450, scrollbars=yes")
    }
	function conditionDisclaimerCustom(url){
        var disclaimerwin = window.open(url, "ConditionDisclaimer", "width=600, height=450, scrollbars=yes")
    }
    
    var imgWindow = null;

    function ShowImage2(path){
	    //var path = img.fullpath;
	    if (path == null) return;
	    //window.showModalDialog(path, "", "center:yes; dialogWidth:960px; dialogHeight:720px;");
	 
	    //alert(path);
    	
	    imgWindow = window.open('LargeImage.aspx?image=' + path,'FullImage','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=480');
    }
    
    function ShowZoomImage(path){
	    //var path = img.fullpath;
	    if (path == null) return;
	    
	    imgWindow = window.open('ZoomImage.aspx?image=' + path,'ZoomImage','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=660');
    }
	
	//This function is called from 'condition_report.aspx.vb'
	function ConditionReportProcess(objID, SaleType){
		var CR_phone_number = document.getElementById('mobile_number');
		var CR_countryCode = document.getElementById('select_country');

		if (CR_phone_number.value == '' || CR_phone_number.value == null || CR_phone_number.value == ' '){
			alert('Please provide your phone contact details.');
			CR_phone_number.focus();
			return false;
		}

		if (CR_countryCode.selectedIndex == 0) {
			alert('Please select a Country.');
			CR_countryCode.focus();
			return false;
		}
		
		var selectedValue = CR_countryCode.options[CR_countryCode.selectedIndex].value;
		conditionReportSaleType(objID, CR_phone_number.value, selectedValue, SaleType);
		return true;
	}
	//This function is called from 'condition_report.aspx.vb'
	function ConditionReportProcessCustom(objID, SaleType){
		var CR_phone_number = document.getElementById('mobile_number');
		var CR_countryCode = document.getElementById('select_country');

		if (CR_phone_number.value == '' || CR_phone_number.value == null || CR_phone_number.value == ' '){
			alert('Please provide your phone contact details.');
			CR_phone_number.focus();
			return false;
		}

		if (CR_countryCode.selectedIndex == 0) {
			alert('Please select a Country.');
			CR_countryCode.focus();
			return false;
		}
		
		var countryCode = CR_countryCode.options[CR_countryCode.selectedIndex].value;
		var phoneNum = CR_phone_number.value
		if(SaleType != 'Gold')SaleType = 'ViewCondition';
		var url="../condition_report.aspx?ObjectID=" + objID; 
		url += '&style=custom';
		url += '&confirm=true'
		url += '&SaleType=' + SaleType;
        url += '&phonenum=' + phoneNum + '&countrycode=' + countryCode;
		url += '&sid=' + Math.random();
		//alert(url);
		//objectid, phonenum, countryCode, SaleType
		conditionReportSaleTypeCustom(url, SaleType);
		return true;
	}
