$(document).ready(function(){

    // Bottom menu additional class
    $('input[type="image"]').addClass('input-submit');
    
    // Toggle box
    $('.toggle-box').each(function(){
        $(this).prepend('<p class="toggle-para toggle-open">[+] Show more content</p>');
        $(this).children('.toggle-para').click(function(){            
            if ($(this).hasClass('toggle-open')) {
                $(this).text('[-] Show less content').removeClass('toggle-open').addClass('toggle-close');
                $(this).siblings('.toggle').show('normal');
            } else {
                $(this).text('[+] Show more content').removeClass('toggle-close').addClass('toggle-open');
                $(this).siblings('.toggle').hide('normal');
            }
        });
    });
    
    // Screenshot tabs
    $('.tabs-nav').tabs('.tabs > .tab', {
        current: 'here'
    });
    
    // Scrollable screenshots
    $('.scrollable').before('<span class="scrollable-nav"><span class="prev browse left">Previous</span><span class="next browse right">Next</span></span>');
    $('.scrollable').scrollable({
        speed: 800
    });
    
    // Scrollable press articles
    $('.scrollable-vertical').after('<span class="scrollable-nav-vertical"><span class="prev browse left">Previous</span><span class="next browse right">Next</span></span>');
    $('.scrollable-vertical').scrollable({
        vertical: true,
        circular: true
    }).autoscroll({
        interval: 5000
    });

    // Fancybox for Vimeo video
    $('.vimeo').fancybox({
        'padding': 0,
        // This is weird... Width x height should be 640x360,
        // but Fancybox seems to append an extra zero, so 640x360 would create 6400x3600,
        // so this is 64x36 now.
        'width': '64',
        'height': '36',
        'autoScale': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'easingIn': 'swing',
        'easingOut': 'swing',
        'type': 'iframe'
    });

    // Fancybox for screenshots
    $('.overlay').fancybox({
        'overlayColor'  : '#333',
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic'
    });
    
    // Subjects select menu
    $('#dropdown-tests').change(function(){
        if ($(this).attr('value') != $(this).children('option:first-child').text()) {
            window.location.href = $(this).attr('value');            
        }
    });

    // Checkout removal; expect Ajax magic here in the future
    $('.invoice').each(function(){
        $(this).find('.remove').click(function(e){
            $(this).parent().parent().hide('normal');
            e.preventDefault();
        });
    });
    
    // Disabled button when checking out
    $('.form-checkout input[type="submit"]').submit(function(e){
        $(this).attr('value', 'Please wait...');
        var loader = $('<img src="../img/ajax-loader.gif" alt="Loading">');
    });
    
    // Sticky footer
    $('#footer, #menu').stickyFooter();

    // Add form classes for easier styling
    $('input[type="button"]').addClass('input-button');
    $('input[type="checkbox"]').addClass('input-checkbox');
    $('input[type="file"]').addClass('input-file');
    $('input[type="hidden"]').addClass('input-hidden');
    $('input[type="image"]').addClass('input-image');
    $('input[type="password"]').addClass('input-text').addClass('input-password');
    $('input[type="radio"]').addClass('input-radio');
    $('input[type="reset"]').addClass('input-reset');
    $('input[type="submit"]').addClass('input-submit');
    $('input[type="text"]').addClass('input-text');
    
    // Add form classes for HTML5 input element types
    $('input[type="email"]').addClass('input-text input-email');
    $('input[type="url"]').addClass('input-text input-url');
    $('input[type="number"]').addClass('input-text input-number');
    $('input[type="range"]').addClass('input-range');
    $('input[type="date"]').addClass('input-date');
    $('input[type="datetime"]').addClass('input-datetime');
    $('input[type="month"]').addClass('input-month');
    $('input[type="week"]').addClass('input-week');
    $('input[type="time"]').addClass('input-time');
    $('input[type="search"]').addClass('input-text input-search');
    $('input[type="color"]').addClass('input-color');

    // Force labels of checkbox and radio elements back to inline
    $('input[type="checkbox"], input[type="radio"]').each(function(){
        $(this).next('label').css({ 'display': 'inline' });
    });

    
    // Add PNG class to target with DD_belatedPNG.fix('.bg_png');
    $('img[src$=".png"]').addClass('bg_png');
    
    // Table zebra rows
    // Remember that "nth-child" is 1-indexed, so odd is the first row
    $('table tbody tr:nth-child(odd) td').addClass('odd');
    $('table tbody tr:nth-child(even) td').addClass('even');
    
    // Focus on an element, probably a text input
    $('.focus').focus();
    
    // Text input placeholder replacement
    $('.placeholder').each(function(){

        // Set CSS, abstract this later
        var blur = {
            'color': '#999',
            'fontWeight': 'bold'
        };
        var focus = {
            'color': $(this).css('color'),
            'fontWeight': $(this).css('font-weight')
        };
        
        $(this).css({ 'color': blur.color, 'font-weight': blur.fontWeight });

        if ($(this).attr('value')) {
            var placeholder = $(this).attr('value');
            $(this).focus(function(){
                if ($(this).attr('value') == placeholder) {
                    $(this).css({ 'color': focus.color, 'font-weight': focus.fontWeight });
                    $(this).attr('value', '');
                }
            });
            $(this).blur(function(){
                if ($(this).attr('value') == '') {
                    $(this).css({ 'color': blur.color, 'font-weight': blur.fontWeight });
                    $(this).attr('value', placeholder);
                }
            });
        } else {
            var placeholder = $(this).text();
            $(this).focus(function(){
                if ($(this).text() == placeholder) {
                    $(this).css({ 'color': focus.color, 'font-weight': focus.fontWeight });
                    $(this).text('');
                }
            });
            $(this).blur(function(){
                if ($(this).text() == '') {
                    $(this).css({ 'color': blur.color, 'font-weight': blur.fontWeight });
                    $(this).text(placeholder);
                }
            });
        }
    });

    // Create a Google Map link
    /*$('.address').each(function(){
        var address = $(this).text().replace(/\ /gi, '+');
        var url = 'http://maps.google.com/?q=' + address;
        $(this).wrap('<a href="' + url + '" />');
    });*/
    
    // Printer link
    $('.print').each(function(){
        $(this).text('Print');
        $(this).click(function(e){
            window.print();
            e.preventDefault();
        });
    });
    
    // New window
    $('a[href^="http://"], a[rel="external"]').click(function(e){
        if(!$(this).hasClass('vimeo')) {
            window.open($(this).attr('href'));
            e.preventDefault();
        }
    });    
    
    // Preload images
    $.fn.preloadImage = function(url, callback){
        return this.each(function(){
            var img = new Image();
            $(img).load(callback).attr('src', url);
        });
    }
    
    // Round image corners by rounding container corners with background image
    $('.round').each(function(){
        var span = $('<span/>', {
            'class': $(this).attr('class'),
            'style': 'background: url(' + $(this).attr('src') + ') no-repeat center center; width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px;'
        });
        $(this).wrap(span).css('opacity', 0);
    });
    
    // Smooth scrolling
    $.fn.smoothScrollTo = function(e){
        return this.live('click', function(){
            $('html,body').animate({ 'scrollTop': $($(this).attr('href')).offset().top-40+'px' }); // 40px buffer to top.
            e.preventDefault();
        });
    }
    
    // Vertically align an element
    // (Not a perfect script, may have to change parent() traversal or margin-top)
    $.fn.vAlign = function(){
        return this.each(function(){
            var ah = $(this).height();
            var ph = $(this).parent().height();
            var mh = (ph - ah) / 2;
            $(this).css('margin-top', mh);
        });
    }


    // Internet Explorer-specific junk HTML
    // Append them here to keep HTML valid :)
    $('head').append('<meta http-equiv="imagetoolbar" content="false">');
    $('head').append('<meta name="MSSmartTagsPreventParsing" content="true">');

    // Internet Explorer trigger haslayout
    // Ex: $('.haslayout').triggerHasLayout();
    $.fn.triggerHasLayout = function(){ 
        return  (! $.browser.msie) ? this : this.each(function(){
            $(this).css('zoom', ($(this).css('zoom') == 1) ? 0 : 1 );  
        });
    };
    
    // IE6 background image hover flicker fix
    try {
        document.execCommand('BackgroundImageCache', false, true);
    } catch(err) {
        
    }

    // Mobile device detection
    if (/(iPhone|iPod)/.test(navigator.userAgent)) {
        
        // If you come from the homepage and you're on an iPhone, redirect no matter what
        // Could get annoying during a single session but, I doubt it
        if ((document.referrer != window.location.href + 'iphone/') && (window.location.pathname == '/')) {
            window.location.replace('/iphone/');
        }
        
        // Hide Apple mobile device address bar
        setTimeout(function() {
            window.scrollTo(0, 1)
        }, 100);
        
        // Prepend iPhone message for iPhones and iPod Touches
        $('#header').prepend('<div id="iphone" class="clearfix"><p><a href="/iphone/"><span class="float-l"><img src="/img/button-store-splash.png" alt="Available on the iPhone App Store"></span><span>Using an <strong>iPhone</strong> or <strong>iPod Touch</strong>?<br><strong class="link">Download our test prep apps</strong> in the iPhone App Store</span></a></p></div>');
    }

	    // cycle slide show on landing pag
		$('.slideshow').cycle({
			fx: 'scrollLeft', 
			    sync:   0, 
			    delay: -1000,
				speed: 400,
				easeIn: null,
				easeOut: null
		});

		$(".slideshow").fadeIn("slow");

    
});

// jQuery "No conflict" version
// 
// var $j = jQuery.noConflict();
// $j(document).ready(function(){
// 
//     $j('#selector').method();
// 
// });

