var currentWindow = 'info_window_live_props';

function setRadio(radio_id, refer_id, group_ids){
    $(radio_id).checked="checked";
    $(refer_id).className = "selected";
    var i;
    for(i=0; i < group_ids.length; i=i+1){
        $(group_ids[i]).className = "";
    }
}

function showButtons(){
    var elements = ['currency_1', 'currency_2', 'currency_3', 'price_1', 'price_2', 'type_1', 'type_2', 'type_3', 'perpage_1', 'perpage_2', 'perpage_3'];

    var i;
    for(i=0; i < elements.length; ++i){
        $('label_'+elements[i]).style.display="none";
        $(elements[i]).style.display="none";
        $('button_'+elements[i]).style.display="inline";
    }
}

function clearOnSubmit(){
    //make sure that any button that submits form clears the location field if it has default text
	var submitButtons=['search_button', 'map_display', 'list_display', 'address', 'bedrooms', 'rent', 'type', 'button_price_1',
	                   'button_price_2', 'button_currency_1', 'button_currency_2',
	                   'button_currency_3', 'button_perpage_1', 'button_perpage_2', 'button_perpage_3',
	                   'button_perpage_4', 'button_type_1', 'button_type_2', 'button_type_3'
	                   ];

	Event.observe("search-form", 'submit', function(event) {
        clearLocationText();
	});
}
function clearLocationText(){
    var defaultSearchText = 'Place or postcode';
    if ($('id_location_text').value == defaultSearchText) {
	    $('id_location_text').value = '';
	    $('id_location_text').style.color = '';
	}
}

function gallerySwitch(property_id, caller_id){

    thumbId = "thumb_"+property_id+"_"+caller_id;
    mainId = "main_"+property_id;

    newImage = $(thumbId).src;
    oldImage = $(mainId).src;

    $(mainId).src = newImage;
    $(thumbId).src = oldImage;
}

function createAdvancedSearchLink(showInitially){
    var defaultSearchText = 'Place or postcode';
    var div = document.getElementById('advancedButton');
    var advancedLink = document.createElement("a");
    advancedLink.setAttribute("href","/search/");

    linkText = document.createTextNode(showInitially ? 'Basic search' : 'Advanced search');
    advancedLink.appendChild(linkText);
    advancedLink.onclick = new Function('showHideAdvancedSearch(this);return false;');
    div.appendChild(advancedLink);
    advancedSearchDiv = document.getElementById('advancedSearch');
    if (showInitially) {
        advancedSearchDiv.style.display='block';
    } else {
        advancedSearchDiv.style.display='none';
    }
    Event.observe('search-form', 'submit', function(event) {
        if ($('advancedSearch').style.display == 'none') {
            $('advancedSearch').innerHTML = '';
        }
    });

    if ($('id_location_text').value == '') {
    	//if (agentName && agentName != "null") {
	    	//$('id_location_text').defaultValue = 'Agent: ' + agentName;
    	//} else {
	    $('id_location_text').defaultValue = defaultSearchText;
	    //}
	    $('id_location_text').value = $('id_location_text').defaultValue;
	    $('id_location_text').style.color = '#ccc';
	}
	Event.observe('id_location_text', 'blur', function(event) {
	    if ($('id_location_text').value == '') {
		    $('id_location_text').value = $('id_location_text').defaultValue;
		    $('id_location_text').style.color = '#ccc';
		}
	});
	Event.observe('id_location_text', 'focus', function(event) {
	    if ($('id_location_text').value == $('id_location_text').defaultValue) {
		    $('id_location_text').value = '';
		    $('id_location_text').style.color = '';
		}
	});
}

function showHideAdvancedSearch(link){
    advancedSearchDiv = document.getElementById('advancedSearch');
    if (advancedSearchDiv.style.display == 'block'){
        advancedSearchDiv.style.display='none';
        link.innerHTML = "Advanced search";
    }else{
        advancedSearchDiv.style.display='block';
        link.innerHTML= "Basic search";
    }
	if (typeof lettingWebMap != 'undefined') {
	    lettingWebMap.update();
	}
}

