/*!
 * Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
 * backgroundPosition cssHook for jquery. Necessary to combat different css property names between browsers
 * https://github.com/brandonaaron/jquery-cssHooks
 * Licensed under the MIT License (LICENSE.txt).
 */
(function($) {
    // backgroundPosition[X,Y] get hooks
    var $div = $('<div style="background-position: 3px 5px">');
    $.support.backgroundPosition   = $div.css('backgroundPosition')  === "3px 5px" ? true : false;
    $.support.backgroundPositionXY = $div.css('backgroundPositionX') === "3px" ? true : false;
    $div = null;

    var xy = ["X","Y"];

    // helper function to parse out the X and Y values from backgroundPosition
    function parseBgPos(bgPos) {
        var parts  = bgPos.split(/\s/),
            values = {
                "X": parts[0],
                "Y": parts[1]
            };
        return values;
    }

    if (!$.support.backgroundPosition && $.support.backgroundPositionXY) {
        $.cssHooks.backgroundPosition = {
            get: function( elem, computed, extra ) {
                return $.map(xy, function( l, i ) {
                    return $.css(elem, "backgroundPosition" + l);
                }).join(" ");
            },
            set: function( elem, value ) {
                $.each(xy, function( i, l ) {
                    var values = parseBgPos(value);
                    elem.style[ "backgroundPosition" + l ] = values[ l ];
                });
            }
        };
    }

    if ($.support.backgroundPosition && !$.support.backgroundPositionXY) {
        $.each(xy, function( i, l ) {
            $.cssHooks[ "backgroundPosition" + l ] = {
                get: function( elem, computed, extra ) {
                    var values = parseBgPos( $.css(elem, "backgroundPosition") );
                    return values[ l ];
                },
                set: function( elem, value ) {
                    var values = parseBgPos( $.css(elem, "backgroundPosition") ),
                        isX = l === "X";
                    elem.style.backgroundPosition = (isX ? value : values[ "X" ]) + " " + 
                                                    (isX ? values[ "Y" ] : value);
                }
            };
            $.fx.step[ "backgroundPosition" + l ] = function( fx ) {
                $.cssHooks[ "backgroundPosition" + l ].set( fx.elem, fx.now + fx.unit );
            };
        });
    }
})(jQuery);

/*!
 * Scroll-based parallax plugin for jQuery
 * Copyright (c) 2011 Dave Cranwell (http://davecranwell.com)
 * Licensed under the MIT License.
 * 2011-05-18
 * version 1.0
 */
(function($){
	$.fn.scrollParallax = function(options) {

	if($.browser.msie && $.browser.version=="7.0") {
		var IEoffset = 100;
	} else {
		var IEoffset = 200;
	}


		var settings = {
			'speed': 0.2,
			'axis': 'x,y',
			'debug': false,
			'ystoppoint' : null,
			'ystoppoint_offset' : null,
			'nav_offset_left_var' : IEoffset,
			'no_stoppoint_y_var' : 72
		}

		function debug(msg){
			if(settings.debug && 'console' in window && 'log' in window.console){
				console.log(msg);
			}
		}
		
		return this.each(function() {
			//defined accessible $this var in standard way for use within functions
			var $this = $(this);
			
			//extend options in standard way
			if (options) {
				$.extend(settings, options);
			}
			
			$this.bind('inview', function (event, visible) {
				if (visible == true) {
					$this.addClass("inview");
					debug("in view");
				}else{
					$this.removeClass("inview");
					debug("out of view");
				}
			});
	
			//find current position so parallax can be relative to it
			var currentPosArray=$this.css("backgroundPosition").split(" ");
			var currentXPos=parseInt(currentPosArray[0].replace(/[^0-9\-]/g, ""));
			var currentYPos=parseInt(currentPosArray[1].replace(/[^0-9\-]/g, ""));

			var offset = $this.offset();
			var navoffset = $(".nav_contents").offset();
			var newxoffset = navoffset.left - settings.nav_offset_left_var;
			var newXPos = currentXPos + newxoffset;

			$this.css({'backgroundPosition':  parseInt(newXPos) + "px " + currentYPos +"px"}); 
						
			//recalculate y position on scroll
			$(window).bind('scroll', function(){
				if($this.hasClass("inview")){			
					var offset = $this.offset();

					if(settings.axis.match(/y/)){
						var attachment = 'fixed';
						var Ypos = offset.top - $(window).scrollTop();
						var newYPos = (-(Ypos) * settings.speed) + currentYPos;

						if(settings.ystoppoint){
							if((settings.speed > 0 && settings.ystoppoint < newYPos) || (settings.speed < 0 && settings.ystoppoint > newYPos)){
								var newYPos = settings.ystoppoint + settings.ystoppoint_offset;
								var attachment = 'scroll';
							}
						} else {
							var newYPos = (-(Ypos) * settings.speed) + currentYPos - settings.no_stoppoint_y_var;
						}
					}
	
					debug("new Y position: " + newYPos);

					var currentPosArray=$this.css("backgroundPosition").split(" ");
					var currentXPos=parseInt(currentPosArray[0].replace(/[^0-9\-]/g, ""));
					$this.css({'backgroundPosition':  currentXPos + "px " + parseInt(newYPos) +"px"}); 
					$this.css({'backgroundAttachment':  attachment}); 
				}
			});
			
			//recalculate x position on resize
			$(window).bind('resize', function(){

				var offset = $this.offset();
				var navoffset = $(".nav_contents").offset();
				var newxoffset = navoffset.left - settings.nav_offset_left_var;
				var newXPos = currentXPos + newxoffset;

				debug("new X position: "+ newXPos);

				var currentPosArray=$this.css("backgroundPosition").split(" ");
				var currentYPos=parseInt(currentPosArray[1].replace(/[^0-9\-]/g, ""));			
				$this.css({'backgroundPosition':  parseInt(newXPos) + "px " + currentYPos +"px"}); 
			});
		});
	};
	
})(jQuery);
