var t;
//live chat stuff
// var ConsSrv = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
var chathandle;

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
// ConsSrv.logStringMessage('here is my logs.. Hello!!!!');

function checkNewChatMessages()
{
  clearTimeout(chathandle);
  var send = document.createElement('script');
  send.setAttribute('src','http://proc1.alloyfish.com/chat/ping.php?site='+window.location.hostname+'&nodelete=1&sender='+readCookie('visitor'));
  document.getElementsByTagName('head')[0].appendChild(send);
  chathandle = setTimeout("checkNewChatMessages()",5000);
}
//----------------

function growprogress(object,ptarget)
{
	var start=0;
	document.getElementById("completion_"+object).style.width=start;
	t=setTimeout("dogrowprogress("+start+",'"+object+"',"+ptarget+")",40);
}
function dogrowprogress(lc,obj,ptarget)
{
	if (document.getElementById("completion_"+obj)===false) alert(obj);
	if (document.getElementById("completion_"+obj)) document.getElementById("completion_"+obj).style.width=lc+'px';
	if (lc<ptarget) t=setTimeout("dogrowprogress("+parseInt((lc+((ptarget-lc)*0.1))+1)+",'"+obj+"',"+ptarget+")",40);
}
function ajax(command)
{
	document.body.className='loading';
	var scriptElem = document.createElement('script');
	var newpath = '?byajax='+escape(command)+'&'+Math.floor(Math.random()*999999999);
// 	top.location=newpath;
	scriptElem.setAttribute('src',newpath);
	document.getElementsByTagName('head')[0].appendChild(scriptElem);
	return false;
}
window.onload=ajaxify;
// window.onload=function(){alert('this here onload')};

function ajaxify()
{
	
	var linkList = document.getElementsByTagName('a');
	var linkLen = linkList.length;
	var ajaxInlineFormat = new RegExp(/([^\,]*)\,([^\,]+)/);
	var myText;
	for (i = 0; i < linkLen; i++)
		{
		var tryMatch = ajaxInlineFormat.exec(linkList[i].href);
		if (tryMatch!=null)
			{
			var checkValid = false;
			if (tryMatch[1]=="http://www.alloyfish.com/?")
				{
				if (top.location.href=="http://www.alloyfish.com/") checkValid = true;
				}
			else 
				{
				if (top.location.href.indexOf(tryMatch[1])!=-1) checkValid = true;
				}
			if (checkValid)
				{
				linkList[i].onclick= new Function("return ajax('"+tryMatch[2]+"');");
				if (typeof(linkList[i].textContent) == 'undefined') myText=linkList[i].innerText;
				else myText=linkList[i].textContent;
				linkList[i].setAttribute('href','magic://'+myText);
				}
			}
		}
}
var acceleratexfade;
function ajamagicxfade(frmd,tod)
{
	var otod=document.getElementById(tod);
	var ofrmd=document.getElementById(frmd)
	otod.style.marginTop=(-1*(14+ofrmd.offsetHeight))+'px';
	setOpacity(otod,0);
	otod.style.display='block';
	acceleratexfade=1;
	setTimeout('doxfade(\''+frmd+'\',\''+tod+'\','+0+')',10);
}

function doxfade(frmd,tod,stage)
{
	var otod=document.getElementById(tod);
	var ofrmd=document.getElementById(frmd);	
	if (acceleratexfade&&(stage>0)) {stage=1}
	if (stage>=1)
		{
		ofrmd.parentNode.innerHTML=otod.innerHTML;
		ajaxify();
		}
	else
		{
		var thresh=0.8;
		if (stage<=thresh) setOpacity(ofrmd,1-stage*(1/thresh));
		if (stage>=(1-thresh)) setOpacity(otod,(stage-thresh)*(1/thresh));
		setTimeout('doxfade(\''+frmd+'\',\''+tod+'\','+(stage+0.1)+')',20);
		}
	acceleratexfade=0;
}

function setOpacity( element, alpha ) {
// 	if (alpha==1) alpha=0.999999;
	if( element.style.opacity !== undefined) { 
		element.style.opacity = alpha;
	}
	else if( element.style.filter !== undefined ) { 
		element.style.filter = 'alpha(opacity='+(alpha*100)+')';
	}
	else if( element.filters.alpha !== undefined ) { 
		element.filters.alpha.opacity = ( alpha * 100 );
	}
	else  if ( element.style.MozOpacity !== undefined ) {
		element.style.MozOpacity = alpha;
	}
} 

// function setOpacity(object,opacityinput) {
// var opacity=opacityinput*100;
// var objStyle = object.style;
// // IE/Win
// // alert(objStyle.opacity );
// if (objStyle.filter) objStyle.filter = "alpha(opacity:"+opacity+")"; // for opera use
// // Safari<1.2, Konqueror
// else if (objStyle.KHTMLOpacity) objStyle.KHTMLOpacity = opacity/100;
// 
// // Older Mozilla and Firefox
// else if (objStyle.MozOpacity) objStyle.MozOpacity = opacity/100;
// 
// // Safari 1.2, newer Firefox and Mozilla, CSS3
// else if (objStyle.opacity) objStyle.opacity = opacity/100;
// }

// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64( input ) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}
function setdiv_default()
{
	if(document.getElementById('branding_development') != null || document.getElementById('branding_development') != undefined)
		document.getElementById('branding_development').style.display='none';
	if(document.getElementById('programming') != null || document.getElementById('programming') != undefined)
		document.getElementById('programming').style.display='none';
// 	for(var i=1; i<=2; ++i)
// 	{
// 		var ele_id='hidden_div'+i;
// 		document.getElementById(ele_id).style.display='none';
// 	}
	
}

function project_scroll(obj_select)
{	
	var project_type = obj_select.options[obj_select.selectedIndex].value;
	switch(project_type)
	{
		case 'web_development' :
			document.getElementById('branding_development').style.display='none';
// 			document.getElementById('programming').style.display='none';
			break;
		case 'branding_development' :
			document.getElementById('web_development').style.display='none';
// 			document.getElementById('programming').style.display='none';
			break;
		case 'programming' :
			document.getElementById('web_development').style.display='none';
			document.getElementById('branding_development').style.display='none';
			break;
	}

// 	document.getElementById(project_type).style.display='block';
	
	show_ing(project_type);
	
}

function show_ing(ele_id)
{
	if(document.getElementById(ele_id))
	{
		document.getElementById(ele_id).style.display='block';
// 		document.getElementById(ele_id).style.opacity=0;
// 		document.getElementById(ele_id).style.filter = alpha(opacity=0);
	}

// 	show_opacity(ele_id, 0);
}

function show_opacity(ele_id, now_opacity)
{
	var cur_ele = document.getElementById(ele_id);
	setOpacity( cur_ele, now_opacity );
	if(now_opacity < 1)
	{
		now_opacity = now_opacity + 0.01;
		setTimeout('show_opacity('+ele_id+', '+now_opacity+')', 10);
	}
	else
	{
		now_opacity = 1;
		setOpacity( cur_ele, now_opacity );
	}
	
		
}