var data = require('data/data').disciplines;

function imageRow(search) {
    var row = Ti.UI.createTableViewRow();

    function unavail() {
        var label = Ti.UI.createLabel({
            text:'Image Unavailable',
            font:{fontSize:14},
            color:'#aaa',
            left:10,
            right: 10,
            textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
            height:44
        });
        row.add(label);
    };
    
    var getData = Titanium.Network.createHTTPClient();

    getData.onload = function() {
        var status = this.status;
        Ti.API.debug(status);    
        if((399 < status) && (600 > status)) {        
            unavail();
        }
        else {
            var response = JSON.parse(this.responseText);
            var result = response.responseData.results[0];
            Ti.API.debug(response.responseData.results);
            if(!result) {
                unavail();
                return;
            }
            var ratio = result.width/Ti.Platform.displayCaps.platformWidth;
            var itemImage = Ti.UI.createImageView({
                defaultImage: 'images/hourglass.png',
                image: result.url,
                width:Ti.Platform.displayCaps.platformWidth,
                height: result.height / ratio,
                borderWidth: 1
            });
            row.add(itemImage);
        }
    };

    getData.open("GET","https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q="+search+"&as_sitesearch=commons.wikimedia.org&rsz=1&userip="+Ti.Platform.address);
    getData.send();
    
    return row;
}

function titleRow(text) {
    var row = Ti.UI.createTableViewRow({
        title: text
    });

    return row;
}

function subtitleRow(text) {
    var row = Ti.UI.createTableViewRow({
        title: text
    });
    
    return row;
}

function placeCollectedRow (item) {
    var row = Ti.UI.createTableViewRow({
            layout: 'vertical'
        }),
        lat = item.lat || false,
        lng = item.lng || false,
        map = false,
        placeName = item.place,
        imgUrl,
        mapParam;
        
    var googleStaticMapUrl = require('ui/common/Map').googleStaticMapUrl,
        googleMapUrl = require('ui/common/Map').googleMapUrl;

    if(item.lat && item.lng) {
        mapParam = {lat: lat, lng: lng};
    }
    else {
        placeName = placeName.replace('(place collected)', '');
        mapParam = {named: placeName};
    }
    imgUrl = googleStaticMapUrl(mapParam);    
    Ti.API.debug(imgUrl);
    
    map = Ti.UI.createImageView({
        image: imgUrl,
        height: 100,
        width:Ti.Platform.displayCaps.platformWidth
    });
    
    map.addEventListener('click', function() {
        Ti.Platform.openURL(googleMapUrl(mapParam));
    });

    row.add(map);
    
    var label = Ti.UI.createLabel({
        text:placeName,
        textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
        font:{fontSize:11},
        color:'#444',
        top:10,
        bottom: 10,
        left: 10,
        right: 10,
        height:Ti.UI.SIZE
    });
    
    row.add(label);
    
    return row;

}

function wikipediaRow (text) {
    var label = Ti.UI.createLabel({
        text:'Learn more on Wikipedia...',
        font:{fontSize:14},
        color:'#0080FF',
        left:10,
        right: 10,
        textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
        height:44,
        touchEnabled: false
    });

    var row = Ti.UI.createTableViewRow({
        search: text,
        selectionStyle:Ti.UI.iPhone.TableViewCellSelectionStyle.GRAY
    });
    
    row.add(label);
    
    row.addEventListener('click', function(e) {
        Ti.Platform.openURL('http://en.wikipedia.org/wiki/Special:Search?search='+e.rowData.search+'&go=Go');
    });
    
    return row;
}

function DetailView() {
	var self = Ti.UI.createView();

    var table = Ti.UI.createTableView({
	    footerView: Ti.UI.createView({height: 0}), 
	    selectionStyle: Ti.UI.iPhone.TableViewCellSelectionStyle.NONE,
		data:[]
	});
	self.add(table);
	
	self.addEventListener('itemSelected', function(e) {
	    Ti.API.debug('detailView itemSelected');

        //Clear out table...
        table.setData([]);

	    var item = data[e.discipline][e.key] || false;
	    if(item == false) {
	        Ti.API.error('Item not found');
	        return;
	    }
        self.paint(item);
	});
	
	self.paint = function(item) {
	    Ti.API.debug(item);
        var tableData = [];
        
        var title = item.common_name || '',
            subtitle = item.scientific_name || '';

        if(title == '') {
            title = item.scientific_name;
            subtitle = '';
        }
        
        tableData.push(imageRow(title));
        
        tableData.push(titleRow(title));
        if('' != subtitle){
            tableData.push(subtitleRow(subtitle));
        }
        
        tableData.push(placeCollectedRow(item));
        tableData.push(wikipediaRow(title));
        table.setData(tableData);
	};
	
	return self;
};

module.exports = DetailView;

@@ -1133,17 +1133,13 @@
 ges/
-hourglass
+clear
 .png
@@ -2071,16 +2071,49 @@
 em.place
+.replace('(place collected)', '')
 ,%0A      
@@ -2297,28 +2297,33 @@
  if(
-item.lat && item.lng
+'unverified' != placeName
 ) %7B%0A
@@ -2346,26 +2346,24 @@
  = %7B
-lat
+named
 : 
+p
 la
-t, lng: lng
+ceName
 %7D;%0A 
@@ -2390,100 +2390,38 @@
     
-placeN
+mapPar
 am
-e
  = 
-placeName.replace('(place collected)', '');%0A        mapParam = %7Bnamed: placeName
+%7Blat: lat, lng: lng
 %7D;%0A 
@@ -2421,24 +2421,25 @@
 lng%7D;%0A    %7D%0A
+%0A
     imgUrl =
@@ -2553,32 +2553,74 @@
  image: imgUrl,%0A
+        defaultImage: 'images/clear.png',%0A
         height: 
@@ -2728,32 +2728,80 @@
 ', function() %7B%0A
+        //Prefer placeName - lon/lat aint great%0A
         Ti.Platf

switch to named