var NOW = NOW || {};

/*
 * NOW.ClickClear
 * @desc based on tumblr style clickclear
 * @requires CSS class styles for .clickclear, .focused, .full
 * @support ie7, ie8, ie9, FF4, Chrome
 * @todo cross browser test on ie6
 */
NOW.ClickClear = function(jQueryElement){
	var $e = jQueryElement;
	$e.addClass('clickclear');
	this.$e = $e;
	// pre check for values
	if($e.children('input').val() !== ''){
		$e.addClass('full');
	}
	this.bind();
}

NOW.ClickClear.prototype.bind = function(){
	var that = this;
	this.$e.children('label').click(function(){
		that.$e.addClass('focused');
		that.$e.children('input').focus();
	});
	
	this.$e.children('input').focus(function(){
		that.$e.addClass('focused');
	});
	
	this.$e.children('input').blur(function(){									
		that.$e.removeClass('focused');
	});
	
	this.$e.children('input').keyup(function(){
		$this = $(this);
		if($this.val() !== ''){
			that.$e.addClass('full');	
		} else {
			that.$e.removeClass('full');
		}
	});
}

//NOW.addMethod adds methods to objects and allows for additional properties like parent.
NOW.addMethod = function(parent, methodName, fn){
	parent.prototype[methodName] = fn;
	fn.parent = parent;
};

/* ---------------------------------------------------------------------------------
	NOW.Banner
	@type	constructor
	@param	options - 	available opitions:
						delay: controls delay between rotation in milliseconds (default: 6000)
						width: width of banners (default: 948)
						speed: sets speed of animation (default: 600)
	
	@return	null
------------------------------------------------------------------------------------ */
NOW.Banner = function(options){
	if(this.options.ul.length > 0){
		for(option in options){
			this.options[option] = options[option];
		}	
		this.setup();
		
		if(this.options.lis().length > 1){
			this.options.controls.css('display', 'block');
			this.bindControls();
			this.control.start();
		}
	}
};

/* -----------------------------------
	NOW.Banner.options default options 
-------------------------------------- */
//NOW.Banner.prototype.options = {
	//delay: 6000,
	//width: 990,
	//speed: 900,
	//ul: $('#banner-slider ul'),
	//lis: function(){
		//return $('#banner-slider ul li');
	//},
	//controls: $('#banner-slider .controls'),
	//fade: function(){
		//return $('#banner-slider .fade');
	//},
	//random: true
//};

/* -----------------------------------
	NOW.Banner.setup
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'setup', function () {

    this.options.lis().css('display', 'none');

    // randomise if random is set to true
    /*var startIndex = (this.options.random == true) ? Math.floor(Math.random() * this.options.lis().length) : 0;
    this.options.lis().eq(startIndex).prependTo( this.options.ul ).css('display', 'block');*/
    if (this.options.random == true) {
        this.options.ul.reorder();
    }

    this.options.lis().eq(0).css('display', 'block');
    this.options.lis().eq(1).css({ left: this.options.width + 'px', display: 'block' });
});

/* -----------------------------------
	NOW.Banner.control
	@method	start
	@method	stop
	@method reset
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'control', {
	_timer: null,
	start: function(){
		var that = this;
		this._timer = setInterval(function(){
					  that.parent.prototype.slide('right');
					}, that.parent.prototype.options.delay);
		return this;
	},
	stop: function(){
		var that = this;
		if(this._timer){
			clearInterval(that._timer);
		}		
		return this;
	},	
	reset: function(){
		if(this._timer){
			this.stop().start();
			return this;
		}
	}		  
});

/* -----------------------------------
	NOW.Banner.bindControls
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'bindControls', function(){
	var that = this;
	this.options.controls.children('.back').click(function(e){
		e.preventDefault();
		that.control.reset();
		that.slide('back');
	});
	this.options.controls.children('.next').click(function(e){
		e.preventDefault();
		that.control.reset();
		that.slide('next');
	});
});

/* -----------------------------------
	NOW.Banner.unbindControls
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'unbindControls', function(){
	var that = this;
	this.options.controls.children('.back').unbind('click').click(function(e){
		e.preventDefault();
	});
	this.options.controls.children('.next').unbind('click').click(function(e){
		e.preventDefault();
	});
});

/* -----------------------------------
	NOW.Banner.slide
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'slide', function(dir){
	
	var that = this;
	this.unbindControls();
	
	var lis = this.options.lis();
	
	/* Direction: back */
	if(dir == 'back'){
		
		lis.eq(0).animate({ left: this.options.width + 'px' }, this.options.speed, 'easeInOutExpo', function(){
			/*$(this).prepenTo( NOW.Banner._data.lis.eq() ).css({ display: 'none', left: 0 });*/
			if(lis.length == 2){
				lis.eq(0).css({ left: that.options.width + 'px', display: 'block' });
			} else {
				lis.eq(2).css({ left: that.options.width + 'px', display: 'block' });
			}
			
			that.bindControls();
		});
		lis.eq(lis.length - 1).prependTo( this.options.ul ).css({ left: '-' + this.options.width + 'px', display: 'block' }).animate({ left: '0' }, this.options.speed, 'easeInOutExpo');
		
	} else {
		
		// move current banner to -948px, on callback append to parent
		lis.eq(0).animate({ left: '-' + this.options.width + 'px' }, this.options.speed, 'easeInOutExpo', function(){
			$(this).appendTo( that.options.ul ).css({ display: 'none', left: 0 });
			if(lis.length == 2){
				lis.eq(0).css({ left: that.options.width + 'px', display: 'block' });
			} else {
				lis.eq(2).css({ left: that.options.width + 'px', display: 'block' });
			}
			
			that.bindControls();
		});
		
		// move offscreen banner to 0
		lis.eq(1).animate({ left: '0' }, this.options.speed, 'easeInOutExpo');
		
	}
	
});

