// HTTP Request class
	function apXMLHttpRequest(url) {
		
		this.url = url;
		this.result;
		

		
		
		// Make an XMLHttpRequest instance
		this.mkRequest = function() {
		
			http_request = false;
			
			if (window.XMLHttpRequest) { // Mozilla, Safari,...
	            http_request = new XMLHttpRequest();
	            if (http_request.overrideMimeType) {
                	http_request.overrideMimeType('text/xml');
	            }
        
	        } else if (window.ActiveXObject) { // IE
	            try {
	                http_request = new ActiveXObject("Msxml2.XMLHTTP");
	            } catch (e) {
	                try {
	                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
	                } catch (e) {  }
	            }
	        }
	        // If an XMLHttpRequest couldn't be made
	        if (!http_request) {
	            alert('ERROR! Couldn\'t create an XMLHTTP instance');
	            return false;
	        }
	        
	        http_request.onreadystatechange = this.contentCallback;
        	http_request.open('GET', this.url, true);
        	http_request.send(null);
			
		}
		
		this.contentCallback = function() {
			
	        if (http_request.readyState == 4) {
	            if (http_request.status == 200) {
	            	
					
		            	if (http_request.responseText == 'sold') {
							document.getElementById('status').style.visibility = 'visible';
						}else{
							document.getElementById('status').style.visibility = 'hidden';
						}
					
					

	            } else {
	                alert('Beklager, det oppstod en feil.');
	            } 
				
	        }
	        
		}
		
	}

// Apartment js

