//
//  code.m
//  Macheads101-OSX
//
//  Created by Alex Nichol on 11/21/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "code.h"


@implementation code
- (IBAction)latestVideoClick:(id)sender
{
	
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/video.html"]
											  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
										  timeoutInterval:60.0];
	[[mainVideoWebView mainFrame] loadRequest:theRequest];
	
	if (mainVideoWindow != NULL) {
		[mainVideoWindow setIsVisible:(TRUE)];
	}
}
- (IBAction)closeLatestVideoClick:(id)sender
{
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol"]
											  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
										  timeoutInterval:60.0];
	[[mainVideoWebView mainFrame] loadRequest:theRequest];
	
	if (mainVideoWindow != NULL) {
		[mainVideoWindow setIsVisible:(FALSE)];
	}
}




- (IBAction)aboutClick:(id)sender
{
	
	
	
	
	// create the request
	
	//
		NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/about101"]
											  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
										  timeoutInterval:60.0];
	
	// create the connection with the request		
	// and start loading the data
	/*
	 NSURLDownload  *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest									 
	 delegate:self];
	 */
	NSData *urlData;
	NSURLResponse *response;
	NSError *error;
	
	
	theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/about101"]
								cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
							timeoutInterval:60.0];
	
	urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
	if (urlData) {
		[aboutText setStringValue:[[NSString alloc] initWithData: urlData encoding: NSASCIIStringEncoding]];
	}
	
	
	
	
	if (aboutWindow != NULL) {
		[aboutWindow setIsVisible:(TRUE)];
	}
	
}




- (IBAction)newsClick:(id)sender
{
	
	
	
	
	// create the request
	
	//
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/macheads101news.txt"]
											  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
										  timeoutInterval:60.0];
	
	// create the connection with the request		
	// and start loading the data
	/*
	 NSURLDownload  *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest									 
	 delegate:self];
	 */
	NSData *urlData;
	NSURLResponse *response;
	NSError *error;
	
	
	theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/macheads101news.txt"]
								cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
							timeoutInterval:60.0];
	
	urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
	if (urlData) {
		[newsText setStringValue:[[NSString alloc] initWithData: urlData encoding: NSASCIIStringEncoding]];
	}
	
	
	
	
	if (newsWindow != NULL) {
		[newsWindow setIsVisible:(TRUE)];
	}
}
- (IBAction)infoClick:(id)sender
{
	
	
	
	
	// create the request
	
	//
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/online101"]
											  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
										  timeoutInterval:60.0];
	
	// create the connection with the request		
	// and start loading the data
	/*
	 NSURLDownload  *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest									 
	 delegate:self];
	 */
	NSData *urlData;
	NSURLResponse *response;
	NSError *error;
	
	
	theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/online101"]
								cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
							timeoutInterval:60.0];
	
	urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
	if (urlData) {
		[infoText setStringValue:[[NSString alloc] initWithData: urlData encoding: NSASCIIStringEncoding]];
	}
	
	
	
	
	if (infoWindow != NULL) {
		[infoWindow setIsVisible:(TRUE)];
	}
}

- (IBAction)newsCloseClick:(id)sender
{
	if (newsWindow != NULL) {
		[newsWindow setIsVisible:(FALSE)];
	}
}

- (IBAction)aboutCloseClick:(id)sender
{
	if (aboutWindow != NULL) {
		[aboutWindow setIsVisible:(FALSE)];
	}
}

- (IBAction)infoCloseClick:(id)sender
{
	if (infoWindow != NULL) {
		[infoWindow setIsVisible:(FALSE)];
	}
}

