// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function fullscreen_popup(url) {
 params  = 'width='+screen.width;
 params += ', height='+screen.height;
 params += ', top=0, left=0'
 params += ', fullscreen=yes';

 newwin=window.open(url,'fw', params);
 if (window.focus) {
		newwin.focus()
	}
 return false;
}

var show_indicator = true;
function in_place_edit_default_text(className, parentElement, defaultText){
  // alert(className);
    // alert(parentElement);
  var fields = document.getElementsByClassName(className, parentElement);
  // alert("hello: " + parentElement.hasChildNodes);
  for(var i = 0; i < fields.length; i++){
    // alert(fields[i].childNodes.firstChild);
    if(!fields[i].hasChildNodes){
      var node = document.createTextNode(defaultText);
      fields[i].appendChild(node);
    }
  }
}

function switch_view(v){
	Element.hide('text_view');
//	Element.hide('photocast_view');
	Element.hide('podcast_view');
	Element.show(v);
	Element.hide('text_view_text');
//	Element.hide('photocast_view_text');
	Element.hide('podcast_view_text');
	Element.show(v + "_text");
	Element.show("text_view_link");
//	Element.show("photocast_view_link");
	Element.show("podcast_view_link");
	Element.hide(v + "_link")
	return false;
}

function remote_search(uri, term){
    if($(term).value.length == 0){
      $(term).value = '請輸入查詢字串';
      $(term).focus();
      return;
    }
    uri = uri + $(term).value;
    if(show_indicator){
    	new Ajax.Request(uri, {
    	                        asynchronous:true, evalScripts:true, 
    	                        onComplete:function(request){Element.toggle('indicator');}, 
    	                        onLoading:function(request){Element.toggle('indicator');}
    	                       }); 
  }else{
	  new Ajax.Request(uri, 
	  	{asynchronous:true, evalScripts:true}); 
  }
}

function ajax_select(action, url, id, warning, confirmation){
	Element.hide(warning);
	if (action != 'new' && id==0){
		Effect.Appear(warning, {duration:0.5});
		return false;
	}
	if (action == 'destroy' && !confirm(confirmation)){
		return false;
	}
	return simple_ajax(url, id);
}

function simple_ajax_extra(url, id, extra){
  if(extra == null){
    uri = url + '/' + id;
  }else{
    uri = url + '/' + id + extra;
  }
  if(show_indicator){
    	new Ajax.Request(uri, {
    	                        asynchronous:true, evalScripts:true, 
    	                        onComplete:function(request){Element.toggle('indicator');}, 
    	                        onLoading:function(request){Element.toggle('indicator');}
    	                       }); 
  }else{
	  new Ajax.Request(uri, 
	  	{asynchronous:true, evalScripts:true}); 
  }
  // alert("hello " + uri);
	return false;
}

function simple_ajax(url, id, page){
  if(page == null){
    uri = url + '/' + id;
  }else{
    uri = url + '/' + id + '?page=' + page;
  }
  if(show_indicator){
    	new Ajax.Request(uri, {
    	                        asynchronous:true, evalScripts:true, 
    	                        onComplete:function(request){Element.toggle('indicator');}, 
    	                        onLoading:function(request){Element.toggle('indicator');}
    	                       }); 
  }else{
	  new Ajax.Request(uri, 
	  	{asynchronous:true, evalScripts:true}); 
  }
  // alert("hello " + uri);
	return false;
}

function init_data_table(table){
	if(!document.getElementById || !document.getElementsByTagName){
		// alert("getElementById error or getElementsByTagName error");
		return false;
	}
	var table = document.getElementById(table);

	if(!table){
		// alert("no table error");
		return false;
	}
	var rows = table.getElementsByTagName('tr');
	stripeRows(rows);
			
	if(isTBodyHover(table)){
		hiliteRows(rows);
	}
}

function stripe_table_rows(table, start){
	var t = $(table);
	var rows = t.getElementsByTagName('tr');
	stripeRows(rows, start);
}
function isTBodyHover(table){
	var tbody = table.getElementsByTagName('tbody');
	return tbody[0].className == 'hover'
}
function clickRow(r, table){
	init_data_table(table);
	r.className = "row_hilite";
	r.onmouseover = r.onmouseout = null;
}
function hiliteRow(id, table){
	var row = document.getElementById(id);
	clickRow(row, table);
}

function stripeRows(rows, start){
	for(var i = start; i < rows.length; i++){
		var odd_or_even = i % 2;
		rows[i].className = "row_" + odd_or_even;
	}
}

function hiliteRows(rows){
	for(i = 1; i < rows.length; i++){
		var odd_or_even = i % 2;
		rows[i].onmouseover = function(){
			this.className = 'row_hover';
		}
		if(odd_or_even == 0){
			rows[i].onmouseout = function(){
				this.className = "row_0";
			}
		}else{
			rows[i].onmouseout = function(){
				this.className = "row_1";
			}
		}
	}
}


function selectPhoto(el, prefix, id){
  var arr = $(el).getElementsByTagName('img');
  for(var i=0; i < arr.length; i++){
    arr[i].style.backgroundColor = 'white';
  }
  $(el + "_" + prefix + id).style.backgroundColor = 'red';
  return false;
}

function updateFields(el, desc, file, img){
   var arr = $(el).getElementsByTagName('img');
   for(var i=0; i < arr.length; i++){
     if(arr[i].style.backgroundColor == 'red'){
       $(file).value = arr[i].alt;
       if(arr[i].alt.length > 0){
         $(desc).value = arr[i].title;
       }
       $(img).src = arr[i].name;
       Element.show(img);
       $(desc).focus();
       break;
     }
   }
   Element.hide(el);
   return false;
}
function reset_links(el){
  var aNodes = getNodes(el, 'a');
  aNodes.each(function(node){
				alert(node.nodeName + ': ' + node.innerHTML);
			});
}

function set_current_link(el, n){
  var aList = $(el).getElementsByTagName('a');
  for(var i=0; i<aList.length; i++){
    if(i == n){
      aList[i].className = 'current';
    }else{
      aList[i].className = '';
    }
  }
}
