﻿var NativeLotDetailsZoomer;
if (typeof (NativeLotDetailsZoomer) == "undefined") {
	NativeLotDetailsZoomer = function(settings) {
		this.initNativeLotDetailsZoomer(settings);
	};
};

NativeLotDetailsZoomer.prototype.initNativeLotDetailsZoomer = function(settings) {
	this.settings = $.extend({
		carouselId: null,
		loadingImagePath: null,
		fullScreenZoomer: null,
		embeddedZoomer: null,
		zoomInButtonId: null,
		fullscreenZoomButtonId: null,
		closeZoomButtonId: null
	}, settings ||
	{});
	var evl = "this.settings.fullScreenZoomer = " + this.settings.fullScreenZoomer;
	eval(evl);
	evl = "this.settings.embeddedZoomer = " + this.settings.embeddedZoomer;
	eval(evl);
	this.settings.embeddedZoomer.setHost($('#topImgWrapper'));
	this.isZoomable = false;
	NativeLotDetailsZoomer.instances.push(this);
	var imageDescriptor = null;
	if (this.settings.imageDescriptors != null && this.settings.imageDescriptors.length > 0) {
		imageDescriptor = this.settings.imageDescriptors[0];
	}
	this.activateButtons(imageDescriptor.previewUrl);
	this.attachEventHandlers();
	this.currentImageDescriptor = null;
	if(this.settings.imageDescriptors != null && this.settings.imageDescriptors.length > 0){
		this.currentImageDescriptor = this.settings.imageDescriptors[0];
	}
	NativeLotDetailsZoomer.raiseReadyHandlers(this.settings.id);
};
NativeLotDetailsZoomer.prototype.attachEventHandlers = function() {
	var me = this;
	$("#" + this.settings.zoomInButtonId).click(function() {
		me.zoomInClicked();
	});
	if (this.settings.fullscreenZoomButtonId != null && this.settings.fullscreenZoomButtonId != "") {
		$("#" + this.settings.fullscreenZoomButtonId).click(function() {
			me.fullScreenZoomClicked();
		});
	}
	$("#" + this.settings.closeZoomButtonId).click(function() {
		me.closeZoomClicked();
	});
	var dPreload = function(){
		me.displayPreload();
	};
	var hPreload = function(){
		me.hidePreload();
	};
	$(this.settings.embeddedZoomer)
        .bind('preload.start', function(){dPreload();})
        .bind('preload.end', function(){hPreload();});
	$(this.settings.fullScreenZoomer)
        .bind('preload.start', function(){dPreload();})
        .bind('preload.end', function(){hPreload();});
};
NativeLotDetailsZoomer.readyHandlers = new Array();
NativeLotDetailsZoomer.zoomOpeningHandlers = new Array();
NativeLotDetailsZoomer.instances = new Array();
NativeLotDetailsZoomer.getInstance = function(id) {
	var instance = null;
	for (var i = 0; i < NativeLotDetailsZoomer.instances.length; i++) {
		if (NativeLotDetailsZoomer.instances[i].settings.id == id) {
			instance = NativeLotDetailsZoomer.instances[i];
			break;
		}
	}
	return instance;
};
NativeLotDetailsZoomer.raiseReadyHandlers = function(id) {
	var instance = NativeLotDetailsZoomer.getInstance(id);
	if (instance == null) {
		return;
	}
	for (var i = 0; i < NativeLotDetailsZoomer.readyHandlers.length; i++) {
		if (NativeLotDetailsZoomer.readyHandlers[i].zoomerId == id) {
			NativeLotDetailsZoomer.readyHandlers[i].handler(instance);
		}
	}
};
NativeLotDetailsZoomer.ready = function(id, fn) {
	var instance = NativeLotDetailsZoomer.getInstance(id);
	if (instance != null) {
		fn(instance);
	}
	else {
		NativeLotDetailsZoomer.readyHandlers.push({
			zoomerId: id,
			handler: fn
		});
	}
};
NativeLotDetailsZoomer.raiseZoomOpeningHandlers = function(id) {
	var instance = NativeLotDetailsZoomer.getInstance(id);
	if (instance == null) {
		return;
	}
	for (var i = 0; i < NativeLotDetailsZoomer.zoomOpeningHandlers.length; i++) {
		if (NativeLotDetailsZoomer.zoomOpeningHandlers[i].zoomerId == id) {
			NativeLotDetailsZoomer.zoomOpeningHandlers[i].handler(instance);
		}
	}
};
NativeLotDetailsZoomer.zoomOpening = function(id, fn) {
	NativeLotDetailsZoomer.zoomOpeningHandlers.push({
		zoomerId: id,
		handler: fn
	});
};
NativeLotDetailsZoomer.prototype.getImageDescriptorByKey = function(key) {
	var result = null;
	for (var i = 0; i < this.settings.imageDescriptors.length; i++) {
		var descriptor = this.settings.imageDescriptors[i];
		if (descriptor.previewUrl.toLocaleLowerCase() == key.toLocaleLowerCase()) {
			result = descriptor;
			break;
		}
	}
	return result;
};
NativeLotDetailsZoomer.prototype.setImageDescriptor = function(descriptorKey) {
	var descriptor = this.getImageDescriptorByKey(descriptorKey);
	this.activateButtons(descriptorKey);
	this.closeZoom();
	this.currentImageDescriptor = descriptor;
};

