/**   
 *  SONOfind 2006 - musiksuche.js
 *  (c) 2007 ettl-software KEG - Michael Ettl 
 *  Please keep in mind that this code is not OpenSource
 *
 **/

	/* show Balloon for Labels */
	function __showBalloon(mouse, id, img, text) {
		var oElem=Element.extend(mouse);
		var oPos=(oElem.cumulativeOffset());
		if(! $(id)) {
			var x=new Element('div',{ 'id': 'id_b'}).insert(new Element('img',{ 'src': '../img/us.gif', 'alt':''}))
			x.insert(new Element('div',{ 'id': 'lib_txt'}));
			$$("body")[0].insert(x);
		}
		$(id).setStyle({top: (oPos.top+15)+"px",
						left: (oPos.left+5)+"px",
						display: 'block'
		});
		$('lib_img').style.height = (img == "../img/us.gif")?"20px":"";
		$('lib_img').src=img;
		$('lib_txt').update(text);
	}
	
	/* hide Balloon */
	function __hideBalloon(id) {
		$(id).hide();
		//document.getElementById(id).style.display = 'none';
	}
	
	/* Show Balloon for Labels - Refactored to AJAX - ettl-me # 2010-05-18 */
	function showBalloon(oElem) {
		cLabel=oElem.id;
		cLabel=cLabel.substr(3);
		var oElem=Element.extend(oElem);
		var oPos=(oElem.cumulativeOffset());
		if(! $('id_b')) {
			var x=new Element('div',{ 'id': 'id_b'}).insert(new Element('img',{ 'id':'lib_img', 'src': '../img/us.gif', 'alt':''}))
			x.insert(new Element('div',{ 'id': 'lib_txt'}));
			$$("body")[0].insert(x);
		}
		$('id_b').setStyle({top: (oPos.top+15)+"px",
						left: (oPos.left+5)+"px"
		});
		var titleLoaded=$('lib_img').title;
		if(titleLoaded == cLabel) {
			$('id_b').show();
		} else {
			$('lib_img').title=cLabel;
			$('lib_img').hide();
			$('lib_img').observe("load", function(Event) { 	this.show(); })
			$('lib_txt').update("Loading...");
			$('id_b').show();
			
		  	var url="../html/ajax/ax_getTrack.php";
			new Ajax.Request(url, {
			  method: 'get',
			  parameters: {'sprache':'DE','type':'label','label':cLabel },
			  onFailure: function(transport) {
				 alert("showBalloon: Error while saving data - URL not available\n\nStatuscode: "+transport.status);
			  },
			  onSuccess: function(transport) {
				 if(! String(transport.responseText).isJSON()) {
					 	 alert("showBalloon: Ajax Response Error:\n"+String(transport.responseText).stripTags().stripScripts());
					 	 return;
				 }
				 var myObj=String(transport.responseText).evalJSON(true);
				 if(myObj.ax_success <=0) {
						 alert("showBalloon: Label not found\nError:"+myObj.ax_msg);
						 return;
				 }
				 showBalloon2(myObj);
			  }
			});
		}
	}
	
	function showBalloon2(oLabels) {
		if("object"==typeof(oLabels)) {
			img=oLabels.labels[0].image;
			text=oLabels.labels[0].name;
			var cHeight = (img == "img/us.gif")?"20px":"auto";
			$('lib_img').setStyle({ height: cHeight });
			$('lib_img').src="../"+img;
			$('lib_txt').update(text);
			$('id_b').observe('mouseout', hideBalloon);
			$('id_b').show();
		}
	}
	
	function hideBalloon(oElem) {
		$('id_b').hide();
	}	
	
	
	// new password / lost password
	function openPasswdNew()  {
		var link = DOWNSERVER+"/customer/newCustomer_index.php?sprache="+language;
		parent.openPopup(link,'password','408','575',language);
	}
	function openPasswdLost() {
		var link = DOWNSERVER+"/customer/pw_forgot.php?sprache="+language;
		parent.openPopup(link,'password','408','575',language);
	}
	
	/* Set Times to Empty in Min/Sec on Focus */
	function reset00(oElem) {
		if(oElem.value=="00")
			oElem.value="";
	}
	
	/* Intellisearch (et) -2007-12 */
	var is_oISearch;
	function is_doSearch(myElem) {
		/* skip as long as we do not have prototype loaded */
		var oElem=Element.extend(myElem);
		var oPos=(oElem.cumulativeOffset());
		oPos.top+=(oElem.getHeight()+2);
		
		var oHElem=$('is_head');
		// ettl-me - 2009-06-17 # check if is_head is already loaded
		if(oHElem) {
			oHElem.setStyle({
				top: oPos.top+'px',
				left: oPos.left+'px'
			});
			oHElem.hide();
			is_oISearch.softsearch(oElem.value);
		}
	}

	function is_setWord(oElem) {
		var myElem=oElem.innerText;
		if ('undefined' == typeof myElem) {
			myElem=oElem.text;
		}
		$('fld_searchword').value='"'+myElem+'"';
		is_close();
	}
	
	function is_close() {
		$('fld_searchword').stopObserving('click');
		$('is_head').hide();
	}
		
	
		/* Intellisearch Callback Handler */
	function is_cbhandler(oResponse) {
		var oJSON=eval(oResponse.responseText);
		if (! oJSON) {
			$('is_head').writeAttribute('cnt',0);
			$('is_list').update('');
			$('is_head').hide();
			return;
		}
		if (oJSON.errmsg)
			alert("cb_setSoftsearch()\nJSON:"+oJSON.action+"\nJSON-errmsg:"+oJSON.errmsg);
			
		var txt='';
		for(i=0;i<oJSON.length;i++) {
			txt=txt+"<div class='is_ssw'><a href='javascript:void(-1)' onclick='is_setWord(this);' class='pulldown_entry'>"+oJSON[i]+"</a></div>";
		}
		$('is_list').setStyle({	height: "auto" });
		$('is_list').update(txt);
		$('is_head').writeAttribute('cnt',oJSON.length);
		is_starttimer();
		$('is_head').show();
		$('is_head').observe('mouseover', is_starttimer);
		oPos=$('is_head').viewportOffset();
		var wHeight=getWindowHeight();
		var maxH=wHeight-oPos.top-80;
		var listH=$('is_list').getHeight();
		myAlert("check height:"+listH+":"+maxH);
		if (listH > maxH) {
			var elemH=$('is_list').firstDescendant().getHeight();
			var listH=parseInt(maxH/elemH)*elemH;
			myAlert("set height to "+listH+":"+elemH+":"+maxH);
			$('is_list').setStyle({	height: listH+"px" });
		}
		myAlert('AJAX found: '+oJSON.length+"<br/>");
	}
	var is_timer;
	
	function is_starttimer() {
		window.clearTimeout(is_timer);
		is_timer=window.setTimeout("is_timerevent()", 5000);
	}
	function is_timerevent() {
		is_close();
		$('fld_searchword').observe('click',function(event) {
				is_show();
		});
	}
	function is_show() {
		$('fld_searchword').stopObserving('click');
		$('is_head').show();
	}
	
	
	/* Intellisearch (et) -2007-12 */	
	
	/* (ettl-me) - 2008-08-29 # Change Duration Layout  */	
	function durationchange(inputElem) {
		var cVal = inputElem.value;
		cVal=cVal.strip();
		var nVal=cVal*1;
		if(! nVal) nVal=0; 
		cVal=nVal.toPaddedString(2);
		inputElem.value=cVal;
		if(cVal != "00" && ! cVal.blank()) {
			inputElem.className="duration_a";
		} else {
			inputElem.className="duration_d";
		}
	}
	
