//
//  IntroViewController.m
//  Allzic
//
//  Created by Alia Bourouis on 29/04/14.
//  Copyright (c) 2014 Alia Bourouis. All rights reserved.
//

#import "IntroViewController.h"
#import "AppDelegate.h"

@interface IntroViewController ()

@end

@implementation IntroViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.title = self.screenName = @"main";
    
    /*
    NSMutableDictionary *event;
    event = [[GAIDictionaryBuilder createEventWithCategory:@"UI"
                                                    action:@"navigation"
                                                     label:@"main"
                                                     value:nil] build];
    [[GAI sharedInstance].defaultTracker send:event];
    [[GAI sharedInstance] dispatch];
    */
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        UIImageView *background = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, appDelegate.screenBounds.size.width, appDelegate.screenBounds.size.height)];
        [background setImage:[UIImage imageNamed:@"tuto-640x1136_background"]];
        if (appDelegate.systemVersion < 7.0) {
            [background setFrame:CGRectMake(0.0f, 0.0f, appDelegate.screenBounds.size.width, appDelegate.screenBounds.size.height - 20.0f)];
        }
        if (appDelegate.screenBounds.size.height == 480.0) {
            [background setImage:[UIImage imageNamed:@"tuto-640x960_background"]];
        }
        [background setContentMode:UIViewContentModeScaleAspectFill];
        [background setClipsToBounds:YES];
        [self.view addSubview:background];
        
        scrollview = [[UIScrollView alloc]initWithFrame:background.frame];
        [scrollview setPagingEnabled:YES];
        [scrollview setDelegate:self];
        scrollview.backgroundColor = [UIColor clearColor];
        scrollview.showsHorizontalScrollIndicator = NO;
        [scrollview setContentSize:CGSizeMake(scrollview.frame.size.width * 7, scrollview.frame.size.height)];
        [self.view addSubview:scrollview];
    }
    return self;
}

