// JavaScript Document
$(document).ready(function(){

	
	if ( document.getElementById('state_id') ) { // if the element exists
		if ( document.getElementById('state_id').value != '' ) {
			
			if ( window.location.toString().match('/directory/') ) {
				getCityMenu(true);
			} else {
				getCityMenu(false);
			}
		}
	
	}
// set admin sort default to descending
//if(!$.cookie('order')){	
//$.cookie('order', 'desc');
//}

 $(".reveal:visible").hide();
//if(!$.cookie('sort_type')){	
//$.cookie('sort_type', 'alpha');
//$("#admin_sort_alpha").addClass("sort_arr_down");
//}

/*	
if(!$.cookie('test')){	
	//alert("there is no cookie set");
	$(".catlist>a").html("collapse");
}
	
if($.cookie('test')=='expand'){
	$(".trial:hidden").show();
	//if($('.trial').is(':visible')){
	$('.catlist').children().removeClass("arr_down");
	$('.catlist').children().addClass("arr_up");
	$('.catlist').children().html("collapse");
	//}
	//$(".catlist>a").html("collapse");


}else if($.cookie('test')=='collapse'){
 	$(".trial:visible").hide();
	
	if($('.trial').is(':hidden')){
		$('.catlist').children().removeClass("arr_up");
		$('.catlist').children().addClass("arr_down");
		$('.catlist').children().html("expand");
	}
 //$(".catlist>a").html("expand");
 
}

*/




//////// BEGIN ADMIN EXPAND/COLLAPSE
$(".reveal_btn").click(function(){
	if($(this).parent().find('.reveal').is(':visible')){
		$(this).children().removeClass("arr_up");
		$(this).children().addClass("arr_down");
		$(this).children().html("expand");
		$(this).parent().find('.reveal').slideUp();
		return false;
	};
	if($(this).parent().find('.reveal').is(':hidden')){
		$(this).children().removeClass("arr_down");
		$(this).children().addClass("arr_up");
		$(this).children().html("collapse");
		$(this).parent().find('.reveal').slideDown();
		return false;
	 };
 });
//////// END ADMIN EXPAND/COLLAPSE


	
	
	
 //set inital links to read "collapse"
 //$(".catlist>a").html("collapse");
 // show hide a single item
$(".catlist").click(function(){
	if($(this).parent().find('.trial').is(':visible')){
		$(this).children().removeClass("arr_up");
		$(this).children().addClass("arr_down");
		$(this).children().html("expand");
		$(this).parent().find('.trial').slideUp();
		return false;
	};
	if($(this).parent().find('.trial').is(':hidden')){
		$(this).children().removeClass("arr_down");
		$(this).children().addClass("arr_up");
		$(this).children().html("collapse");
		$(this).parent().find('.trial').slideDown();
		return false;
	 };
 });
 
 
$("#showAll").click(function(){
	$.cookie('test', 'expand', { expires: 7 });									 
	$(".trial:hidden").slideDown();
	$(".catlist>a").html("collapse");
	$(".catlist>a").removeClass("arr_down");
	$(".catlist>a").addClass("arr_up");
 });
  
 // Hide all
 $("#hideAll").click(function(){ 
	$.cookie('test', 'collapse', { expires: 7 });		
	 $(".trial:visible").slideUp();
	 $(".catlist>a").html("expand");
	 $(".catlist>a").html("expand");
	 $(".catlist>a").removeClass("arr_up");
	 $(".catlist>a").addClass("arr_down");
	 return false; 
 });



////////////////////////////////////
//if there is a promotion underneath the search field on the homepage, set this to true. false if there is no promo

var perfPromo= true;

////////////////////////////////////


//close items
$("div.accordion-items").hide();
var is_open = false;

 ///BEGIN accordion toggle function
$("#accordion-menu > #acc_tab").click( function( ev ) {
	ev.preventDefault();
	
	if (perfPromo==true){
		$("#perf_promo").hide();
	};

	if( is_open == true){
		$("div.accordion-items").slideUp("fast");
		$("div.accordion-items").addClass("closed");
		is_open = false;
		//$(this).find('a').html('widen your search >');
		
		if($(this).find('a').html()=='widen your search &gt;'){
			$(this).find('a').html('narrow your search >');
		}else{
			$(this).find('a').html('widen your search >');
		}
		
		
	}else{
		$("div.accordion-items").slideDown("fast");
		$("div.accordion-items").removeClass("closed");
		is_open = true;
		
		
		if($(this).find('a').html()=='narrow your search &gt;'){
			$(this).find('a').html('widen your search >');
		}else{
			$(this).find('a').html('narrow your search >');
		}
		
	}
}); ///end accordion toggle function


$('.search_go input').focus(function(){
    if ( $(this).val() == "Enter a query" )
    {
	$(this).val('');
	//console.log('Empty');
    }
});

$('form[name="search_form"]').submit(function(e){
    var search_input = $('.search_go input[type="text"]');
    var search_state = $('#state_id');
    var search_cities = $('select[name="cities"]');
    var search_category = $('#category_id');
    var search_audience = $('#user_audience_id');
    if ( ( search_input.val() == '' || search_input.val() == 'Enter a query' ) && ( search_state.val() == '' && search_cities.val() == null && search_category.val() == '' && search_audience.val() == '' ) ) {
    /*
    console.log( search_state.val() );
    console.log( search_cities.val() );
    console.log( search_category.val() );
    console.log( search_audience.val() ); */
    
	e.preventDefault();
	search_input.attr('value', 'Enter a query');
    } else {
    if ( search_input.val() == 'Enter a query' ) { search_input.attr('value',''); }
	$('form[name="search_form"]').submit();
    }
});

}); //end doc ready
