﻿function overlay(curobj, subobjstr, xpos, ypos)
{
	if (document.getElementById)
	{
		var subobj = document.getElementById(subobjstr);
		var screen_mod = document.getElementById("screen_mod");

		subobj.style.display = (subobj.style.display != "block") ? "block" : "none";
		screen_mod.style.display = (screen_mod.style.display != "block") ? "block" : "none";

		subobj.style.left = xpos + "%";
		subobj.style.top = ypos + "%";

		op = 0;
		subobj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + op + ")";
		subobj.style.opacity = op;
		while(op < 100)
		{
			op = op + 10;
			setTimeout("solidMe('" + subobjstr + "', " + op + ")", op * 5);
		}

		return false;
	}
	else return true;
}

function overlayclose(subobj)
{
	document.getElementById(subobj).style.display = "none";

	var screen_mod = document.getElementById("screen_mod");

	screen_mod.style.display = (screen_mod.style.display != "block") ? "block" : "none";
}



function solidMe(subobjstr, op)
{
	var subobj = document.getElementById(subobjstr);
	subobj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + op + ");";
	subobj.style.opacity = op / 100;
}



//----------------------------------------------------------------------------------------------AJAX


function callAHAH(url, pageElement, callMessage, errorMessage)
{
	document.getElementById(pageElement).innerHTML = callMessage;

	try
	{
		req = new XMLHttpRequest(); /* e.g. Firefox */
	}
	catch(e)
	{
		try
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");  /* IE */
		}
		catch (e)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");  /* IE */
			}
			catch (E)
			{
				req = false;
			}
		}
	}

	req.onreadystatechange = function() { responseAHAH(pageElement, errorMessage); };
	req.open("GET", url, true);
	req.send(null);
}

function responseAHAH(pageElement, errorMessage)
{
	var output = '';

 		var subobj = document.getElementById(pageElement);
		op = 0;
		subobj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + op + ")";
		subobj.style.opacity = op;
		while(op < 100) {
			op = op + 10;
			setTimeout("solidMe('" + pageElement + "', " + op + ")", op * 5);
		}

	if(req.readyState == 4)
	{
		if(req.status == 200)
		{
			output = req.responseText;
			document.getElementById(pageElement).innerHTML = output;
		}
		else
		{
			document.getElementById(pageElement).innerHTML = errorMessage+"\n"+output;
		}
	}
}

function makeactive(tab)
{
	document.getElementById("tab1").className = "";
	document.getElementById("tab2").className = "";
	document.getElementById("tab3").className = "";
	document.getElementById("tab4").className = "";

	document.getElementById("tab"+tab).className = "active";

	for(i=1; i < 5; i++) changemargin('b', i);
	changemargin('a', tab);

	callAHAH('/additional/content.php?id= '+tab, 'spec_content', '<p class="loading">Loading...</p>', 'Error: bad request.');
}

function changemargin(met, tab)
{
	var ctab = document.getElementById("tab"+tab);

	mt = 0;
	if(met == "b") mt = 12;

	if(ctab.className != "active") ctab.style.marginTop = mt+"px";
}


//----------------------------------------------------------------------------------------------FADE


function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";

	//make image transparent
	changeOpac(0, imageid);

	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;

	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

// ----- QUESTIONNAIRE

function hideAllTabs(){
	for( var i = 0; i < tabs.length; i++ ){
		$( tabs[ i ] ).style.display = 'none';
		$( tabs[ i ] + '-button' ).className = 'tab-list-element';
	}
}

function showTab( which ){
	hideAllTabs();

	$( which ).style.display = 'block';
	$( which + '-button' ).className = 'tab-list-element active';
	//initTabEffects();
}

/*function initTabEffects(){
	var els = $$( '.tab-list-element' );

	els.each( function( element ) {
		if( element.className.indexOf( "active" ) == -1 ){
			element.addEvent('mouseenter', function(){
				new Fx.Styles( element, {duration:600, wait:false} ).start({
					'background-color': '#3B7CFF',
					'color': '#FFFFFF'
				});

			 	new Fx.Styles( $( element.id + '-link' ), {duration:600, wait:false }).start({
			 		'color': '#FFFFFF'
			 	});

			});

			element.addEvent('mouseleave', function(){
				new Fx.Styles( element, {duration:600, wait:false} ).start({
					'background-color': '#CCCCCC',
					'color': '#78746E'
				});

				new Fx.Styles( $( element.id + '-link' ), {duration:600, wait:false }).start({
			 		'color': '#78746E'
			 	});

			});
			element.fireEvent( 'mouseleave' );
		}else{
			element.removeEvents();
		}
	} );
}*/

var sectionToAdd = '';

