// JavaScript Document

var baseURL = 'http://www.readme.cc/';

togglePreview = function(teaserid) {	
	Effect.toggle('preview_'+teaserid, 'appear', {duration:0.2});
}


function addLinkSet(fieldname, prefix, defaultLabel, defaultURL) {
  var ni = $(fieldname);
  var numi = $(''+ prefix +'_'+fieldname+'_value');
  var num = ($(''+ prefix +'_'+fieldname+'_value').value -1)+ 2;
  
  if(num<10) {
	  numi.value = num; 
	  var newdiv = document.createElement('div');
	  var divIdName = fieldname+'_'+num;
	  newdiv.setAttribute('id',divIdName);
	  newdiv.setAttribute('style',"display: none;"); 
	  newdiv.innerHTML = '<p class="rpad"><input class="text maxw" name="'+prefix+'['+fieldname+']['+num+'][label]" id="'+prefix+'['+fieldname+']['+num+'][label]" type="text" value="'+defaultLabel+'" /><br /><input class="text maxw" name="'+prefix+'['+fieldname+']['+num+'][url]" id="'+prefix+'['+fieldname+']['+num+'][url]" type="text" value="'+defaultURL+'"  /><a class="icon delete topright" href="javascript:removeLinkSet(\''+ fieldname +'\', \'\', \''+divIdName+'\')" title="Delete entry">Delete</a></p>';
	  ni.appendChild(newdiv);
	  Effect.toggle(divIdName, 'appear', {duration:0.8});
  } else
  	alert("Maximum reached");
}


function addEmbedSet(fieldname, prefix, defaultLabel, defaultURL) {
  var ni = $(fieldname);
  var numi = $(''+ prefix +'_'+fieldname+'_value');
  var num = ($(''+ prefix +'_'+fieldname+'_value').value -1)+ 2;
  
  if(num<10) {
	  numi.value = num; 
	  var newdiv = document.createElement('div');
	  var divIdName = fieldname+'_'+num;
	  newdiv.setAttribute('id',divIdName);
	  newdiv.setAttribute('style',"display: none;"); 
	  newdiv.innerHTML = '<p class="rpad"><input class="text maxw" name="'+prefix+'['+fieldname+']['+num+'][label]" id="'+prefix+'['+fieldname+']['+num+'][label]" type="text" value="'+defaultLabel+'" /><br /><input class="text maxw" name="'+prefix+'['+fieldname+']['+num+'][embed]" id="'+prefix+'['+fieldname+']['+num+'][embed]" type="text" value="'+defaultURL+'"  /><a class="icon delete topright" href="javascript:removeLinkSet(\''+ fieldname +'\', \'\', \''+divIdName+'\')" title="Delete entry">Delete</a></p>';
	  ni.appendChild(newdiv);
	  Effect.toggle(divIdName, 'appear', {duration:0.8});
  } else
  	alert("Maximum reached");
}




function removeLinkSet(fieldname,prefix,divIdName) {
  var d = $(fieldname);
  var olddiv = $(divIdName);
  d.removeChild(olddiv);
  
/*   var numi = $(''+ prefix +'_'+fieldname+'_value');
  $(''+ prefix +'_'+fieldname+'_value').value = numi-1; */
}


