var imgs = [
    {"desc":"Every winter, Dominican players flock back home to play in the six team Dominican Winter League. The Licey Tigers, from Santo Domingo are the league's powerhouse.\n",
     "id":1,
     "img":"blog1.jpg"},
    {"desc":"Fans wait eagerly for the action to start at Licey's Estadio Quisqueya.\n",
     "id":2,
     "img":"IMG_1417.jpg"},
    {"desc":"Many Dominican MLBers return to their families in the DR when the MLB season ends. Before Spring Training they will come to their team's academy to tune up.\n",
     "id":3,
     "img":"IMG_1612-copy.jpg"},
    {"desc":"Jean Carlos runs uphill as part of his rigorous training regiment.\n",
     "id":6,
     "img":"IMG_1651.jpg"},
    {"desc":"A child shows off a home made kite.\n",
     "id":7,
     "img":"IMG_1673-copy.jpg"},
    {"desc":"Batters watch and wait for their turn at the plate at Moreno's program in San Pedro de Macor&iacute;s as storm clouds descend.\n",
     "id":8,
     "img":"IMG_1852.jpg"},
    {"desc":"Ernestico, Miguel Angel's adopted younger brother, was put into their care by a relative because they could better afford to raise the child thanks to Miguel's signing bonus.\n",
     "id":9,
     "img":"IMG_2109.jpg"},
    {"desc":"Miguel Angel and his extended family and friends relax at a ranch in the countryside. \n",
     "id":10,
     "img":"IMG_2115-copy.jpg"},
    {"desc":"While Miguel Angel takes baseball very seriously, a smile is never far from his face.\n",
     "id":11,
     "img":"IMG_2120.jpg"},
    {"desc":"Jean Carlos fields one of thousands of grounders hit to him by his trainer, Astin.\n",
     "id":12,
     "img":"IMG_2185.jpg"},
    {"desc":"<span class='italic'>Peloteros</span> typically drop out of school around age 12 or 13. They spend their days at <span class='italic'>el play</span> practicing, training, and occasionally escaping the sun.\n",
     "id":13,
     "img":"IMG_2226.jpg"},
    {"desc":"Ast&iacute;n keeps a watchful eye over his <span class='italic'>peloteros</span>. While most will not sign, he must train and support around 50.\n",
     "id":14,
     "img":"IMG_2237.jpg"},
    {"desc":"<span class='italic'>Peloteros</span> run in the sand at 6 o'clock in the morning to avoid the scorching midday sun.\n",
     "id":15,
     "img":"IMG_2543.jpg"},
    {"desc":"Jean Carlos mid-sprint.\n",
     "id":16,
     "img":"IMG_2583.jpg"},
    {"desc":"Jean Carlos catches his breath in between sprints.\n",
     "id":17,
     "img":"IMG_2591.jpg"},
    {"desc":"Miguel Angel and friends at his 16th birthday party.\n",
     "id":18,
     "img":"IMG_2725.jpg"},
    {"desc":"Turning 16 is a big step for Dominican <span class='italic'>peloteros</span>. Come July 2nd, Miguel Angel will be eligible to sign a professional contract.\n",
     "id":19,
     "img":"IMG_2766.jpg"},
    {"desc":"Miguel Angel fields grounders with a rubber ball on the asphalt.\n",
     "id":20,
     "img":"IMG_3040.jpg"},
    {"desc":"<span class='italic'>Peloteros</span> capitalize on anywhere with enough space to play. Here, a bullpen is carved out of the space behind the dugout.\n",
     "id":21,
     "img":"JuniorStretchedOut.jpg"},
    {"desc":"Miguel Angel is a rare talent. Although he is highly prized for his power bat, his smooth hands and strong arm make him a complete player.\n",
     "id":22,
     "img":"SanoThrowing.jpg"},
    {"desc":"<span class='italic'>Peloteros</span> who don't make it often struggle to find jobs with their minimal education. Many of them, like Sasa pictured here, will continue to work for their trainers as assistants.\n",
     "id":23,
     "img":"SasaHitting.jpg"},
    {"desc":"Trainers like Ast&iacute;n pay all the expenses for their peloteros, and oftentimes provide them with housing as well. They work off of commission and will take as much as 35% when they sign a <span class='italic'>pelotero.</span>\n",
     "id":24,
     "img":"IMG_1583.jpg"},
    {"desc":"From little leagues and training academies to the MLB, and the Winter League, Dominicans are crazy about baseball.\n",
     "id":4,
     "img":"blog2.jpg"},
    {"desc":"The level of play in the Dominican Winter League is fiercely competitive. Many MLB players stay sharp over the winter by playing for the teams they grew up rooting for.\n",
     "id":5,
     "img":"blog3.jpg"},
    {"desc":"<span class='italic'>Peloteros</span> are taught fundamentals by repeating drills over and over every day. While there is a growing emphasis on playing live games, this remains one of the biggest differences between the US and Dominican training styles.\n",
     "id":25,
     "img":"IMG_1870-copy.jpg"},
    {"desc":"Moreno Tejeda, Miguel Angel's trainer.\n",
     "id":26,
     "img":"IMG_2130-copy.jpg"},
    {"desc":"Miguel Angel's extended family grows anxious of posing for a photograph at the end of a long day in the countryside.",
     "id":27,
     "img":"IMG_2159.jpg"}]