function apartment() {
	
	this.currentApartment = 1;
	this.currentFloor = 1;
	this.currentBuilding = 'A';
	this.currentStatus;
	this.currentUrl = '/byala/sys/view_apartment.php?building=A&apartment=1';
	
	
	var floorA = new Array();
	
	floorA[1] = 1;
	floorA[2] = 7;
	floorA[3] = 14;
	floorA[4] = 21;
	floorA[5] = 28;
	
	var floorB = new Array();
	
	floorB[2] = 1;
	floorB[3] = 3;
	
	var floorC = new Array();
	
	floorC[1] = 1;
	floorC[2] = 5;
	floorC[3] = 9;
	floorC[4] = 13;
	
	var floorD = new Array();
	
	floorD[1] = 1;
	floorD[2] = 5;
	floorD[3] = 9;
	floorD[4] = 13;
	floorD[5] = 17;
	
	
	this.nextApartment = function() {
		
		if (this.currentBuilding == 'A') {
			
			if (this.currentFloor == 1) {
				if ( this.currentApartment < 6 )
					this.currentApartment++;
			}
			if (this.currentFloor == 2) {
				if ( this.currentApartment < 13 )
					this.currentApartment++;
			}
			if (this.currentFloor == 3) {
				if ( this.currentApartment < 20 )
					this.currentApartment++;
			}
			if (this.currentFloor == 4) {
				if ( this.currentApartment < 29 )
					this.currentApartment++;
			}
			if (this.currentFloor == 5) {
				if ( this.currentApartment < 29 )
					this.currentApartment++; 
			}
			
		}
		
		if (this.currentBuilding == 'B') {
			
			if (this.currentFloor == 2) {
				if ( this.currentApartment < 2 )
					this.currentApartment++;
			}		
			
		}
		
		if (this.currentBuilding == 'C') {
			
			if (this.currentFloor == 1) {
				if ( this.currentApartment < 4 )
					this.currentApartment++;
			}	
			
			if (this.currentFloor == 2) {
				if ( this.currentApartment < 8 )
					this.currentApartment++;
			}
			
			if (this.currentFloor == 3) {
				if ( this.currentApartment < 12 )
					this.currentApartment++;
			}
			
			if (this.currentFloor == 4) {
				if ( this.currentApartment < 14 )
					this.currentApartment++;
			}
			
		}
		
		if (this.currentBuilding == 'D') {
			
			if (this.currentFloor == 1) {
				if ( this.currentApartment < 4 )
					this.currentApartment++;
			}
			
			if (this.currentFloor == 2) {
				if ( this.currentApartment < 8 )
					this.currentApartment++;
			}	
			
			
			if (this.currentFloor == 3) {
				if ( this.currentApartment < 12)
					this.currentApartment++;
			}	
			
			if (this.currentFloor == 4) {
				if ( this.currentApartment < 16 )
					this.currentApartment++;
			}
			
			if (this.currentFloor == 5) {
				if ( this.currentApartment < 17 )
					this.currentApartment++;
			}	
			
		}
		
		this.preloadImage();
		this.setCurrentView();
		
		
	}
	
	this.previousApartment = function() {
		
		if (this.currentBuilding == 'A') {
			
			if (this.currentFloor == 1) {
				if ( this.currentApartment > 1 )
					this.currentApartment--;
			}
			if (this.currentFloor == 2) {
				if ( this.currentApartment > 7 )
					this.currentApartment--;
			}
			if (this.currentFloor == 3) {
				if ( this.currentApartment > 14 )
					this.currentApartment--;
			}
			if (this.currentFloor == 4) {
				if ( this.currentApartment > 21 )
					this.currentApartment--;
			}
			if (this.currentFloor == 5) {
				if ( this.currentApartment > 28 )
					this.currentApartment--; 
			}
			
		}
		
		if (this.currentBuilding == 'B') {
			
			if (this.currentFloor == 2) {
				if ( this.currentApartment > 1 )
					this.currentApartment--;
			}		
			
		}
		
		if (this.currentBuilding == 'C') {
			
			if (this.currentFloor == 1) {
				if ( this.currentApartment > 1 ) //1
					this.currentApartment--;
			}	
			
			if (this.currentFloor == 2) {
				if ( this.currentApartment > 5 )// 5
					this.currentApartment--;
			}
			
			if (this.currentFloor == 3) {
				if ( this.currentApartment > 9 )//9
					this.currentApartment--;
			}
			
			if (this.currentFloor == 4) {
				if ( this.currentApartment > 13 )// 13
					this.currentApartment--;
			}
			
		}
		
		if (this.currentBuilding == 'D') {
			
			if (this.currentFloor == 1) {
				if ( this.currentApartment > 1 )
					this.currentApartment--;
			}
			
			if (this.currentFloor == 2) {
				if ( this.currentApartment > 5 ) //5
					this.currentApartment--;
			}	
			
			
			if (this.currentFloor == 3) {
				if ( this.currentApartment > 9) //9
					this.currentApartment--;
			}	
			
			if (this.currentFloor == 4) {
				if ( this.currentApartment > 13 ) //13
					this.currentApartment--;
			}
			
			if (this.currentFloor == 5) {
				if ( this.currentApartment > 17 ) //17
					this.currentApartment--;
			}	
			
		}
		
		this.setCurrentView();
		
	}
	
	this.nextFloor = function() {
			
		if (this.currentBuilding == 'A') {
			if (this.currentFloor < 5)
				this.currentFloor++;
				
			this.currentApartment = floorA[this.currentFloor];
				
		}
		if (this.currentBuilding == 'B') {
			if (this.currentFloor < 3)
				this.currentFloor++;
				
			this.currentApartment = floorB[this.currentFloor];
		}
		if (this.currentBuilding == 'C') {
			if (this.currentFloor < 4)
				this.currentFloor++;
				
			this.currentApartment = floorC[this.currentFloor];
		}
		if (this.currentBuilding == 'D') {
			if (this.currentFloor < 5)
				this.currentFloor++;
				
			this.currentApartment = floorD[this.currentFloor];
		}
		
		this.setCurrentView();
			
	}
	
	this.previousFloor = function() {
			
		if (this.currentBuilding == 'A') {
			if (this.currentFloor > 1)
				this.currentFloor--;
				
			this.currentApartment = floorA[this.currentFloor];
			
		}
		if (this.currentBuilding == 'B') {
			if (this.currentFloor > 2)
				this.currentFloor--;
				
			this.currentApartment = floorB[this.currentFloor];
		}
		if (this.currentBuilding == 'C') {
			if (this.currentFloor > 1)
				this.currentFloor--;
				
			this.currentApartment = floorA[this.currentFloor];
			
		}
		if (this.currentBuilding == 'D') {
			if (this.currentFloor > 1)
				this.currentFloor--;
				
			this.currentApartment = floorA[this.currentFloor];
			
		}
		
		this.setCurrentView();
			
	}
	
	this.changeBuilding = function(building) {
		
		if (building == 'A') {
			this.currentApartment = 1;
			this.currentFloor = 1;
		}
		
		if (building == 'B') {
			this.currentApartment = 1;
			this.currentFloor = 2;
		}
		
		if (building == 'C') {
			this.currentApartment = 1;
			this.currentFloor = 1;
		}
		
		if (building == 'D') {
			this.currentApartment = 1;
			this.currentFloor = 1;
		}
		
		var style = 'building' + this.currentBuilding;
		
		if (navigator.appName == "Microsoft Internet Explorer") { 
			if (getInternetExplorerVersion() == 8) {
				document.getElementById(style).attributes[0].value = 'deactivated';
			} else {
				document.getElementById(style).attributes[3].value = 'deactivated';
			}
		} else {
			document.getElementById(style).attributes[1].value = 'deactivated';
		}
		
		this.currentBuilding = building;
		
		style = 'building' + this.currentBuilding;
		
		if (navigator.appName == "Microsoft Internet Explorer") {
			if (getInternetExplorerVersion() == 8) {
				document.getElementById(style).attributes[0].value = 'activated';
			} else {
				document.getElementById(style).attributes[3].value = 'activated';
			}
		} else {
			document.getElementById(style).attributes[1].value = 'activated';
		}
		
		this.setCurrentView();
		
	}

	this.updateInfo = function() {
		
		document.getElementById('info').innerHTML = '<b>Bygning:</b> ' + this.currentBuilding + '<br />\n <b>Etasje:</b> '+ this.currentFloor + '<br />\n <b>Leilighet:</b> ' + this.currentApartment + '<br />';
		
	}
	
	this.preloadImage = function() {
		
		preloadImg = new Image();
		preloadImg.src = '/byala/images/apartments/' + this.currentBuilding + '_' + this.currentFloor + '_' + (this.currentApartment + 1) + '.jpg';
		
	}
	
	
	this.setCurrentView = function() {
		
		if (document.getElementById('status') != null) { 
		
			var requestObject = new apXMLHttpRequest('/byala/sys/apartment_status.php?apartment=' + this.currentApartment + '&building=' + this.currentBuilding);
			requestObject.mkRequest();
			document.getElementById('viewA').src = '/byala/images/apartments/' + this.currentBuilding + '_' + this.currentFloor + '_' + this.currentApartment + '.jpg';
			this.currentUrl = '/byala/sys/view_apartment.php?building=' + this.currentBuilding + '&apartment='+ this.currentApartment;
			this.updateInfo();
		}
		
		//alert(this.currentBuilding + ' ' + this.currentFloor + ' ' + this.currentApartment);
		
	}
	
	this.setApartment = function(building, floor, apartment) {
		
		style = 'building' + this.currentBuilding;
		
		if (navigator.appName == "Microsoft Internet Explorer") { 
			
			//alert(parseFloat(navigator.appVersion)); 
			if (getInternetExplorerVersion() == 8) {
				document.getElementById(style).attributes[0].value = 'deactivated';
			} else {
				document.getElementById(style).attributes[3].value = 'deactivated';
			//alert(document.getElementById(style).attributes[3].value);
			}
		} else {
			document.getElementById(style).attributes[1].value = 'deactivated';
		}
		
		this.currentApartment = apartment;
		this.currentFloor = floor;
		this.currentBuilding = building;
		
		style = 'building' + this.currentBuilding;
		
		if (navigator.appName == "Microsoft Internet Explorer") {
			//alert(parseFloat(navigator.appVersion)); 
			if (getInternetExplorerVersion() == 8) {
				document.getElementById(style).attributes[0].value = 'activated';
			} else {
				document.getElementById(style).attributes[3].value = 'activated';
			//alert(document.getElementById(style).attributes[3].value);
			}
		} else {
			document.getElementById(style).attributes[1].value = 'activated';
		}

		this.setCurrentView();
		
	}
	
}

function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}


var apartmentObject = new apartment();
apartmentObject.setCurrentView();
