$(function() {
    
    // External Links
    $('a[rel=external]')
        .click(
            function(e) {
                open(this.href);
                e.preventDefault();
            }
        )
        .attr('title', 'Opens in a new window');
        
    // AddThis
    $('.addthis_button_facebook_like').attr('fb:like:layout', 'button_count');
    
    // Useful Stuff
    $('li:last-child').addClass('last-item');
    
    $('input.default')
        .focus(
            function() {
                if ($(this).val() == $(this)[0].title) {
                    $(this).val('');
                }
            }
        )
        .blur(
            function() {
                if ($(this).val() == '') {
                    $(this).val($(this)[0].title);
                }
            }
        )
        .blur();
        
    $('input[type=submit]').addClass('submit');
        
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
    }
    else {
        $('input, select, textarea')
            .blur(
                function() {
                    $(this).removeClass('focus');
                }
            )
            .focus(
                function() {
                    $(this).addClass('focus');
                }
            )
            .hover(
                function() {
                    $(this).addClass('hover');
                },
                function() {
                    $(this).removeClass('hover');
                }
            );
    }
    
    // Manipulate Umbraco's image alignment
    $('#content img')
        .each(
            function(e) {
                if ($(this).css('float') == 'left') {
                    $(this).addClass('left');    
                }
                else if ($(this).css('float') == 'right') {
                    $(this).addClass('right');
                }
            }
        );
        
    // Study Venues
    $('.venues ol li')
        .hover(
            function() {
                var venue = $(this).attr('class').replace(' last-item', '');
                
                $('.venues .venue:not([class="venue ' + venue + '"])').stop().fadeTo(400, 0.2);
            },
            function() {
                var venue = $(this).attr('class').replace(' last-item', '');
                
                $('.venues .venue:not([class="venue ' + venue + '"])').stop().fadeTo(400, 1.0);
            }
        );
        
    // Fun Stuff > Quiz
    $('.quiz .answer').hide();
    
    $('.quiz .reveal-answer')
        .click(
            function(e) {
                $('.quiz .answer').fadeIn();
                e.preventDefault();
            }
        );
        
    // Animation
    if ($('.animation').length) {
        
        $('.animation .controls a:first').addClass('selected');
        
        $('.animation .panels').css({ height : $('.window').height() * $('.panels .panel').size() });
        
        rotate = function() {
            $('.animation .controls a').removeClass('selected');
            
            $selected.addClass('selected');
            
            $('.animation .panels').stop().animate({ top : -($selected.attr('rel') - 1) * $('.window').height() }, 1000);
        };
        
        rotateSwitch = function() {
            play = setInterval(function() {
                $selected = $('.animation .controls a.selected').next();
                if ($selected.length === 0) {
                    $selected = $('.animation .controls a:first');
                }
                rotate();
            }, 4000);
        };
        
        //rotateSwitch(); NCLT asked banner to rotate 1 direction
        
        $('.animation .panels .panel')
            .hover(
                function() {
                    clearInterval(play);
                },
                function() {
                    rotateSwitch();
                }
            );
        
        $('.animation .controls a')
            .click(
                function(e) {
                    clearInterval(play);
                    $selected = $(this);
                    rotate();
                    rotateSwitch();
                    e.preventDefault();
                }
            );
    }
    
    // Fancybox
    $('.fancybox-download')
        .fancybox(
            {
                height         : 340,
                overlayColor   : '#000',
                overlayOpacity : .7,
                padding        : 30,
                titlePosition  : 'none',
                transitionIn   : 'elastic',
                transitionOut  : 'elastic',
                type           : 'iframe',
                width          : 540
            }
        );
    
    // Fancybox YouTube Links
    $('a[href*="youtube.com/watch?v="]')
        .click(
            function(e) {
                $.fancybox({
                    autoScale      : false,
                    height         : 385,
                    href           : this.href.replace(new RegExp('watch\\?v=', 'i'), 'v/'),
                    overlayColor   : '#000',
                    overlayOpacity : .7,
                    swf            : {
                                         allowfullscreen : 'true',
                                         wmode           : 'transparent'
                                     },
                    titlePosition  : 'none',
                    transitionIn   : 'elastic',
                    transitionOut  : 'elastic',
                    type           : 'swf',
                    width          : 640
                });
                e.preventDefault();
            }
        );
        
    // Search Form
    $('.search input[type=submit]')
        .click(
            function(e) {
        if ($('.search input[type=text]').val() == $('.search input[type=text]').attr('title')) {
          $('.search input[type=text]').stop()
            .animate({ left : '-12px' }, 100).animate({ left : '8px' }, 100)
            .animate({ left : '-8px' }, 100).animate({ left : '6px' }, 100)
            .animate({ left : '-4px' }, 100).animate({ left : '2px' }, 100)
            .animate({ left : '0' }, 100);
          e.preventDefault();
        }
      }
        );
        
    // Download Application Form
    $('.apply input[type=submit]')
        .click(
            function(e) {
        if ($('.apply select').val() == '0') {
          $('.apply select').stop()
            .animate({ left : '-12px' }, 100).animate({ left : '8px' }, 100)
            .animate({ left : '-8px' }, 100).animate({ left : '6px' }, 100)
            .animate({ left : '-4px' }, 100).animate({ left : '2px' }, 100)
            .animate({ left : '0' }, 100);
        }
        else {
          $.fancybox({
            height         : 340,
            href           : '/request-application.aspx?courseID=' + $('.apply select').val(),
            onClosed       : function() {
                                 if ($.browser.msie && $.browser.version.substr(0, 1) < 9) {
                                     reloadStylesheets();
                                 }
                     },
            overlayColor   : '#000',
            overlayOpacity : .7,
            padding        : 30,
            titlePosition  : 'none',
            transitionIn   : 'elastic',
            transitionOut  : 'elastic',
            type           : 'iframe',
            width          : 540
          });
        }
                e.preventDefault();
            }
        );
        
    // Course Venues Form
    $('.course-venues input[type=submit]')
        .click(
            function(e) {
        if ($('.course-venues select').val() == '0') {
          $('.course-venues select').stop()
            .animate({ left : '-12px' }, 100).animate({ left : '8px' }, 100)
            .animate({ left : '-8px' }, 100).animate({ left : '6px' }, 100)
            .animate({ left : '-4px' }, 100).animate({ left : '2px' }, 100)
            .animate({ left : '0' }, 100);
          e.preventDefault();
        }
      }
        );
    
    // Internal Page Links
    $('a[href*=#]')
        .click(
            function(e) {
                var target = $(this.hash + ', a[name=' + this.hash.slice(1) + ']').offset().top,
                    oldLocation = window.location.href.replace(window.location.hash, ''),
                    newLocation = this;

                if (oldLocation + this.hash == newLocation) {
                    if ($.browser.safari) {
                        var animationSelector = 'body:not(:animated)';
                    }
                    else {
                        var animationSelector = 'html:not(:animated)';
                    }

                    $(animationSelector).animate({ scrollTop: target }, 1000, 'swing', function() { window.location.href = newLocation; });

                    e.preventDefault();
                }
            }
        );
});

// Reload Stylesheets
function reloadStylesheets() {
  var queryString = '?reload=' + new Date().getTime();
  $('link[rel="stylesheet"]')
    .each(
      function () {
        this.href = this.href.replace(/\?.*|$/, queryString);
      }
    );
}
