function photo(pclass) {
	pid = "#"+pclass
	pclass = "."+pclass
	$("#photo").html( $(pclass).html() )
	$("#photos li").css('background', 'none')
	$(pid).parent().css('background', 'url(/css/images/bg_photos.png) no-repeat 3px 17px')
};

function video(vclass) {
	vid = "#"+vclass
	vclass = "."+vclass
	$("#video").html( $(vclass).html() )
	$("#videos li").css('background', 'none')
	$(vid).parent().css('background', 'url(/css/images/bg_photos.png) no-repeat 3px 17px')
};

$(document).ready(function(){
	texto_ngo =  "<div>"
	texto_ngo += "<p>In case you&#8217;re wondering, NGO is short for non governmental organization. These are non-profit organizations, independent from government and they&#8217;re of a charitable, research or educational nature.  </p>"
	texto_ngo += "<p>There are lots of different types of NGO, they vary in size and are often organized around specific issues such as human rights, the environment or health. Some you may have heard of include Amnesty International, Barnados, Oxfam, Plan, and Save the Children. </p>"
	texto_ngo += "</div>"
	$("a.box_ngo").attr('title','what is an NGO?'+texto_ngo)
	$("a.box_ngo").tooltip();

	$("#photos li:first").css('background', 'url(/css/images/bg_photos.png) no-repeat 3px 17px')
	$("#videos li:first").css('background', 'url(/css/images/bg_photos.png) no-repeat 3px 17px')

// CASE STUDY
	// photos
	$("img.photo").click(function() {
		var id = $(this).attr('id')
		$("#photo").html( $("p."+id).html() )
		$("#photos li").css('background', 'none')
		$(this).parent().css('background', 'url(/css/images/bg_photos.png) no-repeat 3px 17px')
	});
	$("#tab_photos").click( function() {
		$("#tab_videos").css({backgroundPosition: '0 -71px'})
		$("#tab_photos").css({backgroundPosition: '0 0'})
		$("#photo, ul#photos").css('display', 'block')
		$("#video, #videos").css('display', 'none')
	})            
	
	// menu cases
	$("select.casestudy").change(function() {
		$("select.casestudy option:selected").each(function () {
			var url = $(this).attr('value')
			if (url.length > 3) window.location.href = url
		});
	});
	
	$("select.casestudy").removeAttr('disabled');

	// videos
	$("#tab_videos").click( function() {
		$("#tab_videos").css({backgroundPosition: '-25px -71px'})
		$("#tab_photos").css({backgroundPosition: '-25px 0'})
		$("#photo, ul#photos").css('display', 'none')
		$("#video, #videos").css('display', 'block')
	})

	$("#videos li a").click(function() {
		var id = $(this).attr('id')
		$("#video").html( $("p."+id).html() )
		$("#videos li").css('background', 'none')
		$(this).parent().css('background', 'url(/css/images/bg_photos.png) no-repeat 3px 17px')
	});

	// se tiver #video na url é pra mostrar a aba de videos
	url = window.location +""
	if ( url.indexOf("#video") > 0 ) {
		$("#tab_videos").css({backgroundPosition: '-25px -71px'})
		$("#tab_photos").css({backgroundPosition: '-25px 0'})
		$("#photo, ul#photos").css('display', 'none')
		$("#video, #videos").css('display', 'block')
	} else {
		$("#tab_videos").css({backgroundPosition: '0 -71px'})
		$("#tab_photos").css({backgroundPosition: '0 0'})
		$("#photo, ul#photos").css('display', 'block')
		$("#video, #videos").css('display', 'none')
	}

// MENU - INTERNAS
	// diagrama small
    $("#abcd_diagram_small").mouseover(function () { $(this).css({backgroundPosition: '-250px 0px'}); });
    $("#abcd_diagram_small").mouseout(function () { $(this).css({backgroundPosition: '0px 0px'}); });

	// bolinhas abcd
    $("li#internal a").mouseover(function () {
		if ($(this).attr('class') == 'cc') { $(this).css({backgroundPosition: '0 -25px'}); $("#abcd_diagram_small").css({backgroundPosition: '-50px 0px'}); }
		if ($(this).attr('class') == 'cp') { $(this).css({backgroundPosition: '-27px -25px'}); $("#abcd_diagram_small").css({backgroundPosition: '-100px 0px'}); }
		if ($(this).attr('class') == 'hcc') { $(this).css({backgroundPosition: '-54px -25px'}); $("#abcd_diagram_small").css({backgroundPosition: '-150px 0px'}); }
		if ($(this).attr('class') == 'ngo') { $(this).css({backgroundPosition: '-81px -25px'}); $("#abcd_diagram_small").css({backgroundPosition: '-200px 0px'}); }
		var text = $(this).attr('title')
		$("li#internal h4").text(text)
    });
    $("li#internal a").mouseout(function () {
		if ($(this).attr('class') == 'cc') { $(this).css({backgroundPosition: '0 0'}); }
		if ($(this).attr('class') == 'cp') { $(this).css({backgroundPosition: '-27px 0'}); }
		if ($(this).attr('class') == 'hcc') { $(this).css({backgroundPosition: '-54px 0'}); }
		if ($(this).attr('class') == 'ngo') { $(this).css({backgroundPosition: '-81px 0'}); }
		$("#abcd_diagram_small").css({backgroundPosition: '0px 0px'});
		$("li#internal h4").text(' ')
    });

	// how it works
    $(".how_it_works h3").mouseover(function () {
		var pos = '0 0'
		$("div.box_howitworks").show()
		switch ($(this).attr('class')) {
			case 't1' : pos = '0 -354px'; break
			case 't2' : pos = '0 -386px'; break
			case 't3' : pos = '0 -418px'; break
			case 't4' : pos = '0 -450px'; break
			case 't5' : pos = '0 -482px'; break
			case 't6' : pos = '0 -514px'; break
			case 't7' : pos = '0 -546px'; break
			case 't8' : pos = '0 -578px'; break
			case 't9' : pos = '0 -610px'; break
			case 't10' : pos = '0 -642px'; break
			case 't11' : pos = '0 -674px'; break
		}

		$("div.box_howitworks div").html( $("span."+$(this).attr('class')).html() )
//		$("div.box_howitworks h3").text( $("h3."+$(this).attr('class')).text() )
		$("div.box_howitworks h3").html('<img class="description" src="/img/description.png" />  ')
		$("div.box_howitworks h3").css('margin-bottom', '-30px')
		$("div.box_howitworks a").css('display', 'none')
		$(this).css('backgroundPosition', pos);
    });
    $(".how_it_works h3").mouseout(function () {
		var pos = '0 0'
		$("div.box_howitworks").hide()
		switch ($(this).attr('class')) {
			case 't1' : pos = '0 0px'; break
			case 't2' : pos = '0 -32px'; break
			case 't3' : pos = '0 -64px'; break
			case 't4' : pos = '0 -96px'; break
			case 't5' : pos = '0 -128px'; break
			case 't6' : pos = '0 -160px'; break
			case 't7' : pos = '0 -192px'; break
			case 't8' : pos = '0 -224px'; break
			case 't9' : pos = '0 -256px'; break
			case 't10' : pos = '0 -288px'; break
			case 't11' : pos = '0 -320px'; break
		}
		$(this).css('backgroundPosition', pos);
    });

	// ngo
/*
    $("a.box_ngo").click(function () {
    	$("div.box_ngo").show()
    });
    $(".close_ngo").click(function () {
    	$("div.box_ngo").hide()
    });
*/
// HOME
	// bolinhas abcd
    $("#sidebar ul#navigation a").mouseover(function () {
		if ($(this).attr('class') == 'cc') { $(this).css({backgroundPosition: '-270px 6px'}); $("#abcd_diagram").css({backgroundPosition: '-680px 0px'}); }
		if ($(this).attr('class') == 'cp') { $(this).css({backgroundPosition: '-270px -28px'}); $("#abcd_diagram").css({backgroundPosition: '-170px 0px'}); }
		if ($(this).attr('class') == 'hcc') { $(this).css({backgroundPosition: '-270px -62px'}); $("#abcd_diagram").css({backgroundPosition: '-340px 0px'}); }
		if ($(this).attr('class') == 'ngo') { $(this).css({backgroundPosition: '-270px -96px'}); $("#abcd_diagram").css({backgroundPosition: '-510px 0px'}); }
    });
    $("#sidebar ul#navigation a").mouseout(function () {
	    $("#abcd_diagram").css({backgroundPosition: '0px 0px'});
		if ($(this).attr('class') == 'cc') $(this).css({backgroundPosition: '0px 6px'});
		if ($(this).attr('class') == 'cp') $(this).css({backgroundPosition: '0px -28px'});
		if ($(this).attr('class') == 'hcc') $(this).css({backgroundPosition: '0px -62px'});
		if ($(this).attr('class') == 'ngo') $(this).css({backgroundPosition: '0px -96px'});
    });

    $("#sidebar #abcd_diagram a").mouseover(function () {
		if ($(this).attr('class') == 'cc_diagram') { $("#sidebar ul#navigation a.cc").css({backgroundPosition: '-270px 6px'}); $("#abcd_diagram").css({backgroundPosition: '-680px 0px'}); }
		if ($(this).attr('class') == 'cp_diagram') { $("#sidebar ul#navigation a.cp").css({backgroundPosition: '-270px -28px'}); $("#abcd_diagram").css({backgroundPosition: '-170px 0px'}); }
		if ($(this).attr('class') == 'hcc_diagram') { $("#sidebar ul#navigation a.hcc").css({backgroundPosition: '-270px -62px'}); $("#abcd_diagram").css({backgroundPosition: '-340px 0px'}); }
		if ($(this).attr('class') == 'ngo_diagram') { $("#sidebar ul#navigation a.ngo").css({backgroundPosition: '-270px -96px'}); $("#abcd_diagram").css({backgroundPosition: '-510px 0px'}); }
		if ($(this).attr('class') == 'how_it_works_diagram') { $("#abcd_diagram").css({backgroundPosition: '-850px 0px'}); }
    });
    $("#sidebar #abcd_diagram a").mouseout(function () {
	    $("#abcd_diagram").css({backgroundPosition: '0px 0px'});
		if ($(this).attr('class') == 'cc_diagram') $("#sidebar ul#navigation a.cc").css({backgroundPosition: '0px 6px'});
		if ($(this).attr('class') == 'cp_diagram') $("#sidebar ul#navigation a.cp").css({backgroundPosition: '0px -28px'});
		if ($(this).attr('class') == 'hcc_diagram') $("#sidebar ul#navigation a.hcc").css({backgroundPosition: '0px -62px'});
		if ($(this).attr('class') == 'ngo_diagram') $("#sidebar ul#navigation a.ngo").css({backgroundPosition: '0px -96px'});
    });
	// fim : bolinhas abcd
	
    $("#sidebar.how_it_works_pages a").mouseover(function () {
		if ($(this).attr('class') == 'cc') { $(this).css({backgroundPosition: '0 -94px'}); $(".diagram_youare").css({backgroundPosition: '0 0px'}); }
		if ($(this).attr('class') == 'cp') { $(this).css({backgroundPosition: '-40px -94px'}); $(".diagram_youare").css({backgroundPosition: '-210px 0px'}); }
		if ($(this).attr('class') == 'hcc') { $(this).css({backgroundPosition: '-80px -94px'}); $(".diagram_youare").css({backgroundPosition: '-420px 0px'}); }
		if ($(this).attr('class') == 'ngo') { $(this).css({backgroundPosition: '-120px -94px'}); $(".diagram_youare").css({backgroundPosition: '-630px 0px'}); }
		var text = $(this).attr('title')
		$("#sidebar.how_it_works_pages h4").text(text)
    });
    $("#sidebar.how_it_works_pages a").mouseout(function () {
		if ($(this).attr('class') == 'cc') { $(this).css({backgroundPosition: '0 -23px'}); }
		if ($(this).attr('class') == 'cp') { $(this).css({backgroundPosition: '-40px 0'}); }
		if ($(this).attr('class') == 'hcc') { $(this).css({backgroundPosition: '-80px 0'}); }
		if ($(this).attr('class') == 'ngo') { $(this).css({backgroundPosition: '-120px 0'}); }
		$(".diagram_youare").css({backgroundPosition: '0px 0px'});
		$("#sidebar.how_it_works_pages h4").text(' ')

		if (atual == 'cc') { $("#sidebar.how_it_works_pages a.cc").css({backgroundPosition: '0 0'}); $(".diagram_youare").css({backgroundPosition: '0 0px'});}
		if (atual == 'cp') { $("#sidebar.how_it_works_pages a.cp").css({backgroundPosition: '-40px -23px'}); $(".diagram_youare").css({backgroundPosition: '-210px 0px'});}
		if (atual == 'hcc') { $("#sidebar.how_it_works_pages a.hcc").css({backgroundPosition: '-80px -46px'});$(".diagram_youare").css({backgroundPosition: '-420px 0px'}); }
		if (atual == 'ngo') { $("#sidebar.how_it_works_pages a.ngo").css({backgroundPosition: '-120px -69px'}); $(".diagram_youare").css({backgroundPosition: '-630px 0px'});}		
    });

	if (! typeof(atual) == "undefined") {
		if (atual == 'cc') { $("#sidebar.how_it_works_pages a.cc").css({backgroundPosition: '0 0'}); $(".diagram_youare").css({backgroundPosition: '0 0px'});}
		if (atual == 'cp') { $("#sidebar.how_it_works_pages a.cp").css({backgroundPosition: '-40px -23px'}); $(".diagram_youare").css({backgroundPosition: '-210px 0px'});}
		if (atual == 'hcc') { $("#sidebar.how_it_works_pages a.hcc").css({backgroundPosition: '-80px -46px'});$(".diagram_youare").css({backgroundPosition: '-420px 0px'}); }
		if (atual == 'ngo') { $("#sidebar.how_it_works_pages a.ngo").css({backgroundPosition: '-120px -69px'}); $(".diagram_youare").css({backgroundPosition: '-630px 0px'});}		
	}

});

$(window).load( function() {

	// ajustar fotos que tem largura excessiva
	function checkImages() {
		$("ul#photos li").each( function(){
			var largura = $(this).children('img').css('width')
			if (largura) {
				largura = largura.substring(0, (largura.length - 2))
				if( largura > 62 ) {
					$(this).children('img').css('width','62px')
				}
			}
		})
	}                            
	checkImages()
	
})	