$(function() {
  $('a[rel="external"]').each(function() {
    $(this).attr('target', '_blank')
  });
});
// Load google map
$(function() {
  if ($('#googlemap').get().length > 0) {
    var mapOptions = {
      zoom: 16,
      center: new google.maps.LatLng(54.60309024627787, -5.930289030075073 ),
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      navigationControl: true,
      navigationControlOptions: {
        style: google.maps.NavigationControlStyle.ZOOM_PAN
      },
      mapTypeControl: false
    };
    var map = new google.maps.Map($('#googlemap').get(0), mapOptions);
    var logoImage = new google.maps.MarkerImage('/images/map-logo.png',
      new google.maps.Size(100, 64), // size
      new google.maps.Point(0, 0),  // origin
      new google.maps.Point(80, 62)// anchor
    );
    var logo = new google.maps.Marker({
      position: new google.maps.LatLng(54.60309024627787, -5.930289030075073 ),
      map: map,
      icon: logoImage,
      clickable: false,
      title: 'The Style Academy'
    });
  }
});
// fade out instructions and error messages
/*
$(function() {
  $('#instructions').fadeOut(8000)
  if (document.getElementById('errors') && $('#errors').hasClass('fade')) {
    $('#errors').fadeOut(8000)
  }
});
*/
// hide form labels on focus
$(function() {
  $('input[type=text], textarea').focus(function() {
    if ($(this).hasClass('inline-label')) {
      if (typeof $(this).attr('rel') == 'undefined') {
        $(this).attr('rel', $(this).attr('value'))
      }
      if ($(this).attr('value') == $(this).attr('rel')) {
        $(this).attr('value', '')
      }
    }
  })
  $('input[type=text], textarea').blur(function() {
    if ($(this).hasClass('inline-label')) {
      if ($(this).attr('value') == '') {
        $(this).attr('value', $(this).attr('rel'))
      }
    }
  })
});

$(function() {
    $('div.model-photos-row img').mouseover(function() {
        $('#enlarged').attr('src', $(this).attr('src').replace('-thumb.jpg', '.jpg'))
    })
})

$(function (){
    if (typeof Moo == 'undefined') {
        return
    }
    slideshowId = Moo.$('homeSlideshow') ? 'homeSlideshow' : (Moo.$('slideshow') ? 'slideshow' : false)
    if (!slideshowId) {
        return false
    }
    var slideshow = new gallery(Moo.$(slideshowId), {
        elementSelector: 'div.slide',
        titleSelector: 'h2',
        timed: true,
        delay: 5000,
        embedLinks: false,
        showInfopane: true,
        showArrows: false
    });
})

Cufon.replace([
    'h1',
    //'h2',
    //'h3',
    'div#secondary-nav li a',
    'div#nav li div a',
    'div#nav li li a'
  ], {
    hover: true
  }
)

// preload images
$(function() {
    $('div.model-photos-row img').each(function() {
        $('<img />').attr('src', $(this).attr('src').replace(/-thumb/, '')).attr('width', 409).attr('width', 523)
    })
})

$(function() {
    if (typeof $.fancybox == 'undefined') {
        return
    }
    $('#events-team a').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'padding'       :   1
	});
})

$(function() {
    $('#guys').css('display', 'none')
    $('#sex_male').change(function() {
        if ($(this).attr('checked')) {
            $('#guys').css('display', 'block')
            $('#girls').css('display', 'none')
        }
    })
    $('#sex_female').change(function() {
        if ($(this).attr('checked')) {
            $('#guys').css('display', 'none')
            $('#girls').css('display', 'block')
        }
    })
})
// Gallery section functionality
$(function() {
    if ($('#gallery-slideshow').get().length == 1 || $('#youtube-player').get().length == 1) {
        // hide all but first large image
        $('#gallery-slideshow li:gt(0)').hide()
        // add click functionality to thumbnails
        $('ul.gallery-thumbnails a').click(function(event) {
            event.preventDefault()
            $(this).parent().parent().find('li').removeClass('item-selected')
            $(this).parent().addClass('item-selected')
            var id = $(this).attr('rel')
            $('#gallery-slideshow li').hide()
            $('#' + id).show()
        })
        // add click functionality to youtube thumbnails
        $('ul.youtube-thumbnails a').click(function(event) {
            event.preventDefault()
            $(this).parent().parent().find('li').removeClass('item-selected')
            $(this).parent().addClass('item-selected')
            var youtubeID = $(this).attr('rel')
            $('#youtube-player h2').text($(this).attr('title'))
            $('#youtube-player p').text($(this).find('img').attr('alt'))
            if (ytplayer) {
                ytplayer.loadVideoById(youtubeID)
            }
        })
        // start the carousel
        $('#carousel').jcarousel({
            scroll: 4
        });
        // show first thumbnail is selected
        $('ul.gallery-thumbnails li:first').addClass('item-selected')
        $('ul.youtube-thumbnails li:first').addClass('item-selected')
    }
})

$(function() {
    if ($('#ytapiplayer').get().length > 0) {
        var youtubeID = $('#ytapiplayer').attr('title')
        var params = { allowScriptAccess: 'always' };
        var atts = { id: 'myytplayer' };
        swfobject.embedSWF('http://www.youtube.com/v/' + youtubeID + '?enablejsapi=1&playerapiid=ytplayer&rel=0&showsearch=0', 'ytapiplayer', '595', '360', '8', null, null, params, atts);
    }
})

function onYouTubePlayerReady(playerId)
{
    ytplayer = document.getElementById('myytplayer')
    //ytplayer.playVideo()
}

$(function() {
    $('#register form').submit(function() {
        var valid = true
        $('#register .required').each(function() {
            if (valid && $(this).get(0).tagName == 'INPUT' && ($(this).val() == '' || $(this).val().toLowerCase() == $(this).attr('title'))) {
                alert('Please enter a valid ' + $(this).attr('title'))
                valid = false
            }
        })
        return valid
    })
})
