var themeRoot = siteRoot+'wp-content/themes/gabriel_v1/';


$(document).ready(function() {

	// DEV SIDEBAR
	$('#closeDev').click(function() {
		$('#dev').fadeToggle();							  
	});
	
	if(!ie6) {
		
		$('#searchBox_ddButton').click(function() {
			
			
				if($(this).hasClass('down')) {
					
					$.mask.close();
					seachBox_ddClose();	
					
				} else {
					searchBox_ddOpen();
				}
			
				
		});
	
	
	
		var pv_ajaxURL = themeRoot+"grc_search/ajax_pv_dbquery.php";
		
		
		
		
		$("#searchBox_accordian").tabs("#searchBox_accordian div.pane", {tabs: 'h3', effect: 'slide', initialIndex: null});
		
	
		// SET TO OPEN FOR HOME PAGE
		if(homePage) {
			
			
			if(ie7) {
				
				searchBoxOpen = false;
				
			
			} else {
				
				searchBoxOpen = true;
				pv_populateYear();
				com_populateMake();
			
				
			}
			
			
			// PRE-POPULATE PV.Year and HD.Make Fields on HOMEPAGE only!
			
			
		} else {
			
			
			
		}
		
		
			// SEARCH BOX DROP DOWN FUNCTIONALITY
		
		if(!ie7) {
		
			if(homePage) {
				
				$('#searchBox').bind('mouseenter', function() {
					if(!pv_populatedYear) {
						pv_populateYear(); 
						pv_populatedYear = true;
					}
					if(!com_populatedMake) {
						com_populateMake();
						com_populatedMake = true;
					}
				});
				
			}
		
			var form = $(".expose").children().bind("click keydown focus", function() {
		
				if($('#searchBox_ddButton').hasClass('down')) {
				
					$(this).expose({
							
							onBeforeLoad: function() {
								
							},
							onLoad: function() {
								
								$('#exposeMask').css({backgroundColor: '#000'});
								if(!homePage) {
									if(!pv_populatedYear) {
										pv_populateYear(); 
										pv_populatedYear = true;
									}
									if(!com_populatedMake) {
										com_populateMake();
										com_populatedMake = true;
									}
								}
							},
							onClose: function() {
								
								if(searchBoxOpen) {
									seachBox_ddClose();	
								}
								
							},
							color: '#000',
							opacity: 0.6
				
					});
					
				}
				
			});
		
		} //ie7
			
			
	} // !ie6
	
	
	
	// WRAP MAIN NAVIGATION SUB MENUS
	$('#mNav ul.sub-menu').wrap('<div class="sub-wrapper" />');
	
		
	// SET EXTERNAL MENU LINKS TO _TARGET BLANK
	$('#menu-item-181 a').attr('target', '_blank');
	$('#menu-item-182 a').attr('target', '_blank');
	$('#menu-item-192 a').attr('target', '_blank');
	$('#menu-item-1140 a').attr('target', '_blank');
	

});


function seachBox_ddClose() {
	
	searchBoxOpen = false;
	
	$('#searchBox_content').slideToggle(400, 'swing', function() {
		$('#searchBox_ddButton').removeClass('down');
		$('#searchBox_ddButton').addClass('up');
		$('#searchBox_ddButton').attr('src', siteRoot+'/wp-content/themes/gabriel_v1/images/search_box/searchbox_dd_button_up2.png');
		
		if($.mask.isLoaded()===true){$.mask.close();}
	});	
	
}

function searchBox_ddOpen() {
	
	$('#searchBox_ddButton').removeClass('up');
	$('#searchBox_ddButton').addClass('down');
	$('#searchBox_ddButton').attr('src', siteRoot+'/wp-content/themes/gabriel_v1/images/search_box/searchbox_dd_button_down.png');
	$('#searchBox_content').slideToggle(400, 'swing', function() {
															   
		searchBoxOpen = true;
		pv_populateYear();
		com_populateMake();
						
	});
	
	
}
// MASK STATUS for HomePage

var homePage = false;
var ie7 = false;
var searchBoxOpen = true;

var pv_populatedYear = false;
var pv_makeAdded = false;
var pv_modelAdded = false;
var pv_engineAdded = false;

var pv_yearMSG = '';

function resetPVpartField() {
	$('#pv_part').attr('value','Or enter a Gabriel Part Number.');			
}

function pv_populateYear() {
	
	var id = '#pv_year';

	// if(!pv_populatedYear) {pv_yearMSG = $(id).html();}

	var ajaxURL = themeRoot+"grc_search/ajax_pv_dbquery.php";
	resetPVpartField();
	
	
	if(!pv_populatedYear) {
		
		$.ajax({
			type: "GET",
			url: ajaxURL,
			data: '',
			cache: false,
			success: function(html) {
				$(id).append(html);
					
				
			},
			failure: function() {'XML failure, please contact the system administrator';}
			
		});
	
	}
	
}

