$(document).ready(function() {

	$('#launch-video').click(function() { 
		$('#promo-container').show({ 
        marginLeft: "-1027px"
      } );
      player.sendEvent("PLAY","true");
	});
	$('#return-video').click(function() { 
		$('#promo-container').hide();
  		player.sendEvent("PLAY","false");
	});

	$(".first").each(function() {
		$(this).css({'padding-left' : '0px'});
		if($(this).css({'padding-left' : '0px'})) {
			$(this).css({'width' : ($(this).width())+15 + 'px'});
		};
	});
	$(".last").each(function() {
		$(this).css({'padding-right' : '0px'});
		if($(this).css({'padding-right' : '0px'})) {
			$(this).css({'width' : ($(this).width())+15 + 'px'});		
		};
	});
	$("#sel" + $('#entry_id').attr('class')).addClass("selected");

	
		$('dl#issues dd a').corners("4px");
	$('#promo-video').corners("4px");
	$('.testimony-list li a').corners("4px");
	$('#selected-profile li a').corners("4px");
	$('.pages a').corners("4px");	
	$('#tabs a').corners("4px");	
	$('.tile').corners("8px");
	$('#spotlight a').corners("4px");	
	$('#webzine-cover').corners("4px");
	$('#webzine-cover dl dd a').corners("4px");	
	$('#webzine-nav').corners("4px");
	$('#author-nav a').corners("4px");
	$('#articles-nav a').corners("4px");

		






});



/*HISTORY IN THE MAKING*/


