//*****slide function to toggle the header content on the home page******************************//
function sliderControls(){
	
	$('#slider ul').cycle({
		fx:			'fade',
		pause:	1,
		speed:	'slow',
		timeout: 12000,
		pager: 	'#slider-nav',
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#slider-nav li:eq(' + idx + ') a'; 
    },
		activePagerClass: 'active'
	});
	
	$('#slider-content').cycle({
		fx:			'fade',
		pause:	1,
		speed:	'slow',
		timeout: 12000,
		pager: 	'#slider-nav',
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#slider-nav li:eq(' + idx + ') a'; 
    },
		activePagerClass: 'active'
	});
	
	$('#billboard').mouseenter(function() {                  
		//pause anim  
		$('#slider ul').cycle('pause');
		$('#slider-content').cycle('pause');
	});
	
	$('#billboard').mouseleave(function() {       
		//start anim  
		$('#slider ul').cycle('resume');
		$('#slider-content').cycle('resume');
	});
}

//*****showcase function to toggle the header content on the showcase page******************************//
function showcaseControls(){
	$('#showcase').cycle({
		fx:			'scrollHorz',
		pause:	1,
		timeout: 12000,
		speed:	'slow',
		next:		'a#next',
		prev:		'a#prev',
		pager: 	'#showcase-thumbs',
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#showcase-thumbs li:eq(' + idx + ') a'; 
    },
		activePagerClass: 'active'
	});
}

//*****Replace align attribute with class**************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName('img')) return false;
  $('img[align="left"]').addClass('left').removeAttr('align');
  $('img[align="right"]').addClass('right').removeAttr('align');
  $('img[align="middle"]').addClass('middle').removeAttr('align');
}
//*****Replace target attribute with class**************************************************//
function replaceTarget(){
	$('a[target]').not('a[target=_self]').removeAttr('target').addClass('newwindow');
  $('a[target]').removeAttr('target');
}
//*****The following function make it possible to have web standard popups**************************************************//
function strictNewWindow() {
  $('a.newwindow').live('click', function(){
    window.open($(this).attr('href'));
    return false;
  });
}
//*****jQuery clear value from july 21st 2009 comment on http://www.joesak.com/2008/11/19/a-jquery-function-to-auto-fill-input-fields-and-clear-them-on-click*****//
function clearDefaultValue() {
  $(':input').focus(function() {
    if($(this).val() == $(this).attr('title')) {
      $(this).val('');
    }
  }).blur(function() {
    if($(this).val() == '') {
      $(this).val($(this).attr('title'));
    }
  });
  $('#btnNext').mousedown(function() {
	  $(':input').each(function (i) {
      if($(this).val() == $(this).attr('title')) {
        $(this).val('');
      }
    });
  });
	
	$('#footer input').each(function() {
     $(this).val('');
  })
	
}
//*****Append mask to images on the Meet the Team page *******************//
function imageMaskTeamPage(){
	if($('.page-template-team-php').length){
		$('.page-template-team-php #main #contentContainer ul li img').after('<span class="mask"></span>');
	}
}
function footerToolTip(){
	$('#footer p.more-info a').click(function() {
	  var z = $('#footer #tooltip').queue('fx');
		if(!z.length){
			if($(this).text() == "Click here to hide info"){
				$(this).text("Click here for more info");
				$(this).attr("title","Click here for more info");
			}else{
				$(this).text("Click here to hide info");
				$(this).attr("title","Click here to hide info");
			}
			$('#footer #tooltip').animate({
				"opacity": "toggle"
			}, 500, function() {
				// Animation complete.
			});
		}
		return false;
  });
}