function activeTagMode(mode) {
	switch(mode)
	{
	case "content":
	  $('keywordbox_content').setAttribute('style', 'display: block;');
	  $('keywordbox_style').setAttribute('style', 'display: none;');
	  $('keywordbox_recommendation').setAttribute('style', 'display: none;');
	  $('tab_content').className="active"
	  $('tab_style').className=""
	  $('tab_recommendation').className=""
	  $('keywords_content').setAttribute('style', 'display: block;');
	  $('keywords_style').setAttribute('style', 'display: none;');
	  $('keywords_recommendation').setAttribute('style', 'display: none;');
	  keywordMode = mode;
	break    
	case "style":
	  $('keywordbox_content').setAttribute('style', 'display: none;');
	  $('keywordbox_style').setAttribute('style', 'display: block;');
	  $('keywordbox_recommendation').setAttribute('style', 'display: none;');
	  $('tab_content').className=""
	  $('tab_style').className="active"
	  $('tab_recommendation').className=""
	  $('keywords_content').setAttribute('style', 'display: none;');
	  $('keywords_style').setAttribute('style', 'display: block;');
	  $('keywords_recommendation').setAttribute('style', 'display: none;');
	  keywordMode = mode;
	break
	case "recommendation":
	  $('keywordbox_content').setAttribute('style', 'display: none;');
	  $('keywordbox_style').setAttribute('style', 'display: none;');
	  $('keywordbox_recommendation').setAttribute('style', 'display: block;');
	  $('tab_content').className=""
	  $('tab_style').className=""
	  $('tab_recommendation').className="active"
	  $('keywords_content').setAttribute('style', 'display: none;');
	  $('keywords_style').setAttribute('style', 'display: none;');
	  $('keywords_recommendation').setAttribute('style', 'display: block;');
	  keywordMode = mode;
	break
	}
}

function pickKeyword(keyword, realm) {
	var currentValue = $('keywords_'+keywordMode).value;
	if(currentValue == '')
		$('keywords_'+keywordMode).value += keyword;
	else
		$('keywords_'+keywordMode).value += ", "+keyword;
}

// initialize keyword mode - default: content
var keywordMode = "content";

function getTags(filter) {
	new Ajax.Updater("ajax_keywords_"+keywordMode, ""+ baseURL +"index.php?id=41&tx_readmecc_pi1[mode]=tagfilter&tx_readmecc_pi1[keywordcategory]="+keywordMode+"&tx_readmecc_pi1[tagfilter]="+filter+"&L="+window.L);
}


function prepareKW(id) {
	var currentValue = $(id).value;
	if(currentValue != '')
		$(id).value += ", ";
}

function cleanKW(id) {
	var currentValue = $(id).value;
	if (currentValue.substring(currentValue.length-2, currentValue.length) == ', ')
		$(id).value = currentValue.substring(0,currentValue.length-2);
	else if (currentValue.substring(currentValue.length-1, currentValue.length) == ',')
		$(id).value = currentValue.substring(0,currentValue.length-1);
	
}

function previewAV(sourceid) {
	var AVtag = $(sourceid).value;	
	$('preview_audiovideo').innerHTML = AVtag;
	$('preview_audiovideo').setAttribute('style', 'display: block;');
}



function booktipAutoComplete() {
	//alert("invoked");
	//Effect.toggle('loading', 'fade', {duration:0.2});
	
	var isbn = $('tx_readmecc_pi1[isbn]').value;
	// clear value
	isbn = isbn.replace(/[^a-zA-Z0-9]/g, "");
	$('tx_readmecc_pi1[isbn]').value = isbn;
	
	if(isbn == '') {
		alert("Enter ISBN");
		return false;
	}
		
	// loading indicator	
	$('loading').setAttribute('style', 'display: block;');
	
	// fetch data
	new Ajax.Request(''+ baseURL +'index.php?id=41&tx_readmecc_pi1[mode]=autocomplete&tx_readmecc_pi1[isbn]='+isbn,{onSuccess:autoCompleteResponse});
}

var autoCompleteResponse = function(t) {
	//alert("response");
    //json = eval(t.getResponseHeader('X-JSON'));  
    var json = t.getResponseHeader('X-JSON').evalJSON();

	// fix this ... what is wrong?
	Effect.toggle('loading', 'appear', {duration:0.2});
	
	if(json.results == "notfound") {
    	alert("No results");
    	return false;	
    }
      
	$('tx_readmecc_pi1[title]').value = json.title;
	$('tx_readmecc_pi1[publisher]').value = json.publisher;
	$('tx_readmecc_pi1[last_name]').value = json.last_name;
	$('tx_readmecc_pi1[first_name]').value = json.first_name;
	$('tx_readmecc_pi1[other]').value = json.other;
	$('tx_readmecc_pi1[publisher]').value = json.publisher;
	$('tx_readmecc_pi1[year]').value = json.year;
	$('tx_readmecc_pi1[rel_author]').value = json.rel_author;
	

	//$("loading").style.display = "none";
}