(function($) {

function History()
{
	this._curHash = '';
	this._callback = function(hash){};};

$.extend(History.prototype, {

	init: function(callback) {
		this._callback = callback;
		this._curHash = location.hash;

		if($.browser.msie) {
			// To stop the callback firing twice during initilization if no hash present
			if (this._curHash == '') {
				this._curHash = '#';
			}

			// add hidden iframe for IE
			$("body").prepend('<iframe id="jQuery_history" style="display: none;"></iframe>');
			var iframe = $("#jQuery_history")[0].contentWindow.document;
			iframe.open();
			iframe.close();
			iframe.location.hash = this._curHash;
		}
		else if ($.browser.safari) {
			// etablish back/forward stacks
			this._historyBackStack = [];
			this._historyBackStack.length = history.length;
			this._historyForwardStack = [];
			this._isFirst = true;
			this._dontCheck = false;
		}
		this._callback(this._curHash.replace(/^#/, ''));
		setInterval(this._check, 100);
	},

	add: function(hash) {
		// This makes the looping function do something
		this._historyBackStack.push(hash);
		
		this._historyForwardStack.length = 0; // clear forwardStack (true click occured)
		this._isFirst = true;
	},
	
	_check: function() {
		if($.browser.msie) {
			// On IE, check for location.hash of iframe
			var ihistory = $("#jQuery_history")[0];
			var iframe = ihistory.contentDocument || ihistory.contentWindow.document;
			var current_hash = iframe.location.hash;
			if(current_hash != $.history._curHash) {
			
				location.hash = current_hash;
				$.history._curHash = current_hash;
				$.history._callback(current_hash.replace(/^#/, ''));
				
			}
		} else if ($.browser.safari) {
			if (!$.history._dontCheck) {
				var historyDelta = history.length - $.history._historyBackStack.length;
				
				if (historyDelta) { // back or forward button has been pushed
					$.history._isFirst = false;
					if (historyDelta < 0) { // back button has been pushed
						// move items to forward stack
						for (var i = 0; i < Math.abs(historyDelta); i++) $.history._historyForwardStack.unshift($.history._historyBackStack.pop());
					} else { // forward button has been pushed
						// move items to back stack
						for (var i = 0; i < historyDelta; i++) $.history._historyBackStack.push($.history._historyForwardStack.shift());
					}
					var cachedHash = $.history._historyBackStack[$.history._historyBackStack.length - 1];
					if (cachedHash != undefined) {
						$.history._curHash = location.hash;
						$.history._callback(cachedHash);
					}
				} else if ($.history._historyBackStack[$.history._historyBackStack.length - 1] == undefined && !$.history._isFirst) {
					// back button has been pushed to beginning and URL already pointed to hash (e.g. a bookmark)
					// document.URL doesn't change in Safari
					if (document.URL.indexOf('#') >= 0) {$.history._callback(document.URL.split('#')[1]);} else {$.history._callback('');}
					$.history._isFirst = true;
				}
			}
		} else {
			// otherwise, check for location.hash
			var current_hash = location.hash;
			if(current_hash != $.history._curHash) {
				$.history._curHash = current_hash;
				$.history._callback(current_hash.replace(/^#/, ''));
			}
		}
	},

	load: function(hash) {
		var newhash;
		
		if ($.browser.safari) {
			newhash = hash;
		} else {
			newhash = '#' + hash;
			location.hash = newhash;
		}
		this._curHash = newhash;
		
		if ($.browser.msie) {
			var ihistory = $("#jQuery_history")[0]; // TODO: need contentDocument?
			var iframe = ihistory.contentWindow.document;
			iframe.open();
			iframe.close();
			iframe.location.hash = newhash;
			this._callback(hash);
		}
		else if ($.browser.safari) {
			this._dontCheck = true;
			// Manually keep track of the history values for Safari
			this.add(hash);
			
			// Wait a while before allowing checking so that Safari has time to update the "history" object
			// correctly (otherwise the check loop would detect a false change in hash).
			var fn = function() {$.history._dontCheck = false;};
			window.setTimeout(fn, 200);
			this._callback(hash);
			// N.B. "location.hash=" must be the last line of code for Safari as execution stops afterwards.
			//      By explicitly using the "location.hash" command (instead of using a variable set to "location.hash") the
			//      URL in the browser and the "history" object are both updated correctly.
			location.hash = newhash;
		}
		else {this._callback(hash);}
	}
});

$(document).ready(function() {
	$.history = new History(); // singleton instance
});

})(jQuery);

function callback(hash)
{

	var url = document.location.toString();	
	if (url.match('stories#')) {
		$('.testimony').html("<span class='ajax-loader'>Loading...</span>");
		testimony = url.split('#')[1];
		$('.column-3').load('http://www.thisisnext.org/stories/load.testimony/' + testimony);
		$('.testimony-list li').removeClass("selected");
		$('#sel' + testimony).addClass("selected");
	}
	if (url.match('conference#')) {
		$('#conference-tab').html("<span class='ajax-loader'>Loading...</span>");
		var tab = url.split('#')[1];
		url = url.replace("#", "/");
		$('#conference-tab').load(url);

		$('#tabs li a').removeClass("selected");
		$('#tabs li a.' + tab).addClass("selected");
	}
	if (url.match('webzine')) {
		if (url.match('#')) {
			$('#webzine-content').show();
			$('#webzine-content').html("<span class='ajax-loader'>Loading...</span>");
			
			var article = url.split('#')[1];
			if (url.match('2009')||url.match('2010')) {
				var month = url.split('webzine/')[1];
				month = '/' + month.split('#')[0];
			} else {
			var month = "";
			}
			$('#webzine-cover').hide();
			$('#webzine-nav').show();
			$('#webzine-content').load('http://www.thisisnext.org/webzine/loader/' + article + month);			
			$('#author-nav a').removeClass("selected");
			$('#articles-nav a').removeClass("selected");
			$('.' + article).addClass("selected");
		} else {
			$('#webzine-content').hide();
			$('#webzine-cover').show();		
			$('#webzine-nav').hide();
	//		sIFR.replace(akkurat_bold, {
 	//			selector: '#webzine-cover h1',
	//			wmode: 'transparent',
	//			css: '.sIFR-root { background-color: #333333; font-size : 46px; letter-spacing : -2; color: #ffffff; }'
	//		});
		}
	}
}
$(document).ready(function() {
    $.history.init(callback);
    $("a[@rel='history']").click(function(){
        $.history.load(this.href.replace(/^.*#/, ''));
        return false;
    });
    /*
    $('#mailinglist_form').ajaxForm(function() { 
    	if ($('#email').val() === 'Email Address') {
			$('#fail_email').fadeIn();
			$('#fail_email').animate({opacity: 0}, 5000);    	
    	} else {
			$('#success_email').fadeIn();
			$('#success_email').animate({opacity: 0}, 5000);    	
    	}
    }); 
    */
    var url = document.location.toString();	
	if (url.match('#update')) {
			$('#success_email').fadeIn();
			$('#success_email').animate({opacity: 0}, 5000);    		
	}
});

function verifyForm() {
   if ((document.next_update.email.value.indexOf(".") > 2) && (document.next_update.email.value.indexOf("@") > 0))
        {
        document.next_update.submit();
            return true;
        }
        else
        {
			$('#fail_email').fadeIn();
			$('#fail_email').animate({opacity: 0}, 5000);    	
        }
    }
  



if (document.images)
{
	preload_image_object = new Image();
	// set image url
	image_url = new Array();
	image_url[0] = "http://www.thisisnext.org/images/ui/tile.bg.o.png";
	var i = 0;
	for(i=0; i<=3; i++) 
	    preload_image_object.src = image_url[i];
}

/* sIFR 


var akkurat = { src: '../fla/akkurat.swf' };
var akkurat_bold = { src: '../fla/akkurat-bold.swf' };

sIFR.activate(akkurat, akkurat_bold);

sIFR.replace(akkurat_bold, {
  selector: '#home-hero h1',
  wmode: 'transparent',
  css: '.sIFR-root { background-color: #fff; font-size : 56px; letter-spacing : -2; color: #545454; }'
});
sIFR.replace(akkurat_bold, {
  selector: '#home-hero h2',
  wmode: 'transparent',
  css: '.sIFR-root { background-color: #fff; font-size : 40px; letter-spacing : -2; color: #8e1d67; }'
});
sIFR.replace(akkurat_bold, {
  selector: '#home-hero h3, h1.letter',
  wmode: 'transparent',
  css: '.sIFR-root { background-color: #fff; font-size : 40px; letter-spacing : -2; color: #545454; }'
});
sIFR.replace(akkurat, {
  selector: '#home-hero h4',
  wmode: 'transparent',
  css: '.sIFR-root { background-color: #fff; font-size : 30px; letter-spacing : -2; leading: -11; color: #545454; }'
});
sIFR.replace(akkurat, {
  selector: '#home-hero h5',
  wmode: 'transparent',
  css: '.sIFR-root { background-color: #fff; font-size : 13px; color: #545454; }'
});
sIFR.replace(akkurat_bold, {
  selector: '.speakers strong',
  wmode: 'transparent',
  css: '.sIFR-root { background-color: #fff; font-size : 22px; color: #545454; }'
});
sIFR.replace(akkurat, {
  selector: '.speakers p',
  wmode: 'transparent',
  css: '.sIFR-root { background-color: #fff; font-size : 22px; leading: -5; color: #545454; }'
});

sIFR.replace(akkurat_bold, {
  selector: 'h1.testimonies-header, h1.standard-header',
  wmode: 'transparent',
  css: [
  		'.sIFR-root { background-color: #fff; font-size : 40px; letter-spacing : -3; color: #000000; }',
  		'a {color: #000000; text-decoration: none;}',
        'a:hover {color: #000000;}'
       ]
});
sIFR.replace(akkurat_bold, {
  selector: '#webzine-cover h1',
  wmode: 'transparent',
  css: '.sIFR-root { background-color: #333333; font-size : 46px; letter-spacing : -2; color: #ffffff; }'
});
*/