// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function newMapWindow(latitude, longitude) {
    mywindow=open('/property/map','myname','resizable=no,width=850,height=400');
    mywindow.location.href = '/property/map?latitude='+latitude+'&longitude='+longitude;
    if (mywindow.opener == null) mywindow.opener = self;
}

function closeMapWindow(latitude, longitude) {
    opener.document.getElementById('property_latitude').value = latitude
    opener.document.getElementById('property_longitude').value = longitude
    top.close();
    }
    
function createNamedElement(type, name) {
   var element = null;
   // Try the IE way; this fails on standards-compliant browsers
   try {
      element = document.createElement('<'+type+' name="'+name+'">');
   } catch (e) {
   }
   if (!element || element.nodeName != type.toUpperCase()) {
      // Non-IE browser; use canonical method to create named element
      element = document.createElement(type);
      element.name = name;
   }
   return element;
}
    
function makeNewFileField(image_id, image_number) {

    image_self = document.getElementById(image_id)
    image_self_row = document.getElementById('image_' + image_number + '_row')
    next_image_number = image_number + 1
    if (document.getElementById('image_' + next_image_number + '_row') == null) {
        propertycreatetable = document.getElementById('property_create_table')

        next_image_row = propertycreatetable.insertRow(propertycreatetable.rows.length-2)
        
        next_image_row.setAttribute('id', 'image_' + next_image_number + '_row');
    
        next_image_row_th = document.createElement('th');
        next_image_row_th.innerHTML = "Image " + next_image_number;
        
        next_image_row.appendChild(next_image_row_th)
        
        next_image_row_td = document.createElement('td');
        next_image_row.appendChild(next_image_row_td, next_image_row_th)
            
        next_image = createNamedElement('input', 'images[image_' + next_image_number + ']') // next_image = document.createElement('input');
        next_image.setAttribute('type', 'file');
        next_image.setAttribute('id', 'images_image_' + next_image_number);
        next_image.setAttribute('size', '30');
        next_image.setAttribute('onclick', 'javascript:makeNewFileField("images_image_' + next_image_number + '",' + next_image_number +')');        
        next_image_row_td.appendChild(next_image)

        next_image.parentNode.innerHTML = next_image.parentNode.innerHTML; // this is required because IE 6/7 doesnt update certain attributes when they change.
        //next_image_row_td.parentNode.innerHTML = next_image_row_td.parentNode.innerHTML;        
    }
    
}

function showNormalImage(image_id) {
    new_normal_image = document.getElementById(image_id)
    // remove active from all normal images
    normal_images = getElementsByClassName(document.getElementById('gallery'), 'img', 'normal' )
    for(var i = 0; i < normal_images.length; i++) {
        normal_images[i].className = ' normal'
    }
    // add active class onto our new favourite normal image
    new_normal_image.className = new_normal_image.className + ' active'
    }

function highlightThumbImage(image_id) {
    thumb_image = document.getElementById('div-'+image_id)
    // remove active from all the thumbs
    thumb_images = getElementsByClassName(document.getElementById('gallery'), 'div', 'thumb' )
    for(var i = 0; i < thumb_images.length; i++) {
        thumb_images[i].className = 'thumb'
    }
    // add active class to the new active thumb image
    thumb_image.className = thumb_image.className + ' active'
    }
    
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}
    