- (void)displayTutorial {
    
    UIImageView *slideIntro1 = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollview.frame.size.width, scrollview.frame.size.height)];
    [slideIntro1 setImage:[UIImage imageNamed:@"tuto-640x1136_intro"]];
    if (appDelegate.screenBounds.size.height == 480.0) {
        [slideIntro1 setImage:[UIImage imageNamed:@"tuto-640x960_intro"]];
    }
    [slideIntro1 setContentMode:UIViewContentModeScaleAspectFit];
    [slideIntro1 setClipsToBounds:YES];
    [scrollview addSubview:slideIntro1];
    
    UIImageView *slide1 = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollview.frame.size.width, scrollview.frame.size.height)];
    [slide1 setImage:[UIImage imageNamed:@"tuto-640x1136_slide1"]];
    if (appDelegate.screenBounds.size.height == 480.0) {
        [slide1 setImage:[UIImage imageNamed:@"tuto-640x960_slide1"]];
    }
    [slide1 setContentMode:UIViewContentModeScaleAspectFit];
    [slide1 setClipsToBounds:YES];
    [scrollview addSubview:slide1];
    
    UIImageView *next1 = [[UIImageView alloc] initWithFrame:CGRectMake(slide1.frame.size.width - 30.0f, (slide1.frame.size.height / 2) - 40.0f, 16.0f, 28.0f)];
    [next1 setImage:[UIImage imageNamed:@"next"]];
    [next1 setContentMode:UIViewContentModeScaleAspectFit];
    [next1 setClipsToBounds:YES];
    [slide1 addSubview:next1];
    
    UIButton *nextButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButton1 setFrame:CGRectMake(slide1.frame.origin.x, slide1.frame.origin.y, slide1.frame.size.width, slide1.frame.size.height)];
    nextButton1.tag = 1;
    [nextButton1 addTarget:self action:@selector(goToNextSilde:) forControlEvents:UIControlEventTouchUpInside];
    [scrollview addSubview:nextButton1];
    
    
    
    UIImageView *slideIntro2 = [[UIImageView alloc] initWithFrame:CGRectMake(scrollview.frame.size.width, 0.0f, scrollview.frame.size.width, scrollview.frame.size.height)];
    [slideIntro2 setImage:[UIImage imageNamed:@"tuto-640x1136_intro"]];
    if (appDelegate.screenBounds.size.height == 480.0) {
        [slideIntro2 setImage:[UIImage imageNamed:@"tuto-640x960_intro"]];
    }
    [slideIntro2 setContentMode:UIViewContentModeScaleAspectFit];
    [slideIntro2 setClipsToBounds:YES];
    [scrollview addSubview:slideIntro2];
    
    UIImageView *slide2 = [[UIImageView alloc] initWithFrame:CGRectMake(scrollview.frame.size.width, 0.0f, scrollview.frame.size.width, scrollview.frame.size.height)];
    [slide2 setImage:[UIImage imageNamed:@"tuto-640x1136_slide2"]];
    if (appDelegate.screenBounds.size.height == 480.0) {
        [slide2 setImage:[UIImage imageNamed:@"tuto-640x960_slide2"]];
    }
    [slide2 setContentMode:UIViewContentModeScaleAspectFit];
    [slide2 setClipsToBounds:YES];
    [scrollview addSubview:slide2];
    
    UIImageView *next2 = [[UIImageView alloc] initWithFrame:CGRectMake(slide2.frame.size.width - 30.0f, (slide1.frame.size.height / 2) - 40.0f, 16.0f, 28.0f)];
    [next2 setImage:[UIImage imageNamed:@"next"]];
    [next2 setContentMode:UIViewContentModeScaleAspectFit];
    [next2 setClipsToBounds:YES];
    [slide2 addSubview:next2];
    
    UIButton *nextButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButton2 setFrame:CGRectMake(slide2.frame.origin.x, slide2.frame.origin.y, slide2.frame.size.width, slide2.frame.size.height)];
    nextButton2.tag = 2;
    [nextButton2 addTarget:self action:@selector(goToNextSilde:) forControlEvents:UIControlEventTouchUpInside];
    [scrollview addSubview:nextButton2];
    
    
    UIImageView *slide3 = [[UIImageView alloc] initWithFrame:CGRectMake(scrollview.frame.size.width * 2, 0.0f, scrollview.frame.size.width, scrollview.frame.size.height)];
    [slide3 setImage:[UIImage imageNamed:@"tuto-640x1136_slide3"]];
    if (appDelegate.screenBounds.size.height == 480.0) {
        [slide3 setImage:[UIImage imageNamed:@"tuto-640x960_slide3"]];
    }
    [slide3 setContentMode:UIViewContentModeScaleAspectFit];
    [slide3 setClipsToBounds:YES];
    [scrollview addSubview:slide3];
    
    UIImageView *next3 = [[UIImageView alloc] initWithFrame:CGRectMake(slide3.frame.size.width - 30.0f, (slide1.frame.size.height / 2) - 40.0f, 16.0f, 28.0f)];
    [next3 setImage:[UIImage imageNamed:@"next"]];
    [next3 setContentMode:UIViewContentModeScaleAspectFit];
    [next3 setClipsToBounds:YES];
    [slide3 addSubview:next3];
    
    UIButton *nextButton3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButton3 setFrame:CGRectMake(slide3.frame.origin.x, slide3.frame.origin.y, slide3.frame.size.width, slide3.frame.size.height)];
    nextButton3.tag = 3;
    [nextButton3 addTarget:self action:@selector(goToNextSilde:) forControlEvents:UIControlEventTouchUpInside];
    [scrollview addSubview:nextButton3];
    
    
    UIImageView *slide4 = [[UIImageView alloc] initWithFrame:CGRectMake(scrollview.frame.size.width * 3, 0.0f, scrollview.frame.size.width, scrollview.frame.size.height)];
    [slide4 setImage:[UIImage imageNamed:@"tuto-640x1136_slide4"]];
    if (appDelegate.screenBounds.size.height == 480.0) {
        [slide4 setImage:[UIImage imageNamed:@"tuto-640x960_slide4"]];
    }
    [slide4 setContentMode:UIViewContentModeScaleAspectFit];
    [slide4 setClipsToBounds:YES];
    [scrollview addSubview:slide4];
    
    UIImageView *next4 = [[UIImageView alloc] initWithFrame:CGRectMake(slide4.frame.size.width - 30.0f, (slide1.frame.size.height / 2) - 40.0f, 16.0f, 28.0f)];
    [next4 setImage:[UIImage imageNamed:@"next"]];
    [next4 setContentMode:UIViewContentModeScaleAspectFit];
    [next4 setClipsToBounds:YES];
    [slide4 addSubview:next4];
    
    UIButton *nextButton4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButton4 setFrame:CGRectMake(slide4.frame.origin.x, slide4.frame.origin.y, slide4.frame.size.width, slide4.frame.size.height)];
    nextButton4.tag = 4;
    [nextButton4 addTarget:self action:@selector(goToNextSilde:) forControlEvents:UIControlEventTouchUpInside];
    [scrollview addSubview:nextButton4];
    
    
    UIImageView *slide5 = [[UIImageView alloc] initWithFrame:CGRectMake(scrollview.frame.size.width * 4, 0.0f, scrollview.frame.size.width, scrollview.frame.size.height)];
    [slide5 setImage:[UIImage imageNamed:@"tuto-640x1136_slide5"]];
    if (appDelegate.screenBounds.size.height == 480.0) {
        [slide5 setImage:[UIImage imageNamed:@"tuto-640x960_slide5"]];
    }
    [slide5 setContentMode:UIViewContentModeScaleAspectFit];
    [slide5 setClipsToBounds:YES];
    [scrollview addSubview:slide5];
    
    UIImageView *next5 = [[UIImageView alloc] initWithFrame:CGRectMake(slide5.frame.size.width - 30.0f, (slide1.frame.size.height / 2) - 40.0f, 16.0f, 28.0f)];
    [next5 setImage:[UIImage imageNamed:@"next"]];
    [next5 setContentMode:UIViewContentModeScaleAspectFit];
    [next5 setClipsToBounds:YES];
    [slide5 addSubview:next5];
    
    UIButton *nextButton5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButton5 setFrame:CGRectMake(slide5.frame.origin.x, slide5.frame.origin.y, slide5.frame.size.width, slide5.frame.size.height)];
    nextButton5.tag = 5;
    [nextButton5 addTarget:self action:@selector(goToNextSilde:) forControlEvents:UIControlEventTouchUpInside];
    [scrollview addSubview:nextButton5];
    
    
    UIImageView *slide6 = [[UIImageView alloc] initWithFrame:CGRectMake(scrollview.frame.size.width * 5, 0.0f, scrollview.frame.size.width, scrollview.frame.size.height)];
    [slide6 setImage:[UIImage imageNamed:@"tuto-640x1136_slide6"]];
    if (appDelegate.screenBounds.size.height == 480.0) {
        [slide6 setImage:[UIImage imageNamed:@"tuto-640x960_slide6"]];
    }
    [slide6 setContentMode:UIViewContentModeScaleAspectFit];
    [slide6 setClipsToBounds:YES];
    [scrollview addSubview:slide6];
    
    UIImageView *next6 = [[UIImageView alloc] initWithFrame:CGRectMake(slide6.frame.size.width - 30.0f, (slide1.frame.size.height / 2) - 40.0f, 16.0f, 28.0f)];
    [next6 setImage:[UIImage imageNamed:@"next"]];
    [next6 setContentMode:UIViewContentModeScaleAspectFit];
    [next6 setClipsToBounds:YES];
    [slide6 addSubview:next6];
    
    UIButton *nextButton6 = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButton6 setFrame:CGRectMake(slide6.frame.origin.x, slide6.frame.origin.y, slide6.frame.size.width, slide6.frame.size.height)];
    nextButton6.tag = 6;
    [nextButton6 addTarget:self action:@selector(goToNextSilde:) forControlEvents:UIControlEventTouchUpInside];
    [scrollview addSubview:nextButton6];
    
    
    UIImageView *slide7 = [[UIImageView alloc] initWithFrame:CGRectMake(scrollview.frame.size.width * 6, 0.0f, scrollview.frame.size.width, scrollview.frame.size.height)];
    [slide7 setImage:[UIImage imageNamed:@"tuto-640x1136_slide7"]];
    if (appDelegate.screenBounds.size.height == 480.0) {
        [slide7 setImage:[UIImage imageNamed:@"tuto-640x960_slide7"]];
    }
    [slide7 setContentMode:UIViewContentModeScaleAspectFit];
    [slide7 setClipsToBounds:YES];
    [scrollview addSubview:slide7];
    
    UIButton *launchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [launchButton setFrame:CGRectMake(slide7.frame.origin.x, slide7.frame.origin.y, slide7.frame.size.width, slide7.frame.size.height)];
    [launchButton addTarget:self action:@selector(launchAppButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [scrollview addSubview:launchButton];
    
    tabbarScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0f, scrollview.frame.size.height - 87.0f, 352.0f, 87.0f)];
    tabbarScrollview.scrollEnabled = NO;
    tabbarScrollview.backgroundColor = [UIColor clearColor];
    [tabbarScrollview setContentSize:CGSizeMake(352.0f + tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height)];
    [self.view addSubview:tabbarScrollview];
    
    UIImageView *tabbarImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height)];
    [tabbarImage setImage:[UIImage imageNamed:@"tuto_tabbar"]];
    [tabbarImage setContentMode:UIViewContentModeScaleAspectFit];
    [tabbarImage setClipsToBounds:YES];
    [tabbarScrollview addSubview:tabbarImage];
    
    UIButton *nextButtonTabbar = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButtonTabbar setFrame:CGRectMake(0.0f, 0.0f, tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height)];
    [nextButtonTabbar addTarget:self action:@selector(goToNextSilde:) forControlEvents:UIControlEventTouchUpInside];
    [tabbarScrollview addSubview:nextButtonTabbar];
}

