// JavaScript Document
var $j = jQuery.noConflict();

$j(function(){
	
/*------------------- IE6 WRAPPER RESIZE FIX FUNCTIONS -------------------*/ 	
   //Detect IE6
	if ( $j.browser.msie && parseInt($j.browser.version, 10) == 6 ) {
   
		//Get body width
		var  winW = $j('body').width();
	
		//Init wrapper at body width
		if(winW > 1420){
			$j('#wrapper').width(1420);
		}else{
			$j('#wrapper').width() == winW;
		}
		
		if (window.console) console.log(winW);
			//Resize wrapper on window resize and set a max width to stretch too
			//if its under that we set too 100%
			$j(window).resize(function() {
			
				winW = $j('body').width();
	
				if(winW > 1420){
					$j('#wrapper').width(1420);
		  		}else if(winW > 1000 && winW < 1420 ){
					$j('#wrapper').css('width','100%');
				}else{
					$j('#wrapper').css('width','1000px');
				}
				if (window.console) console.log(winW);
		});
	}
	
/*------------------- HEADER NEWS FUNCTIONS -------------------*/ 


	
//Var for image anchor tag set to first image div link
//var img_link = $j('#rotator .img-item:eq(0) a');
//if (window.console) console.log(header_link);
			
//Make header info link var and set to first image's link 
var info_link = $j('#header-news a');
info_link.attr('href', $j('#rotator .img-item:eq(0) a').attr('href'));

//vars for header strong and div elements
var header_strong = $j('#header-news strong');
var header_div = $j('#header-news div');

	//Make a placeholder div to hold the current title, this will be used to format 
	//the height and width of the header news div
	$j('#header').prepend('<div id="placeholder"><span></span></div>');
	
	//set var for placeholder and span tag
	var phold = $j('#placeholder');
	var phold_span = $j('#placeholder span');
	
	//Give the placeholder div the same css as target and position it off the window
	phold.css({ 
		color: '#000',
		font: 'normal 42px/54px Georgia, "Times New Roman", Times, serif',
		position:'absolute',
		top:'-1000px',
		left:'-1000px'
	});
	
	//Put the first image title text into the placeholder
	phold_span.html($j('#rotator .img-item img:eq(0)').attr('title'));
	
	//See if the placeholder width is longer than 600px if it is set to 600px
	//otherwise set to empty string
	var headerDim = setHeaderDimensions(0);

	//Set height of container div based on placeholder height	
	header_div.height(headerDim[1]);
	//set width of container div based on placeholder width
	header_div.width(headerDim[0]);
	//set the header strong html
	header_strong.html($j('#rotator .img-item img:eq(0)').attr('title'));
	
//Only run if we have multiple header images	
if($j('#rotator .img-item').length > 1){
	
	
	

	
	//Cycle through divs
	$j('#rotator').cycle({ 
    	fx: 'fade',
		timeout: 8000,
		delay: 8000,
		startingSlide: 0,
		after: onAfterFade
		//before: onBeforeFade 
	});/**/
	
	//set var to stop fade on first run
	var first_run = true;
}
	
	//function to get placeholder dimensions, set header div dimensions and either show or animate
	//text in header strong, var a is animate flag, curr_title is text to feed the animation
	function setHeaderDimensions(a,curr_title){
		
		
		//See if the placeholder width is longer than 600px if it is set to 600px
		//otherwise set to empty string
		if(phold.width() > 600){
			phold.width(600);
			var w = phold_span.width();
		}else{
			phold.css('width','');
			var w = phold.width();
		}
		
		//Get placeholder height		
		var h = phold.height()+20;
	
		//animate or just set text without animation
		if(a!=1){
			//Set height of container div based on placeholder height	
			header_div.height(h);
			//set width of container div based on placeholder width
			header_div.width(w);
		}else{
			//Animate header div and fade current text back in
			header_div.animate({width: w}, 400, function() {
				header_div.animate({height: h}, 400, function() {
  					header_strong.html(curr_title).fadeIn(500, function(){
						$j(this).css({filter:''});
					});
  				});
  			});
		}
	
		return [w, h];
	}
	

	//function to change text after image fade in	
	function onAfterFade(curr,next,opts) {
		
		//Set some vars for next and current image titles
		var curr_title = $j('#rotator .img-item img:eq('+opts.currSlide+')').attr('title');
		var next_title = $j('#rotator .img-item img:eq('+opts.nextSlide+')').attr('title');
		
		//Check if current image title is the same as placeholder text before animating
		if(curr_title != phold_span.html()){
			
			//Set placeholder span text to current image title
			phold_span.html(curr_title);
			
			//var for anchor tag
			info_link.attr('href', $j('#rotator .img-item:eq('+opts.currSlide+') a').attr('href'));
			//info_link = .attr('href', $j('#rotator .img-item:eq('+opts.currSlide+') a').attr('href'));
			if (window.console) console.log('infolink='+info_link);
			
			if(first_run!= undefined){
		
				//Fade header text out
				header_strong.fadeOut(500, function(){
				 
					//IE8 Cleartype fix, not sure if working properly	
					$j(this).css({filter:''});
					
					//run animation
					var headerDim = setHeaderDimensions(1,curr_title);
					
				});//end fade out function
				
			//give var a value to run the animation on the second image
			first_run=false;
			}
		}
	}	
	
/*------------------- NAVIGATION MENU FUNCTIONS -------------------*/ 
	
	//Hide subnav lists
	$j('ul.sub-menu').hide();
	
	//Show and hide subnav lists on hover using hoverIntent plugin
	$j('.sub-menu').each(function () {
		
		$j(this).parent().eq(0).hoverIntent({
			timeout: 100,
			over: function () {
				var current_menu = $j('.sub-menu:eq(0)', this);
				current_menu.slideDown(100);
				$j('a:eq(0)', this).addClass('hover');
				//console.log($j(this));
			},
			out: function () {
				//Set var to remove link hover class AFTER menu has slid up
				var current_parent = $j('a:eq(0)', this);
				var current_menu = $j('.sub-menu:eq(0)', this);
				//current.fadeOut(200);
				current_menu.slideUp(100, function(){
					current_parent.removeClass('hover');
					//console.log(current_parent);
				});
			}
		});
		
	//Subnav link rollover effect
	$j('.sub-menu a').hover(function () {
		$j(this).addClass('sn-hover');
		}, function () {
			$j(this).removeClass('sn-hover');
		});
	});
	
/*------------------- FEATURED NEWS FUNCTIONS -------------------*/
	
	//Check that featured news is longer than 1 article before running the cycling code
	if($j('#featured-news .news-article').length > 1){
		
		
		//Hide all but first featured news div
		$j('#featured-news .news-article:gt(0)').hide();
	
		//Set var to store height of longest article div
		var newsH = 0;
	
		//Create an array to store the href attributes of each read-more link
		var hrefs = new Array();
		
		//Create an array to store the news or events class of each article this will
		//power the banner fading
		var classes = new Array();

		//loop through each news article getting the longest in height
		//and getting the href attribute of links or setting to blank if none exist
		//may also need to remove the images then get the height and set image div to a certain height as well
		$j('#featured-news .news-article').each(function(index) {
		
			//links
			if ($j('a.read-more',this).length) {
				hrefs.push($j('a.read-more',this).attr('href'));
			}else{
				hrefs.push('#');
			}
		
		//get classes
		classes.push( $j('div.news h2', this).attr('class') );

			//heights	
			if($j(this).height() > newsH){
				newsH = $j(this).height();
			}
    	
  		});
  		
		if(classes[0]=='f-news'){
			$j('#ft-right').css({'left':'-35px'});
		}else{
			$j('#ft-right').css({'left':'-15px'});
		}
  
  		//Clone first read-more link and add to featured news div
  		$j('#featured-news .news-article .read-more:eq(0)').clone().appendTo('#featured-news');
		//$j('<div class="read-more"><span id="rm-right">&nbsp;</span><span id="rm-left"><a href="#">Read More&#8230;</a></span></div>').appendTo('#featured-news');
	
	
	
	
		//Remove all read-more links
		$j('#featured-news .news-article .read-more').remove();
  
		//Set all articles to height of longest article
		$j('#featured-news .news-article').css('height', newsH +'px');
	
		//add pager div to bottom of featured news
	 	//$j('<div id="pager"></div>').appendTo('#featured-news');
		$j('<div id="pager"></div>').insertAfter('#featured-news');
		
		


		////////////////////////////////////////////////////////////////////
		//COULD TRY USING THE JQUERY TOOLS SCROLL FUNCTIONS INSTEAD OF CYCLE
		///////////////////////////////////////////////////////////////////
		//Cycle through articles
		$j('#featured-news #article-wrapper').cycle({ 
    		fx: 'scrollLeft',
			timeout: 10000,
			cleartype: !$j.support.opacity,
			cleartypeNoBg: true,
			pager: '#pager', 
    		delay: 10000,
			pause: 1,
			//before: onBeforeScroll, 
			after: onAfterScroll
			
		});
	
		
	
		$j('#pager a').html('&nbsp;');
	
	}else{//we only have one article just reposition read more link
		$j('#featured-news .news-article .read-more:eq(0)').clone().appendTo('#featured-news');
		//Remove all read-more links
		$j('#featured-news .news-article .read-more').remove();
		
	}//end if($j('#featured-news .news-article').length > 0)
	
	//Hide the read-more link if its href is 'blank' in array, 
	//not using this function now because read more link will never be blank
	/*function onBeforeScroll(curr,next,opts) {
			//var caption = 'Image ' + (opts.currSlide + 1) + ' of ' + opts.slideCount;
			//$j('#featured-news .read-more').attr('href', hrefs[opts.currSlide]);
			//console.log(opts.nextSlide);
			if(hrefs[opts.nextSlide] == '#'){
			
				$j('#featured-news .read-more a').hide();
				$j('#featured-news .read-more #rm-left').animate({width:'0px'},350);

				//if (window.console) console.log('before blank');
			
			}else{
			
				$j('#featured-news .read-more #rm-left').animate({width:'103px'},350);
				$j('#featured-news .read-more a').show();
			}
		}
		*/
		
	
		
		
		
		
	
	function onAfterScroll(curr,next,opts) {
		
		//Add the href of the appropriate link to the read more button
		if (window.console) console.log('href='+hrefs[opts.currSlide]);
		$j('#featured-news a.read-more').attr('href', hrefs[opts.currSlide]);
		
		
	
		//if($j('h1#featured-title span').html(classes[opts.currSlide])!=$j('h1#featured-title span').html()){
		
		if (window.console) console.log('class='+classes[opts.currSlide]);
		//}
		$banner_txt = $j('#featured-title a').html();
		
		if( classes[opts.currSlide] == "f-news" && $banner_txt == "FEATURED EVENTS"){
			
			$j('#featured-title a').fadeOut(200, function(){
				
				$j('#ft-right').animate({'left':'-35px'},350, function(){
					
					$j('#featured-title a').html("FEATURED NEWS").fadeIn(200);
				
				});
				
			});
		}
		
		
		if( classes[opts.currSlide] == "f-events" && $banner_txt == "FEATURED NEWS"){
			$j('#featured-title a').fadeOut(200, function(){
				
				$j('#ft-right').animate({'left':'-15px'},350, function(){
						
						$j('#featured-title a').html("FEATURED EVENTS").fadeIn(200);
					
					});
			});
		}
		
	}
	
/*------------------- PHOTO GALLERY FUNCTIONS -------------------*/
		
		//remove ngg clear div
  		$j('.gallery-photo .ngg-clear').remove();
		
		//move album scroller to intro div
		$j('.ngg-albumoverview').appendTo( $j('#photo-album-nav') );
			
		//hide album scroller to prevent screen flash in IE
		//$j('.ngg-albumoverview').hide(function(){
		
			
		
		
		
			//add next/prev buttons
			$j('<div id="nav-prev"></div>').insertBefore('.ngg-albumoverview');
			$j('<div id="nav-next"></div>').insertAfter('.ngg-albumoverview');
		
			//move title to after image div
			$j('.ngg-album').each(function(index){
				$j('.ngg-albumtitle',this).insertAfter($j('.ngg-thumbnail',this));
			});
		
			//remove last paragraph containing number of photos and move to after header
			$j('.ngg-description').each(function(){
			
		
				var para = $j(this).find("p").filter(":last");
				var htmlStr = para.html();
				//htmlStr = htmlStr.unwrap("<strong>");
				$j('<div class="ngg-album-total">'+htmlStr+'</div>').insertBefore($j(this));
				if (window.console) console.log(htmlStr);
				para.remove();
      	
			
			});
		
		//show album scroller to prevent screen flash in IE
		//$j('.ngg-albumoverview').show();
		
		//});
		
		//move page title to photo description div
		$j("#ngg-gallerypage-title").prependTo("#photo-description");
		
		//move page header to before gallery nav
		$j("#ngg-gallery-pagination-title").prependTo("#photo-gallery-wrapper");
		
		//var altTitle = $j('.ngg-thumbnail img').attr('alt');
	
		//$j('<h3>'+altTitle+'</h3>').insertAfter.($j('.ngg-description'));
		
				//$j('.ngg-description').each(function(index){
					
					//altTitle = $j(this).parent().find($j('img').attr('alt'));
					//$j(this).prepend('<h3>'+altTitle+'</h3>');
					//$j(this).prepend('<h3>');	
					//if (window.console) console.log(altTitle);
					//});	
					
					
					
		//$j('.ngg-description').prepend(function() {
  //return '<h2 class="' + this.nodeValue + '" />';
//});		
		//$j('.ngg-albumtitle').each(function () {
			//$j(this).insertAfter( $j('.ngg-thumbnail') );
		//});
		
		//move gallery pagination above thumbnails
	$j('.ngg-navigation').prependTo( $j('#photo-gallery-wrapper') );
		//move thumbnails to wrapper div
	$j('.ngg-galleryoverview').appendTo( $j('#photo-gallery-wrapper') );
	//alter navigation links
	$j('.ngg-navigation .prev').html("Previous");
	$j('.ngg-navigation .next').html("Next");
	$j('<strong class="pipe">|</strong>').insertAfter($j('.ngg-navigation a'));
	$j('<strong class="pipe">|</strong>').insertAfter($j('.ngg-navigation span'));
	$j('.ngg-navigation .pipe:last').remove();
	
	
	
	
	
	//try some ajax on pagination links	
	//$j('.ngg-navigation a').click(function(){
		
		//var href = $j(this).attr('href');
		
		//var str = href+' .ngg-galleryoverview';
		//if (window.console) console.log(str);
		
		
		//$j('.ngg-galleryoverview').load(str);
		
		//return false;
	//});
	

	
		//Cycle through articles
		$j('.gallery-photo .ngg-albumoverview').cycle({ 
    		fx: 'none',
			timeout: 0,
			cleartype: !$j.support.opacity,
			cleartypeNoBg: true,
			//pager: '#pager',
			
			next: '#nav-next',
			prev: '#nav-prev', 
    		delay: 0,
			pause: 1
			//before: onBeforeScroll, 
			//after: onAfterScroll
			
		});
	
/*------------------- VIDEO GALLERY FUNCTIONS -------------------*/

//add some classes to video gallery pagination		
$j("#gallery-wrap .tubepress_container div.pagination a:contains('prev')").addClass('prev');		
$j("#gallery-wrap .tubepress_container div.pagination span:contains('prev')").addClass('prev');	
$j("#gallery-wrap .tubepress_container div.pagination a:contains('next')").addClass('next');		
$j("#gallery-wrap .tubepress_container div.pagination span:contains('next')").addClass('next');
//$j('<br style="clear:both;">').appendTo("#gallery-wrap div.tubepress_normal_embedded_wrapper"); 
//Want to use ajax on pagination to prevent screen refresh, not working yet
//$j("#gallery-wrap .tubepress_container div.pagination a").click(function(){
 //$(this).load("");
//});

//add a class to the secure form textarea container div
$j(".secure-form textarea").parent().addClass('text-area');


/*------------------- TWITTER FOOTER FUNCTIONS -------------------*/


 $j("#twitter").tweet({
        join_text: "",
        username: "Mancshowchoir",
        avatar_size: 0,
        count: 4,
        auto_join_text_default: "",
        auto_join_text_ed: "",
        auto_join_text_ing: "",
        auto_join_text_reply: "",
        auto_join_text_url: "",
        loading_text: "",
		template: function(i){return i["text"] + i["time"]}
      });

	/*$j("#twitter").getTwitter({
		userName: "Mancshowchoir",
		numTweets: 4,
		loaderText: "&nbsp;",
		slideIn: false,
		showHeading: true,
		headingText: "MSC Tweets",
		showProfileLink: true
	});*/
	
 
	
});