function add_pvMakeSelect() {
	
	var year = document.getElementById('pv_year').value;
	var dataString = 'year='+year;
	
	
	var ajaxURL = themeRoot+"grc_search/ajax_pv_dbquery.php";
	resetPVpartField();
	
	if(!pv_makeAdded) {
		
		var id = '#pv_selects';
		
		// alert(dataString)
		
		$.ajax ({
			type: "GET",
			url: ajaxURL,
			data: dataString,
			cache: false,
			success: function(html, dataString) {
				
				var newHTML = '<select id="pv_make" name="pv_make" class="marginTop" onchange="add_pvModelSelect();"><option value="0">Please enter the MAKE of your vehicle</option></select>'; 		
				$(id).append(newHTML);	
				$('#pv_make').append(html);
				slideFade('#pv_make');
			
			},
			failure: function() {'XML failure, please contact the system administrator';}
			
		});
		
		pv_makeAdded = true;
	
	} else {
		
		// CLEAR OUT CURRENT SELECTIONS
		
		$.ajax ({
			type: "GET",
			url: ajaxURL,
			data: dataString,
			cache: false,
			success: function(html, dataString) {

				$('#pv_make').empty();
				$('#pv_make').append('<option value="0">Please enter the MAKE of your vehicle</option>'+html);
			
			},
			failure: function() {'XML failure, please contact the system administrator';}
			
		});
		
	}	
	
	// CLEAR OUT DEPENDENT SELECTS
	
	if(pv_engineAdded) {
		$('#pv_engine').empty();
		$('#pv_engine').append('<option value="0">Please enter the ENGINE BASE of your vehicle</option>');
	}
	
	if(pv_modelAdded) {
		$('#pv_model').empty();
		$('#pv_model').append('<option value="0">Please enter the MODEL of your vehicle</option>');
	}
	
	
	
}

function add_pvModelSelect() {
	
	var year = document.getElementById('pv_year').value;
	var make = document.getElementById('pv_make').value;
	
	var dataString = 'year='+year+'&make='+make;
	
	
	var ajaxURL = themeRoot+"grc_search/ajax_pv_dbquery.php";
	resetPVpartField();
	
	if(!pv_modelAdded) {
		
		var id = '#pv_selects';
		
		// alert(dataString)
		
		$.ajax ({
			type: "GET",
			url: ajaxURL,
			data: dataString,
			cache: false,
			success: function(html, dataString) {
				
				var newHTML = '<select id="pv_model" name="pv_model" class="marginTop" onchange="add_pvEngineSelect();"><option value="0">Please enter the MODEL of your vehicle</option></select>'; 		
				$(id).append(newHTML);	
				$('#pv_model').append(html);
				slideFade('#pv_model');
			
			},
			failure: function() {'XML failure, please contact the system administrator';}
			
		});
		
		pv_modelAdded = true;
	
	} else {
		
		// CLEAR OUT CURRENT SELECTIONS
		
		$.ajax ({
			type: "GET",
			url: ajaxURL,
			data: dataString,
			cache: false,
			success: function(html, dataString) {

				$('#pv_model').empty();
				$('#pv_model').append('<option value="0">Please enter the MODEL of your vehicle</option>'+html)
			
			},
			failure: function() {'XML failure, please contact the system administrator';}
			
		});
		
	}
	
	// CLEAR OUT DEPENDENT SELECTS
	if(pv_engineAdded) {
		
		$('#pv_engine').empty();
		$('#pv_engine').append('<option value="0">Please enter the ENGINE BASE of your vehicle</option>');
	}

	
}

function add_pvEngineSelect() {
	
	var id = '#pv_selects';
	resetPVpartField();
	
	if(!pv_engineAdded) {
	
		var html = '<select id="pv_engine" name="pv_engine" class="marginTop"><option value="">Please enter the ENGINE BASE of your vehicle</option><option value="1">All</option></select>'; 		
		$(id).append(html);
		
		slideFade('#pv_engine');
		
		pv_engineAdded = true;
		
	} else {
		
		$('#pv_engine').empty();
		$('#pv_engine').append('<option value="0">Please enter the ENGINE BASE of your vehicle</option><option value="All">All</option>');
		
	}
	
}


function pvSearchSubmit() {
	
	var year = document.getElementById('pv_year').value;
	if(document.getElementById('pv_make')) {var make = document.getElementById('pv_make').value;} else {var make=0;}
	if(document.getElementById('pv_model')) {var model = document.getElementById('pv_model').value;} else {var model=0;}
	if(document.getElementById('pv_engine')) {var engine = document.getElementById('pv_engine').value;} else {var engine=0;}
	
	if(year != 0 && document.getElementById('pv_part').value == 'Or enter a Gabriel Part Number.') {
		
		if(year != 0 && make != 0 && model != 0 && engine != 0) {
					
			document.getElementById('pvSearchSubmit').click();	
		
		} else {
			
			alert('You must fill out the YEAR, MAKE, MODEL, and ENGINE BASE to proceed with a Car, Light Truck, Van search.');
		
		}
		
	} else {
		
		document.getElementById('pvSearchSubmit').click();	
		
	}
	
}


