jQuery.fn.extend({ scramble :  function() { 
    var i = this.size(); 
    if (!i) return this; 
    while (--i) { 
        var j = Math.floor(Math.random() * (i+1)); 
        var tmp1 = this.slice(i, i+1); 
        this.slice(j, j+1).after(tmp1).insertAfter(tmp1); 
    } 
    return this; 
}}); 

$(document).ready(function(){

	// Tooltip on charity images
	$(".charityimages a").tooltip({ effect: 'slide', predelay: 200, position: 'top center', offset: [14,0]});
	
	$("p.copyright").tooltip({ effect: 'slide', predelay: 200, position: 'top left', relative: true, offset: [0,120]});

	/*
$("p.copyright").hover(
		function () {
			$(this).next().fadeToggle();
			return false;
		}
	);
*/




	$('.carousel div.slide').scramble();

	$('.carousel').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		pause: 1,
		timeout: 8000
	});

	


	// people click
	$('ul#people li a').click(function(){
		// get data
		var pUrl = $(this).attr('href');
		var pName = $(this).find('span.name').text()
		var pOrganisation = $(this).find('span.organisation').text()
		var pDescription = $(this).find('span.description').text()
		var pImgurl = $(this).find('img').attr('src');

		// place data
		$('#pBox a').attr('href', pUrl);
		$('#pBox a .name').prepend(pName);
		$('#pBox a .description').prepend(pDescription);
		$('#pBox a .link').prepend('Read more about '+pName+'&hellip;');
		
		// Change url to /picture.preview.3533.jpg from http://princescharities.org/files/profiles/picture.flash.2882.jpg
		pImgurl = pImgurl.replace('.flash.','.preview.');
		$('div#pBox a .image').prepend('<img src="'+pImgurl+'"/>');
		
		// make box appear
		$('#pBox').fadeIn('slow');
		return false;
	});

	// leave peopleBox
	$('#pBox').click(function(){
		$('#pBox a span').empty();
		$('#pBox').fadeOut('fast');
	});
	$('#close').click(function(){
		$('#pBox a span').empty();
		$('#pBox').fadeOut('fast');
	});

	// TAB BOX

	//Tabs rollover thing
	$(".tab-box li").click(function(){
		window.location = $(this).find("a").attr("href");
	});

	// Move title into tabs
	$("<ul id=\"tabs-picker\"></ul>").insertAfter("#tabs-latest h2");
	$('#tabs-latest div.tab-box').each(function() {
		var tabName = $(this).find('h3').text();
		var tabId = $(this).attr('id');
		// alert('bob '+tabName+' '+tabId);
		$('<li class="'+tabId+'">'+tabName+'</li>').appendTo('ul#tabs-picker');
	});
	$('.tab-box h3').remove();	

	//Set up tabs
	$('#tabs-picker li').eq(0).addClass('on');
	
	$('.tab-box').hide();
	$('.tab-box').eq(0).show();

	$("#tabs-picker li").click(function(){
		//
		$('.tab-box').hide();
		var tabName2 = $(this).attr("class");
		$('#'+tabName2).show();
		//change class of tabs-picker li
		$('#tabs-picker li').removeClass('on');
		$(this).addClass("on");
	});

	// --------



	// charitypicker -------------------

	// carousel	
	//$("#charitypickercontent").scrollable();

	// tips
	/*
$("#charityinfo").fadeTo("fast", 0.1);

	$(".arrow a img").mouseover(function(){
		$("#charityinfo").fadeTo("fast", 0.66);
		var charityinfo = $(this).attr("alt");
		$("#charityinfo").text("Click to "+charityinfo);
	});
	$(".arrow a img").mouseout(function(){
		$("#charityinfo").fadeTo(0.1, 0.1);
		$("#charityinfo").html("&nbsp;");
	});
	$(".charitylogo a img").mouseover(function(){
		$("#charityinfo").fadeTo("fast", 0.66);
		var charityinfo = $(this).attr("alt");
		$("#charityinfo").html("Click to find out more about <strong>"+charityinfo+"</strong>");
	});
	$(".charitylogo a img").mouseout(function(){
		$("#charityinfo").fadeTo(0.1, 0.1);
		$("#charityinfo").html("&nbsp;");
	});
*/
	
	
	
	// add wrapper around blockquotes
	// this makes it possible to have the speech bubble triangle thing
	
	$("#bodytext blockquote").wrap("<div class=\"blockquotewrap\"></div>");
	
	// add quotes on the charitiespage to blockquote
	$(".charitiespage blockquote p").prepend("&ldquo;");
	$(".charitiespage blockquote p").append("&rdquo;");
	
	// adds external links icons and makes them open new windows
	
	$("#main a[href^='http://']").not("a[href^='http://princescharities.org']").not("a[href^='http://www.princescharities.org']").not("a[href^='http://maps.google.co.uk']").append("&nbsp;<img src=\"http://princescharities.org/assets/externallink.gif\" alt=\"external link\" class=\"externallinkicon\" />");
	
	$("#main a[href^='http://']").not("a[href^='http://princescharities.org']").not("a[href^='http://www.princescharities.org']").click(function(){
		window.open(this.href,'external');
		return false;
	});
	
	
	
	// user profile editing page rearrange ----------

	$('label#edit-picture-uploadas').parent().addClass('edit-profile-picture');






});


