// Sitewide scripts using jquery
//requires includes/scripts.php

$(document).ready(function() { 
//create rollover images and prelode by passing "true"
	//$('img.rollover').rollover(true);
	
//activate png transparency fix
	//$(document).pngFix();
	
//back in history button
	$('a.btn_back').click(function() {
		history.go(-1);
		return false;
	});
	
// Add target="_blank" to external links
	$("a").filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}).attr("target", "_blank");

//remove the outline of a link when clicked
	$("a").click(function() {
		this.blur();
	});
	
//Function to hide email addresses
  $('span.mailme').each(function(){
    var $this = $(this);
    var at = / everyone /;
    var dot = / goes /g;
    var addr = $($this).text().replace(at, "@").replace(dot, ".");
    $($this).after('<a href="mailto:' + addr + '">' + addr + '</a>');
    $($this).remove();
  });

//colorbox photo gallery
	$('.gallery a').colorbox();
	$().bind('cbox_open', function(){
        $('embed, object, select').css({'visibility' : 'hidden'});
	});
	$().bind('cbox_closed', function(){
        $('embed, object, select').css({'visibility' : 'visible'});
	});

//function to embed a youtube video into a colorbox overlay
  var video_embed = function(object, url) {
    //video size
    var v_width  = '640', //must be at least 425px to allow the resolution controls to be present
        v_height = '390'; //added 30px for the control bar

    var href = url + '&amp;hl=en_US&amp;fs=1&amp;autoplay=1&amp;rel=0&amp;wmode=transparent'; // &hd=1 to force HD
    var embed = '<iframe class="youtube-player" type="text/html" width="' + v_width + '" height="' + v_height + '" src="' + url + '" frameborder="0" allowfullscreen></iframe>';

    object.colorbox({
      html: embed,
      innerWidth: v_width,
      innerHeight: v_height
    });
  }

// turn links to youtube into colorbox popups
  $("a.video-link").each(function() {
    //rewrite url
    var href = $(this).attr("href");
    //replace the standard youtube url to the embed format
    var results = href.match("[\\?&/]v[/=]([^&#]*)");
    //html5 video from youtube
    var url = ( results === null ) ? href : 'http://www.youtube.com/embed/' + results[1];

    video_embed($(this), url);
  });

//get the latest video
  $.getJSON('http://gdata.youtube.com/feeds/users/HipTonesMusic/uploads?alt=json-in-script&callback=?&max-results=1', function(data) {
    $.each(data.feed.entry, function(i, item) {
      var title = item['title']['$t'];
      var video = item['id']['$t'];
      var tn = item['media$group']['media$thumbnail'][1]['url'];
      video = video.replace('http://gdata.youtube.com/feeds/videos/','http://www.youtube.com/embed/');
      $('#latest-video').append('<div class="latest-title"><a href="' + video + '" class="video-link">' + title + '</a></div><a href="' + video + '" class="video-link"><img src="' + tn + '" alt="' + title + '" /></a>');
      video_embed($('#latest-video .video-link'), video);
    });
  });

	
//iSelect Popup
	$('#iselect_design').hover(function() {
		$(this).find('#iselect_promo:hidden').fadeIn(500);
	}, function() {
		$(this).find('#iselect_promo:visible').fadeOut(500);
	});

//end document.ready()
});

//cufon text replacement
Cufon.replace("#navigation li a");
Cufon.replace(".content h1");
