// Quorum REDESIGN
var current_theme_color = "#A58D68";
var tooltip_timeout = 0;

$(document).ready(function() {

	$('#search_form').submit(function() {
			doTextBoxSearch();
	});	
	
	if ($("body").is("#home")) {
		change_home_image();
	} else {
		change_theme_color();
	};
	
	reset_more_search_options();
	
	if ($("body").is("#dealerLocator")) {
		$("#dealerResults h2").click(function(){
			$(this).next().toggle()
			if ($(this).attr("class") == "expanded") {
				$(this).toggleClass('collapsed');
			} else {
				$(this).toggleClass('expanded');
			}
		})
	};
	
	$("#more_options_link").click(function(){
		if (!$("#online_catalog_list").is(":hidden")) {
			$("#online_catalog_list").fadeOut("400ms");
		};
		if ($("#more_search_options").is(":hidden")) {
			$("#more_search_options").slideDown(1000);	
		} else {
			$("#more_search_options").slideUp(1000);
		};
	})
	
	$("#cancel_more_options_link").click(function(){
		$("#more_search_options").slideUp(1000);
	})
	
	$("#online_catalog_link").click(function(){
		if (!$("#more_search_options").is(":hidden")) {
			$("#more_search_options").fadeOut("400ms");
		};
		if ($("#online_catalog_list").is(":hidden")) {
			$("#online_catalog_list").slideDown(1000);	
		} else {
			$("#online_catalog_list").slideUp(1000);
		};
	})
	
	$("#cancel_online_catalog_link").click(function(){
		$("#online_catalog_list").slideUp(1000);
	})
	
	$("#filter_type ul li").click(function(){
		toggle_class_type(this, "selected");
		toggle_checkbox_check_type(this);
		
	})
	
	$("#filter_finish ul li img").click(function(){
		toggle_class_finish(this, "selected");
		toggle_checkbox_check_finish(this);		
	})
	
	$("#filter_finish ul li img").hover(
		function(){
			$(this).everyTime("1s", "hoverTime", function(){
				show_finish_label(this);
			})
			$(this).parent().css("z-index", "100");	
		},
		function(){
			$(this).stopTime("hoverTime");
			$(this).siblings("label").animate({
				width: "90px",
				top: "48px",
				opacity: 0
			}, 500, null, function(){
				$(this).parent().children("label").css("display", "none");
				$(this).parent().css("z-index", "1");
			})
			
		}
	);
});

function toggle_class_type (e, newClass) {
	if ($(e).attr("class") != newClass) {
		$(e).attr("class", newClass);
		$(e).css("background", current_theme_color);
		
		if (current_theme_color == "#8f1c2d" || current_theme_color == "#493340") {
			$(e).children("label").css("color", "#fff");
		};
		
	} else {
		$(e).attr("class", "");
		$(e).css("background", "");
		
		if (current_theme_color == "#8f1c2d" || current_theme_color == "#493340") {
			$(e).children("label").css("color", "#000");
		};
	};
}

function toggle_checkbox_check_type (e) {
	var input_id = "#" + $(e).children("input").attr("id");
	if (!$(input_id).is(":checked")) {
		$(input_id).attr("checked", "checked");
	} else {
		$(input_id).attr("checked", "");		
	};
}

function toggle_class_finish (e, newClass) {
	if ($(e).parent().attr("class") != newClass) {
		$(e).parent().attr("class", newClass)
		$(e).parent().css("background", current_theme_color);
		
	} else {
		$(e).parent().attr("class", "")
		$(e).parent().css("background", "");
		
	};
}

function toggle_checkbox_check_finish (e) {
	var input_id = "#" + $(e).siblings("input").attr("id");
	
	if (!$(e).is(":checked")) {
		$(input_id).attr("checked", "checked");
	} else {
		$(input_id).attr("checked", "");
	};
}

function show_finish_label (e) {
	$(e).siblings("label").animate({
		width: "90px",
		top: "45px",
		opacity: 1
	}, 500)
}
function reset_more_search_options () {
	$("#filter_type :input").each(function(){
		$(this).attr("checked", "");
	})
	$("#filter_finish :input").each(function(){
		$(this).attr("checked", "");
	})
}

