$(document).ready(function()
{
	$('.leadButton').removeAttr('onclick');
	Shadowbox.init();
	$('.leadButton').click(function() {
		$("#video").hide();
	});
	
	var tn = $('#featuredContainer .tab.active').attr('tab');
	$('#'+tn).addClass('active');
	$('#featuredContainer .tab_content:not(.active)').hide();
	bindFeaturedEvents();
	
	bindSearchEvent();
	bindSearchClickEvent();
	bindDropdownClickEvent();
	bindDocClick();
});

// 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();
    $(this).unbind('click');
    SetToSearchModel();
  });
}

function bindSearchEvent() {
  $('#searchContainer .searchField').click(function(){
    if($(this).next().hasClass('dd')) {
      hideDD();
    } else {
      hideDD();
      $(this).next().addClass('dd').show();
    }
  });
}

function bindSearchClickEvent()
{
  $('#searchContainer .submit_button').click(function() {
    var searchYear = validateSearch($('#usedYear').attr('value'));
	var searchMake = validateSearch($('#usedMake').attr('value'));
    var searchModel = validateSearch($('#usedModel').attr('value'));
    var searchBody = validateSearch($('#usedBodytype').attr('value'));
//alert( searchYear + " :: " + searchMake + " :: " + searchModel + " :: " + searchBody);
    window.location='used.php?s2=1&s0=0&search.year_start='+searchYear+'&search.year_end='+searchYear+'&search.make='+searchMake+'&search.model='+searchModel+'&search.body='+searchBody;
  });
}

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)
  {
    var matched = false;
    if($(e.target).hasClass('searchField') == true)
      matched = true;

    if(matched == false)
      hideDD();
  });
}
