$(document).ready(function()
{
	$('.leadButton').removeAttr('onclick');
	Shadowbox.init();
	/*$('.leadButton').click(function() {
		$("#video").hide();
	});*/
	
	// Manage the fake BAP select drop down on the home page.
	$('#bap_model').change(function() {
		$("#selectbap_model").html($(this).val());
	});
	
	// Bind the events for the search feature.
	bindSearchEvent();
	bindSearchClickEvent();
	bindDropdownClickEvent();
	bindDocClick();
});

$(document).ready(function()
{
	// Initiates the slider on the home page for used vehicles
	var featuredUsed = new Shiftr(
	{
		bindTo: '#featuredUsedVehiclesInner',
		animation: 'slide_horizontal',
		jumpAmount: 1
	}); 
	// Initiates the slider on the home page for new vehicles
	var featuredNew = new Shiftr(
	{
		bindTo: '#featuredNewVehiclesInner',
		animation: 'slide_horizontal',
		jumpAmount: 3
	}); 
});


// Application Buttons //
var ISOCODE;
//Service & Body Shop
var imgOnAppointment = new Image(253,48);
imgOnAppointment.src = '/images/image.php?file=buttons/ENG/appointment2.gif';
var imgOffAppointment = new Image(253,48);
imgOffAppointment.src = '/images/image.php?file=buttons/ENG/appointment1.gif';

// MORE INFO BUTTON
var imgOnInfo = new Image(253,48);
imgOnInfo.src = '/images/image.php?file=buttons/ENG/more_info2.gif';
var imgOffInfo = new Image(253,48);
imgOffInfo.src = '/images/image.php?file=buttons/ENG/more_info1.gif';

//Parts
var imgOnPartsRequest = new Image(253,48);
imgOnPartsRequest.src = '/images/image.php?file=buttons/ENG/parts_request2.gif';
var imgOffPartsRequest = new Image(253,48);
imgOffPartsRequest.src = '/images/image.php?file=buttons/ENG/parts_request1.gif';

//Finance
var imgOnCreditApp = new Image(253,48);
imgOnCreditApp.src = '/images/image.php?file=buttons/ENG/credit_app2.gif';
var imgOffCreditApp = new Image(253,48);
imgOffCreditApp.src = '/images/image.php?file=buttons/ENG/credit_app1.gif';

function showStatus(sMsg) {//used often
	//setTimeout("window.status = \'" + sMsg + "\';", 0);
    window.status = sMsg ;
    return true ;
}
function openWindow(url,width,height){
	newWin = window.open(url, 'viewWin', 'toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,left='+(screen.width-width)/2+',top='+(screen.height-height)/2+',width=' + width + ',height=' + height + '');
}		

function changeImg(imgName,imgPre) {
	if(document.images){
		document.images[imgName].src = eval(imgPre + '.src');
	}
}

function changeStyle(obj, new_style) {
    obj.className = new_style;
}

/*
Written by Kyle Somogyi
*/
var qs=false;

function bindDropdownClickEvent() {
  $('#searchContainer .searchDropdown li').click(function() {
    $(this).parent().parent().prev().html($(this).html());
    $(this).parent().parent().prev().attr('value', $(this).attr('value'));
    var dropdown = $(this).parent().parent().attr('id');
    if(dropdown == "makeDropdown") {
	  $('#modelDropdown  li:first').click();
      $('#bodytypeDropdown  li:first').click();
    }
    hideDD();
	$('#usedModel').show();
    $(this).unbind('click');
    SetToSearchModel();
	SetToSearchNewModel();
  });
}

function bindSearchEvent() {
	$('#searchContainer .searchField').click(function(){
		if($(this).next().hasClass('dd')) {
			hideDD();
			if($(this).attr("id")=="usedMake") {
				$('#usedModel').show();
			}
		} else {
			hideDD();
			$(this).next().addClass('dd').show();
			if($(this).attr("id")=="usedMake") {
				$('#usedModel').hide();
			}
		}
	});
}

function bindSearchClickEvent()
{
  $('#searchContainer .submit_button').click(function() {
	var searchMake = validateSearch($('#usedMake').attr('value'));
    var searchModel = validateSearch($('#usedModel').attr('value'));
    var searchPrice = validateSearch($('#usedPrice').attr('value'));
    window.location='used.php?s2=1&s0=0&search.make='+searchMake+'&search.model='+searchModel+'&search.price_end='+searchPrice;
  });
  $('#searchContainer .submit_button_new').click(function() {
	var searchMake = validateSearch($('#newMake').attr('value'));
    var searchModel = validateSearch($('#newModel').attr('value'));
    var searchPrice = validateSearch($('#newPrice').attr('value'));
    window.location='new-inventory.php?s2=1&s0=0&search.make='+searchMake+'&search.model='+searchModel+'&search.price_end='+searchPrice;
  });
}



function validateSearch(val)
{
  if(val <= 0)
    return '';
  else
    return val;
}

function hideDD() {
  $('#searchContainer .dd').hide().removeClass('dd');
}

function bindFeaturedEvents()
{
	$('#featuredHeader .tab').click(function()
	{
		if($(this).hasClass('active') == false)
		{
			$('#featuredContainer .tab.active').removeClass('active').addClass('deactive');
			$(this).addClass('active').removeClass('deactive');
			var tt = $(this).attr('tab');
			$('#featuredBody .active').css('z-index', 20);
			$('#featuredBody #'+tt).css('z-index', 18).fadeIn('fast');
			$('#featuredBody .active').fadeOut('fast').removeClass('active');
			$('#'+tt).addClass('active');

			$('#featuredHeader .tab').unbind('click');
			bindFeaturedEvents();
		}
	});
}

function addBookmark(url, title)
{
	if(document.all)
		window.external.AddFavorite(url, title);
	else if(window.sidebar)
		window.sidebar.addPanel(title, url,'');
	else
		alert('To add a bookmark to this page, please press Ctrl+D.')
}

//Document click event to hide open search dropdowns
function bindDocClick()
{
  $(document).click(function(e)
  {
    $('#usedModel').show();
	var matched = false;
    if($(e.target).hasClass('searchField') == true)
      matched = true;

    if(matched == false)
      hideDD();
  });
}

function toggleSearchField(_elemID_tbh, _elemID_tbs)
{
	document.getElementById(_elemID_tbh).style.display = "none";
	document.getElementById(_elemID_tbs).style.display = "block";
}