function next_index(x, ul) { return (x == 0) ? ul : ((x == ul+1) ? 1 : x); }
function switch_gal(mod) {
    var i = next_index(parseInt($('.gal:visible')[0].id.replace('gal','')) + mod, 3); // #gal1-3
    var gal = document.getElementById('gal' + i);

    if(visible('.section:not(.gal)')) {
	$('.section').fadeOut(function() { $(gal).fadeIn(); });
    } else {
	var visg = $('.gal:visible');
	visg.hide();
	visg.children('a').stop(true,true);
	$(gal).children('a').hide();
	$(gal).show();
	$(gal).children('a').fadeIn('slow');
    }
}
function switch_img(mod) { show_img('img/medium/' + find_img('id', next_index(find_img_like('img', document.getElementById('pic').src)['id'] + mod, 27))['img']); }
function find_img(attr, val) { // exact match
   var img;
    $(imgs).each(function(i,e) { if(val == e[attr]) { img = e; return false; }});
    return img;
}
function find_img_like(attr, val) {
    var img;
    $(imgs).each(function(i,e) { if(val.indexOf(e[attr]) != -1) { img = e; return false; }});
    return img;
}
function show_img(src) {
    $('.section:visible').not('#img').stop(true, true).fadeOut();
    if(!visible('#img')) $('#img').fadeIn();

    document.getElementById('pic').src = src;
    var desc = $('#img div')[0];
    desc.style.display = 'none';
    desc.innerHTML = find_img_like('img', src)['desc'];
    Cufon.replace(desc);
    desc.style.display = 'block';

/*    $('#pic').stop(true, true).fadeOut(function() {
	this.src = src;
	$(this).fadeIn(3000);
    });
    $('#img div').stop(true, true).fadeOut(function() {
	this.innerHTML = find_img_like('img', src)['desc'];
	Cufon.replace(this);
	$(this).fadeIn(3000);
    }); */
}
//function visible(sel) { return $(sel + ':visible').size() == 1; } // single element.?
function visible(sel) { return $(sel + ':visible').size() >= 1; } // new

jQuery(document).ready(function(){
    // characters sub nav
    $('.char-sec').hide();
    $('.nav a').not('#characters, .char-sec a').click(function() {
	$(".char-sec").hide('slow');
    });
    $('#characters').click(function() {
	$('.char-sec').toggle('slow', function() { if(visible(this)) $('#miguel').trigger('click'); });
    });

    // "home" link
    $(document.getElementById('home')).click(function() { $('.section:visible').stop(true,true).fadeOut(); }); // setInterval for bg transitioning gets called in supersized plugin

    // nav links
    $('.nv').click(function() {
	$('.sl').not(this).removeClass('cp');
	$(this).addClass('cp');
	Cufon.replace('.sl', { fontFamily: 'DIN' });
    });

    $('.nv').hover(function() { $(this).addClass('red'); Cufon.replace('.sl', { fontFamily: 'DIN' });},
		   function() { $(this).removeClass('red'); Cufon.replace('.sl', { fontFamily: 'DIN' }) });

    // social networking roll overs
    $(".social").hover(function() { this.src = this.src.replace("_up","_down"); },
		       function() { this.src = this.src.replace("_down","_up"); });

    // nav links
    $("[section]").click(function() {
	var sid = this.getAttribute('section');
	var sec = document.getElementById(sid);

	if(!visible('#' + sid)) {
	    $('.section:visible').stop(true, true).fadeOut();
		$('.quote:visible').hide();
	    $(sec).fadeIn();
	}
    });

    // gallery next arrow
    $('.arr').click(function() { switch_gal(1); });

    // gallery previous arrow
    $('.arl').click(function() { switch_gal(-1); });

    // gallery thumbnail
    $('.gal a').not('.arl, .arr').click(function() { show_img($(this).children()[0].src.replace('/thumb/','/medium/')); });
    
    // image next arrow
    $('#par').click(function() { switch_img(1); });

    // image previous arrow
    $('#pal').click(function() { switch_img(-1); });
});