NativeLotDetailsZoomer.prototype.displayPreload = function(){
	var imgWrap = $('div.topImgWrapper');
	var img = $(imgWrap.find('img'));
	imgWrap.append($('<img src="' + this.settings.loadingImagePath + '" />')
               .addClass('preload-indicator')
               .css({
               	'position': 'absolute',
               	'top': (img.height() / 2 - 16) + 'px',
               	'left': (img.width() / 2 - 16) + 'px'
    }));
};
NativeLotDetailsZoomer.prototype.hidePreload = function(){
	var imgWrap = $('div.topImgWrapper');
	$(imgWrap).find('img.preload-indicator').remove();
};

NativeLotDetailsZoomer.prototype.openFullScreenZoom = function() {
	NativeLotDetailsZoomer.raiseZoomOpeningHandlers(this.settings.id);
	this.settings.fullScreenZoomer.displayZoom(this.currentImageDescriptor.thumbnailUrl);
};

NativeLotDetailsZoomer.prototype.openZoomIn = function() {
	NativeLotDetailsZoomer.raiseZoomOpeningHandlers(this.settings.id);
	this.settings.embeddedZoomer.displayZoom(this.currentImageDescriptor.thumbnailUrl);
};

NativeLotDetailsZoomer.prototype.closeZoom = function() {
	this.settings.embeddedZoomer.hideZoom();
	this.settings.fullScreenZoomer.hideZoom();
	this.showZoomInHideCloseButton();
};
NativeLotDetailsZoomer.prototype.hideZoomButtons = function() {
	$("#" + this.settings.zoomInButtonId).hide();
	if (this.settings.fullscreenZoomButtonId != null && this.settings.fullscreenZoomButtonId != "") {
		$("#" + this.settings.fullscreenZoomButtonId).hide();
	}
	$("#" + this.settings.closeZoomButtonId).hide();
};
NativeLotDetailsZoomer.prototype.showOpenZoomButtons = function() {
	$("#" + this.settings.zoomInButtonId).show();
	if (this.settings.fullscreenZoomButtonId != null && this.settings.fullscreenZoomButtonId != "") {
		$("#" + this.settings.fullscreenZoomButtonId).show();
	}
};
NativeLotDetailsZoomer.prototype.activateButtons = function(descriptorKey) {
	var descr = this.getImageDescriptorByKey(descriptorKey);
	this.isZoomable = false;
	this.hideZoomButtons();
	if (descr != null) {
		this.isZoomable = descr.mapXmlUrl != null && descr.mapXmlUrl != "";
	}
	if (this.isZoomable == true) {
		this.showOpenZoomButtons();
	}
};
NativeLotDetailsZoomer.prototype.closeZoomClicked = function() {
	this.closeZoom();
};

NativeLotDetailsZoomer.prototype.zoomInClicked = function() {
	$("#" + this.settings.zoomInButtonId).hide();
	$("#" + this.settings.closeZoomButtonId).show();
	this.openZoomIn();
};
NativeLotDetailsZoomer.prototype.showZoomInHideCloseButton = function() {
	this.hideZoomButtons();
	if (this.isZoomable == true) {
		this.showOpenZoomButtons();
	}
};
NativeLotDetailsZoomer.prototype.fullScreenZoomClicked = function() {
	this.openFullScreenZoom();
};




















if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();