window.addEvent('domready', function() {
	if($('header')) {
		search_tab();
		drop_down();
		admin_drop_down();
		get_cities_ajax();
		//input_value_replace();
		reset_select();
	}
});


function find_map_elements() {

	var map_buttons = $$('.toggle_map');
	$each(map_buttons, function(e) {
		map_id = e.get('rel');	
		
		var maps = $$('.google_map');
		$each(maps, function(i) {
			google_id = i.get('rel');
               
			if(map_id == google_id) {
				e.addEvent('click', function(ev) {
				address = this.get('title');
					ev.preventDefault();
                         mapBuilder(address,google_id);					
					var cur = i.getStyle('display');
					if(cur == 'none') {
						i.setStyle('display', 'block');
					} else {
						i.setStyle('display', 'none');
					}
				});
			}
		});
	});

}
function get_cities_ajax(){
  $('state_box').addEvent('change', searchfilterchange );
  $('category').addEvent('change', searchfilterchange );
}

function searchfilterchange(e){
    my_value = $('state_box').value;	
	my_category = $('category').value;

var dmn = $('base_url').value;
if(!dmn) dmn = 'http://www.completebeautylist.com/';

//	var dmn = 'http://www.completebeautylist.com/';
    while(my_value.indexOf(" ") > -1){
      my_value = my_value.replace(" ", "_");
    }
ajaxurl = dmn+'get_cities/state/'+my_value+'/'+my_category;
    var req = new Request.HTML({
				url: ajaxurl,
	
		onRequest: function(txt){
			txt = "<option value=\"\">Loading...</option>";
			$('city_box').set('html', txt);
		 },
		
		onSuccess: function(txt){
			
			if(txt.length == 0){
				txt = "<option value=\"all_cities\">then a City &nbsp;</option>";
				$('city_box').set('html', txt);
			}else{
				$('city_box').set('html', '');
				$('city_box').adopt(txt);
				
			}
		},
	
		onFailure: function(){
			txt = "<option value=\"\">Request Failed</option>";
			$('city_box').set('html', txt);
		}

	});

  	req.send();
	}


