var d=this.document;

//check htmlspecialvars
function htmlspecialchars(str,typ) {
    if(typeof str=="undefined") str="";
    if(typeof typ!="number") typ=2;
    typ=Math.max(0,Math.min(3,parseInt(typ)));
    var from=new Array('/&/g','/</g','/>/g');
    var to=new Array("&amp;","&lt;","&gt;");
    if(typ==1 || typ==3) {from.push(/\'/g); to.push("&#039;");}
    if(typ==2 || typ==3) {from.push(/\"/g); to.push("&quot;");}
    for(var i in from) str=str.replace(from[i],to[i]);
    return str;
}

//getElementById quickRef Function
function g(id){return d.getElementById(id)}

//let javascript sleep for x milliseconds
function sleep(millis) 
{
	var date = new Date();
	var curDate = null;				
	do { curDate = new Date(); } 
	while(curDate-date < millis);
}


/*finds position coords of an element*/
function p(obj) /*original: www.quirksmode.org extended: www.tschetsch.ch*/
{
	var l=t=0;
	if (obj.x && obj.y)
	{
		l+=obj.x;
		t+=obj.y;
	}
	else if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			l += obj.offsetLeft
			t += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	pos=[l,t];
	return pos;
}

/*randomizes an array in FisherYates shuffel algorythm*/
function randomizeArray ( myArray ) { /*original: http://sedition.com/perl/javascript-fy.html extended: tschetsch.ch*/
  i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}

/**************************************
** Image ChangeFlow
**************************************/

var pc_image_changeflow_overlayTime=3000; //time it should take to make the overlay transition
/*var pc_image_changeflow_element; //these are defined and deleted inside the looping function pc_image_changeflow_change
var pc_image_changeflow_image;*/ //these are defined and deleted inside the looping function pc_image_changeflow_change
var pc_image_changeflow_imagePreload=[];
var pc_image_changeflow_loopCount=0;
var pc_image_changeflow_picPointerAt=0;
var pc_image_changeflow_imageCoords;
var pc_image_changeflow_elementId;

function pc_image_changeflow(elementId)
{
	pc_image_changeflow_elementId=elementId;
	//get offset Coords for the absolute positioned image
	pc_image_changeflow_imageCoords=p(g(elementId));
	
	//randomize the list of images
	randomizeArray(pc_image_changeflow_pics);
	
	//Start preloading all images in background
	u=0;
	while(u<pc_image_changeflow_pics.length)
	{
		pc_image_changeflow_imagePreload[u]= new Image(950,300);
		pc_image_changeflow_imagePreload[u].src=pc_image_changeflow_pics[u];
		u++;
	} 
	
	pc_image_changeflow_change(); //make sure, that the first image is shown asap!!
	//Start Looping process
	setInterval("pc_image_changeflow_change()",8000);
}

//make a setTimeout call for the
function pc_image_changeflow_ieTransparent(opacity)
{
	$('#randompicActive').css("filter","progid:DXImageTransform.Microsoft.Alpha(opacity="+opacity+")");
}

//change the image
function pc_image_changeflow_change()
{
	//define overlay transitions main variables
	pc_image_changeflow_element=$('#'+pc_image_changeflow_elementId);
	
	pc_image_changeflow_image=$('#randompicActive');
	pc_image_changeflow_image.css({"left":"151px",
									"top":pc_image_changeflow_imageCoords[1]+"px"});
	
	//make sure, the loopCount doesn't go over the amount of pictures
	if (pc_image_changeflow_picPointerAt==pc_image_changeflow_pics.length)
		pc_image_changeflow_picPointerAt=0;
	
	
	newpath=pc_image_changeflow_pics[pc_image_changeflow_picPointerAt];	
	
	//make sure, the image is already loaded!!
	if ( pc_image_changeflow_imagePreload[pc_image_changeflow_picPointerAt].complete == true || pc_image_changeflow_loopCount==0 ) //image is loaded
	{
		if (pc_image_changeflow_loopCount>0) //not first function call -> we have to do a nice transition between 2 pics
		{
			//lay div with background-image over the image, to hide the image and change the src
			pc_image_changeflow_element.css({"background-image":"url("+pc_image_changeflow_image.attr("src")+")",
											"z-index":"1"});
			//hide the image and delete itself & regen
			pc_image_changeflow_image.css({"z-index":"-1"});
			
			//check if browser support the css3 opacity property
			if ($.browser.msie && parseInt($.browser.version)<=7) //IE6 doesn't feature opacity, but a filter instead
			{
				pc_image_changeflow_image.attr("src",newpath);
				pc_image_changeflow_element.css({"z-index":"-1"});
				
				pc_image_changeflow_image.css({"filter":"progid:DXImageTransform.Microsoft.Alpha(opacity=0)",
											   "z-index":"1"});											   
																											
				opacityChangeCount=1;			
				interval=60;
				while(opacityChangeCount<=(pc_image_changeflow_overlayTime/interval))
				{
					tempOpacity=100/(pc_image_changeflow_overlayTime/(interval*opacityChangeCount));
					setTimeout("pc_image_changeflow_ieTransparent("+tempOpacity+")",(interval*opacityChangeCount));
					opacityChangeCount++;
				}
				if (tempOpacity<100) //make sure, we've got 100% opacity at time end!
					setTimeout("pc_image_changeflow_ieTransparent(100)",pc_image_changeflow_overlayTime);
			}
			else
			{
				pc_image_changeflow_image.attr("src",newpath);
				pc_image_changeflow_element.css({"z-index":"-1"});
				pc_image_changeflow_image.css({"opacity":"0",
											  "z-index":"1"});
				pc_image_changeflow_image.animate({"opacity":"1"},pc_image_changeflow_overlayTime);
			}
		}
		else // first function call -> display directly
		{
			pc_image_changeflow_image.attr("src",newpath);
		}
	}
	
	//delete main variables, to create them next time again to save memory
	delete pc_image_changeflow_image;
	delete pc_image_changeflow_element;
	
	pc_image_changeflow_picPointerAt++;	
	pc_image_changeflow_loopCount++;
}



/**************************************
** Navigation Accordion Effect
**************************************/
var hasActive=false; //Information if a navigation point is active, because the user is on a page
var pc_navi_navipoints=new Array(); //all navigationpoints, which should be in the navigation accordion effect
function pc_navi_Accordion_add(name)
{
	pc_navi_navipoints[pc_navi_navipoints.length]=name;
}

//makes the navigation react to the accordion (assigns the events)
function pc_navi_Accordion_init()
{
	for (i=0;i<pc_navi_navipoints.length;i++)
	{
		temp=$("#"+pc_navi_navipoints[i]);
		temp.bind("click",pc_navi_Accordion_onClick);
		if (!temp.hasClass("active")) //first, lets deactivate every non-specifically activated navigation and hide it quick!
		{
			//check if children are active
			hasActiveChildren=pc_navi_Accordion_checkActiveChildren(temp);
			
			if (hasActiveChildren==false) //Close the accordion, if no children are active!			
				pc_navi_Accordion_close(temp,2);
		}
	}
}

//the onClick event for an navigation point
function pc_navi_Accordion_onClick(){
	//close every navigation point
	for (i=0;i<pc_navi_navipoints.length;i++)
	{
		temp=$("#"+pc_navi_navipoints[i]);
		pc_navi_Accordion_close(temp,750);
	}
	
	//open the navigation point
	pc_navi_Accordion_open($(this));								
}


//check if a navigation point has an active children navigation point, so that the navigation would remain open
function pc_navi_Accordion_checkActiveChildren(toCheck)
{
	tempHasActive=false;
	//check all child navi points of the 'toCheck' navipoint if it is active
	$.each($("#box_"+toCheck.attr("id")).children(),function(){
		if ($(this).hasClass("active"))
			tempHasActive=true;
	});
	
	if (tempHasActive==true)
		hasActive=true; //set global info, so that we know, the user is on a page and the navigation is active
	
	return tempHasActive;
}

//Opens a Navigation point as default, if no other navigation point is already active
function pc_navi_Accordion_openAsDefault(obj)
{
	if (hasActive!=true) //check if a pre
		pc_navi_Accordion_open(obj);
}

//opens a navigation point with the accordion effect
function pc_navi_Accordion_open(obj)
{
	if (obj.hasClass("inactive")) //make sure this isn't already open
	{
		obj.removeClass("inactive");
		obj.unbind("click"); //remove clickable navigation point, so that always one navigation point remains open
		
		//open content
		objContent=$("#box_"+obj.attr("id"));
		objContent.removeClass("invisible");
		objContent.slideDown(750);
	}
}
//closes a navigation point with the accordion effect
function pc_navi_Accordion_close(obj,duration)
{
	if (!obj.hasClass("inactive")) //make sure this isn't already closed
	{
		obj.addClass("inactive");
		obj.bind("click",pc_navi_Accordion_onClick); //add clickable navigation point, so that this navigation point can be opened again
		
		//close content
		objContent=$("#box_"+obj.attr("id"));
		if ($.browser.msie && parseInt($.browser.version)==7)
		{
			//setTimeout("objContent.addClass('invisible')", duration);
			objContent.slideUp(duration).addClass('invisible');			
		}	
		else //all other normal browsers
			objContent.slideUp(duration,function(){$(this).addClass("invisible")});
	}
}