(function($) {
	var HCW = {
		Config : {
			sImgPath : "/-/img/"
			,sHTMLtag : "can-has-js"
			,videoType : ''
			,state_base_url : 'http://rrmf.happycogwestdev.com/raw-milk-regulations/state/'
		}

		/*
			STOP EDITING HERE
		*/
		,init : function() {
			var c = HCW;
			var p = c.Project;

			$(document).ready(function() {
				p.tagIt();
				p.setup_state_select();
				p.clearVehicle();
				p.setup_faqs();
				
				
					if( $.browser.msie && parseInt( $.browser.version, 10 ) == 9 ){
						c.Config.videoType = 'flash';
						
						$( '.video-js-box' ).each(function(){
							
							var autoplay = false;
							
							if ( $('body.rrmf-home').length == 0 ) {
								if( jQuery.type(VideoJS) == 'function' ){
									$(this).children('video').VideoJS();
									autoplay = true;
								}
							}
							
							$(this).css( 'width', '404px' );
							$(this).css( 'height', '332px' );
							$(this).flowplayer('http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf', {
								playlist: [ $(this).children('video').eq(0).attr( 'poster' ) , {
									url: $(this).children('video').eq(0).children('source').eq(0).attr( 'src' ),
									autoPlay: autoplay,
									autoBuffering: true
								}]
							});
			
							$(this).flowplayer(0).load();
							
						});
						
						if ( $('body.rrmf-home').length > 0 ) {
							p.setup_video_switcher();
						}
					} else {
						if( jQuery.type(VideoJS) == 'function' ){
							
							$( '.video-js-box' ).each(function(){
								
								$(this).children('video').VideoJS();
								
								if ( $('body.rrmf-home').length > 0 ) {
								
									if( $(this).children('video')[0].player.html5Supported() == true ){
										c.Config.videoType = 'html5';
									} else if( $(this).children('video')[0].player.flashSupported() == true ){
										c.Config.videoType = 'flash';
									
										$(this).css( 'width', '404px' );
										$(this).css( 'height', '332px' );
										$(this).flowplayer('http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf', {
											playlist: [ $(this).children('video').eq(0).attr( 'poster' ), {
												url: $(this).children('video').eq(0).children('source').eq(0).attr( 'src' ),
												autoPlay: false,
												autoBuffering: true
											}]
										});
				
										$(this).flowplayer(0).load();
									}
									
								}
							
								$(this).children('video')[0].player.width(404);
								//$(this).children('video')[0].player.height(332);
							
							});
							
							if ( $('body.rrmf-home').length > 0 ) {
								p.setup_video_switcher();
							}
							
						}
					}
				
			});
		}

		/*
			CLIENT-SPECIFIC FUNCTIONS
		*/
		,Project : {
			tagIt : function() {
				var c = HCW;
				$("html").addClass(c.Config.sHTMLtag);
			}
			,clearVehicle : function() {
				$('input[type="text"]').each(function() {
					var val = $(this).attr("value");
					
					$(this).focus(function() {
						if ($(this).attr("value") == val) {
							$(this).attr("value","").addClass("selected");
						};
					});
					
					$(this).focusout(function() {
						if ($(this).attr("value") == "") {
							$(this).attr("value",val).removeClass("selected");
						};
					});
				});
			}
			,setup_video_switcher : function() {
				$('ul.playlist li a').click(function(){
					if( HCW.Config.videoType == '' ){
						return true;
					} else {
						$('ul.playlist li').removeClass('selected');
						var paths = $(this).attr('rel').split('|');
					
						if( HCW.Config.videoType == 'html5' ){
							$('video')[0].player.pause();
							//$('video source').attr( 'src', paths[0] );
							$('video')[0].poster = paths[1];
							$('video')[0].src = paths[0];
						
							$('video')[0].load();
							$('video')[0].player.play();
						} else if( HCW.Config.videoType == 'flash' ){
							$( '.video-js-box' ).flowplayer(0).play( paths[0] );
						}
						$(this).parent().addClass('selected');
						return false;
					}
				});
			}
			,setup_faqs : function() {
				//$('.answer').not( ':first' ).hide();
				//$('.answer').eq(0).addClass('active');
				$('.answer').hide();
				
				$('.question').each(function() {
					var tis = $(this), state = false, answer = tis.next('div');
					tis.click(function() {
						state = !state;
				    	answer.slideToggle(state);
				    	tis.toggleClass('active',state);
						return false;
					});
				});
			}
			,setup_state_select : function() {
				$('#obdb-organism').selectbox();
				$('#obdb-state').selectbox();
				$('#state-jump').selectbox({
					onChange: function (val, inst) {
						if( val != '' ){
							window.top.location = HCW.Config.state_base_url + val;
						}
					}
				});
			}
		}
	};

	HCW.init();
})(jQuery);

