﻿var active_flag=false
var actual_position=0
var sequence=1
var direction
		
		
function $(div){return document.getElementById(div)}
	
function initIndex()
{
	$('button-left').onclick=function(){if(!active_flag){direction=1;index.move()}}
	$('button-right').onclick=function(){if(!active_flag){direction=0;index.move()}}
		
	for (i=1; i<url_array.length+1; i++)
	{
		$('port-link-'+i).innerHTML='<a href="http://'+url_array[i-1]+'" target="_blank">visit this website</a> <span class="rarr">&rarr;</span><span class="ie6">&gt;&gt;</span>'
	}
}

function initNews()		
{
	if ($('newsreader'))
	{
		b=$('newsreader').getElementsByTagName('div')
		for (var i=0; i<3; i++)
		{
			b[i].onclick = function()
			{
				if (!ajax_flag)
				{
					ajax_flag=true
					newsreader_content.className='newsreader-loading'
					newsreader_content.innerHTML=''
					$('one').className='news-link';$('two').className='news-link';$('three').className='news-link'
					$(this.getAttribute('id')).className='news-link-active'
					ajaxGet('proxy.php?q=' + this.getAttribute('id') + '&action=news&rand=' + Math.random()*999)
				}
			}
		}
			
		$('next-news').onclick=function(){news_count++;news_count==10?news_count=0:'';headline()}
		$('previous-news').onclick=function(){news_count--;news_count==-1?news_count=9:'';headline()}	
	}
}

function headline()
{
	news_array[news_count]?newsreader_content.innerHTML=news_array[news_count]:newsreader_content.innerHTML='Sorry, this channel is temporarily offline'
	$('showing').innerHTML='showing headline '+(news_count+1)+' of 10'
}

function initWeb()
{
	content_travel = 390
	scrollbar_travel = 290 

	drag_flag=false
	dragY=0
	scroll_bar=$('scrollbar')
	scroll_bar.style.top='0px'
	scroll_div=$('glossary').style
	content_height=$('glossary').offsetHeight
		
	function moveHandler(e)
	{
		if(e==null){e=window.event}
		if(e.button<=1&&drag_flag)
		{
			if((e.clientY-dragY)>-1&&(e.clientY-dragY)<(scrollbar_travel+1))
			{
				target.style.top=e.clientY-dragY+'px'
				var docY=0-(e.clientY-dragY)*(content_height-content_travel)/scrollbar_travel
				scroll_div.top=docY+"px"
				return false
			}
		}
	}

	function cleanup(e)
	{
		document.onmousemove=null
		document.onmouseup=null
		dragOK=false
	}

	function dragHandler(e)
	{
		if(e==null){e=window.event}
		target=e.target!=null?e.target:e.srcElement
		if(target.className=='scrollbar')
		{
			drag_flag=true
			dragY=e.clientY-parseInt(scroll_bar.style.top)
			document.onmousemove=moveHandler
			document.onmouseup=cleanup
			return false
		}
	}
	document.onmousedown=dragHandler		
}

function XHR(method)
{
	http_request=false
	if(window.XMLHttpRequest)
	{
		http_request=new XMLHttpRequest()
		if(method=='post') {if(http_request.overrideMimeType){http_request.overrideMimeType('text/html')}}
	}
	else if(window.ActiveXObject)
	{
		try {http_request=new ActiveXObject('Msxml2.XMLHTTP')}
		catch(e)
		{
			try {http_request=new ActiveXObject('Microsoft.XMLHTTP')}
			catch(e){}
		}
	}
	else return false
}

function ajaxGet(url)
{
	XHR('get')
	http_request.onreadystatechange=function() 
	{
		if (http_request.readyState==4 && http_request.status==200)
		{
			ajax_flag=false
			news_array=http_request.responseText.split("|")
			newsreader_content=$('newsreader-content')
			newsreader_content.className=''
			news_count=0
			headline()
		}
	}
	http_request.open('GET', url, true)
	http_request.send(null)
}

var index={scroll_div:'portfolio-inner', number_items:5, total_scroll:3296, total_increment:102, total_threshhold:3253, indiv_scroll:824, indiv_increment:82, indiv_threshhold: 809, move:move}

function move()
{	
	scroller_images=$(this.scroll_div).style
	active_flag=true
	actual_scroll=0
	init_acc=true	
		
	if ((sequence==1 && direction==1)||(sequence==this.number_items && direction==0))
	{
		if (direction==1){sequence=this.number_items;direction=0}
		else{sequence=1;direction=1}
		scroll_distance=this.total_scroll; increment=this.total_increment;threshhold=this.total_threshhold
	}
				
	else
	{
		direction==1?sequence--:sequence++
		scroll_distance=this.indiv_scroll;increment=this.indiv_increment;threshhold=this.indiv_threshhold
	}

	timer=setInterval(scroll, 20)
	
	function scroll()
	{
		if (actual_scroll<scroll_distance)
		{	
			if (init_acc)
			{
				actual_scroll--
				direction==1?actual_position--:actual_position++
				actual_scroll==-10?init_acc=false:''
			}
			else
			{
				if (actual_scroll<threshhold)
				{
					actual_scroll+=increment
					direction==1?actual_position+=increment:actual_position-=increment
				}
				else
				{
					actual_scroll+=1
					direction==1?actual_position++:actual_position--				
				}
			}

			scroller_images.left=actual_position+'px'
		}
		else 
		{
			clearInterval(timer)
			active_flag=false
			$('port-'+sequence).innerHTML='<img idth="375" height="300" alt="" src="portfolio/'+portfolio_array[sequence-1]+'" />'
			$('counter').className='slide-counter slide-'+sequence
		}		
	}	
}





