// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//var key_pressed;
var	floatDiv = $('#float');

//document.onkeydown = keypress;

//window.onfocus = function() {search.disabled = false;}

window.onload = function() {
	var pForm = document.forms[0];
  $('#statesinput').focus();
  var key_pressed = false;
//  pForm.disabled = true;
}

// function keypress(e){
// try {var key_s = (window.event) ? e.keyCode : e.keyCode; } catch(e) { key_s = event.keyCode }
// key_pressed = true;
// if(key_s == 13){
// 	submit_pawst();
// 	return false
// 	}
// }	

function set_search(element) {
  var search = document.getElementById('statesinput');
  window.key_pressed ? null : search.value = element.innerHTML.replace(/\+/g,' ');
}

function submit_search() {
	var default_string = "what !?";
  var search = document.getElementById('statesinput');
  var search_type = $('#search_type').val();
  var type_parameter = "";
	var pForm = document.forms[0];

	if ((search_type == 'image') | (search_type == 'video') | (search_type == 'blog')) {
		type_parameter = "&type="+search_type;
	 }

	if (search.value != "" && search.value != default_string) {
	 	  pForm.action = "/search?q="+search.value+type_parameter;
			pForm.submit();
		} else {
     search.value = default_string;
	   search.focus();
	   return false;
	}
}


function submit_pawst() {
 var default_string = "shuffle what ?!";
 var search = document.getElementById('statesinput');
 var search_type = $('#search_type').val();
 var pForm = document.forms[0];

 pForm.action = '/'+search.value;
// pForm.disabled = true;

 var type_parameter = ""

 if ((search_type == 'image') | (search_type == 'video') | (search_type == 'blog')) {
	type_parameter = search_type+"s/";
 }
	
  if (search.value != "" && search.value != default_string) {
	   location.href='/'+type_parameter+document.getElementById('statesinput').value.replace(/\s/g, '+')+( document.all ? "?ie="+Math.round(Math.random() * 10000000) : '' );
	   return false
	} else {
   search.value = default_string;
   search.focus();
   return false;
//   search.disabled = false;
	}

}

function stop(e) {
 if (!e) e = window.event;
 (e.stopPropagation) ? e.stopPropagation() : e.cancelBubble = true;
 (e.preventDefault) ? e.preventDefault() : e.returnValue = false;
 return false;
}

function adult_filter(status) {
 $.cookie('adultfilter', status, { expires: 700, path: '/' });
 alert("Adult filter is now " + (status == 1 ? "off" : "on") + ".");
 location.href = location.href;
}

function post_url_input() {
	var post_url = $('#post_url');
	post_url.toggle();
	post_url.select();
}

function toggle() {
	if (document.getElementById('toolbar').style.display == 'none') {
	  document.getElementById('toolbar').style.display = 'block';
	} else {
	   var bar = document.getElementById('toolbar');
	   bar.style.display = 'none';		
	}
}

function handleKeyPress(e,element){
var key=e.keyCode || e.which;
if (key==13){
  location.href='/'+document.getElementById('statesinput').value;
   }
 }

function update_list() {
	  var current_id = $(".updates div:first-child").attr('id').toString();
		$(".updates").prepend("<li class='add_this' style='display:none'></li>");
	  $(".updates .add_this").load("/ajax/updates?ie="+Math.round(Math.random() * 10000000)+"&id="+current_id, function(x,y,z) { 
		 if (z.status == 304) 
		  { 
			  // console.log("same: " + z.status);
				$(".updates li:first-child").remove();
			 } 
		 else {
			// console.log('changing: ' + z.status);
			$('.updates .add_this').show('slow');
			$('.updates .add_this').removeClass('add_this');
			$(".updates li:last-child").hide('slow', function() { 
			  $(".updates li:last-child").remove();
			 });
		 }
      });
}

// Add Search Plugin
function addToolBar() {

 try{
  window.external.AddSearchProvider("http://"+document.domain+"/opensearch.xml?e=1");
 }

 catch(eX){
  if (70 == (eX.number & 0xFFFF))alert("For security reasons, you must use the mouse\n(or the Enter key) to click the Install button.");
  else alert("Unable to add search provider. The Create Your\nOwn tool requires Internet Explorer.   [" + (eX.number & 0xFFFF) + "]");
 }

}


function WallaCheckBrowser()
{
	var navFamily = navigator.userAgent.toLowerCase();
	if (navFamily.indexOf('msie')!=-1) {
		var ver = parseFloat( navFamily.substring(navFamily.indexOf('msie')+5,

			navFamily.length) );
		scriptStr='SCRIPT';
		navFamily = 'IE7';
	} else if (navFamily.indexOf('gecko')!=-1) {
		var ver = parseFloat( navFamily.substring(navFamily.indexOf('gecko')+6,

			navFamily.length) );
		scriptStr='script';
		navFamily = 'Gecko';
	} else {
		scriptStr='script';navFamily = 'Gecko';
	}
	
	return navFamily;
}
var WallaBrowser = WallaCheckBrowser();

function OnChoose(choice) {
    /* code for Mozilla, etc.*/
    if (WallaBrowser=='Gecko')
    {
        //writeSearchCookie("mozsearch", "1", "Mon, 04 Oct 2021 12:00:00 GMT");
        if (choice)
        {
            if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function"))
            {
                window.sidebar.addSearchEngine (
                    "http://pawst.com/javascripts/mozilla.src",/* engine URL */
                    /* warrning!!: must be the same path with the same name */
                    "http://pawst.com/images/favicon.png",/* icon URL */
                    "pawst",/* engine name */
                    "Web Tool" );/* category name */
            }
            else
            {
                alert("Mozilla M15 or later is required to add a search engine.");
            }
        }
    }
    /* code for IE */
    else if (WallaBrowser=='IE7')
    {
        //writeSearchCookie("ie7search", "1", "Mon, 04 Oct 2021 12:00:00 GMT");
        if (choice)window.external.AddSearchProvider('http://pawst.com/javascripts/ie7.xml');
    }
return false;
}

// Add Search Plugin