function slideFade(el) {
		
	$(el).animate({
									   
		opacity: 'toggle',
		height: 'toggle'			
	
	}, 200, 'swing', function() {});
		
	
}




var com_populatedMake = false;
var com_seriesAdded = false;

function com_populateMake() {
	
	// alert(themeRoot+"grc_search/ajax_hd_dbquery.php");
	var ajaxURL = themeRoot+"grc_search/ajax_hd_dbquery.php"
	
	
	if(!com_populatedMake) {
		
		$.ajax({
			type: "GET",
			url: ajaxURL,
			data: '',
			cache: false,
			success: function(html) {
				$("#com_make").append(html);
				
				
			},
			failure: function() {'XML failure, please contact the system administrator';}
			
		});
	
	}

	

}


function com_populateSeries() {
	
	
	var make = document.getElementById('com_make').value;
	var dataString = 'make='+make;
	
	if(!com_seriesAdded) {
		
		var id = '#com_selects';

		// alert(dataString)
		$.ajax ({
			type: "GET",
			url: themeRoot+"grc_search/ajax_hd_dbquery.php",
			data: dataString,
			cache: false,
			success: function(html, dataString) {
				
				var newHTML = '<select id="com_series" name="com_series" class="marginTop"><option value="0">Please enter the SERIES of your vehicle</option></select>'; 		
				$(id).append(newHTML);	
				$('#com_series').append(html)
				slideFade('#com_series');
			
			},
			failure: function() {'XML failure, please contact the system administrator';}
			
		});
		
		com_seriesAdded = true;
	
	} else {
		
		$.ajax ({
			type: "GET",
			url: themeRoot+"grc_search/ajax_hd_dbquery.php",
			data: dataString,
			cache: false,
			success: function(html, dataString) {

				$('#com_series').empty();
				$('#com_series').append('<option value="0">Please enter the SERIES of your vehicle</option>'+html)
			
			},
			failure: function() {'XML failure, please contact the system administrator';}
			
		});
		
	}
}

function comSearchSubmit() {
	
	var partSearch = document.getElementById('com_part').value;
	var compSearch = document.getElementById('com_compnum').value;
	
	if(partSearch != 'Please enter an OEM or Gabriel Part #' || compSearch != 'Please enter a Competitor Part #') {
		
		if(partSearch.length >= 4 && compSearch == 'Please enter a Competitor Part #') {
			var ok = true;
		} else if (compSearch.length >= 4 && partSearch == 'Please enter an OEM or Gabriel Part #') {
			var ok = true;
		} else {
			var ok = false;	
		}		
		
	} else {
		var ok = true;	
	}
	
	// alert(ok+', partSearch = '+partSearch+', compSearch = '+compSearch);
	
	if(ok) {document.getElementById('comSearchSubmit').click();} else {alert ('Please enter at least 4 characters to preform a Commercial/Heavy-Duty: OEM, Gabriel, or Competitive Part Number Search.');}
	

}

function reset_com_part() {
	
	document.getElementById('com_part').value = 'Please enter an OEM or Gabriel Part #';
	
}

function reset_com_compnum() {
	
	document.getElementById('com_compnum').value = 'Please enter a Competitor Part #';
	
}

function navDD(id) {
	
	window.location = $(id).attr('value');
	
}


// COOKIE FUNCTIONS
function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

// DEV - Post 
function hideDevPost() {

	setCookie('hideDevPost', 'true')
	window.location = '';
}

function checkDevPost() {
	
	var devPost = getCookie('hideDevPost');
	if(devPost) {
		$('#dev').hide();	
	}
}

	
function makeActiveSidebarNavMarkers(lvl1, lvl2) {
	
	if(!lvl2) {
		
		$('.page-item-'+lvl1).css('font-weight', 'bold');
		$('.page-item-'+lvl1+' a').not('ul ul a').css('color', '#444444');
		$('.page-item-'+lvl1+' a:hover').not('ul ul a').css('color', '#FFFFFF');
		$('.page-item-'+lvl1+':hover a').not('ul ul a').css('color', '#FFFFFF');
		$('.page-item-'+lvl1+' a').not('ul ul a').hover(
			function() {$(this).css('color', '#FFFFFF')}, 
			function() {$(this).css('color', '#444444')}
		);
		
	} else {
		
		$('.page-item-'+lvl1).css('font-weight', 'bold');
		$('.page-item-'+lvl1+' a').not('ul ul a').css('color', '#444444');
		$('.page-item-'+lvl1+' ul').css('display', 'block');
		$('.page-item-'+lvl1+' li').css('font-weight', 'normal');
		$('.page-item-'+lvl1+'li a').css('color', '#666666');
		$('.page-item-'+lvl2).css('font-weight', 'bold');
		$('li.page-item-'+lvl2+' a').css('color', '#444444');
	
	}
	
}


