/*****************************************************************************************************/
/*                                                                                                   */
/*                                        'HOME PAGE RESIDENCES'                                     */          
/*                                                                                                   */
/*****************************************************************************************************/

function RESIDENCES(){
	var JSObject = this;
	this.type = "newresidences"; 
	
	this.DOMDoc; //document object from thickbox window
	
	this.container; 		//residences container   :td
	this.div_container;		//residences general div :div
	this.div_box;			//residences box div 	 :div
	this.residence_div;		//first residence div 	 :div
	this.residence_table;	//first residence table  :table
	this.residences_menu;	//residences menu        :td
	this.loader;			//
	
	this.current_residence = 0; //index of current residences visible
	this.animation = false;
	this.HTML_residence_div;    //a copy of html for a residence
	
	this.JSON;				//residences information
	
	this.step = 432;        //number of pixels for a residence container
	this.no_divs = 1;
	this.logged_in;         //if user is logged in: true
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT                                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		
		this.container = $('#newresidences_cell',this.DOMDoc).get(0);
		this.div_container = $('div:first-child',this.container).get(0);
		this.div_box = $('#newresidences_box',this.DOMDoc).get(0);
		
		//first residence
		this.residence_div = $('#newresidences_container',this.DOMDoc).get(0);
		this.residence_table = $('#newresidences_table',this.DOMDoc).get(0);
		this.HTML_residence_div = $(this.residence_div).html();
		
		//right residences menu
		this.residences_menu = $('#newresidences_menu',this.DOMDoc).get(0);
		
		this.loader = $('#newresidences_loader',this.DOMDoc).get(0);
		
		//set div_box width for x overflow
		$(this.div_box).width(this.step*2);
		
		this.setMenu();
		this.setSizes();
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION SET SIZES                                           */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.setSizes = function(){
		var table = $('table[id="newresidences_table"]:eq('+(this.no_divs-1)+')',this.DOMDoc);
		var height = table.height()+5;
		table.parent().height(height);
		
		$(this.div_container).height(height);
		$(this.container).height(height);
		$(this.residences_menu).height(parseInt(height)-36);
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION SET MENU                                            */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.setMenu = function(){
		
		for (var i=0; i<this.JSON.length; i++){
			
			var residence_link = $('#residence_link',this.residences_menu).get(i);
			$(residence_link).data('index',i);
			residence_link.href = "javascript:void(0)";
			
			$(residence_link).click(function(){
											 
				if (JSObject.animation == false){
					
					$(JSObject.loader).css('display','block');
					
					JSObject.newResidence($(this).data('index'));
					
				}
			
			})
			
		}
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION NEW RESIDENCE                                       */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.newResidence = function(index){
		
		//if ($('div[id="newresidences_container"]:eq(1)',this.DOMDoc)) $('div[id="newresidences_container"]:eq(1)',this.DOMDoc).remove();
		
		$(this.div_box).append('<div id="newresidences_container" align="left" style="float:left; width:417px; height:300px; margin-right:15px;">'+this.HTML_residence_div+'</div>');
		
		this.no_divs++;
		$(this.div_box).width(this.step*this.no_divs);
		
		
		var newresidence_container = $('div[id="newresidences_container"]',this.DOMDoc).get(this.no_divs-1);
		var newresidence_table = $('#newresidences_table',newresidence_container).get(0);
		var newresidence_picture = $('#newresidences_picture',newresidence_container).get(0);
		var newresidence_title = $('#newresidences_title',newresidence_container).get(0);
		var newresidence_link = $('#newresidences_link',newresidence_container).get(0);
		
		//set title
		$(newresidence_title).html(unescape(escape(this.JSON[index].title)));
		
		//set link
		var href = this.JSON[index].details_url;

		newresidence_link.href = "javascript:void(0)";
		$(newresidence_link).data('href',href);
		$(newresidence_link).click(function(){
			
			/*if (JSObject.logged_in && JSObject.logged_in == true){
				JSObject.DOMDoc.location = $(this).data('href');	
			}
			else{
				alert("You must be logged in ")	
			}*/
			JSObject.DOMDoc.location = $(this).data('href');
			
		})
		
		
		this.setSizes();
		
		//load picture
		newsrc = this.JSON[index].picture;
		var newPicture = new Image();
		newPicture.src = newsrc;
		
		$(newresidence_container).unbind("checkImageLoad");
		$(newresidence_container).bind("checkImageLoad", function(){
								
								if (!newPicture.complete){
									return;
								}
								
								if (typeof newPicture.naturalWidth != "undefined" && newPicture.naturalWidth == 0) {
									return;	
								}

								$(this).unbind("checkImageLoad")
								clearTimeout($(this).data("imageInterval"))
								$(this).removeData("imageInterval")
								
								newresidence_picture.src = newsrc;
								
								JSObject.playAnimation();
							 });
		
		$(newresidence_container).data("imageInterval", setInterval(function(){$(newresidence_container).trigger("checkImageLoad")},10));
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION PLAY ANIMATION                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.playAnimation = function(){
		
		if (this.animation == false){
			
			$(JSObject.loader).css('display','none');
			this.animation = true;
			
			$(this.div_box).animate({'left': -JSObject.step * (JSObject.no_divs-1)},600, 'easeOutSine',function(){JSObject.endAnimation();});
		}
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION END ANIMATION                                       */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.endAnimation = function(){
		
		if (this.animation == true){
			
			this.animation = false;
			
			//$('div[id="newresidences_container"]:eq('+(this.no_divs-2)+')',this.DOMDoc).empty();
			
		}
		
	}
}
