$(document).ready(function(){
    //fonts should load on every page
    initFonts();

    //if profile page
    if ($('.profile').length){
        initProfile();
    }

    //if homepage
    if ($('#twitter_box').length){
        initTwitter();
    }

    loadOpenings();

});

function _log(msg){

    //debug loggin function (comment out or put return false before live)
    console.log(msg);

}

function initFonts(){

    //cufon font replacement. If sluggish in browsers use more generic selectors

    Cufon.replace('#header li,#caseOverview.listHover a,#caseOverview li,.ourClients li a,#caseOverview li h3 span',  {
        hover: true
    });

    Cufon.replace('.profile, .tabs li, #intro p, #intro h2, #keyMsgs h2, #tweetMsg h3, .profile h3, #moreAbout h3, .keyMsgTxt, .profile span, .tabs li, #diffBtm h3, #diffBtm h4, .diffTop h2, .diffTop p, #colLarge h3, #colLarge li a, #colLarge h4, #sendCV, .caseStudy h2, .caseStudy h3, #cStudyQuote, .sliderCont h2, #auMap, #cStudyTxt h4, #cStudyResults h4, #caseOverview h3, #caseOverview p, #moreAbout li, #clientNav li, a.clickHere, #proDetails li, #proTxt h2, #proTxt p.title, #proTxt p, #filter a, #filter span, #threeCol h3, #slider p, .randomBtn a, .caseFilter form label, .intForm label, #jobSubmit, .ourClients');

}

function initProfile(){

    //make whole div clickable
    $('.profile').click(function(){

        //using find as will allow link to sit anywhere in div
        var link = $(this).find('a').attr('href');

        //if link is found
        if (link) {
            window.location = link;
        } else {
            //debug... (need error handling here if above if statement will ever be true)
            _log('no link was found!');
        }
    });

    //show on class
    $('.profile').mouseover(function(){
        //on class dictates children styles in css, remove from the others so only one on at a time
        $('.profile').removeClass('on');
        //add on to item with mouse over
        $(this).addClass('on');
        //reload cufon to show hover states
        //Cufon.refresh();
    });

    //revert
    $('.profile').mouseleave(function(){
        //remove on class from ALL profiles
        $('.profile').removeClass('on');
        //reload cufon to show hover states
        //Cufon.refresh();
    });
	
}

function initTwitter(){
    //load tweets - count is how many tweets to show
    $('#twitter_box').load('/ajax/twitter.php?count=5');

}

function twitterLoaded(){

    //show first tweet
    $('.tweet_on').show();

    //every XX seconds loop through
    var twitterFade = setInterval(function(){

        //fade out first tweet
        $('.tweet_on').fadeOut('slow', function(){

            //next in line
            $(this).next('.tweet').addClass('tweet_on');

            //back of the queue..
            $(this).removeClass('tweet_on');

            //must be called after queue adjustment to stop two tweets fading in at once
            $('.tweet_on').fadeIn('slow');

            //back of the queue..
            $('#twitter_box').append('<div class="tweet">'+$(this).html()+'</div>');

            //get outta here
            $(this).remove();

        });

    },5000);

}



// when the page is loaded..
$(function() {
    // make the cursor over <li> element to be a pointer instead of default
    $('.listHover li:not(.show_more)').css('cursor', 'pointer')
    // iterate through all <li> elements with CSS class = "clickable"
    // and bind onclick event to each of them
    .click(function() {
        // when user clicks this <li> element, redirect it to the page
        // to where the fist child <a> element points
        window.location = $('a', this).attr('href');
    });
});

function stripLastSlash(sText) {
	var lastChar = sText.charAt(sText.length -1)
	//console.log("LAST: " + lastChar);
	if(lastChar == "/")
	{
		return sText.substring(0, sText.length - 1);
	} else {
		return sText;
	}
}

$(function() {
	$('#positionNav li a[href*="' + stripLastSlash(location.pathname) + '"]').addClass('active');
	//console.log(stripLastSlash(location.pathname));
});


var LEARN_MORE_PREFIX = 'Learn More About ';
function loadOpenings(){
    
    $('.show_more_detail').each(function () {
    	// put a prefix in front of the link in Why We Are Differernt - Better4You
	    $(this).text(LEARN_MORE_PREFIX +  $(this).text());
	    $(this).wrap('<a href="javascript:void()"></a>');		// makes the link more buttony
    });
    
    // used on the Better4You sub page
    $('.show_less_detail').click(function(event){
    	$(this).toggleBetter4YouDetail();
    });
    
    // used on the Better4You sub page
    $('.show_more_detail').click(function(event){
    	$(this).toggleBetter4YouDetail();
    });
    
	$('.show_hidden').click(function(event){
    	var $target = $(event.target);
    	var opening = $(this).text() != "[less]";
		if (opening) {
			$(this).css('display','block');
			$(this).parent().css('height','160px');
			$(this).text("[less]");
		} else {
			$(this).css('display','inline');
			$(this).parent().css('height','auto');
			$(this).text("[more]");
		}
		$target.parent().find('.hidden').attr('display','inline');
		$target.parent().find('.hidden').slideToggle(300, function () {
			if (opening)
				$target.parent().find('.hidden').css('display','inline');
		});
   }); 

	$('.show_hidden_list').click(function(event){
    	var $target = $(event.target);
    	var opening = $(this).text() != "[less]";
		if (opening) {
			$(this).css('display','block');
			//$(this).parent().css('height','160px');
			$(this).text("[less]");
		} else {
			$(this).css('display','inline');
			//$(this).parent().css('height','auto');
			$(this).text("[more]");
		}
		$target.parent().find('.hidden').attr('display','block');
		$target.parent().find('.hidden').slideToggle(300, function () {
			if (opening)
				$target.parent().find('.hidden').css('display','block');
		});
   });
   
   // do homepage rollovers
   $('.tab1Teaser img').hover(
		function() {
			this.src = this.src.replace("_off","_on");
		},
		function() {
			this.src = this.src.replace("_on","_off");
		}
   );
}

jQuery.fn.toggleBetter4YouDetail = function() {
    var $o = $(this[0]) // It's your element
    $holder = $o.closest('.better4YouSection');
    $heading = $holder.find('h4');
	var collapsed = $heading.text().indexOf(LEARN_MORE_PREFIX) == 0;
	if (collapsed) {
		$heading.css('background-image','url("/images/less-arrow.png")');
		$heading.text($heading.text().substring(LEARN_MORE_PREFIX.length));
	} else {
		$heading.text(LEARN_MORE_PREFIX + $heading.text());
		$heading.css('background-image','url("/images/more-arrow.png")');
	}
	$holder.find('.hidden').slideToggle(300);
};