function change_home_image () {
	// total 7, random number starts from 0 so we add 1 in the gen_random_num()
	var random_num = gen_random_num(7);
	var background_style = "background: #fff url(\'img/bg_home/" + random_num + ".jpg\') no-repeat left top;";	
	$("#wrap").attr("style", background_style);
	if (random_num == 3) {$("#tag_line p").attr("style", "background: url('img/tag_line_black-trans.png') left top no-repeat")};	
	if (random_num == 4) {$("#tag_line p").attr("style", "background: url('img/tag_line_white-trans.png') left top no-repeat")};	
	if (random_num == 5) {$("#tag_line p").attr("style", "background: url('img/tag_line_black-trans.png') left top no-repeat")};	
}

function change_theme_color () {
	// total 10, random number starts from 0 so we add 1 in the gen_random_num()
	var random_num = gen_random_num(9);
	var bg_colors = ["#bf7725", "#6f784b", "#8f1c2d", "#a58d68", "#c62c34", "#0093cd", "#e79a47", "#5da300", "#91a3c7", "#493340"];
	current_theme_color = bg_colors[random_num];
	// header background
	$("#header").css("background-color", current_theme_color);
	
	// search button
	$("#btn_search").css("background-color", current_theme_color);
	
	// search button
	$("p#btn_filter input").css("background-color", current_theme_color);
	
	// finish labels
	$("#filter_finish ul li label").css("background-color", current_theme_color);
}
 
function gen_random_num (num) {
	return Math.floor(Math.random()*num+1);
}

// Quorum REDESIGN


// JavaScript Document
/*	------------------------------------	*/
/* sample usage:
 onfocus="clearTextField(this, 'SEARCH')" onblur="populateTextField(this, 'SEARCH')" 
*/
function clearTextField(txtFld, defaultVal){
	if(txtFld.value == defaultVal){
		txtFld.value = "";
	}
	return;
}
/*	------------------------------------	*/
function populateTextField(txtFld, defaultVal){
	if(txtFld.value == ""){
		txtFld.value = defaultVal;
	}
	return;
}

function wishListDelete(url){
	if(confirm("Are you sure you want to remove this item from your wishlist?")){
		document.location.href = url;
	}else{
		return;	
	}
}
function saveWishList( argForm ){
	argForm.wishListAction.value = "save";
	argForm.submit();
}
function checkProductSort( argPulldown ){
	if(argPulldown.options[argPulldown.selectedIndex].value ==""){
		alert("Select a SORT option.");
		return false;
	}else{
		return true;	
	}
}
function checkProductFilter( classesBox, finishesBox){
	var totalClasses = classesBox.length;
	var totalFinishes= finishesBox.length;
		
	var i = 0;
	var itemChecked = false;
	for(i=0; i < totalClasses; i++){
		if(	classesBox[i].checked ){
			return true;	
		}
	}
	for(i=0; i < totalFinishes; i++){
		if(	finishesBox[i].checked ){
			return true;	
		}
	}

	if(!itemChecked){
		alert("Select at least one FILTER option.");
		return false;
	}
}

/*	------------------------------------	*/
/* sample usage:
 onfocus="clearTextField(this, 'SEARCH')" onblur="populateTextField(this, 'SEARCH')" 
*/
function clearTextField(txtFld, defaultVal){
	if(txtFld.value == defaultVal){
		txtFld.value = "";
	}
	return;
}
/*	------------------------------------	*/
function populateTextField(txtFld, defaultVal){
	if(txtFld.value == ""){
		txtFld.value = defaultVal;
	}
	return;
}


function setSearchCkBoxAsSelected(argForm, argFieldName, val){
	var len = argForm[argFieldName].length;
	var i =0;
	for(i=0; i<len; i++){
		if(argForm[argFieldName][i].value == val ){
			if(argForm[argFieldName][i].checked){
				argForm[argFieldName][i].checked = false;
			}else{
				argForm[argFieldName][i].checked = true;
			}
		}
	}
}
// google search trackign
function doTextBoxSearch(){
	var searchStr = jQuery.trim($("#text_search").val());
	if(searchStr == ""){
		return false;
	}
	// Google Analytics
	if(INCLUDE_GOOGLE_SEARCH == "true"){
		pageTracker._trackPageview('/?keywordsearch=' + searchStr);
	}
	return true;
}
