// Sitewide scripts using jquery
// place this at the bottom of other script includes to activate jquery plugins


// activate google analitics with jquery.google-analytics.js plugin
// edit the Google Tracking number
$.trackPage('UA-xxxxxxx-x');


//these scripts will fire after html has loaded
$(document).ready(function() { 
													 
// activate png transparency fix
	$(document).pngFix(); //uncomment to create png transparencies in IE6
	
//password recovery display
	$("#recover_pw").click(function() {
		$("#recover_box").toggle();
		return false;
	});
	
// Function to hide email addresses
	$('span.mailme').each(function(){
		var $this = $(this);
		var at = / at /;
		var dot = / dot /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();*/
	$(document).bind('cbox_open', function(){
        $('embed, object, select').css({ 'visibility' : 'hidden' });
	});
	$(document).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, autoplay) {
    //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
		
		//get the video url
		var href = object.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];
		
		
		//autoplay video
		url += '&amp;hl=en_US&amp;fs=1&amp;rel=0&amp;wmode=transparent'; // &hd=1 to force HD
		if (autoplay == "autoplay") {
			url += "&amp;autoplay=1";
		}
		
		var embed = '<iframe class="youtube-player" type="text/html" width="' + v_width + '" height="' + v_height + '" src="' + url + '" frameborder="0" allowfullscreen></iframe>';

		//insert into the video player
		$('#video-player').html(embed);
  }

// turn links to youtube into colorbox popups
	var video_click = function() {
		$("a.video-link").click(function() {
			video_embed($(this), "autoplay");
			
			//scroll to the video player
			$('html, body').animate({
				scrollTop: $("#video-player").offset().top
			}, 200);
			
			return false;
		});
	}
	
//get the latest video
 	//var ytfeed = 'http://gdata.youtube.com/feeds/users/ProGripTieDowns/uploads'
	var ytfeed = 'http://gdata.youtube.com/feeds/api/playlists/3A50CA692C376331';
	
	$.getJSON(ytfeed + '?alt=json-in-script&callback=?', function(data) {
		var videos = new Array();

		$.each(data.feed.entry, function(i, item) {
			var title = item['title']['$t'];
			var video = item['media$group']['media$content'][0]['url'];
      var tn = item['media$group']['media$thumbnail'][1]['url'];
      videos[i] ='<div class="video-container"><div class="video"><div class="thumbnail"><a href="' + video + '" class="video-link" title="' + title + '"><img src="' + tn + '" alt="' + title + '" border="0" /></a></div></div><div class="video-title">' + title + '</div></div>';
    });
		$('#videos').append(videos.join("")); //insert into the html
	
		//bind to click
		video_click();  
		//load the first video into the player if not already populated
		if (!$("#video-player iframe").length) {
			video_embed($('#videos a:first'));
		}
  });
	
	
	
// iSelect Popup
	$('#iselect_design').hover(function() {
		$(this).find('#iselect_promo:hidden').fadeIn(500);
	}, function() {
		$(this).find('#iselect_promo:visible').fadeOut(500);
	});
	
//end $(document).ready();
}); 
