/**
 * Helper functions js file:
 * 
 * @version (c)2011 Industrial Computer Source (Deutschland) GmbH 
 *  
 */


/**
 * 
 * @param String div_id, string name of the id
 * @param Integer 
 * @param Integer aniVar, value of current size
 *
 * 
 */
function resizeMeTo(div_id, div_id_inner, aniVar){
	var div1 = document.getElementById(div_id);
	var div2 = document.getElementById(div_id_inner);
	
	//div2.style.display = 'none';
	
	if(aniVar < 100){
		aniVar+=2;
		//div1.style.height = aniVar+'%';
		//div1.style.width = aniVar+'%';
		//div1.style.margin = "0% 0% 0% 50%";
		//div1.style.left = '-'+(aniVar)/2+'%';
		div1.style.opacity = ''+(aniVar*aniVar)/12000+'';
		
		window.setTimeout("resizeMeTo('" + div_id +"','" + div_id_inner +"',"+aniVar+");", 1);
	}
}


/**
 * Description: 
 * Checks user Y scroll position and moves an element (given by id)
 * up and down during scroll action.
 *   
 * @param, String id, name of the element (id) to be moved
 * @return Element element, the element moved
 * 
 */

function menuMove(id){
	// configuration
	var startScroll = 350; 	// pixel y from content top
	var timeout = 25;		// in ms for refresh
	var speed = 3;			// x/speed in %, higher value -> smoother moving (D{ 1-oo,!0} )
	
	// some vars
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var element = document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
	var sy = 0;
	
	window[id + "_obj"] = element;
	if(document.layers)
		element.style = element;
	element.cy = element.sy = sy;
	element.sPos = function(y){
		this.style.top = y+"px";
	}

	element.move = function(){
		var userY;
		
		// get the scroll position from user interaction
		userY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		
		// calculate the maximum pixel movement down
		maxed = document.getElementById('sub_ipc_products').offsetHeight;
		maxed += document.getElementById('sub_industrial_automation').offsetHeight;
		maxed += document.getElementById('sub_business_automation').offsetHeight;
		maxed += document.getElementById('sub_embedded_products').offsetHeight;

		maxed += 6*28; // add height of main pgrp of menu

		fullHeight = document.getElementById('content').offsetHeight + 275;
				
		
		// moving between scrolling is > 350px AND scrolling is < fullheight of content div
		if(userY > startScroll && userY < fullHeight-maxed){
			this.cy += (userY-startScroll + this.sy - this.cy)/speed;
			this.sPos(this.cy);
		}
		else // snap to bottom
		if(userY > fullHeight-maxed){
			//this.cy = fullHeight-maxed-355;
			this.sPos(fullHeight-maxed-385);
		}
	

		
		// snap to start pos when usr scrolling back to top 
		if(userY < startScroll){
			this.sPos(0);
			this.cy = 0;
		}
	
		
		setTimeout(this.id + "_obj.move()", timeout);
	}
	return element;
}


/**
 * Mark given option by value.
 * 
 * @param String selectId, id of the object to check for
 * @param String optionValue, value to check for and flag as "selected"
 * 
 * @retun void
 * 
 */
function selectOption(selectId, optionValue){
	for(var i = 0; i < document.getElementById(selectId).length; i++){	
		if(document.getElementById(selectId).options[i].value==optionValue){
			document.getElementById(selectId).options[i].selected=true;
			break;
		}
	}
}


/**
 * This function switches the css style -> display:block|none.
 * 
 */
function switchlayer(Layer_Name){
	var GECKO = document.getElementById ? 1 : 0;
	var NS = document.layers ? 1 : 0;
	var IE = document.all ? 1 : 0;
	
	if(GECKO){
		document.getElementById(Layer_Name).style.display = (document.getElementById(Layer_Name).style.display == 'block') ? 'none' : 'block';
	}
	else 
		if(NS){
			document.layers[Layer_Name].display = (document.layers[Layer_Name].display == 'block') ? 'none' : 'block';
		}
		else 
			if(IE){
				document.all[Layer_Name].style.display = (document.all[Layer_Name].style.display == 'block') ? 'none' : 'block';
			}
}


/**
 * A standart function to make a timeout inside code.
 *
 * @param millis, int milliseconds
 * @return void
 */
function pausecomp(millis){

	var date = new Date();
	var curDate = null;

	do{
		curDate = new Date();
	}
	while(curDate-date < millis);
} 


/**
  * Checks if given value matches the "case" then remove it. 
  *   
  * @param, String s
  * @return void
  * 
  */
function clearField(s){
	
	switch (s){
		case "mainSearchInput":
			document.psearch_form.psearch_input.value = "";
			//element.length = "go";
			break;
	}
}


/**
 * This function sets given element name to mid.
 * 
 * @require jquery library
 * 
 */
function makeMeCenter(el, bug){
	
	left = $(window).width()/2-$(el).width()/2-bug;
	if(left<=20)
		left = 20;
	$(el).css('left', left + 'px');
}


/**
 * This function counts the chars and writes
 * the count into given dom element.
 */
function countChars(textid, limit, infodiv){
	var text = $('#' + textid).val();
	var textlength = text.length;
	
	if(textlength > limit){
		$('#' + textid).val(text.substr(0,limit));
		return false;
	}
	else{
		$('#' + infodiv).val(limit-textlength);
		return true;
	}
}


