/**
 *
 * @var string imgDir
 * @var Array boxLinks
 *
 * @author   Ahmet Sacan
 * @package  COW
 * @version  $Revision: 1.0 $
 */

var imgDir = 'Img/';
var boxLinks = new Array();
function CheckBoxLinks(elem){
	for (i=0; i<boxLinks.length; i++){
		if(boxLinks[i][0]==elem){
			location.href=boxLinks[i][1];
		}
	}
}
function HideSeek(elem, display){
	box = document.getElementById(elem);
	kutu = document.getElementById('kutu_'+elem);
	if (box.style.display == 'none' && display != 'hide'){
		box.style.display = '';
		if(kutu)
			kutu.src = imgDir+'minus.gif'
		CheckBoxLinks(elem);
	}
	else{
		box.style.display = 'none';
		if(kutu)
			kutu.src = imgDir+'plus.gif';
	}
}

function cow_findObj(n, d) { //v4.0
	var p,i,x;
	if(!d)
		d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length){
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all)
		x=d.all[n];
	for (i=0;!x&&i<d.forms.length;i++)
		x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++)
		x=cow_findObj(n,d.layers[i].document);
	if(!x && document.getElementById)
		x=document.getElementById(n);
	return x;
}

/*
function cow_showHideLayers() {
		alert('hey');
	var i,p,v,obj,args=cow_showHideLayers.arguments;
	for (i=0; i<(args.length-1); i+=2)
	if ((obj=cow_findObj(args[i]))!=null) {
			v=args[i+1];
		if (obj.style)
			obj=obj.style;
		if(v=='toggle')
			v = (obj.visibility=='visible' ? 'hidden' : 'visible';
		if (obj.style)
			v=(v=='show')?'visible':(v='hide')?'hidden':v;
		v='show';
		obj.visibility=v;
		alert(v);
	}
}
*/
function cow_showHideLayers() {
	var isStyle,i,p,v,obj,args=cow_showHideLayers.arguments;
	for (i=0; i<(args.length-1); i+=2){
			if ((obj=cow_findObj(args[i]))!=null) {
				isStyle = obj.style;
				v=args[i+1];
				if (isStyle)
					obj=obj.style;
				if(v=='toggle')
					v=(obj.visibility=='visible')?'hide':'show';
				if (isStyle)
					v=(v=='show')?'visible':(v='hide')?'hidden':v;
				obj.visibility=v;
		}
	}
}


//-----------------------------------------------------------------------------
// Path to arrow images
var arrowImage = TM_IMAGE_DIR+'/select_arrow.gif';	// Regular arrow
var arrowImageOver = TM_IMAGE_DIR+'/select_arrow_over.gif';	// Mouse over
var arrowImageDown = TM_IMAGE_DIR+'/select_arrow_down.gif';	// Mouse down

var selectBoxIds = 0;
var currentlyOpenedOptionBox = false;
var editableSelect_activeArrow = false;

function selectBox_switchImageUrl()
{
	if(this.src.indexOf(arrowImage)>=0){
		this.src = this.src.replace(arrowImage,arrowImageOver);	
	}else{
		this.src = this.src.replace(arrowImageOver,arrowImage);
	}
	
	
}

function selectBox_showOptions()
{
	if(editableSelect_activeArrow && editableSelect_activeArrow!=this){
		editableSelect_activeArrow.src = arrowImage;
		
	}
	editableSelect_activeArrow = this;
	var optionDiv = document.getElementById('selectBoxOptions' + this.id.replace(/[^\d]/g,''));
	if(optionDiv.style.display=='block'){
		optionDiv.style.display='none';
		this.src = arrowImageOver;	
	}else{			
		optionDiv.style.display='block';
		this.src = arrowImageDown;	
		if(currentlyOpenedOptionBox && currentlyOpenedOptionBox!=optionDiv)currentlyOpenedOptionBox.style.display='none';	
		currentlyOpenedOptionBox= optionDiv;			
	}
}

