//
//  QuranResultsViewController.m
//  QuranSearch
//
//  Created by Ahmed El-Helw on 10/25/10.
//  Copyright 2010 None. All rights reserved.
//

#import "Ayah.h"
#import "QuranResultsViewController.h"

@implementation QuranResultsViewController

@synthesize popover;
@synthesize webview;
@synthesize selectedAyah;

- (void)refresh {
	if (selectedAyah != nil){
		NSString* urlStr = [[[NSString alloc]
							 initWithFormat:@"%s/%d_%d.png",
							 [@"http://quran.com/images/ayat" UTF8String],
							 selectedAyah.sura, selectedAyah.ayah, nil]
							autorelease];
		NSURL *url = [NSURL URLWithString:urlStr];
		NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
		[webview loadRequest:requestObj];
	}
}

- (void) viewWillAppear:(BOOL)animated {
    [self refresh];
}

- (void)resultSelectionChanged:(Ayah *)curSelection {
	/* if (popover != nil)
		[popover dismissPopoverAnimated:YES]; */
	[self setSelectedAyah:curSelection];
	[self refresh];
}

- (void)splitViewController: (UISplitViewController*)svc 
	 willHideViewController:(UIViewController *)aViewController 
		  withBarButtonItem:(UIBarButtonItem*)barButtonItem 
	   forPopoverController: (UIPopoverController*)pc {
    barButtonItem.title = @"Search";    
	
    UINavigationItem *navItem = [self navigationItem];
    [navItem setLeftBarButtonItem:barButtonItem animated:YES];
	
    self.popover = pc;
}

- (void)splitViewController: (UISplitViewController*)svc 
	 willShowViewController:(UIViewController *)aViewController 
  invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {    
	
    UINavigationItem *navItem = [self navigationItem];
    [navItem setLeftBarButtonItem:nil animated:YES];
	
	[popover release];
	popover = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)dir {
    return YES;
}

- (void)dealloc {
	[webview release];
	[selectedAyah release];
	[popover release];
	[super dealloc];
}

@end
