/*
*************************************************

MOZILLA.ORG
JavaScript Functions

Created by the friendly folks at Happy Cog
http://www.happycog.com/

*************************************************
*/

// VARIABLES --------------------------------------------------
var HCP = {};
HCP.mozilla = function() {
	return {
		init: function() {
			// general functions
			HCP.mozilla.greyInitialValues();
			HCP.mozilla.initSearchText();
			
			// home page
			HCP.mozilla.augmentCommunityHovers();
			
			// project carousel
			if (!jQuery.browser.msie || (jQuery.browser.msie && jQuery.browser.version > 6)) {
				HCP.mozilla.carousel.init();
			}
			
			if (window.location.toString().indexOf('liveresize') >= 0) {
				setInterval(function() {
					HCP.mozilla.gutters();
				}, 50);
			} else if (window.location.toString().indexOf('cssresize') == -1) {
				HCP.mozilla.gutters();
			}
		}
	}
}();




// INITIAL ACTIONS --------------------------------------------------

$(document).ready(function() { HCP.mozilla.init(); });







// FUNCTIONS --------------------------------------------------


/* =======================================
	Generic functions 
======================================= */

HCP.mozilla.greyInitialValues = function(){
	
	$('input.filled').each(function(){
	
		this.startText = $(this).attr('value');
	
	}).focus(function(){		
		
		if($(this).val() == this.startText){
			$(this).val('');
			$(this).removeClass('filled');	
		}
		
	}).blur(function(){
		
		if($(this).val() == ''){
			$(this).val(this.startText);
			$(this).addClass('filled');
		}
		
	});	
	
}



/* =======================================
	initSearchText
======================================= */
HCP.mozilla.initSearchText = function(){

	$('#q').focus(function(){
		if($(this).val() == ''){
			$('label[for="q"]').fadeOut('fast');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$('label[for="q"]').fadeIn('fast');
		}
	});
	
}


/* =======================================
	Augment Community Hovers
	(Contains the whole block)
======================================= */

HCP.mozilla.augmentCommunityHovers = function(){

	$('#community-sub .rows a').mouseenter(function(){
		$(this).parent().addClass('over');
		/*$(this).parent().animate({
			backgroundColor: '#f5f6f6'
		}, 250);*/
	}).mouseleave(function(){
		$(this).parent().removeClass('over');
		/*$(this).parent().animate({
			backgroundColor: '#ffffff'
		}, 250);*/
	});

}

/* =======================================
	Project Carousel (Homepage)
======================================= */
HCP.mozilla.carousel = function() {
	var currentPage = 1;
	var perPage = 1;
	var totalPages = 1;
	var projectWidth = 225;
	
	var carouselPagination = function() {
		$('.projects').css('width', '');
		perPage = Math.floor(parseFloat($('.projects').width())/projectWidth);
		totalPages = Math.ceil($('.projects .project').size() / perPage);
		$('.projects').css('width', perPage*projectWidth);
		
		if (!$('.project-carousel .pagination').size()) {
			$('#our-projects').after('<p class="pagination"></p>');
		}
		
		$('.project-carousel .pagination').html(
			currentPage+' of '+totalPages+
			' <a href="#" class="prev'+(currentPage==1?' disabled':'')+'">Prev</a>'+
			'<a href="#" class="next'+(currentPage==totalPages?' disabled':'')+'">Next</a>');
		
		$('a.prev, a.next').click(function() {
			if ($(this).hasClass('disabled')) {
				return false;
			}
		
			var direction = ($(this).hasClass('prev'))?-1:1;
			var movement = -direction * (perPage*projectWidth);
			currentPage+=direction;
		
			$('.project').each(function(i) {
				$(this).animate({
					left: parseFloat($(this).css('left').replace(/px$/))+movement
				}, 50);
			});
		
			carouselPagination();
			return false;
		});
	}

	var carouselStageListener = function() {
		
		if (window.location.toString().indexOf('cssresize') == -1) {
			HCP.mozilla.gutters();
		}
		
		carouselPagination();
	}
	
	return {
		init: function() {
			$('.projects').wrap('<div class="projects-wrap"></div>');
			$('.projects .project').each(function(i) {
				$(this).absolutize();
				$(this).css('left', i*projectWidth);
			});
			
			carouselPagination();
			
			$(window).resize(carouselStageListener);
		}
	}
}();

HCP.mozilla.gutters = function() {
	var minWidth = 900;
	var maxWidth = minWidth+332;
	var margin = 0;
	
	if (window.location.toString().indexOf('fixedmargins') >= 0) {
		if ($(window).width() <= 1140) {
			margin = 0;
		} else {
			margin = 166;
		}
	} else {
		if($(window).width() > minWidth && $(window).width() < maxWidth) {
			margin = ($(window).width()-minWidth)/2;
		} else if ($(window).width() <= minWidth) {
			margin = 0;
		} else if ($(window).width() >= maxWidth) {
			margin = 166;
		}
	}
	
	marginLeft = marginRight = margin;
	if ($(document.body).hasClass('home') && marginRight > 0) {
		marginRight-= 10;
	}
	
	$(document.body).css('background-position', '-'+(166-marginLeft)+'px 0');
	$(document.body).css('margin', '0 '+(marginRight)+'px 0 '+marginLeft+'px');
	
	$('#header').css('margin-right', '-'+(marginRight)+'px');
	$('#footer-wrap').css('margin-right', '-'+(marginRight)+'px');
	
	if (!jQuery.browser.msie || (jQuery.browser.msie && jQuery.browser.version > 6)) {
		$('.project-carousel').css('margin-right', '-'+(marginRight+295)+'px');
		$('.project-carousel .projects').css('margin-right', (marginRight+295)+'px');
		$('.project-carousel .pagination').css('right', (marginRight+295)+'px');
	}
	
	if (marginLeft < 23) {
		marginLeft = 0;
	} else {
		marginLeft-=23;
	}
	
	$('#nav').css('margin-right', (marginLeft+43)+'px');
	$('#quick-search').css('right', marginLeft+'px');
}