function setRating(score) {
	var htmlout='';	
	for (i=1; i<=6; i++) {
		if(i <= score) {
			htmlout += '<a href="#" onmouseover="setRating(' + i + ');"><img src="'+ baseURL +'fileadmin/i/ico_star.gif" alt="*" /></a>';
		} else {
			htmlout += '<a href="#" onmouseover="setRating(' + i + ');"><img src="'+ baseURL +'fileadmin/i/ico_star_lo.gif" alt="*" /></a>';
		}
	}
	$('ratingscore').innerHTML = '';
	$('ratingscore').innerHTML = htmlout;
	$('scoremessage').innerHTML = window.scoreMessages[score - 1];
	$('scoredata').value = score;
}



function preview(elementId, id, left, top, language) {
	//TODO: change eval into objects	
	// check preview visibility
	
	var previewDisplay = $('preview_'+elementId).getStyle("display");
	
	//check current active preview
	if(id == eval("window.activePreview_"+elementId)) {
		Effect.toggle('preview_'+elementId, 'appear', {duration:0.2});
	}
	else {
		// fetch data
		new Ajax.Request(''+ baseURL +'index.php?id='+ pid_ajax_booktips +'&L='+ language +'&tx_readmecc_pi1[mode]=preview&tx_readmecc_pi1[uid]='+id,
		{
			method:'get',
			onSuccess: function(transport){
				//var json = t.getResponseHeader('X-JSON').evalJSON();
				prehtml = transport.responseText;	
				htmlout = '<div class="booktipdetail" style="position: absolute; top: '+ top +'px; left: '+ left +'px">' +
					prehtml +
				'</div>';
				//alert('preview_'+elementId);
				$('preview_'+elementId).innerHTML = htmlout;
				if(previewDisplay == "none")
					Effect.toggle('preview_'+elementId, 'appear', {duration:0.2});
				//set current active preview
				eval("window.activePreview_"+elementId+"= id");
			}
		});
	}
}


/* --- method chagned

function preview(elementId, id, left, top) {
	//TODO: change eval into objects	
	// check preview visibility
	var previewDisplay = $('preview_'+elementId).getStyle("display");
	
	//check current active preview
	if(id == eval("window.activePreview_"+elementId)) {
		Effect.toggle('preview_'+elementId, 'appear', {duration:0.2});
	}
	else {
		// fetch data
		new Ajax.Request('http://readmecc.super-fi.com/index.php?id=41&tx_readmecc_pi1[mode]=single&tx_readmecc_pi1[uid]='+id,
		{
			method:'get',
			onSuccess: function(transport){
				//var json = t.getResponseHeader('X-JSON').evalJSON();
				json = eval(transport.responseText);
				
				htmlout = '<div class="booktipdetail" style="position: absolute; top: '+ top +'px; left: '+ left +'px">' +
				'<div class="previewmask" style="position: relative;">' +
				'<h2>'+ json.title +'</h2>' +
				'<p>' + json.authorname +' ';		
				if(json.other != '')
				 	htmlout += '('+ json.other +')';
				 htmlout += '</p><ul class="nobullet">';
				 
				if(json.counters.links_etext)
					htmlout += '<li><a class="icontext www" href="'+ json.detaillink +'#etext" title="">e-Text ('+ json.counters.links_etext +')</a></li>';
				if(json.counters.links_audiovideo)
					htmlout += '<li><a class="icontext audio" href="'+ json.detaillink +'#audiovideo" title="">audio/video ('+ json.counters.links_audiovideo +')</a></li>';
				if(json.counters.comments)
					htmlout += '<li><a class="icontext comments" href="'+ json.detaillink +'#comments" title="">comments ('+ json.counters.comments +')</a></li>';
				
				htmlout += '<li><a class="icontext library" href="'+ json.borrowlink +'" title="">borrow this book</a></li></ul><div class="rt">' +
				'<p><em>[ book tip by <a href="' + json.userlink +'">'+ json.username +'</a> ]</em> ' + json.text + '</p>' +
				'</div>' +
				'</div>' +
				'</div>';
				//alert('preview_'+elementId);
				$('preview_'+elementId).innerHTML = htmlout;
				if(previewDisplay == "none")
					Effect.toggle('preview_'+elementId, 'appear', {duration:0.2});
				//set current active preview
				eval("window.activePreview_"+elementId+"= id");
			}
		});
	}
} */