function search_tab() {
	
	// Setup the search tab
	
	var search_box = $('search_box');
	var search_tab = $('search_toggle');
	var search_area = $('search');
	
	var toggle_search = new Fx.Slide(search_box);
	
	var readCookie = Cookie.read('search_position');
	
	if(readCookie == 'closed') {
		toggle_search.hide();
		flag = 'set';
		search_tab.innerHTML = "Show Search";
	} else {
		flag = '';
		search_tab.innerHTML = "Hide Search";
	}
	
	search_tab.addEvent('click', function(i) {
		i.stop();
		var dmn = $('base_url').value.replace(/^http(s)?:\/\//, '');
		dmn = dmn.replace(/(\/)?$/, '');
		if(!dmn) dmn = 'completebeautylist.com';
		if(flag == '') {
			toggle_search.slideOut();
			var searchCookie = Cookie.write('search_position', 'closed', {domain:dmn});
			flag = 'set';
			this.innerHTML = "Show Search";
		} else {
			toggle_search.slideIn();
			var searchCookie = Cookie.write('search_position', 'open', {domain: dmn});
			flag = '';
			this.innerHTML = "Hide Search";
		}
	});
	
}

function drop_down() {
	
	// Create Drop Down Menu
	
	var drop_down = $$('.drop_down').each(function(e) {
		var the_anchor = e.getParent();
		var sub_menu = the_anchor.getNext();
		hide_sub_menu = '';
		curr_bg_color = the_anchor.getStyle('background-color');
		
		the_anchor.addEvent('mouseenter', function(i){
			i.preventDefault();
			$clear(hide_sub_menu);
			drop_down_enter(the_anchor);
		});
		
		the_anchor.addEvent('mouseleave', function(i) {
			i.preventDefault();
			hide_sub_menu = drop_down_leave.delay(400, the_anchor);
		});
		
		sub_menu.addEvent('mouseenter', function(i){
			i.preventDefault();
			$clear(hide_sub_menu);
			drop_down_enter(sub_menu);
		});
		
		sub_menu.addEvent('mouseleave', function(i) {
			i.preventDefault();
			hide_sub_menu = drop_down_leave.delay(400, the_anchor);
		});
		
		if($('admin_navigation')) {
			var close_drop = $('admin_navigation').getElement('.admin_drop');
			close_drop.getParent().addEvent('mouseenter', function() {
				sub_menu.setStyle('display', 'none');
			});
		}
	});
}

function drop_down_enter(x) {
	if (x.get('tag') == 'a') {
		var e = x.getNext();
	} else {
		var e = x;
	}
	e.setStyle('display', 'block');
	x.setStyles({'background-color': '#788788', 'color': '#fff'});
}

function drop_down_leave() {
	if (this.get('tag') == 'a') {
		var e = this.getNext();
		this.setStyles({'background-color': curr_bg_color, 'color': '#000'});
	} else {
		var e = this;
	}
	e.setStyle('display', 'none');
}

function admin_drop_down() {
	var admin_drop = $$('.admin_drop');
	var sub_menu = $('user_account_links');
	hide_admin_sub = '';
	
	admin_drop.each(function(e) {
		hit = e.getParent();
		hit.addEvent('mouseenter', function(i) {
			i.preventDefault();
			$clear(hide_admin_sub);
			admin_sub_show(hit, sub_menu);
		});
		hit.addEvent('mouseleave', function(i) {
			i.preventDefault();
			hide_admin_sub = admin_sub_hide.pass([hit, sub_menu]).delay(300);
		});
		
		// Sub Menu
		sub_menu.addEvent('mouseenter', function(i) {
			i.preventDefault();
			$clear(hide_admin_sub);
			admin_sub_show(hit, sub_menu);
		});
		sub_menu.addEvent('mouseleave', function(i) {
			i.preventDefault();
			hide_admin_sub = admin_sub_hide.pass([hit, sub_menu]).delay(300); //(hit, sub_menu)
		});
		
	});
}

function admin_sub_hide(hit, sub_menu) {
	hit.setStyle('background-color', '#88b5c0');
	sub_menu.setStyle('display', 'none');
}

function admin_sub_show(hit, sub_menu) {
	hit.setStyle('background-color', '#304246');
	sub_menu.setStyle('display', 'block');
}

/*function input_value_replace() {
	var inputs = $$('form input');
	inputs.each(function(v) {
		var values = v.getProperty('value');
		if(values != "") {
			v.addEvent('focus', function() {
				v.setProperty('value', '');
			});
			v.addEvent('blur', function() {
				if(v.value == values || v.value == '') {
					v.setProperty('value', values);
				} else {
					v.setProperty('value', v.value);
				}
			});
		}
	});		
}
*/
function reset_select() {
	var selects = $$('form select');
	selects.each(function(e) {
	    my_id = e.get('id');
	    if(my_id == "category" || my_id == "state_box" || my_id == "city_box"){
		e.selectedIndex = 0;
		}
	});
}

function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}


function searchType(field)
{
        if(field == 'zip')
        {
                $('search-zip').style.display = '';
                $('search-city').style.display='none';
        }
        else
        {
                $('search-zip').style.display = 'none';
                $('search-city').style.display='';
        }
}

function validateZipSearch()
{
	var zip = $('zip_text').value;
	if(isZip(zip) || isPostal(zip))
		return true;
	return false;
}

function isZip(str)
{
	return str.test(/^\d{5}$/);
}
function isPostal(str)
{
	return str.test(/^[a-zA-Z][\d][a-zA-Z][\s]*[\d][a-zA-Z][\d]$/);
}

function checkSearch(searchform)
{
	if(!$('category').options.selectedIndex) {alert('You must select a category'); return false;}
	if(!validateZipSearch() && !$('state_box').options.selectedIndex) {alert('You must select a province or enter a zip/postal code'); return false;}
	if(validateZipSearch())
	searchform.action = searchform.action.replace(/query_city/, 'query_zip');

	return true;
}