function addSection( type ){
	for( var i = 0; i < tabs.length; i++ ){
		if( tabs[ i ] == type )
			return;
	}

	tabs[ tabs.length ] = type;
	sectionToAdd = type;

	var d = document.createElement( 'div' );
	d.className = 'tab';
	d.id = type;

	$( 'tab-panels' ).appendChild( d );

	new Ajax( 'property_ajax?get_form=true&type=' + type + '&propertyId=' + propertyId, { onSuccess: completeAddSection, evalScripts: true, update: $( type ) } ).request();

	var section = sectionToAdd;
	var sectionName = '';

	switch( section ){
		case 'interior-details':
			sectionName = 'Interior Details';
			break;
		case 'interior-details-cont':
			sectionName = 'Interior Details Continued';
			break;
		case 'exterior-details':
			sectionName = 'Exterior Details';
			break;
		case 'area-information':
			sectionName = 'Area Information';
			break;
		case 'location-map':
			sectionName = 'Location Map';
			break;
		case 'things-to-note':
			sectionName = 'Things to Note';
			break;
		case 'floor-plan':
			sectionName = 'Floor Plan';
			break;
	}

	var addSection = $( 'add-section-link' );
	$( 'tab-list' ).removeChild( addSection );
	$( 'tab-list' ).innerHTML += '<li><p class="tab-list-element" id="' + section + '-button"><a id="' + section + '-button-link" href="javascript:showTab( \'' + section + '\' );">' + sectionName + '</a></p></li>';
	$( 'tab-list' ).appendChild( addSection );

	showTab( section );
	sectionToAdd = '';
}

function completeAddSection( text, xml ){
	//$( 'tab-panels' ).innerHTML += text;

	var section = sectionToAdd;
	if( section == 'location-map' ){
		if( $( 'location' ).value == '' ){
			$( 'location' ).value = $( 'address' ).value;
		}
		gMapsLoad();
		showAddress( $('location').value );
	}

}

function addPictures( pId, sId ){
	$( 'pictures_frame' ).src = "/property_pictures?propertyId=" + pId + "&sectionId=" + sId;
	overlay( null, 'popup_pictures', '20', '35');
}


var map = null;
var geocoder = null;

function gMapsLoad() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(37.4419, -122.1419), 13);
	geocoder = new GClientGeocoder();
	var mapControl = new GMapTypeControl();
	map.addControl(mapControl);
	map.addControl(new GSmallMapControl());
  }
}

function showAddress(address) {
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  alert(address + " not found");
		} else {
		  map.setCenter(point, 13);
		  var marker = new GMarker(point);
		  map.addOverlay(marker);
		  marker.openInfoWindowHtml(address);
		}
	  }
	);
  }
}

function getShortlistPage( dir ){
	var response = new Ajax( '/property_ajax?get_page=' + dir, {
		update: $( 'shortlist-content' ),
		onComplete: function(data) {
			if(data.indexOf('<h3 class="shortlist">Properties</h3>') > -1) {
				$('shortlist-nav').setStyle('display', 'none');
				$$('.shortlist-my-properties').setStyle('display', 'none');
			} else {
				$('shortlist-nav').setStyle('display', 'block');
				$$('.shortlist-my-properties').setStyle('display', 'block');
			}
		},
		evalScripts: true
	} ).request();

}

function openProperty( id ){
	new Ajax( '/property_ajax?open_property=' + id, {
		update: $( 'shortlist-content' ),
		onComplete: function(data) {
			$('shortlist-nav').setStyle('display', 'block');
			$$('.shortlist-my-properties').setStyle('display', 'block');
		}
	}).request();
}

function getShortlistHome( id ){
	new Ajax( '/property_ajax?home_page=true', {
		update: $( 'shortlist-content' ),
		onComplete: function(data) {
			$('shortlist-nav').setStyle('display', 'none');
			$$('.shortlist-my-properties').setStyle('display', 'none');
		}
	}).request();
}

function getShortlistImage( dir ){
	new Ajax( '/property_ajax?get_image=' + dir, {update: $( 'shortlist-images' )} ).request();
}

function checkRentSelected(){
	if( !$( 'rent-selection' ).checked ){
		$( 'per-week' ).style.display = 'inline';
		$( 'k-symbol' ).style.display = 'none';
	}else{
		$( 'per-week' ).style.display = 'none';
		$( 'k-symbol' ).style.display = 'inline';
	}
}

function validateRequirements(){
	if( $( 'max_budget' ).value == '' ){
		alert( "You need to enter a maximum budget" );
		return false;
	}else{
		return true;
	}
}

function checkBudget(){
	var budget = parseInt( $( 'max_budget' ).value );

	if( $( 'rent-selection' ).checked ){
		if( budget < 250 ){
			$( 'max_budget' ).value = '250';
			alert( "Sorry, we only deal with rental searches above £250 per week" );
		}
	}else{
		if( budget < 250 ){
			$( 'max_budget' ).value = '250';
			alert( "Sorry, we only deal with property searches above £250,000" );
		}
	}
}