function selectOptionValue()
{
	var parentNode = this.parentNode.parentNode;
	var textInput = parentNode.getElementsByTagName('INPUT')[0];
	textInput.value = this.innerHTML;	
	this.parentNode.style.display='none';	
	document.getElementById('arrowSelectBox' + parentNode.id.replace(/[^\d]/g,'')).src = arrowImageOver;
}
var activeOption;
function highlightSelectBoxOption()
{
	if(this.style.backgroundColor=='#316AC5'){
		this.style.backgroundColor='';
		this.style.color='';
	}else{
		this.style.backgroundColor='#316AC5';
		this.style.color='#FFF';			
	}	
	
	if(activeOption){
		activeOption.style.backgroundColor='';
		activeOption.style.color='';			
	}
	activeOption = this;
	
}
function createEditableSelect(destid)
{
	var dest = document.getElementById(destid);
	dest.className='selectBoxInput';
	var div = document.createElement('DIV');
	div.style.styleFloat = 'left';
	div.style.width = dest.offsetWidth + 16 + 'px';
	//this is an internet explorer issue...!
	div.style.position = 'relative';
	div.id = 'selectBox' + selectBoxIds;
	var parent = dest.parentNode;
	parent.insertBefore(div,dest);
	div.appendChild(dest);	
	div.className='selectBox';
	div.style.zIndex = 10000 - selectBoxIds;

	var img = document.createElement('IMG');
	img.src = arrowImage;
	img.className = 'selectBoxArrow';
	
	img.onmouseover = selectBox_switchImageUrl;
	img.onmouseout = selectBox_switchImageUrl;
	img.onclick = selectBox_showOptions;
	img.id = 'arrowSelectBox' + selectBoxIds;

	div.appendChild(img);
	
	var optionDiv = document.createElement('DIV');
	optionDiv.id = 'selectBoxOptions' + selectBoxIds;
	optionDiv.className='selectBoxOptionContainer';
	if(dest.offsetWidth)
		optionDiv.style.width = div.offsetWidth-2 + 'px';
	else{
		div.style.width = '220px';
		optionDiv.style.width = '220px';
	}
	div.appendChild(optionDiv);

	if(dest.getAttribute('selectBoxOptions')){
		var options = dest.getAttribute('selectBoxOptions').split(';');
	}
	else{
		eval('var options='+destid+'_options');
	}
	if(options.length){
		var optionsTotalHeight = 0;
		var optionArray = new Array();
		for(var no=0;no<options.length;no++){
			var anOption = document.createElement('DIV');
			anOption.innerHTML = options[no];
			anOption.className='selectBoxAnOption';
			anOption.onclick = selectOptionValue;
			anOption.style.width = optionDiv.style.width.replace('px','') - 2 + 'px'; 
			anOption.onmouseover = highlightSelectBoxOption;
			optionDiv.appendChild(anOption);	
			optionsTotalHeight = optionsTotalHeight + anOption.offsetHeight;
			optionArray.push(anOption);
		}
		if(optionsTotalHeight > optionDiv.offsetHeight){				
			for(var no=0;no<optionArray.length;no++){
				optionArray[no].style.width = optionDiv.style.width.replace('px','') - 22 + 'px'; 	
			}	
		}		
		optionDiv.style.display='none';
		optionDiv.style.visibility='visible';
	}
	
	selectBoxIds = selectBoxIds + 1;
}	



//usage: 
//<input name='fruit' type='text' class='textbox' title="Opening" backspace='false' onkeyup="autocomplete(this,fruits)">
//fruits = new Array('apple','pear','orange','mango','durain','grapes','starfruit');
//WORKS ONLY FOR IE. Firefox doesn't recognize event variable
function autocomplete(n,ac_array){
	if (n.value == "") return 0;
	if (event.keyCode == 8 && n.backspace){
		n.value = n.value.substr(0,n.value.length-1);
		n.backspace = false;
	}
	
	var r = n.createTextRange();
	tmp= n.value;
	if (tmp == "")return 0;
	for (z=0;z<ac_array.length;z++){
		tmp2 = ac_array[z];
		count = 0;
		for (i = 0;i<tmp.length;i++){
			if (tmp2.charAt(i) == tmp.charAt(i)){
				count++
			}
		}
		if (count == tmp.length){
			diff = tmp2.length - tmp.length;
			if (diff <= 0) break;
			kap = "";
			for (i=0;i<tmp2.length;i++){
				if (i >= tmp.length) kap += tmp2.charAt(i);
			}
			n.backspace = true;
			r.text += kap;
			r.findText(kap,diff*-2);
			r.select();
			return 0;
		}
	}
	n.backspace = false;
	return 0;
}
