// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var RSGApp = {};

(function($){
	$(document).ready(function(){
		var defaultSearchText = 'Search', $sf = $("#searchform");
		
		if($sf.find("#s").val() != '' && $sf.find("#s").val() != defaultSearchText){ 
			$sf.addClass("js-active");
		}

		$sf.addClass("js").find("#s").focus(function(){
			$sf.addClass("js-active");
			if($(this).val() == defaultSearchText){
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val() == '') {
				$(this).val(defaultSearchText);
				$sf.removeClass("js-active");
			}
		}).after($("<a href='#'>Search</a>").click(function(){
			if($("#s").val() != "" && $("#s").val() != defaultSearchText){
				$sf[0].submit();
			}
			return false;
		})).blur();
	});
	
	RSGApp.show_hidden_filter_objects = function(key, link) {
		var top_items = $("#" + key + "-filter-top-items", $("#sidebar"));
		var overflow = $("#" + key + "-filter-overflow", $("#sidebar"));

		overflow.slideDown();
		top_items.hide();
		$(link).fadeOut(200);
	};
	
	$.RatingControl = function(el, opts){
		var rc = this; // Makes this object availible when this might be reassigned
		var $this = $(el); // Store a jQuery object for this object
		$this.data("rating_control", this);
		this.rating = 0;
	
		this.init = function(){
			
			$this.addClass("rq-js");
			$(opts.widget_key, $this).ratingWidget();
			$(opts.yes_no_widget_key, $this).ratingYesNoWidget();
			$(opts.yes_no_widget_no_rating_key, $this).ratingYesNoWidget(true);
			this.$question_widgets = $(opts.widget_key + ", " + opts.yes_no_widget_key + ", " + opts.yes_no_widget_no_rating_key, $this);
			$(opts.dropdown_key, $this).hide();
			this.point_value = parseInt($(opts.point_value_key).val());
			this.totalQuestions = this.$question_widgets.size;
			
			this.$question_widgets.bind("value_changed", function(el, new_value){
				rc.checkRating();
			});
			this.checkRating();
		};
		
		this.checkRating = function(){
			var i = 0;
			rc.$question_widgets.each(function(){
				var value = $(this).data("rating").value;
				if($(this).data("rating").rating_type == "yes_no"){
					if(value == 1){
						value = (100 - (rc.multipleChoiceQuestions() * rc.point_value));
					} else {	
						value = 0;
					}
				} else if ($(this).data("rating").rating_type == "multiple") {
					if(value == 6) {
						value = rc.point_value;
					} else {
						value = value * (rc.point_value / 5);
					}
						
				} else {
					value = 0;
				}
				
				i += value;
			});
			rc.rating = i;	
			$this.trigger("rating_changed", rc.rating);
		};
		
		this.multipleChoiceQuestions = function(){
			return $(opts.widget_key,$this).size();
		};
		
		this.answeredQuestions = function(){
			return $("select[value!=0]",$this).size();
		};
		
		this.unansweredQuestions = function(){
			return $("select[value=0]",$this).size();
		};
		
		this.init();
	};
	
	$.RatingControl.defaultOptions = {
		widget_key: ".rating-widget",
		dropdown_key: ".rating-dropdown",
		yes_no_widget_key: ".yes-no-rating-widget",
		yes_no_widget_no_rating_key: ".yes-no-rating-widget-no-rating",
		point_value_key: "#point_value"
	};
	
	$.RatingYesNoWidget = function(el, no_save){
		if(no_save !== true) no_save = false;
		
		var rw = this; // Makes this object availible when this might be reassigned
		var $this = $(el); // Store a jQuery object for this object
		$this.data("rating", this);
		this.$select = $this.prev().find("select");
		this.value = 0;
		this.rating_type = (no_save ? "yes_no_no_save" : "yes_no");
		
		this.init = function(){
			this.$select = $this.parent().find("select");
			this.$buttons = $("span", $this);
			this.$yes = $("span:eq(0)", $this);
			this.$no = $("span:eq(1)", $this);
			
			this.$buttons.each(function(i, val){
				var value = i + 1;
				$(this).click(function(){
					rw.setValue(value);
				}).hover(function(){
					$(this).addClass("on");
				}, function(){
					if(rw.value != value){
					    $(this).removeClass("on");
					}
						
				});
			});
			
			$this.hover(function(){},
				function(){
					if(rw.value === 0){
						rw.$buttons.removeClass("on");
					} else if(rw.value === 1){
						rw.$no.removeClass("on");
					} else {
						rw.$yes.removeClass("on");
					}
				});
			$this.show();
			this.setValue(parseInt(this.$select.val()));
		};
		
		this.setValue = function(new_value){
			var old_value = this.value;
			if(new_value === 1 || new_value === 2){
				this.value = new_value;
				this.$select.val(this.value);
				
				if(new_value === 1){
					this.$yes.addClass("on");
					this.$no.removeClass("on");
				} else {
					this.$no.addClass("on");
					this.$yes.removeClass("on");
				}

			} else {
				this.value = 0;
				this.$buttons.removeClass("on");
			}
			
			if(old_value != this.value){
				$this.trigger("value_changed", new_value);
			}
			
			
		};
		
		this.init();
	};
	
	$.RatingWidget = function(el){
		var rw = this; // Makes this object availible when this might be reassigned
		var $this = $(el); // Store a jQuery object for this object
		$this.data("rating", this);
		this.$select = $this.prev().find("select");
		this.value = 0;
		this.rating_type = "multiple";
		
		this.init = function(){
			this.$options = $("span:lt(5)", $this);
			this.$na = $("span:last", $this);
			
			this.$options.each(function(i, val){
				var num = i + 1;
				$(this).hover(function(){
					$obj = $(this);
					if(rw.value === 0 || rw.value === 6 || num >= rw.value) {
						$obj.prevAll().andSelf().addClass("on");
						$obj.nextAll().removeClass("on");						
					}
				}, function(){}).click(function(){
					rw.setValue(num);
				});
			});
			
			this.$na.hover(function(){
				if(rw.value === 0 || rw.value === 6){
					rw.$options.removeClass("on");
				}
				$(this).addClass('na-on');
			}, function(){
				if(rw.value < 6)
					$(this).removeClass('na-on');
			}).click(function(){
				rw.setValue(6);
			});
			
			$this.hover(function(){},function(){
				rw.setValue(rw.value);
			});
			
			this.setValue(parseInt(this.$select.val()));
			$this.show();
			$(".rating-scale").show();
		};
		
		this.setValue = function(new_val){
			var old_value = this.value;
			if(new_val >= 0 && new_val < 6){
				this.value = new_val;
				$("span:lt(" + (new_val) + ")", $this).addClass("on");
				if(new_val > 0)
					$("span:gt(" + (new_val - 1) + ")", $this).removeClass("on");
				else
					$("span", $this).removeClass("on");
				$this.parent().removeClass("na");
				this.$na.removeClass("na-on");
			} else if(new_val == 6) {
				this.$options.removeClass("on");
				this.value = 6;
				this.$na.addClass("na-on");
				$this.parent().addClass("na");
			}
			this.$select.val(this.value);
			if(old_value != new_val && new_val >= 0 && new_val <= 6){
				$this.trigger("value_changed", new_val);
			}
		};
		
		this.init();
	};
	
	$.RatingScoreWidget = function(el, dataSource){
		var rsw = this; // Makes this object availible when this might be reassigned
		var $this = $(el); // Store a jQuery object for this object
		this.$dataSourceControl = $(dataSource);
		this.dataSource = $(dataSource).data("rating_control");

		this.init = function(){
			this.$children = $(".product-rating-num, .product-rating-sep", $this);
			this.$numerator = $(".product-rating-num", $this);
			this.$outOf = $(".product-rating-sep", $this);
			this.$noRating = $(".product-no-rating", $this).remove();
			
			this.$dataSourceControl.bind("rating_changed", function(el, new_rating){
				rsw.updateScore();
			});
			this.updateScore();
			

		};
		
		this.updateScore = function(){
			var newScore = this.dataSource.rating;
			this.$children.stop().css('opacity',0);
			if(newScore == 100){
				this.$numerator.html("<em>1</em>00");
				$this.addClass("perfect");
			} else {
				this.$numerator.text(newScore);
				$this.removeClass("perfect");
			}
			this.$numerator.show();
			this.$outOf.show();
			this.$children.animate({opacity: 1.0}, 1000);
		};
		
		this.init();
	};
	
	$.RatingChecklist = function(el, dataSource){
		var rc = this; // Makes this object availible when this might be reassigned
		var $this = $(el); // Store a jQuery object for this object
		this.$dataSourceControl = $(dataSource);
		this.dataSource = $(dataSource).data("rating_control");

		this.init = function(){
			this.$survey = $("li:eq(0)", $this);
			this.$answeredQuestions = $(".questions-answered", this.$survey);
			this.$unansweredQuestions = $(".questions-to-answer", this.$survey);
			
			this.$additional_data = $("> li:eq(1)", $this);
			this.$comments = $(" > li:eq(2)", $this);
			this.$button = $("#rating_submit_button");
			
			this.$dataSourceControl.bind("rating_changed", function(el, new_rating){
				rc.updateView();
			});
			
			$("#additional_information input, #additional_information textarea, #comment-area input, #comment-area textarea").keyup(function(){
				rc.checkCommentsAndData();
			}).change(function(){
				rc.checkCommentsAndData();
			});
			
			this.$button.click(function(){
				if(!$(this).hasClass("disabled")){
					if(confirm('You are about to submit your rating.\n Click Cancel to Review, Ok to Submit.')){
						$("#new_rating, .edit_rating").submit();
					}
				}
			});
			
			this.updateView();
			this.checkCommentsAndData();
		};
		
		this.updateView = function(){
			var qA = this.dataSource.answeredQuestions();
			var qUA = this.dataSource.unansweredQuestions();
			var total = this.dataSource.totalQuestions;
			
			if(qA === 0){
				this.$answeredQuestions.addClass("not-answered");
			} else {
				this.$answeredQuestions.removeClass("not-answered");
			}
			this.$answeredQuestions.text(qA != 1 ? qA + " Items Complete" : "1 Item Complete")
		
			if(qUA === 0){
				this.$unansweredQuestions.addClass("answered");
				this.$survey.css({backgroundColor: "#fdf8c0"}).animate({height:30, backgroundColor:"#dddddd", color:"#777"}, 800);
				this.$survey.addClass("on");
				this.$button.removeClass("disabled");
			} else {
				this.$unansweredQuestions.removeClass("answered");
				this.$survey.removeClass("on");
				this.$button.addClass("disabled");
			}
			this.$unansweredQuestions.text(qUA != 1 ? qUA + " Items Incomplete" : "1 Item Incomplete");
		};
		
		this.checkCommentsAndData = function(){
			var data = false;
			$(":text, textarea", "#additional_data").each(function(){
				if($(this).val() != ""){
					data = true;
					return false;
				}
			});
			var checks = $(":checked", "#additional_data").size();
			if (data === true || checks > 0){
				if(!this.$additional_data.hasClass("on")){
					this.$additional_data.addClass("on");
					this.$additional_data.css({backgroundColor: "#fdf8c0"}).animate({backgroundColor:"#dddddd", color:"#777"}, 800);					
				}
			} else {
				this.$additional_data.stop().removeClass("on").css({backgroundColor:"transparent", color: "#000000"});
			}
			
			if($("#comment-area textarea:eq(0)").val() != "" || $("#comment-area textarea:eq(1)").val() != ""){
				if(!this.$comments.hasClass("on")){
					this.$comments.addClass("on");
					this.$comments.css({backgroundColor: "#fdf8c0"}).animate({backgroundColor:"#dddddd", color:"#777"}, 800);					
				}
			} else {
				this.$comments.stop().removeClass("on").css({backgroundColor:"transparent", color: "#000000"});
			}
				
				
		};
		
		this.init();
	};
	
	
	$.fn.ratingWidget = function(){
		return this.each(function(){
			var rw = new $.RatingWidget(this);
		});
	};
	
	$.fn.ratingYesNoWidget = function(no_score){
		return this.each(function(){
			var rw = new $.RatingYesNoWidget(this, no_score);
		});
	};
	
	$.fn.ratingControl = function(opts){
		return this.each(function(){
			var options = $.extend({},$.RatingControl.defaultOptions, opts);
			var rc = new $.RatingControl(this, options);
		});
	};
	
	$.fn.ratingScoreWidget = function(dataSource){
		return this.each(function(){
			var rsw = new $.RatingScoreWidget(this, dataSource);
		});
	};
	
	$.fn.ratingChecklist = function(dataSource){
		return this.each(function(){
			var rsw = new $.RatingChecklist(this, dataSource);
		});
	};
	
})(jQuery);

(function($){
	$(document).ready(function(){
		$(".home #left li, #recent_ratings li, #top li").click(function(e){
			window.location = $(this).find('a').attr('href');
		}).find('a').click(function(e){
			e.stopPropagation();
		});
		$("#top h2").click(function(){
        var $h2 = $(this);
        var $ol = $(this).next();
        
        $h2.addClass('open');
        $h2.siblings("h2").each(function(){
            $(this).removeClass('open');
        });
        
        if($ol.is(":visible")) return;
        $ol.slideDown(300).siblings("ol").slideUp(300);
    });
    $("#top ol:gt(0)").hide();
    });
})(jQuery);

