/*
 * lucy
 * v1110 Detail.js
 */



var Detail = new Class({

	Extends : Application,
	
	// vars

	/////

	// ** contructor **  //////////////////////////////

	initialize : function(options){
		
		this.parent(options);
		
		this.initContactActions();
		
		this.initImages();
		
		if($('denounce-btn')){
			this.initDenounceForm();
		}
		
		if($('detail-container') == null){
			$('back-to-list').setStyle('display','none');
		}
		
		if($('back-to-list') && $('detail-container')){
			
			this.focusDetail();
			
			$('back-to-list').addEvent('click',(function(event){
				event.stop();
				this.focusBackground();
				
				$j('#detail-container').animate({
				    width: '0px'
				 }, 500, function() {
				    // Animation complete.
					 $('detail-container').set('html','<div class="crl-loading"></div>');
					 $$('#detail-container .crl-loading').setStyle('display','none');
					 
				});
				
			}).bind(this));
			
			window.addEvent('keydown',(function(event){
				
				if($j('#jquery-overlay').css('opacity') > 0) return;
				
				if(event.key == 'esc'){
					
					event.stop();
					this.focusBackground();
					
					$j('#detail-container').animate({
					    width: '0px'
					 }, 500, function() {
					    // Animation complete.
						 $('detail-container').set('html','<div class="crl-loading"></div>');
						 $$('#detail-container .crl-loading').setStyle('display','none');
						 
					});
				}
				
			}).bind(this));
		}
		
	},

	//* end contructor **  //////////////////////////////
	
	// ** initContactActions ** //////////////////////////////
	initContactActions : function(){
		
		if($$('.view-data')){
			$$('.view-data').addEvent('click',(function(e){
				$$('.view-data').setStyle('display','none');
				$('phone-view').setStyle('display','block');
				
				var url = baseUrl + '/ads/add-one-dataview/';
				
				
				var request = new Request({

					method		: 'post',
					url			: url,
					data        : 'id=' + e.target.getProperty('id'),
					onFailure	: function(){alert('error');},
					onSuccess : this.addDataviewResponse.bind(this)

				});

				request.send();
				
				
			}).bind(this));	
		}
	
		if($('answerform')){
			$('answerform').addEvent('submit', (function (event) {
				$$('.alert').set('text','Enviando datos...');
				return this.postForm(event, this.formResponse.bind(this),false);
			}).bind(this));
		}
		
	},
	//* end initContactActions **  //////////////////////////////
	
	// ** addDataviewResponse ** //////////////////////////////
	addDataviewResponse : function(response){
		
		//alert(response); 
		//return false;
		
		$$('.view-data').setStyle('display','none');
		$('phone-view').setStyle('display','block');
	},
	//* end addDataviewResponse **  //////////////////////////////
	
	// ** formResponse /////////////////////////////////////////
	
	formResponse : function (response) {
		
		//alert(response); 
		//return false; 
		
		$$('#answerform .error').removeClass('error');
		response= this.decodeAjaxResponse(response);
		
		$$('.alert').removeClass('success');
		$$('.alert').removeClass('error');
		
		if(response.success){
			$$('#answerform .button input').setStyle('visibility','hidden');
			$$('.alert').set('html','Datos enviados');
			$$('.alert').addClass('success');
		}else{
			
			if(response['invalids']) $$('.alert').set('text','Campos obligatiorios');
			
			$$('.alert').addClass('error');
			$each(response['invalids'], function(obj, input){
				$$('#answerform input[name='+input+']').getParent('div').addClass('error');
			});
		}
		
		return false;
		
	},
	//* end formResponse **  //////////////////////////////
	
	// ** initImages /////////////////////////////////////////
	initImages : function (){
		
		if($('thumbs')){
			$$('.image-m-ct.img-0').setStyle('display','block');
			this.activeThumbs();
		}
		
		if($('images-container')){
			$$('#zoom-btn').addEvent('mouseover',function(){
				this.addClass('on');
			});
			
			$$('#zoom-btn').addEvent('mouseout',function(){
				this.removeClass('on');
			});
		}
		

		 $j('#image-m a').lightBox({
			 
			 	imageLoading  :	baseUrl + '/assets/com.jquery/plugins/lightbox/images/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
				imageBtnPrev  :	baseUrl + '/assets/com.jquery/plugins/lightbox/images/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
				imageBtnNext  :	baseUrl + '/assets/com.jquery/plugins/lightbox/images/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
				imageBtnClose :	baseUrl + '/assets/com.jquery/plugins/lightbox/images/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
				imageBlank    : baseUrl + '/assets/com.jquery/plugins/lightbox/images/lightbox-blank.gif'			// (string) Path and the name of a blank image (one pixel)
				
		 });
		 
		 this.scaleImages (319, 220, 'testImage');
	},
	//* end initImages **  //////////////////////////////
	
	// ** activeThumbs /////////////////////////////////////////
	activeThumbs : function(){
		
		$$('.imgS').addEvent('click',function(){
			
			$$('.image-m-ct').setStyle('display','none');
			
			var imgInfo = this.getProperty('id').split('_');
			$$('.image-m-ct.' + imgInfo + '').setStyle('display','block');
		});
		
	},
	//* end activeThumbs **  //////////////////////////////
	
	// ** initDenounceForm /////////////////////////////////////////
	initDenounceForm : function(){
		
		$('denounce-btn').addEvent('click',function(){
			this.setStyle('display','none');
			$$('#denounce-options #denounce-form').setStyle('display','block');
		});
		
		$$('input[name=denounce_email]').addEvent('click',function(){
			if(this.getProperty('value') == 'Tu email') this.set('value','');
		});
		
		$$('textarea[name=denounce_comment]').addEvent('click',function(){
			if(this.getProperty('value') == 'Tu comentario') this.set('value','');
		});
		
		if($('denounce-form')){
			$('denounce').addEvent('submit', (function (event) {
				$$('.button-denounce .alert-d').set('text','Enviando...');
				$$('.button-denounce input[name=submit]').setStyle('display','none');
				return this.postForm(event, this.formDenounceResponse.bind(this),false);
			}).bind(this));
		}
	},
	//* end initDenounceForm **  //////////////////////////////
	
	// ** formDenounceResponse /////////////////////////////////////////
	
	formDenounceResponse : function (response) {
		
		//alert(response); 
		//return false; 
		
		$$('#denounce .error').removeClass('error');
		response= this.decodeAjaxResponse(response);
		
		//$$('.alert').removeClass('success');
		//$$('.alert').removeClass('error');
		
		if(response.success){
				$$('#denounce-form').set('html','<h4>Datos enviados, gracias por su atención</h4>')
		}else{
			
			if(response['invalids']) {
				$$('.button-denounce input[name=submit]').setStyle('display','block');
				$$('.button-denounce .alert-d').set('text','');
				
				$each(response['invalids'], function(obj, input){
					$$('#denounce input[name='+input+']').addClass('error');
				});
			}else{
				
			
				$$('.button-denounce .alert-d').set('text','Error');
			}
		}
		
		return false;
		
	},
	//* end formDenounceResponse **  //////////////////////////////
	
	// SPECIFIC DETAIL OVERLAY FUNCTIONS
	/*
	 * focusDetail
	 */
	
	focusDetail : function(){
		 
		var overlayDiv = new Element('div',{
			
			'id':'overlayDetail',
			'styles': {
				'position': 'absolute',
				'left': 0,
				'z-index': 100,
				'background-color': '#000',
				'width': window.getWidth(),
				'height': window.getHeight(),
				'top': window.getScrollTop()
			} // /style
		});

		 $$('body').adopt(overlayDiv);
		 
		 this.windowOnResize(overlayDiv);
		 this.windowOnScroll(overlayDiv);
		 
		 var fxTweenOp = new Fx.Tween(overlayDiv, {property:'opacity', duration: 200, wait: false});
		 fxTweenOp.set(0);
		 fxTweenOp.start(0.5);
		 
		 
	},
	
	focusBackground : function(){
		 
		var overlayDiv = $('overlayDetail');
		var fxTweenOp = new Fx.Tween(overlayDiv, {property:'opacity', duration: 200, wait: false});
		fxTweenOp.set(0.5);
		fxTweenOp.start(0).chain(function(){
			// when effect is finished
			overlayDiv.dispose();
		});
	 
	},
	
	// windowOnResize //////////////////////////////
	windowOnResize : function(element){

		window.addEvent('resize', function() {
			element.setStyles({
									'width': window.getWidth(),
									'height': window.getHeight(),
									'top': window.getScrollTop()
			});
		});

		return false;

	},
	// /windowOnResize //////////////////////////////

	// windowOnScroll //////////////////////////////
	windowOnScroll : function(element){

		window.addEvent('scroll', function() {
			element.setStyle('top', window.getScrollTop());
		});

		return false;

	}
	
	/////////SPECIFIC overlay for detail page
	
});