- (void)goToNextSilde:(UIButton *)sender {
    
    /*[scrollview scrollRectToVisible:CGRectMake((scrollview.frame.size.width * sender.tag), 0.0f, scrollview.frame.size.width, scrollview.frame.size.height) animated:YES];
    
    if (sender.tag <= 3) {
        [tabbarScrollview scrollRectToVisible:CGRectMake(0.0f, 0.0f, tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height) animated:YES];
    } else if (sender.tag == 4) {
        [tabbarScrollview setContentOffset:CGPointMake(32.0f, 0.0f) animated:YES];
    } else if (sender.tag > 4) {
        [tabbarScrollview scrollRectToVisible:CGRectMake(tabbarScrollview.contentSize.width - tabbarScrollview.frame.size.width, 0.0f, tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height) animated:YES];
    }
    */
    
    CGFloat pageWidth = scrollview.frame.size.width;
    float fractionalPage = scrollview.contentOffset.x / pageWidth;
    NSInteger currentPage = lround(fractionalPage);
    int nextPage = currentPage + 1;
    
    [scrollview scrollRectToVisible:CGRectMake((scrollview.frame.size.width * nextPage), 0.0f, scrollview.frame.size.width, scrollview.frame.size.height) animated:YES];
    
    if (nextPage <= 3) {
        [tabbarScrollview scrollRectToVisible:CGRectMake(0.0f, 0.0f, tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height) animated:YES];
    } else if (nextPage == 4) {
        [tabbarScrollview setContentOffset:CGPointMake(32.0f, 0.0f) animated:YES];
    } else if (nextPage > 4) {
        [tabbarScrollview scrollRectToVisible:CGRectMake(tabbarScrollview.contentSize.width - tabbarScrollview.frame.size.width, 0.0f, tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height) animated:YES];
    }
}

