﻿jQuery.fn.exists = function(){return jQuery(this).length>0;}

function contentLink(div,px, animate) {
    	if(!animate) {
    		var animate = 'true';
    	} else {
    		animate = 'false';
    	}
    	applyHeight(div, px, animate);
        $('div#tabs > div').not(div).fadeOut(600);
        $(div+' div.the-story').hide();
}

function widgetLink(div,px, animate) {
    	if(!animate) {
    		var animate = 'true';
    	} else {
    		animate = 'false';
    	}
    	applyHeight(div, px, animate);
        $('div#tabs > div').not(div).fadeOut(600);
}


function applyHeight(id, px, animate) {
	    var oHeight = $('div.inner.sub').height();
	   	if($(id).exists()) {
	    }
	    var thisHeight = $(id).height();
	    if (animate == 'false') {
	    	$('div.inner.sub').css({height: thisHeight+px+'px'});
	    } else if ($(id+' .story').exists()) {
	    	addWidget(id,230);
	    } else {
	    	$('div.inner.sub').css({height: oHeight+'px'}).stop().animate({height: thisHeight+px+'px'},800, function(){
	    		$(id).fadeIn(500);
	    	});
	    }
}

function addWidget(id,px) {
	var oHeight = $('div.inner.sub').height();
	$(id+' .bio').width(450);
	$(id).css('padding-right','30px');
	$(id).css('text-align','justify');
	/* Get the story excerpt */
	var excerpt = $(id+' > div.story > span.excerpt').html();
	if($(id+' > .widget').exists()) {
		
	}
	else {
	$(id).append('<div class=\"widget\" style="display:none;"><div class=\"widget-top\"><span>Success Story</span></div>\n<div class=\"widget-content\"><p>'+excerpt+'</p></div><div class=\"widget-bottom\"><a href="#" onclick=\"widgetLink(\'div#tabs > div'+id+'-story\',150);\">read full story &raquo;</a></div></div>');
	$('div.widget').fadeIn(1500);
	}
	if($(id+ ' > .widget').height() > $(id).height()) {
		
	} else {
	var thisHeight = $(id).height();
	$('div.inner.sub').css({height: oHeight+'px'}).stop().animate({height: thisHeight+px+'px'},900, function(){
	    		$(id).fadeIn(600);
	    	});
	}
}


/*function storyHeight(id,dest, px, animate) {
	    var oHeight = $('div.inner.sub').height();
	    var thisHeight = $(id+' .story').height();
	    if (animate == 'false') {
	    	$('div.inner.sub').css({height: thisHeight+px+'px'});
	    }
    	$('div.inner.sub').css({height: oHeight+'px'}).stop().animate({height: thisHeight+px+'px'},900, function(){
    		$(id+' .story').fadeIn(600);
    	});
}*/

$(document).ready(function() {
	$('.story').each(function(index) {
		var storyFellow = $('.story:eq('+index+')').parent('div').attr('id');
		var title = $('.story:eq('+index+')').parent('div').find('h3').html();
		$('.story:eq('+index+')').attr('id',storyFellow+'-story');
		$(this).clone().appendTo('div#tabs');
		$('div#tabs > #'+storyFellow+'-story').prepend('<h3>'+title+' Success Story</h3>');
	});
	$('#tabs ul li a img:first').width(75);
	$('#tabs ul li a img:first').height(75);
	$('#tabs ul li a img:first').css('border','3px solid #fed334');
	$('#tabs ul li a img').click(function() {
		$('#tabs ul li a img').height(70);
		$('#tabs ul li a img').width(70);
		$('#tabs ul li a img').css('border','none');
		$(this).height(75);
		$(this).width(75);
		$(this).css('border','3px solid #fed334');
		});
	$('.story').hide();
	var pathname = window.location.pathname;
	if (pathname == '/meet-the-fellows') {
		contentLink('#Merita-Behluli',170);
	}
	if (pathname == '/fall-2010') {
		contentLink('#Arbërie-Nagavci',250);
	}
	if (pathname == '/spring-2011') {
		contentLink('#Afërdita-Rushiti-',250);
	}
	if (pathname == '/current-fellows') {
		contentLink('#Adelina-Nura',250);
	}
	jQuery('#slideshow').cycle({ 
	    fx:     'fade', 
	    speed:  750, 
	    timeout: 5000, 
	    next:   '#feature-btnNext', 
	    prev:   '#feature-btnPrev',
	    pause: true
	});
	
	
	$('ul.child').mouseover(function() {
		$('li.dir > a').css('background','url(http://naac-hf.org/wp-content/themes/naac-hf/assets/img/bg_navHoverLeft.jpg) no-repeat 0% 0%');
		$('li.dir > a').css('color','white');
	}).mouseout(function() {
		$('li.dir > a').css('color','#1C5A95');
		$('li.dir > a').css('background','none');
	});
	$('li.dir > a').mouseover(function() {
		$(this).css('color','white');
		$('li.dir > a').css('background','url(http://naac-hf.org/wp-content/themes/naac-hf/assets/img/bg_navHoverLeft.jpg) no-repeat 0% 0%');
	}).mouseout(function() {
		$(this).css('color','#1C5A95');
		$('li.dir > a').css('background','none');
	});
	

});
