// detectBrowser
// Checks the browser and adds classes to the body to reflect it.
function detectBrowser() {
    var userAgent = navigator.userAgent.toLowerCase();
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
    
    // Is this a version of IE?
    if($.browser.msie){
        $('body').addClass('browserIE');
        
        // Add the version number
        $('body').addClass('browserIE' + $.browser.version.substring(0,1));
    }

    // Is this a version of Chrome?
    if($.browser.chrome){
    
        $('body').addClass('browserChrome');
        
        //Add the version number
        userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('browserChrome' + userAgent);
        
        // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
        $.browser.safari = false;
    }
    
    // Is this a version of Safari?
    if($.browser.safari){
        $('body').addClass('browserSafari');
        
        // Add the version number
        userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('browserSafari' + userAgent);
    }
    
    // Is this a version of Mozilla?
    if($.browser.mozilla){
        
        //Is it Firefox?
        if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
            $('body').addClass('browserFirefox');
            
            // Add the version number
            userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
            userAgent = userAgent.substring(0,1);
            $('body').addClass('browserFirefox' + userAgent);
        }
        // If not then it must be another Mozilla
        else{
            $('body').addClass('browserMozilla');
        }
    }
    
    // Is this a version of Opera?
    if($.browser.opera){
        $('body').addClass('browserOpera');
    }
}
// document ready
$(function() {
	detectBrowser();
	
	// navigation fadein/fadeout effects for DESKTOPS
	if ($('body').hasClass('ipad') == false) {
		$('li.nav a').hover(
			function(){
				$(this).find('span').fadeIn(300);
			},
			function(){
				$(this).find('span').fadeOut(300);
			}
		);
		$('#contact .social a').hover(
			function(){
				$(this).find('span').fadeIn(300);
			},
			function(){
				$(this).find('span').fadeOut(300);
			}
		);
	}
	
	// .hover state slideup effects
	if ($('.home-thumb-blank').length) {
		$('.home-thumb-blank a').hover(
			function(){
				$(this).find('.hover').animate({height:'toggle'}, 300);
			},
			function(){
				$(this).find('.hover').animate({height:'toggle'}, 300);
			}
		);
	}
	if ($('.home-video-thumb').length) {
		$('.home-video-thumb a').hover(
			function(){
				$(this).find('.hover').animate({height:'toggle'}, 300);
			},
			function(){
				$(this).find('.hover').animate({height:'toggle'}, 300);
			}
		);
	}

	if ($('.home-thumb').length) {
		$('.home-thumb a').hover(
			function(){
				$(this).find('.hover').animate({height:'toggle'}, 300);
			},
			function(){
				$(this).find('.hover').animate({height:'toggle'}, 300);
			}
		);
	}
	if ($('.reel-thumb').length) {
		$('.reel-thumb a').hover(
			function(){
				$(this).find('.hover').animate({height:'toggle'}, 300);
			},
			function(){
				$(this).find('.hover').animate({height:'toggle'}, 300);
			}
		);
	}
	
	// contact area show/hide
	if ($('body').hasClass('ipad')) {
		$('.contact-trigger').toggle(
			function(){
				$('html, body').animate({scrollTop: $('#contact-bg').height()}, 300);
			},
			function(){
				$('html, body').animate({scrollTop: -($('#contact-bg').height())}, 300);
			}
		);
	} else {
		$('.contact-trigger').click(function(){
			if ($('#contact-bg').is(':visible')) {
				$('#contact-bg').slideUp(300);
			} else {
				$('#contact-bg').slideDown(300, function(e){
					$('html, body').animate({scrollTop: $('#contact-bg').height()}, 300);
				});
			}
			return false;
		});
	}
	
	// books page events
	if ($("#tabs").length) {
		// events pagination tabs
		$("#tabs").tabs("div.pane");
	}
	if ($('a.view-all-events').length) {
		if ($('body').is('.browserSafari, .browserChrome')) {
			// Webkit behaviors
			$('a.view-all-events, a.view-all-events-trigger').click(function(){
				if ($("#f1_container").hasClass('hover_effect')) {
					$("#f1_container").removeClass('hover_effect');
				} else {
					$("#f1_container").addClass('hover_effect');
				}
				return false;
			});
		} else {
			// All other browsers do this
			$('.face.back').hide();
			$('a.view-all-events, a.view-all-events-trigger').click(function(){
				if ($(".face.back").is(':hidden')) {
					$(".face.back").fadeIn(500);
					$(".front.back").fadeOut(500);
				} else {
					$(".face.back").fadeOut(500);
					$(".front.back").fadeIn(500);
				}
				return false;
			});
		}
	}
	if ($('#video-thumbs .video-thumb').length) {
		$('#video-thumbs .video-thumb').hover(
			function(){
				$(this).find('span').animate({height:'toggle'}, 300);
			},
			function(){
				$(this).find('span').animate({height:'toggle'}, 300);
			}
		);
	}
	
	// subnav show/hide
	$('.gallery, .gallery-current').click(function(){
		$('.sub_nav').fadeOut(300);//$('#books_nav').fadeOut(300);
		if ($('#album_nav').is(':visible')) {
			$('#album_nav').fadeOut(300);
		} else {
			$('#album_nav').animate({height:'toggle'}, 300);
		}
		return false;
	});
	$('.books, .books-current').click(function(){
		$('.sub_nav').fadeOut(300);//$('#album_nav').fadeOut(300);
		if ($('#books_nav').is(':visible')) {
			$('#books_nav').fadeOut(300);
		} else {
			$('#books_nav').animate({height:'toggle'}, 300);
		}
		return false;
	});
	$('.reel, .reel-current').click(function(){
		$('.sub_nav').fadeOut(300);//$('#videos_nav').fadeOut(300);
		if ($('#videos_nav').is(':visible')) {
			$('#videos_nav').fadeOut(300);
		} else {
			$('#videos_nav').animate({height:'toggle'}, 300);
		}
		return false;
	});
	$('.sub_nav .close').click(function(){
		var parent = $(this).parent();
		if (parent.is(':visible')) {
			parent.fadeOut(300);
		}
	});
});