/* ---------------------------------------------------------------------------------
	NOW.Gallery
	@type	constructor
	@param	options - 	available opitions:
	
	@return	null
------------------------------------------------------------------------------------ */
NOW.Gallery = function(){
	if($(this.options.id).length > 0){
		this.setup();
	}
};

NOW.Gallery.prototype.options = {
	id: '#gallery',
	control: '#gallery .control-images',
	controlBtns: '#gallery .control-images li a',
	imageContainer: '#gallery .main-images',
	images: '#gallery .main-images li'
};

NOW.Gallery.prototype._data = {
	images: new Array(),
	currentImage: null
};

NOW.addMethod(NOW.Gallery, 'setup', function(){
											 
	this.setCurrentImage(0);
	//this.setHeight(); Disable this, it is no longer needed.
	this.bindControls();
	
	this.hideAll();
	this.show(0);
});

NOW.addMethod(NOW.Gallery, 'loadImages', function(img){
	var that = this;	
	$(this.options.controlBtns).each(function(){
		var image = $('<img />');
			image.attr(this.href);
			image.load(function(){
						
			});		
	});
	for(var i = 0, l = this._data.images.length; i < l; i++){
		this._data.images[i].load(function(){})
	}
});

// accepts quick or blank
NOW.addMethod(NOW.Gallery, 'setHeight', function(type){
		var height = 0;
		
		$(this.options.imageContainer).children().each(function(){
			var $this = $(this);
			
			if($this.children().height() > height){
				height = $this.children().height();
			};
		});
		
		$(this.options.imageContainer).height(height);
		
});

NOW.addMethod(NOW.Gallery, 'setCurrentImage', function(index){
	this._data.currentImage = $(this.options.imageContainer).children().eq(index).children('img');
});

NOW.addMethod(NOW.Gallery, 'getCurrentImage', function(){
	return this._data.currentImage;
});

NOW.addMethod(NOW.Gallery, 'show', function(index){
	$(this.options.imageContainer).children().eq(index).css('display', 'block');
});

NOW.addMethod(NOW.Gallery, 'hide', function(index){
	$(this.options.imageContainer).children().eq(index).css('display', 'none');
});

NOW.addMethod(NOW.Gallery, 'hideAll', function(){
	$(this.options.imageContainer).children().css('display', 'none');
});

NOW.addMethod(NOW.Gallery, 'bindControls', function(){
	var that = this;
	$(this.options.controlBtns).click(function(e){
											   
		e.preventDefault();
		var $this = $(this),
			index = $this.parent().index();
		
		// remove active from all control li's
		$(that.options.control).children('li').removeClass('active');
		// add active to this parent
		$this.parent().addClass('active');
		
		//hide all and then show appropriate image
		that.hideAll();
		that.show(index);
		
	});
});

NOW.addMethod(NOW.Gallery, 'unbindControls', function(){
	$(this.options.controlBtns).unbind('click').click(function(e){
		e.preventDefault();
	});
});


/** IE6 Drop down menu fix **/
sfHover = function() {
	var sfEls = document.getElementById("main-navigation").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" hover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

$.fn.reorder = function (callback) {

    function randOrd() { return (Math.round(Math.random()) - 0.5); }

    return ($(this).each(function () {
        var $this = $(this);
        var $children = $this.children();
        var childCount = $children.length;

        if (childCount > 1) {
            $children.hide();

            var indices = new Array();
            for (i = 0; i < childCount; i++) { indices[indices.length] = i; }
            indices = indices.sort(randOrd);
            $.each(indices, function (j, k) {
                var $child = $children.eq(k);
                $child.appendTo($this);
                if (callback != undefined) {
                    callback($child);
                }
            });

        }

    }));
};

$(document).ready(function(){
	
	var test = new NOW.ClickClear($('#field1'));
	var test1 = new NOW.ClickClear($('#field2'));
	
  //var Banner = new NOW.Banner();
	
	Cufon.replace('.home .content p', { fontFamily: 'HelveticaNeueLT Com 75 Bd' } );
	Cufon.replace('.standard-page-heading h1', { fontFamily: 'Helvetica Neue cond Bd' } );
	Cufon.replace('.footer ul li a, ul li.social', { fontFamily: 'TradeGothic Bd' } );
	
});