- (IBAction)checkUpdatesClick:(id)sender;
{
	
	// create the request
	
	//
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/macversion101"]
											  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
										  timeoutInterval:60.0];
	
	// create the connection with the request		
	// and start loading the data
	/*
	 NSURLDownload  *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest									 
	 delegate:self];
	 */
	NSData *urlData;
	NSURLResponse *response;
	NSError *error;
	
	
	theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/macversion101"]
								cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
							timeoutInterval:60.0];
	
	urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
	if (urlData) {
		// [urlData writeToFile:(@"/Applications/Macheads101Leopard.dmg") atomically:(TRUE)];
		[updateText setStringValue:[[NSString alloc] initWithData: urlData encoding: NSASCIIStringEncoding]];
		// [updateText setStringValue:[[NSString alloc] initWithFormat:@"***%@***", [updateText stringValue]]];
		// [updateText setIntValue:[[updateText stringValue] length]];
		// char textBuff[512];
		// NSRange
		if ([[updateText stringValue] compare:[[NSString alloc] initWithFormat:@"1.3\n"]] == NSOrderedSame) {
			[updateText setStringValue:[[NSString alloc] initWithFormat:@"%@Is the latest version", [updateText stringValue]]];
			[updateButton setEnabled:(FALSE)];
		} else {
			[updateText setStringValue:[[NSString alloc] initWithFormat:@"%@Is availible", [updateText stringValue]]];
			[updateButton setEnabled:(TRUE)];
		}
		
		
		// sprintf(textBuff, "%s Is the latest version.", textBuff);
		// [[updateText setStringValue:()];
	}
	
	
	
	if (updateWindow != NULL) {
		[updateWindow setIsVisible:(TRUE)];
	}
}

- (IBAction)updateClick:(id)sender
{
	// create the request
	
	//
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/Macheads101Leopard.dmg"]
											  cachePolicy:	NSURLRequestReloadIgnoringLocalCacheData							  
										  timeoutInterval:60.0];
	
	// create the connection with the request		
	// and start loading the data
	/*
	 NSURLDownload  *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest									 
	 delegate:self];
	 */
	NSData *urlData;
	NSURLResponse *response;
	NSError *error;
	
	
	theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://home.comcast.net/~alexnichol/Macheads101Leopard.dmg"]
								cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
							timeoutInterval:60.0];
	
	urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
	if (urlData) {
		[updateText setStringValue:@"Updating ..."];
		[urlData writeToFile:(@"/Applications/Macheads101Leopard.dmg") atomically:(TRUE)];
		system("mv /Applications/Macheads101Leopard.dmg ~/Desktop");
		[updateText setStringValue:@"Done!"];
		// char textBuff[512];
		
		/*if ([[updateText stringValue] compare:[[NSString alloc] initWithFormat:@"1.0\n"]]) {
			[updateText setStringValue:[[NSString alloc] initWithFormat:@"%@: Is the latest version", [updateText stringValue]]];
		} else {
			[updateText setStringValue:[[NSString alloc] initWithFormat:@"%@: Is availible", [updateText stringValue]]];
		}*/
		
		
		// sprintf(textBuff, "%s Is the latest version.", textBuff);
		// [[updateText setStringValue:()];
	}
	
	
}

- (IBAction)updateCloseClick:(id)sender
{
	

	
	if (updateWindow != NULL) {
		[updateWindow setIsVisible:(FALSE)];
	} else {
		exit(0);
		system("killall Macheads101-OSX");
	}
}

- (IBAction)youtubeButtonClick:(id)sender
{
	///////
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://youtube.com/macheads101"]
											  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
										  timeoutInterval:60.0];
	[[youtubeWebView mainFrame] loadRequest:theRequest];
	///////
	if (youtubeWindow != NULL) {
		[youtubeWindow setIsVisible:(TRUE)];
	} else {
		exit(0);
		system("killall Macheads101-OSX");
	}
	
}

- (IBAction)youtubeClick:(id)sender
{
	///////
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://youtube.com/macheads101"]
											  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData								  
										  timeoutInterval:60.0];
	[[youtubeWebView mainFrame] loadRequest:theRequest];	
	///////
	if (youtubeWindow != NULL) {
		[youtubeWindow setIsVisible:(TRUE)];
	} else {
		exit(0);
		system("killall Macheads101-OSX");
	}
}

- (IBAction)youtubeCloseClick:(id)sender
{
	if (youtubeWindow != NULL) {
		[youtubeWindow setIsVisible:(FALSE)];
	} else {
		exit(0);
		system("killall Macheads101-OSX");
	}
}

@end