/**
 * Init vars
 */
var categoryId = null;
var subCategoryId = null;


jQuery(document).ready ( function() {

	/**
	 * Highlight first step on load
	 */
	jQuery('#steps li#step1').addClass('highlight');
	
	
	/**
	 * Set autocompleter searchfield
	 */
/*	jQuery("#zip_code").autocomplete ( 
		"wp-content/plugins/companies/zip_autocomplete.php", {
			delay: 10,
			minChars: 1,
			matchSubset: 1,
			matchContains: 1,
			cacheLength: 10,
			autoFill: true
		}
	);
*/	
	jQuery("#search_keywords").autocomplete (
		"/wp-content/plugins/companies/search_autocomplete.php", {
			delay: 20, //10
			minChars: 3,
			matchSubset: 0,
			matchContains: 1,
			cacheLength: 1, //10
			autoFill: false,
			formatItem:formatItem,
			onItemSelect:selectItem
		}
	);
	
	function formatItem(row) {
		return row[0] + "<br><i id='category_id' style='display:none'>" + row[1] + "</i><i id='category_parent' style='display:none'>" + row[2] + "</i>";
	}
	
	function selectItem(li) {
		if (li.extra) {
			//alert("That's '" + li.extra[0] + "' you picked.")
			jQuery('#c_id').val(li.extra[0]);
			jQuery('#c_parent').val(li.extra[1]);
		}
	}

	
/*	jQuery("#search_zip").autocomplete ( 
		"wp-content/plugins/companies/zip_autocomplete.php", {
			delay: 10,
			minChars: 1,
			matchSubset: 1,
			matchContains: 1,
			cacheLength: 10,
			autoFill: true
		}
	);
*/	
	jQuery("#search_start").click (function() { 	
		return false;
	});
	
	jQuery("#form_search").children('input[type="submit"]').click(function() { 
	
		if ((jQuery("#search_zip").val() == 'Zip Code') || (jQuery("#search_zip").val() == '') || (jQuery('#search_keywords').val() == 'Search') || (jQuery('#search_keywords').val() == ''))
		{
			alert('Please enter Keyword(s) and Zip Code');
			return false;
		}
			
		jQuery("#form_search").attr("action",'http://teamdavelogan.com/?page_id=307&keywordname='+jQuery('#search_keywords').val()+'&keyword='+jQuery('#search_keywords').val()+'&zip='+jQuery('#search_zip').val());
		jQuery("#form_search").submit();
		//return false;
	});
	
	jQuery("#search_keywords").click (function() { 
		if (jQuery(this).val() == 'Search')
			jQuery(this).val('');
	});

	jQuery("#search_zip").click (function() { 
		if (jQuery(this).val() == 'Zip Code')
			jQuery(this).val('');
	});

	
});


/**
 * Show main categories
 */
function showCategories()
{
    // Slide view to main categories
    jQuery(".widearea").animate({ marginLeft: "0px" }, 300);
    jQuery('#mediaspace1').show();
    jQuery('#mediaspace2').hide();
    jQuery('#mediaspace3').hide();
}


/**
 * Show sub categories
 * 
 * @param newCategoryId
 */
function showSubCategories ( newCategoryId )
{
	categoryId = newCategoryId;
	subCategoryId = null;
	
	// Load sub categories by AJAX
	var url = '/wp-content/plugins/companies/show-subcategories.php?category_id=' + categoryId;
	jQuery.get(url, function(data) {
	    // Load data
	    jQuery('.widearea #div2_left')[0].innerHTML = data;
	    // Slide view to sub categories
	    jQuery(".widearea").animate({ marginLeft: "-960px" }, 300);
	});
	
	// Highlight step 2
	jQuery('#steps li').removeClass('highlight');
	jQuery('#steps li#step2').addClass('highlight');
	jQuery('#sub_passed').val('1');
	
	player1.sendEvent('STOP')
	jQuery('#mediaspace1').hide();
	jQuery('#mediaspace2').show();
	jQuery('#mediaspace3').hide();
}


/**
 * Show zip code form
 * 
 * @param categoryId
 */
function showZipForm ( newSubCategoryId )
{
	subCategoryId = newSubCategoryId;
	jQuery('input#zip_code')[0].value = '';
    jQuery(".widearea").animate({ marginLeft: "-1920px" }, 300);

	// Highlight step 3
	jQuery('#steps li').removeClass('highlight');
	jQuery('#steps li#step3').addClass('highlight');
	jQuery('#zip_passed').val('1');
	
	player2.sendEvent('STOP')
	jQuery('#mediaspace1').hide();
	jQuery('#mediaspace2').hide();
	jQuery('#mediaspace3').show();
}


/**
 * Loading companies from server
 * 
 * @param subCategoryId
 * @param zip
 */
function loadCompanies ( search )
{
	var zip = jQuery('input#zip_code')[0].value;
	
	if ( zip )
	{
		if (subCategoryId == 0)
		{
			var url = '/?page_id=307&sub=' + categoryId + '&zip=' + zip + '&parent=1';
			window.location = url;
		}
		else
		{
			var url = '/?page_id=307&sub=' + subCategoryId + '&zip=' + zip;
			window.location = url;
		}
	}
	else
	{
		alert ( 'Please enter zip code' );
	}
	
	return false;
}


/**
 * Switch tabs
 * 
 * @param tab
 */
function switchTab ( tab, content )
{
	jQuery('#company_info div.tab').hide();
	jQuery('#company_info ul.tabs li').removeClass('selected');
	jQuery('#company_info div#tab_' + content).show();
	jQuery(tab).addClass('selected');
}
