$(document).ready(function() {
	var img_path = '/wp/wp-content/themes/fulscrn/img/';

    $('header label').label_as_input();
    $('ul li:last-child').addClass('last');
    $('#services li:odd').addClass('odd');
    $('.cols .col:last-child').addClass('last');
    
	$('#what_human').attr('checked', 'checked');
	$('#contact-form .field-what').hide();

    $('#contact-form').submit(function() {
        var form = $(this);
        var post_data = form.serialize();
        $.post(form.attr('action') + '?ajax=1', post_data, function(data) {
            form.html(data);
            $('.error-msg, .error', form).hide().fadeIn();
			$('#contact-form .field-what').hide();
        });
        return false;
    });
	
	$('#commentform input[type=submit]').replaceWith('<input class="send-button" type="image" value="Submit &raquo;" src="' + img_path + 'blank.png" alt="" />');

	$('#work article').css({
		'opacity': '0.7'
	});

	$('#work article').hover(function() {
		$(this).stop().animate({"opacity": "1"}, 300);
	},
	function() {
		$(this).stop().animate({"opacity": "0.7"}, 300);
	});

	$('#groups .group').gridify();

	$('.page-title').each(function() {
		var txt = $(this).html();
		txt = txt.replace('Our', '<span>Our</span>');
		txt = txt.replace('Us', '<span>Us</span>');
		$(this).html(txt);
	});

	var imgs = [
		'arrow-hover.png',
		'arrow-left-hover.png',
	];
	$(imgs).each(function() {
		var img = new Image();
		img.src = this;
	});

	// Open external links in new window
    $('a[href^=http]').each(function() {
        var href = $(this).attr('href');
        if (href && !href.match(/fulscrn.com/)) {
            $(this).attr('target','_blank')
        }
    });    

	FSC.Carousel.init();
    FSC.Comments.init();
});

var FSC = {

	Carousel : {
		current : 0,
		delay : 8000,
		timer : null,
		
		init : function() {
			if (!$('body.home').get(0)) return;
			
			FSC.Carousel.setTimer();
			
			$('#services').after('<div id="fnav" class="col"><ul></ul></div>');
			
			$('#featured li').each(function(i) {
				var bg = $(this).css('background-image');
				bg = bg.replace('url("', '');
				bg = bg.replace('")', '');
				
				var img = new Image();
				img.src = bg;
				
				$('#fnav ul').append('<li><a href=""></a></li>');
				$('#fnav li:last-child a').click(function() {
					FSC.Carousel.clearTimer();
					FSC.Carousel.show(i);
					return false;
				});
			});

			$('#fnav ul').append('<li class="pause"><a href=""></a></li>');
			$('#fnav li:last-child a').toggle(function() {
				FSC.Carousel.clearTimer();
				$(this).addClass('play');
				return false;
			},
			function() {
				window.setTimeout(FSC.Carousel.next, 1000);
				$(this).removeClass('play');
				return false;
			});
			
			var ul_width = 13 * $('#fnav li').size();
			$('#fnav ul').width(ul_width);
			
			FSC.Carousel.upnav();
		},
		
		show : function(i) {
			$('#featured li').eq(FSC.Carousel.current).fadeOut(1500);
			FSC.Carousel.current = i;
			
			$('#featured li').eq(FSC.Carousel.current).fadeIn(1500);
			
			FSC.Carousel.upnav();
			
			FSC.Carousel.setTimer();
		},
		
		next : function() {
			var x = FSC.Carousel.current + 1;
			
			if (x >= $('#featured li').size())
				x = 0;
			
			FSC.Carousel.show(x);
		},
		
		upnav : function() {
			$('#fnav a').removeClass('current');
			$('#fnav a').eq(FSC.Carousel.current).addClass('current');
		},
		
		setTimer : function() {
			FSC.Carousel.timer = window.setTimeout(FSC.Carousel.next, FSC.Carousel.delay);
		},
		
		clearTimer : function() {
			window.clearTimeout(FSC.Carousel.timer);
		}
	},

    Comments : {
        
        init : function() {
            $('#commentform').ajaxError(function() {
                if (!$('.error-msg', this).get(0)) {
                    $('.comment-notes', this).after('<p class="error-msg copy">Please fill in all the required fields below.</p>');
                }
                $('.error-msg', this).hide().fadeIn();
            });
            
            $('#commentform').submit(function() {
                $.post($(this).attr('action'), $(this).serialize(), function(data) {
                    $('#comment').val('');
                    window.location.reload();
                });
                return false;
            });
        }
        
    }
	
};


(function($){
    
 	$.fn.extend({ 
 		label_as_input: function() {
            
    		return this.each(function() {
                var input = $(this).siblings('input.text,textarea').eq(0);
                var txt = $(this).text();
                
                $(this).hide();
                
                var _focus = function() {
                    if (input.val() == txt) {
                        input.removeClass('dim');
                        input.val('');
                    }
                };
                
                var _blur = function() {
                    if (input.val() == '') {
                        input.addClass('dim');
                        input.val(txt);
                    }
                };
            
                input.focus(_focus);
                input.blur(_blur);
                
                _focus();
                _blur();
                
                input.parents('form').submit(_focus);
                
                return this;
    		});
    	}
	});

    $.fn.center = function() {
        // Always return each...
        return this.each(function() {
            var t = $(this);

            var leftMargin = t.width() / 2;
            var topMargin = t.height() / 2;

            if( typeof( window.pageYOffset ) == 'number' ) {
                // Netscape
                var scrollOffset = window.pageYOffset;
            } else if( document.body && document.body.scrollTop ) {
                // DOM
                var scrollOffset = document.body.scrollTop;
            } else if( document.documentElement && document.documentElement.scrollTop ) {
                // IE6 standards compliant mode
                var scrollOffset = document.documentElement.scrollTop;
            }
            else {
                var scrollOffset = 0;
            }

            var topOffset = ($(window).height() / 2) + scrollOffset - topMargin;
            var leftOffset = ($('body').width() / 2) - leftMargin;
            
            if (topOffset < 0)
                topOffset = 0;

            t.css({
                position: 'absolute',
                left: leftOffset + 'px',
                top: topOffset + 'px'
            });
        });
    };

    $.fn.gridify = function(per_row) {
        if (!per_row && this.size()) {
            var container = this.parent();
            var item = this.eq(0);
            per_row = Math.floor(container.width() / item.width());
        }
        
		var row_num = 1;
        return this.each(function(i) {
            if ((i+1) % per_row == 0) {
                $(this).addClass('last');
                
                var max_height = 0;
                var el = $(this);
                for (var x = 1; x <= per_row; x++) {
                    var h = el.height();
                    if (h > max_height)
                        max_height = h;
                    el = el.prev();
                }
                
                var el = $(this);
                for (var x = 1; x <= per_row; x++) {
                    el.height(max_height);
					el.addClass('row-' + row_num);
					el.addClass('col-' + (per_row - x + 1));
                    el = el.prev();
                }
				
				row_num++;
            }
        });
    };
    
})(jQuery);