function propogateAdvanced(property_type){
    if ($('advancedSearch').style.display == 'none') {
            $('advancedSearch').innerHTML = '';
    } else if (property_type) {
    	var select = $('id_property_type');
    	select.selectedIndex = 0;
    	for (var i = 0; i < select.options.length; i++) {
    		if (select.options[i].value == property_type.value) {
    			select.selectedIndex = i;
    			break;
    		}
    	}
    }
}

function addInfoEvent(elementId){
    infoElement = document.getElementById(elementId);
    infoElement.onmouseover = new Function('showInfoWindow();');
    infoElement.onmouseout = new Function('hideInfoWindow();');

    document.onmousemove = mouseMove;
    var infoWindow = document.createElement("div");
    infoWindow.id="info_window";
    infoWindow.className="info_window";
    var infoText = document.createElement("p");
    infoText.innerHTML = "<b>Search count is updated nightly.</b>"
    infoWindow.appendChild(infoText);
    infoWindow.style.padding="5px";
    infoWindow.style.display="none";
    infoElement.appendChild(infoWindow);
}
function showInfoWindow(){
    infoWindow = document.getElementById('info_window');
    infoWindow.style.display="block";
}
function hideInfoWindow(){
    infoElement = document.getElementById('info_window');
    infoElement.style.display="none"
}
function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);
    infoWindow = document.getElementById('info_window');
    infoWindow.style.left=mousePos.x-245+"px";
    infoWindow.style.top=mousePos.y-115+"px";
}
function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:event.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:event.clientY - document.body.scrollTop  - document.body.clientTop
	};
}




function addReportInfoEvent(){
    currentWindow = 'info_window_live_props';
    var link_text = '<p>Click to view information in a new window.</p>';
    var infoTexts=[['live_props', link_text],
                   ['photo_props', link_text],
                   ['floorplan_props', link_text],
                   ['tour_props', link_text],
                   ['unanswered_calls', link_text],
                   ['enq_per_prop', link_text],
                  ];

    document.onmousemove = mouseMove2;
    for(i=0; i < infoTexts.length; i++){
        createInfoEvent(infoTexts[i]);
    }
}

function addPostcodeInfoEvent(){
    currentWindow = 'info_window_postcode_info';
    var infoTexts=[['postcode_info', '<p>Find a postcode from an address. Opens a link to the Royal Mail postcode finder in a new window</p>']
                  ];

    document.onmousemove = mouseMove2;
    for(i=0; i < infoTexts.length; i++){
        createInfoEvent(infoTexts[i]);
    }
}

function createInfoEvent(text){
        infoElement = document.getElementById(text[0]);
        infoElement.onmouseover = new Function('showInfoWindow2(this.id);');
        infoElement.onmouseout = new Function('hideInfoWindow2(this.id);');
        infoElement.style.background="url(" + MEDIA_URL + "img/furniture/info_button.png) no-repeat right";

        var infoWindow = document.createElement("div");
        infoWindow.id="info_window_"+text[0];
        infoWindow.className="info_window";
        infoWindow.innerHTML = text[1];
        infoElement.appendChild(infoWindow);
}

function showInfoWindow2(windowId){
    infoWindow = document.getElementById('info_window_'+windowId);
    infoWindow.style.display="block";
    currentWindow = 'info_window_'+windowId
}
function hideInfoWindow2(windowId){
    infoElement = document.getElementById('info_window_'+windowId);
    infoElement.style.display="none"
}
function mouseMove2(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);
	mousePos = mouseCoords(ev);

    infoWindow = document.getElementById(currentWindow);
    infoWindow.style.left=mousePos.x+25+"px";
    infoWindow.style.top=mousePos.y-10+"px";
}
