// properties.class.js

function _properties_class_register_events() {
    var _portfolio_view_switch_link = document.getElementById("portfolio_view_switch_link");
	if(_portfolio_view_switch_link) {
		_portfolio_view_switch_link.onclick = _properties_class_portfolio_view_switch;
	}
    
	var _portfolio_category_images = document.getElementsByTagName("div");
	if(_portfolio_category_images && _portfolio_category_images.length && _portfolio_category_images.length > 0) {
		for(var i=0,ic=_portfolio_category_images.length;i<ic;i++) {
			if(_portfolio_category_images[i] && _portfolio_category_images[i].getAttribute) {
				if(_portfolio_category_images[i].getAttribute("name") == "portfolio_category_image") {
                    
                    // alert("found element with the correct element name: portfolio_category_image, "+_portfolio_category_images[i].getAttribute("image_over"));
                    
					(function() {
						var _element    = _portfolio_category_images[i];
						var _image_over = _portfolio_category_images[i].getAttribute("image_over");
						var _image_out  = _portfolio_category_images[i].getAttribute("image_out");
						
						if((_image_over && _image_over.length && _image_over.length > 0) && (_image_out && _image_out.length && _image_out.length > 0)) {
                            
                            // alert("found element with the correct element name: portfolio_category_image, "+_portfolio_category_images[i].getAttribute("image_over"));
                            
                            _portfolio_category_images[i].onclick     = function() { var __element = _element; var __image = _image_over; _properties_class_background_image_swap(__element,__image); };
							_portfolio_category_images[i].onmouseover = function() { var __element = _element; var __image = _image_over; _properties_class_background_image_swap(__element,__image); };
							_portfolio_category_images[i].onmouseout  = function() { var __element = _element; var __image = _image_out;  _properties_class_background_image_swap(__element,__image); };
						}
					})();
				}
			}
		}
	}
	
	var _property_item_image_large = document.getElementById("properties_item_image_large");
	if(_property_item_image_large) {
		var _property_item_image_alts = [];
		
		_property_item_image_alts[0] = document.getElementById("properties_item_image_alt_01");
		_property_item_image_alts[1] = document.getElementById("properties_item_image_alt_02");
		_property_item_image_alts[2] = document.getElementById("properties_item_image_alt_03");
		
		for(var i=0,ic=_property_item_image_alts.length;i<ic;i++) {
			if(_property_item_image_alts[i]) {
				(function() {
					var _element = _property_item_image_large;
					var _image   = _property_item_image_alts[i].getAttribute("image");
					
					_property_item_image_alts[i].onclick = function() { var __element = _element; var __image = _image; _properties_class_background_image_swap(__element,__image); }
					_property_item_image_alts[i].onmouseover = function() { var __element = _element; var __image = _image; _properties_class_background_image_swap(__element,__image); }
					
					// _property_item_image_alts[i].onclick = function() { var __element = _element; var __image = _image; _properties_class_background_image_swap(__element,__image); }
				})();
			}
		}
	}
}

function _properties_class_background_image_swap(element_id,image_file) {
    // alert("_properties_class_background_image_swap("+element_id+","+image_file+")");
    
	if(element_id && (image_file && image_file.length > 0)) {
		var _img = new Image();
		_img.onload = function() { _properties_class_background_image_update(element_id,image_file); };
        // .src needs to be defined after onload for IEs sake as otherwise, the second time this function is called, IE will not cause the onload event to be triggered
        // by specifying onload first, the onload event is triggered correctly
		_img.src = image_file; 
	}
}

function _properties_class_background_image_update(element_id,image_file) {
    // alert("_properties_class_background_image_update('"+element_id+"','"+image_file+"')");
    
	if(element_id && (image_file && image_file.length > 0)) {
        
        // alert("_properties_class_background_image_update('"+element_id+"','"+image_file+"') passed check...");
        
		element_id.style.backgroundImage = "url("+image_file+")";
	}
}

function _properties_class_portfolio_view_switch() {
	var _link = document.getElementById("portfolio_view_switch_link");
	var _link_anchor = document.getElementById("portfolio_view_switch_link_anchor");
	if(_link && _link_anchor) {
		var _view_list = document.getElementById("portfolio_view_list");
		var _view_map = document.getElementById("portfolio_view_map");
		if(_view_list && _view_map) {
            if(_view_map.style.zIndex == 1) {
                _view_map.style.zIndex  = 0;
                _view_list.style.zIndex = 1;
                _link_anchor.style.background = "url(/images/styling/buttons/portfolio_view_map.png) no-repeat";
            } else {
                _view_map.style.zIndex  = 1;
                _view_list.style.zIndex = 0;
                _link_anchor.style.background = "url(/images/styling/buttons/portfolio_view_list.png) no-repeat";
            }
            
            /*
			if(_view_map.style.display == "none") {
				_view_list.style.display = "none";
				_view_map.style.display = "block";
				_link_anchor.style.background = "url(/images/styling/buttons/portfolio_view_map.png) no-repeat";
			} else {
				_view_map.style.display = "none";
				_view_list.style.display = "block";
				_link_anchor.style.background = "url(/images/styling/buttons/portfolio_view_list.png) no-repeat";
			}
            */
		}
	}
	return false;
}

Event.observe(window,"load",_properties_class_register_events);