- (void)launchAppButtonPressed {
    
    [tabbarScrollview removeFromSuperview];
    
    UIView *homeView = [[UIView alloc] initWithFrame:CGRectMake(scrollview.contentSize.width, 0.0f, scrollview.frame.size.width, scrollview.frame.size.height)];
    [homeView setBackgroundColor:[UIColor whiteColor]];
    [scrollview addSubview:homeView];
    
    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake((homeView.frame.size.width - 82.0f)/2, 40.0f, 82.0f, 125.0f)];
    [logo setImage:[UIImage imageNamed:@"logo_home"]];
    [logo setContentMode:UIViewContentModeScaleAspectFit];
    [logo setClipsToBounds:YES];
    [homeView addSubview:logo];
    
    UIView *searchBackground = [[UIView alloc] initWithFrame:CGRectMake(20.0f, logo.frame.origin.y + logo.frame.size.height + 20.0f, homeView.frame.size.width - 40.0f, 38.0f)];
    [searchBackground setBackgroundColor:[UIColor whiteColor]];
    searchBackground.layer.borderWidth  = 0.5;
    searchBackground.layer.borderColor  = [UIColor colorWithRed:31.0f/255.0f green:139.0f/255.0f blue:186.0f/255.0f alpha:1.0f].CGColor;
    [homeView addSubview:searchBackground];
    
    UIImageView *searchImage = [[UIImageView alloc] initWithFrame:CGRectMake(searchBackground.frame.origin.x, searchBackground.frame.origin.y, searchBackground.frame.size.height, searchBackground.frame.size.height)];
    [searchImage setImage:[UIImage imageNamed:@"search_home"]];
    [searchImage setContentMode:UIViewContentModeScaleAspectFill];
    [searchImage setClipsToBounds:YES];
    [homeView addSubview:searchImage];
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setFrame:searchBackground.frame];
    [searchButton addTarget:self action:@selector(searchButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [homeView addSubview:searchButton];
    
    UIImageView *allzicImage = [[UIImageView alloc] initWithFrame:CGRectMake((homeView.frame.size.width / 2) - 100.0f, homeView.frame.size.height - 220.0f, 90.0f, 90.0f)];
    [allzicImage setImage:[UIImage imageNamed:@"bt_allzic"]];
    [allzicImage setContentMode:UIViewContentModeScaleAspectFill];
    [allzicImage setClipsToBounds:YES];
    [homeView addSubview:allzicImage];
    
    UIButton *allzicButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [allzicButton setFrame:allzicImage.frame];
    [allzicButton addTarget:self action:@selector(allzicButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [homeView addSubview:allzicButton];
    
    UIImageView *categoriesImage = [[UIImageView alloc] initWithFrame:CGRectMake((homeView.frame.size.width / 2) + 10.0f, homeView.frame.size.height - 220.0f, 90.0f, 90.0f)];
    [categoriesImage setImage:[UIImage imageNamed:@"bt_categories"]];
    [categoriesImage setContentMode:UIViewContentModeScaleAspectFill];
    [categoriesImage setClipsToBounds:YES];
    [homeView addSubview:categoriesImage];
    
    UIButton *categoriesButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [categoriesButton setFrame:categoriesImage.frame];
    [categoriesButton addTarget:self action:@selector(categoriesButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [homeView addSubview:categoriesButton];
    
    UIImageView *exploreImage = [[UIImageView alloc] initWithFrame:CGRectMake((homeView.frame.size.width / 2) - 100.0f, homeView.frame.size.height - 110.0f, 90.0f, 90.0f)];
    [exploreImage setImage:[UIImage imageNamed:@"bt_explorer"]];
    [exploreImage setContentMode:UIViewContentModeScaleAspectFill];
    [exploreImage setClipsToBounds:YES];
    [homeView addSubview:exploreImage];
    
    UIButton *exploreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [exploreButton setFrame:exploreImage.frame];
    [exploreButton addTarget:self action:@selector(exploreButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [homeView addSubview:exploreButton];
    
    UIImageView *favoriteImage = [[UIImageView alloc] initWithFrame:CGRectMake((homeView.frame.size.width / 2) + 10.0f, homeView.frame.size.height - 110.0f, 90.0f, 90.0f)];
    [favoriteImage setImage:[UIImage imageNamed:@"bt_favoris"]];
    [favoriteImage setContentMode:UIViewContentModeScaleAspectFill];
    [favoriteImage setClipsToBounds:YES];
    [homeView addSubview:favoriteImage];
    
    UIButton *favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [favoriteButton setFrame:favoriteImage.frame];
    [favoriteButton addTarget:self action:@selector(favoriteButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [homeView addSubview:favoriteButton];
    
    [scrollview setContentSize:CGSizeMake(scrollview.contentSize.width + scrollview.frame.size.width, scrollview.frame.size.height)];
    [scrollview setContentOffset:CGPointMake(scrollview.contentSize.width - scrollview.frame.size.width, 0.0f) animated:YES];
    [scrollview setScrollEnabled:NO];
}

- (void)searchButtonPressed {
    
    [appDelegate.dataController.userPreferences setValue:@"1" forKey:@"isIntroAlreadyShown"];
    [appDelegate.dataController.userPreferences synchronize];
    [appDelegate setOldVC:appDelegate.allzicVC];
    [appDelegate.searchVC refreshTableWithData:nil];
    [appDelegate.window setRootViewController:appDelegate.searchVC];
    appDelegate.isSearching = YES;
    [appDelegate.searchVC.searchTextField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.2];
}

- (void)allzicButtonPressed {
    
    [appDelegate.dataController.userPreferences setValue:@"1" forKey:@"isIntroAlreadyShown"];
    [appDelegate.dataController.userPreferences synchronize];
    [appDelegate.window setRootViewController:appDelegate.allzicVC];
}

- (void)categoriesButtonPressed {
    
    [appDelegate.dataController.userPreferences setValue:@"1" forKey:@"isIntroAlreadyShown"];
    [appDelegate.dataController.userPreferences synchronize];
    [appDelegate.window setRootViewController:appDelegate.categoriesVC];
}

- (void)exploreButtonPressed {
    
    [appDelegate.dataController.userPreferences setValue:@"1" forKey:@"isIntroAlreadyShown"];
    [appDelegate.dataController.userPreferences synchronize];
    [appDelegate.window setRootViewController:appDelegate.exploreVC];
}

- (void)favoriteButtonPressed {
    
    [appDelegate.dataController.userPreferences setValue:@"1" forKey:@"isIntroAlreadyShown"];
    [appDelegate.dataController.userPreferences synchronize];
    [appDelegate.favoriteVC refreshTableWithData:[NSArray arrayWithObjects:appDelegate.dataController.favoriteCategoryArray,appDelegate.dataController.favoriteRadioArray, nil]];
    [appDelegate.window setRootViewController:appDelegate.favoriteVC];
}


//-----------------------------------------
//		ScrollView Delegate
//-----------------------------------------

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
	
	CGFloat pageWidth = scrollView.frame.size.width;
    float fractionalPage = scrollView.contentOffset.x / pageWidth;
    NSInteger page = lround(fractionalPage);
    if (page <= 3) {
        [tabbarScrollview scrollRectToVisible:CGRectMake(0.0f, 0.0f, tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height) animated:YES];
    } else if (page == 4) {
        [tabbarScrollview setContentOffset:CGPointMake(32.0f, 0.0f) animated:YES];
    } else if (page > 4) {
        [tabbarScrollview scrollRectToVisible:CGRectMake(tabbarScrollview.contentSize.width - tabbarScrollview.frame.size.width, 0.0f, tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height) animated:YES];
    }
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
	if (!decelerate) {
		CGFloat pageWidth = scrollView.frame.size.width;
        float fractionalPage = scrollView.contentOffset.x / pageWidth;
        NSInteger page = lround(fractionalPage);
        if (page <= 3) {
            [tabbarScrollview scrollRectToVisible:CGRectMake(0.0f, 0.0f, tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height) animated:YES];
        } else if (page == 4) {
            [tabbarScrollview setContentOffset:CGPointMake(32.0f, 0.0f) animated:YES];
        } else if (page > 4) {
            [tabbarScrollview scrollRectToVisible:CGRectMake(tabbarScrollview.contentSize.width - tabbarScrollview.frame.size.width, 0.0f, tabbarScrollview.frame.size.width, tabbarScrollview.frame.size.height) animated:YES];
        }
    }
    
}
//-----------------------------------------


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