/* ----- deprecated

function userPrivateData(user) {

	new Ajax.Request('http://readmecc.super-fi.com/index.php?id=61&tx_readmecc_pi2[mode]=private&tx_readmecc_pi2[user]='+user,
	{
		method:'get',
		onSuccess: function(t){
			
			var json = t.getResponseHeader('X-JSON').evalJSON();
			
			//alert(json.email);
			
			htmlout = ' '+ json.email +' ';		
			$('privatedata').innerHTML = htmlout;
		}
	});
}

*/



function setFilter(id, letter) {
	$(id).value = letter;
	$('filter_form').submit();	
}

function openLink() {
	var URL = $('linklist').options[$('linklist').selectedIndex].value;
	if(URL != '') {
		var load = window.open(URL);
	}
}


function openSelectURL(id) {
	var URL = $(id).options[$(id).selectedIndex].value;
	if(URL != '') {
		document.location.href = baseURL + URL;
	}
}



function exposeForm() {
	Effect.toggle('exposeform', 'blind', {duration:0.2});	
}


function fadeView(divid) {
	Effect.toggle(divid, 'appear', {duration:0.2});	
}


function fetchComments(divid, lat, long, language) {
	
	// calc. hash out of latitude/longitude
	hash = hex_md5(""+lat+""+long+"");

	new Ajax.Request(''+ baseURL +'index.php?id=102&L='+ language +'&tx_readmecccomments_pi1[pid]=96&tx_rggooglemap_pi1[hash]='+ hash,
	{
		method:'get',
		onSuccess: function(transport, json){

		/*	try {
				json = eval(transport.responseText);
			} catch (e){}
			if (json == null) {
				//alert(transport.responseText);
			} */

			
			$(divid).innerHTML = transport.responseText;
		}
	});
}	


function gallerySelect(group) {
		
	if(group == "location") {
		var toShow = $('fotobotselect').options[$('fotobotselect').selectedIndex].value;	
		for (var n = 0; n < window.locations.length; n++){
			where = window.locations[n];
		    if(where == toShow) {
		    	$(where).style.display = "block";	
		    } else {
		    	$(where).style.display = "none";
		    }
		}
	} else {
		var toShow = $('galleryselect').options[$('galleryselect').selectedIndex].value;
		
		if(toShow == "mms") {
			$('gallery_mms').setAttribute('style',"display: block;");
			$('gallery_fotobot').setAttribute('style',"display: none;");
			$('gallery_images').setAttribute('style',"display: none;");
		} else if (toShow == "images") {
			$('gallery_mms').setAttribute('style',"display: none;");
			$('gallery_fotobot').setAttribute('style',"display: none;");
			$('gallery_images').setAttribute('style',"display: block;");
		} else {
			$('gallery_mms').setAttribute('style',"display: none;");
			$('gallery_fotobot').setAttribute('style',"display: block;");
			$('gallery_images').setAttribute('style',"display: none;");		
		}
	}
}




