// $Id$

/**
 *      _                        _ 
 *     | |                      | |
 *   __| |_ __ _   _ _ __   __ _| |
 *  / _` | '__| | | | '_ \ / _` | |
 * | (_| | |  | |_| | |_) | (_| | |
 *  \__,_|_|   \__,_| .__/ \__,_|_|
 *                  | |
 *                  |_|
 * @file
 * TODO: update click function for mobile-friendly / performance
 * TODO: update resize for mobile-friendly (orientationChange)
 * TODO: break desktop functions out to base.desktop.js
 */


(function ($) {
  $(document).ready(function(){
    
    // open external links in a new window
    //$('a[href^="http:"]').not('[href*="ninjagirl.com"]').attr('rel', 'external').attr('target', '_blank');
		
		// append download query to #files downloads (.htaccess)
		$('#files a').each(function() {
			$(this).attr('href', $(this).attr('href') + '?download=1');
		});
		
		// back to top
		$('a#top').click(function() {
		  $.scrollTo('#page', 500);
		  return false;
	  });
		
		/**
		 * Footer
		 */
		
		// only display the footer bgs if we have enough space
		var display_footer_bg = function() {
			if ($(window).width() > ($('#page').width() + $('#bg-left').width())) {
				$('#bg-left').fadeIn('slow');
			} else {
				$('#bg-left').fadeOut('slow');
			}
		}
		$(window).bind('resize', display_footer_bg);
		display_footer_bg();
		
		/**
		 * Play
		 * http://jqueryui.com/demos/dialog
		 * http://elijahmanor.com/webdevdotnet/post/jQuery-UI-Dialog-w-Resizable-iFrame.aspx
		 */
		var play = $('#play');
		var playlist = $('#play .menu');
		
		/* fix for localhost basepath */
		$('#play li a').each(function() {
			$(this).attr('href', Drupal.settings.basePath + $(this).attr('href').substring(1));
		});
		
		$('#play-toggle').click(function() {	
			playlist.slideToggle('fast', function() {
				play.toggleClass('expanded');
			});
			return false;
		});
		
		$('a.dialog').click(function() {
			var width = 560;
			var height = 400;
	    
			/*$('<iframe class="frame-play" src="' + this.href + '" />').dialog({
	      title: ($(this).attr('title')) ? $(this).attr('title') : 'Play',
	      autoOpen: true,
	      width: width,
	      height: height,
				minWidth: 400,
	      modal: false,
	      resizable: true,
				autoResize: true,
	      overlay: {
	        opacity: 0.5,
	        background: 'black'
	      }
	    });*/
			
			var test = $(window).width();
			var width = 600; /* subtract 10 below for scrollbars */
      var height = 400;
								
			$('<iframe class="frame-play" src="' + this.href + '" />').dialog({
	      title: ($(this).attr('title')) ? $(this).attr('title') : 'Play',
				width: width,
				height: height
	    }).width(width - 10).height(height);
			
			return false;
		});
		
		/**
		 * Twitter Tweet Button
		 * TODO: add previous D6 data-counturl to D7 data-counturl (@see template.php / readme.txt)
		 * TODO: not currently possible to migrate counts from old posts at a differnt url (https://dev.twitter.com/discussions/3861)
		 * @see https://dev.twitter.com/docs/intents/events
		 * @see	http://ottopress.com/2010/twitters-new-tweet-button-and-the-count-api
		 * 			http://urls.api.twitter.com/1/urls/count.json?url=URL-TO-GET-COUNT-FOR&callback=bob
		 */
		
		// wait for the asynchronous resources to load
		twttr.ready(function(twttr) {
			//console.log('test: ' + $('.tweet').html);
			//console.log($('.twitter-share-button  .count-o a').html);
			console.log(twttr);
			// bind our custom intent events
			twttr.events.bind('click', function(e) {
				console.log('tweet button clicked: ' + e.region);
				//console.log(this);
				//console.log($(this));
			});
		});
		
  });
})(jQuery);;

