#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){	 
	
	ofBackground(0,0,0);	

	
	verdana.loadFont("verdana.ttf",13, true, true);
	verdana.setLineHeight(20.0f);
	
   
	connection.getWebPageAsString("http://drudgereport.com/");
 	myNode = parser.parseHtml(connection.getString()); 	
    nodes = parser.getContentOfXpathExpression(myNode,"//a");

 	ofSetWindowTitle("http://drudgereport.com/");
	
}

//--------------------------------------------------------------
void testApp::update(){
}

//--------------------------------------------------------------
void testApp::draw(){

	ofSetColor(0xFFFFFF);
	
	int height = 100;
	
	for (int i = 0; i < nodes.size(); i++){
 		if (nodes[i].getHasAttribute("href")){
 			if (nodes[i].getNumChildren() == 0) { // you are not an image link
	 			string content = nodes[i].getContent();
				verdana.drawString(content.c_str(), 100, height);
				height += verdana.getStringBoundingBox(content.c_str(), 0, 0).height + 3;
 			}
  		}
 	}

}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
}

//--------------------------------------------------------------
void testApp::keyReleased  (int key){ 
	
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}


static int counter = 0;
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	
	char webpages[5][255] = {"www.cnn.com","www.slashdot.org", "www.myspace.com", "www.digg.com", "www.yahoo.com"};
	counter++;
	counter %= 5;
	//parser.reset();
	connection.getWebPageAsString(webpages[counter]);
	//connection.getWebPageAsString("http://www.cnn.com");
    //cout << connection.getString() << endl << "----------";
    
    myNode = parser.parseHtml(connection.getString());
    nodes = parser.getContentOfXpathExpression(myNode,"//a");
    myNode.print();
 	ofSetWindowTitle(webpages[counter]);
    //ofSetWindowTitle(num.str());
    
    
}

//--------------------------------------------------------------
void testApp::mouseReleased(){

}
