$(document).ready(function() {
	
	/*
	 * Members Page Hack
	 *
	 * Find all images in the members table and replace the
	 * src with the development server image url
	 */
	/*var members = $('.members_tbl img');
	if(members.length > 0) {
				
		// loop through images
		members.each(function() {
			
			// find and replace both previous ip addresses with new url
			var src = $(this).attr('src').replace('192.168.1.14', 'www.zone1media.net/development_sites');
			src = src.replace('192.168.1.9', 'www.zone1media.net/development_sites');
			
			// replace image src
			$(this).attr('src', src);
		});
	}*/
	
	// ghost text on resources password field
	$('form[name=resources_form] input[name=password]').focus(function() {
		// if no change to field then clear on focus
		if($(this).val() == 'password') {
			$(this).val('');
		}
	}).blur(function() {
		// if empty set back to 'password'
		if($(this).val().length == 0) {
			$(this).val('password');
		}
	});
	
	// ghost text on search field
	$('form#searchform input[name=s]').focus(function() {
		// if no change to field then clear on focus
		if($(this).val() == 'Keyword(s)') {
			$(this).css('fontStyle', 'normal').val('');
		}
	}).blur(function() {
		// if empty set back to 'password'
		if($(this).val().length == 0) {
			$(this).css('fontStyle', 'italic').val('Keyword(s)');
		}
	});
	
	/* 
	 * Accordion Navigation
	 */
	var activeAcc = 3;
	
	// start accordion in appropriate place
	if(_PAGE == "Active Groups") {
		activeAcc = 0;
	} else if(_PAGE == "Groups in Formation") {
		activeAcc = 1;
	}
	
	// accordion init
	$('#navigation').accordion({
		active : activeAcc,
		change : function(event, ui) {
			
			// find current
			var header = ui.newHeader.find('a').html();
			
			// if business matters start fadeIn feed
			if(header == "Business Updates") {
				
				setTimeout(function() {
					$('.loader').fadeOut(750, function() {
						$('#business_matters').fadeIn(750);
					});
				}, 350);	
				
			// else hide feed and display loader
			} else {
				$('#business_matters').hide();
				$('.loader').show();
				
			}
		}
	});
	
	$('#navigation').bind('accordionchange', function(event, ui) {
		if(ui.newHeader.attr('id') == 'partner_link') {
			window.location = ui.newHeader.find('a').attr('href');
			return false;
		}
	});
	
	
	/*
	 * Accordion Div Arrow Scrolling
	 */
	var limitCounter = 0;
	
	$('.accordion_block .scroll_holder a.scroll_up, .accordion_block .scroll_holder a.scroll_down').click(function() { 
	
		var scrollClass = $(this).attr('class');
		var holder = $(this).parents('.accordion_block').children('ul');
		var scrollHolder = $(this).parents('.scroll_holder').children('ul');
		var holderLimit = $(this).parents('.accordion_block').children('ul').children('li').length;
		
		// switch direction
		switch(scrollClass) {
			
			case 'scroll_up':
			
				// if not at the start animate div 
				if(holder.scrollTop() > 0) {
					holder.animate({
						scrollTop : (holder.scrollTop() - holder.height())+'px'
					}, {
						duration : 500,
						easing : 'easeOutQuad'
					});
					
					if($(this).attr('data-visnum') !== 'undefined') {
						limitCounter -= $(this).attr('data-visnum');
					}
				}
				
			break;
			
			case 'scroll_down':
			
				// if not at the end animate div
				if(limitCounter !== holderLimit && $(this).attr('data-visnum') !== 'undefined') {
					holder.animate({
							scrollTop : (holder.scrollTop() + holder.height())+'px'
					}, {
						duration : 500,
						easing : 'easeOutQuad'
					});
					
					limitCounter += $(this).attr('data-visnum');
				}
				
			break;
			
		}
		
		return false;
		
		
	});


	/*
	 * Testimonials Cycle
	 */
	// hide all quotes and fade in first
	$('.quote_text').hide();
	$('.quote_text').eq(0).fadeIn(500);
	
	var quoteCounter = 1;
	
	// set timer to fade out previous quote and fade in the next
	setInterval(function() {
		
		$('.quote_text').hide();
		$('.quote_text').eq(quoteCounter).fadeIn(500);
					
		quoteCounter = (quoteCounter == 5) ? 0 : quoteCounter + 1;
		
	}, 5000);
	
	
	/*
	 * FAQ
	 */
	if(_PAGE == "FAQ" || _PAGE == "BRX Tips and Advice" || _PAGE == "Partners") {
		
		// hide all answers
		$('.answer').hide();
		
		// hide all answers and slide down appropriate answer on click of a question
		$('.question').css('cursor', 'pointer').each(function(index) {
			$(this).click(function() {		
				if($('.answer').eq(index).is(':hidden')) {
					$('.answer').slideUp(200, function() {
						setTimeout(function() {	
							$('.answer').eq(index).slideDown(200);
						}, 250);
					});
				}
			});
		});
	}
	
	
	/*
	 * Leads
	 *
	 * HACK!! (sorry rushing!)
	 * 
	 * Remove unwanted leads from RSS feed. Ben or Alek
	 * if you can do this in the PHP it would be way better!
	 */
	$('#leads_list li:gt(29)').remove();
	
	
	/*
	 * Buzz
	 *
	 * Same again!! (see above)
	 */
	$('#brxbuzz ul#buzzfeed li:gt(3)').hide();
	
	// scroll through 3 pages of BRX Buzz
	$('#brxbuzz ul#foot_nav li a').click(function() {
		
		var index = $(this).parent().index();
		var multiplier = index + 1;
		var from = index * 4;
		var to = multiplier * 4;
		
		$('#brxbuzz ul#buzzfeed li').hide();
		$('#brxbuzz ul#buzzfeed li').slice(from, to).fadeIn(500);
		
		$('#brxbuzz ul#foot_nav li a').removeClass('active');
		
		$(this).addClass('active');
		
		return false;
		
	});
	
	/*
	 * Twitter feed
	*/
	if(window.getTwitters) {
		getTwitters('twitter_feed', {
			id: 'brxnet', 
			clearContents: true,
			count: 4, 
			withFriends: true,
			ignoreReplies: false,
			newwindow: true,
			callback: function() {
				$('#twitter_feed ul').fadeIn(500);
			}
		});
	}
	
	/*
	 * Calendar preview cycle (copy of testimonials)
	 * This is a bit hacked...
	 */
	// hide all quotes and fade in first
	$('.calendar_homepage_preview').hide();
	$('.calendar_homepage_preview').eq(0).fadeIn(1000);
	$('.calendar_homepage_preview').eq(1).fadeIn(1000);
	$('.calendar_homepage_preview').eq(2).fadeIn(1000);
	
	var calCounter = 0;

	// set timer to fade out previous quote and fade in the next
	setInterval(function() {
		calCounter = (calCounter >= $('.calendar_homepage_preview').size()-3) ? 0 : calCounter + 3;
		$('.calendar_homepage_preview').hide();
		$('.calendar_homepage_preview').eq(calCounter).fadeIn(1000);		
		$('.calendar_homepage_preview').eq(calCounter+1).fadeIn(1000);		
		$('.calendar_homepage_preview').eq(calCounter+2).fadeIn(1000);		
	}, 10000);
	
	$(".discussions_video a").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'		: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	
		return false;
	});

	//banner
	$('#banner').rotating_banner({'time': 15000, 'child': 'div', 'effect': 'fade'});
	
});