/* (ettl-me) - 2010-04-29 # Functions taken from musicsuche.php */	
	
  function setHeader(cID,cCode) {
  	oElem=$(cID);
  	if(! oElem)
	  	return;
  	oElem.innerHTML=cCode;
  	if(cID=="erg_labels") {
  		/* Resize on Change of this DIV */
	  	resizeSearchFrm();
  	}
  }

  /*
  Refine Search - click on a label
  */
  function showLabel(cLabel,oElem) {
  	
  	/* Keeping Old Functionality with 2 Params */
  	if('object'== typeof(cLabel)) {
  		oElem=cLabel;
		cLabel=oElem.id;
		cLabel=cLabel.substr(3);
  	}
  	
	$$('#erg_labels a.black_sm_link').each(function (myElem) {
			myElem.removeClassName('black_sm_link');
			myElem.addClassName('red_sm_link');
	});
	oElem.addClassName("black_sm_link");
	
  	if (IFrameSuchtreffer) {
  		if (! ('undefined' == typeof IFrameSuchtreffer.cSearchType)) {
  			document.RefineSearch.searchtype.value=IFrameSuchtreffer.cSearchType;
  		}
  	}
  	
  	document.RefineSearch.label.value=cLabel;
  	document.RefineSearch.sopt_notes.value=(document.FormSuche.sopt_notes.checked==true?1:0);
  	document.RefineSearch.show_titles.value=($('frm_show_titles').checked==true?1:0);
  	if (document.RefineSearch.searchtype.value=='note') {
  		document.RefineSearch.sopt_notes.value=1;
  	}
  	if (document.RefineSearch.searchtype.value=='title') {
  		document.RefineSearch.show_titles.value=1;
  	}
  	writeSearchInfo(IFrameSuchtreffer,suchtext);
  	document.RefineSearch.submit();
  }


  function showSingleTracks(oElem) {
  	bIsActive=oElem.checked;
  	/* Must use Timer - otherwise Checkbox is blocked until Switching the Tracks is done */
  	if (IFrameSuchtreffer.searchHits) {
	  	IFrameSuchtreffer.setTimeout('showAllTracks('+bIsActive+');',5);
  	}
  }


  /*
  showDiv - Changes all the Left DIV Segments including the Nav.Menu 
  changeMenu => Changes the Menu Entry choosen
  switchLeftDiv => Changes the DIV Displayed  (1=Searchword/2=add.searches list/3=add.searches Pulldown/4=mySonofind)
  */
  function showDiv(status) {

  	//deactivateOption(3,0);
	if(oPDInstr)
		oPDInstr.pdEnable();

  	// hide all divs
  	switchLeftDiv('01',0);
  	switchLeftDiv('02',0);
  	switchLeftDiv('03',0);
  	switchLeftDiv('04',0);

  	switch (status) {
  		case "02":
	  		/* Liste mit weiteren Suchmöglichkeiten */
	  		changeMenu('i_02');
	  		switchLeftDiv('02',1);
	  		//document.getElementById('s_10').style.top = "0px";
	  		activeIframe='';
	  		break;
  		case "03":
	  		/* Weitere Suchmöglichkeit in DropDown Box */
	  		changeMenu('i_02');
	  		switchLeftDiv('03',1);
	  		//document.getElementById('s_10').style.top = "0px";
	  		//document.getElementByName(IFrameSuchtreffer).src = "repertoire.php";
	  		break;
  		case "04":
	  		/* Instruments */
	  		view='i';
	  		changeMenu('i_02');
	  		switchLeftDiv('03',1);
	  		activeIframe='IFrameInstr';
	  		writeSearchInfo(IFrameInstr,suchtext);
	  		document.getElementById('IFrameInstr').src = "list_instr.php?"+SID;
	  		resetSWHeaders();
	  		doResetMain('instr');
	  		doResetTop();
	  		if(oPDInstr)
	  			oPDInstr.pdDisable();
	  		break;
  		case "05":
	  		/* Composers */
	  		view='c';
	  		changeMenu('i_02');
	  		switchLeftDiv('03',1);
	  		activeIframe='IFrameInstr';
	  		writeSearchInfo(IFrameInstr,suchtext);
	  		document.getElementById('IFrameInstr').src = "list_comp.php?"+SID;
	  		resetSWHeaders();
	  		doResetMain('comp');
	  		doResetTop();
	  		break;
  		case "06":
	  		/* KnownAs */
	  		view='b';
	  		changeMenu('i_02');
	  		switchLeftDiv('03',1);
	  		activeIframe='IFrameInstr';
	  		writeSearchInfo(IFrameInstr,suchtext);
	  		resetSWHeaders();
	  		doResetMain('knownas');
	  		document.getElementById('IFrameInstr').src = "list_knownas.php?"+SID;
	  		doResetTop();
	  		break;
  		case "07":
	  		/* My Sonofind */
	  		changeMenu('i_04');
	  		switchLeftDiv('04',1);
	  		loadProjects();
	  		break;
  		case "08":
	  		/* Rep&Covers */
	  		view='c';
	  		changeMenu('i_02');
	  		switchLeftDiv('03',1);
	  		activeIframe='IFrameInstr';
	  		writeSearchInfo(IFrameInstr,suchtext);
	  		document.getElementById('IFrameInstr').src = "list_rep.php?"+SID;
	  		resetSWHeaders();
	  		doResetMain('rep');
	  		doResetTop();
	  		break;
  		case "09":
	  		/* New CDs */
	  		view='c';
	  		changeMenu('i_02');
	  		switchLeftDiv('03',1);
	  		activeIframe='IFrameInstr';
	  		writeSearchInfo(IFrameInstr,suchtext);
	  		document.getElementById('IFrameInstr').src = "list_rep.php?view=ncd&"+SID;
	  		resetSWHeaders();
	  		doResetMain('newcd');
	  		doResetTop();
	  		break;
  		default:
	  		/* Suchwortsuche - zurücksetzten der Suchwortliste/Kat.Liste */
	  		myChangePulldown('c_link1','c_link1_ch0','01','');
	  		showDivMen(1);
	  		switchLeftDiv('01',1);
	  		activeIframe='IFrameSuchbegriffe';
	  		doReset();
	  		break;
  	}
  }

  /* 
  4 Zustände der Suche (swicthLeftDiv())
  1. Suchwortsuche
  (div s_01)
  2. Liste mit Weiteren Suchmöglichkeiten
  (div s_02)
  3. Weitere Suchmöglichkeit in DropDown Box
  (div s_03)
  4. MySonofind
  (div s_04)
  */
  function switchLeftDiv(cId,show)
  {
  	x='s_'+cId;
  	i='i_'+cId;
	if(show==0) {
		$(x).hide();
	} else {
		$(x).show();
		if (cId == '03') {
	  		/* div 03 & 02 lead to same menu i_02 */
	  		i='i_02';
			changeMenu($(i));
	  	}
	}
  }

  function openView(myView) {
  	if("undefined"==typeof(myView)) {
  		var myView=view;
  	}
  	//alert("View:"+myView);
  	switch (myView) {
  		case 'i':
  			/* Instrument Search */
	  		showDivMen(1);
	  		showDiv('04');
	  		break;
  		case 'c':
			/* Composer-Search */
	  		showDivMen(1);
	  		showDiv('05');
	  		break;
  		case 'b':
			/* KnownAs-Search */
	  		showDivMen(1);
	  		showDiv('06');
	  		break;
  		case 'e':
	  		showDiv('02');
	  		showDivMen(2);
	  		break;
  		case 'ncd':
  			 /* New Tracks */
	  		showDiv('02');
	  		showDivMen(1);
	  		doSearch('newcd');
	  		break;
  		case 'no':
	  		showDiv('02');
	  		//pSearchType='single';
	  		showDivMen(3);
	  		break;
  		case 'r':
	  		/* Repertoire & Covers */
	  		showDiv('08');
	  		showDivMen(5);
	  		break;
  		case 'n':
	  		/* New CDs */
	  		showDiv('09');
	  		showDivMen(5);
	  		break;
  		case 'myno':
	  		showDiv('07');
	  		showDivMen(4);
	  		break;
  		case 'mysono':
	  		showDiv('07');
	  		break;
	  	case 'cat':
	  		showDivMen(1);
	  		showDiv('01');
	  		showCategories('catlist');
	  		break;
	  	case 's':
	  		showDivMen(1);
	  		showDiv('01');
	  		showAllSearchwords();
	  		break;
		case 'mysonofind':
	  		openMySonofind();
	  		break;
  		case 'myprojects':
	  		openProject();
	  		break;
  		case 'custsearch':
	  		IFrameSuchtreffer.location.href="../customer/custSearches.php?"+SID;
	  		break;
  		case 'custstat':
	  		IFrameSuchtreffer.location.href="../customer/custStatistic.php?"+SID;
	  		break;
  		case 'custdown':
	  		IFrameSuchtreffer.location.href="../customer/custDownloads.php?"+SID;
	  		break;
  		default:
	  		myView='s';
	  		showDefaultSearchwordList();
	  		break;
  	}
  	highlightMen(myView);
  	doResize();
  }

  /** Highlight Menu Entry + Select Pulldown Entry **/
  function highlightMen(myElem) {
  	if ('object' == typeof myElem) {
  		cElem = myElem.id;	// is a_i
  		myElem=cElem.substr(2);
  	} else {
  		cElem = "a_"+myElem;
  	}
  	/** Menu Entries are in Div s_02 */
  	var oChoosen=$$("div#s_02 a.subnavi_kl_a")[0];
  	if(oChoosen) {
  		oChoosen.addClassName("subnavi_kl");
  		oChoosen.removeClassName("subnavi_kl_a");
  	}
  	var oChoosen=$$("div#s_02 a#"+cElem)[0];
  	if(oChoosen) {
  		oChoosen.addClassName("subnavi_kl_a");
  		oChoosen.removeClassName("subnavi_kl");
  	}
  	if(oPDMenu) {
  		//			alert("PDSelect:"+myElem);
  		oPDMenu.pdSelectValue(myElem,false,true);
  	}

  	/*
  	for(i=0;i<aElem.length;i++) {
  	oElem=document.getElementById(aElem[i]);
  	if (oElem) {
  	if (cElem==aElem[i]) {
  	oElem.className='subnavi_kl_a';
  	} else {
  	oElem.className='subnavi_kl';
  	}
  	}
  	}
  	*/
  }
  
   
  function myGetHeight(cElem) {
  	var myElem=$(cElem);
  	if(myElem.visible()) {
  		retVal=myElem.getHeight();
  	} else {
  		retVal=0;
  	}
  	return(retVal);
  }
  

  

  function changeMenu(myElem) {

  	$$('div.lmenubtna').each(function(oElem) {	oElem.removeClassName('lmenubtna')	});
  	if(typeof(myElem)=="string") {
  		myElem=$(myElem);
  	}
  	if(typeof(myElem)=="undefined") { return; }
  	var oElem=Element.extend(myElem);
  	oElem=oElem.up('.lmenubtn');
  	if(oElem)
  	oElem.addClassName('lmenubtna');
  }