//*****Footer form CSS and such********************************************//
function footerForm(){
	$("#footer form input[type*='text']").not('#txtFootSubmit').focus(function() {
	  $(this).css('background-color','#ffffff');
	});
	$("#footer form input[type*='text']").not('#txtFootSubmit').blur(function() {
    if($(this).val() == '') {
      $(this).css('background','none');
    }
  });
}
//*****Sidebar form CSS and such********************************************//
function sidebarForm(){
	$("#sidebar form input[type*='text']").focus(function() {
	  $(this).css('background-color','#ffffff');
	});
	$("#sidebar form input[type*='text']").blur(function() {
    if($(this).val() == '') {
      $(this).css('background','none');
    }
  });
}
//*****showcase function to toggle the header content on the showcase page******************************//
function tourSlider(){

	$(".carousel").jCarouselLite({
			btnNext: "#tour-next",
			btnPrev: "#tour-prev",
			scroll: 3,
			circular: false,
			speed: 400
	});
	
	$(".carousel ul li:eq(0) a").addClass("active");
	$("#tour-slider .tour-slide:eq(0)").show();
	
	$(".carousel ul li a").click(function() {
		var temp = $(this).parent().index();
		$("#tour-slider .tour-slide").hide();
		$("#tour-slider .tour-slide:eq(" + temp + ")").fadeIn('slow');
		$('.carousel ul li a').removeClass('active');
		$(this).addClass('active');
		return false;
	});

}
function printTV(){		
	$('.tubepress_meta_description').each(function(){
		if($(this).text().length > 200){
			// get the text within the a
			// remove leading and trailing spaces
			// get first 20 characters
			// separate characters into an array of words
			// remove the last full or partial word
			// combine into a single string and append "..."
			var shortText = $(this).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
			var shortText = shortText.substring(0, 200).split(" ").slice(0, -1).join(" "); 
			$(this).text(shortText);
			$(this).append("&hellip;");
		}
	});
}
/*
function FacebookYo(){
	$.getJSON('https://graph.facebook.com/106424762725193/feed&callback=?',
  function(data) {
    $('#supportContentContainer').prepend('<ul id="facebookFeed"> \n');
		var count = 1;
    $(data.data).each(function() {
			if(count <= 2){
      $('#supportContentContainer').find('#facebookFeed').append('\t<li class="story"> \n');
			
        string = findLinks(this.message);
				if(!string) string = "";
        $('#supportContentContainer').find('#facebookFeed li:last').append('\t\t<div class="description">' + string + '</div> \n');
				
        //Build the attachments section
        if ((this.link) || (this.caption) || (this.description)) {
          $('#supportContentContainer').find('#facebookFeed li.story:last').append('\t\t<div class="attachment"> \n');
            if ((this.link) || (this.name)) {$('#supportContentContainer').find('#facebookFeed li.story:last div.attachment:last').append('\t\t\t<div class="link">');}
              if (this.link) {$('#supportContentContainer').find('#facebookFeed li.story:last div.attachment:last div.link:last').append('<a rel="nofollow" class="newwindow" href="' + this.link + '">');}
              if (this.name) {$('#supportContentContainer').find('#facebookFeed li.story:last div.attachment:last div.link:last a:last').append(this.name);}
              if (this.link) {$('#supportContentContainer').find('#facebookFeed li.story:last div.attachment:last div.link:last').append('</a>');}
            if ((this.link) || (this.name)) {$('#supportContentContainer').find('#facebookFeed li.story:last div.attachment:last').append('</div><!--end .link--> \n');}
            //if (this.caption) {$('#supportContentContainer').find('#facebookFeed li.story:last div.attachment:last').append('\t\t\t<div class="caption">' + this.caption + '</div><!--end .caption--> \n');}
            if (this.description) {$('#supportContentContainer').find('#facebookFeed li.story:last div.attachment:last').append('\t\t\t<div class="description">' + this.description + '</div><!--end .description--> \n');}
          $('#supportContentContainer').find('#facebookFeed li.story:last').append('\t\t</div><!--end .attachment--> \n');
        }
				
				//Build the picture
        if (this.picture) {$('#supportContentContainer').find('#facebookFeed li.story:last').append('\t\t<img src="' + this.picture + '" width="94" alt="Facebook Feed" /> \n');}
				
        //Build the meta data
        if (this.created_time) {$('#supportContentContainer').find('#facebookFeed li.story:last').append('\t\t<p class="meta">' + $.timeago(this.created_time) + '</p> \n');}
        
      $('#supportContentContainer').find('#facebookFeed').append('\t</li> \n');
		}
		count++;
		});
    $('#supportContentContainer').append('</ul> \n');
  });
  function findLinks(message) {
    //find all the links in a message and make them an html link
    var regex = new RegExp(/http:\/\/\S+/);
    var match = regex.exec(message);
  	var string = message;
    if (match == null) {
    	//window.console.log("No match");
    } else {
    	if (match.length > 0) {
    		$(match).each(function(i) {
    		  string = string.replace(match[i], '<a rel="nofollow" class="newwindow" href="' + match[i] + '">' + match[i] + '</a>');
  			});
    	}
    	//window.console.log(string);
    }
    return string;       
  }
}
*/
function listBgFix(){
	if($('.page-id-583').length){
		$('#main #contentContainer .post ul li:last').css('background','none');
	}
	if($('.page-id-1054').length){
		$('#main #contentContainer .post ul li:last').css('background','none');
	}
	
}
function tweet_feed() {
	$("#tweet_feed").tweet({
		username: "MidtownPrint",
		count: 3,
		loading_text: "",
		template: "{text}"
	});
}
//*****Load all functions**************************************************//
$(document).ready(function(){
  replaceAlign();
  replaceTarget();
  strictNewWindow();
  //clearDefaultValue();
	sliderControls();
	showcaseControls();
	imageMaskTeamPage();
	footerToolTip();
	footerForm();
	sidebarForm();
	tourSlider();
	printTV();
	if($('.home').length){
		//FacebookYo();
		tweet_feed();
	}
	listBgFix();
});
