// JavaScript Document
var zooming = false;
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var showStatus = new Array();

function divZoomInStart(startX, startY, showDiv) {
	$('textBg').style.display = 'none';
	if(zooming) {
		setTimeout(divZoomInStart, 20, startX, startY, showDiv);
		return;
	}
	zooming = true;
	var timesLeft = 20;
	if($('aniDiv') != null) {
		document.body.removeChild($('aniDiv'));
	}
	var aniDiv = document.createElement('div');
	aniDiv.id = 'aniDiv';
	aniDiv.style.width = '1px';
	aniDiv.style.height = '1px';
	aniDiv.style.left = startX + 'px';
	aniDiv.style.top = startY + 'px';
	aniDiv.showDiv = showDiv;
	document.body.appendChild(aniDiv);
	var target = $(showDiv);
	var endX = parseFloat(target.offsetLeft);
	var endY = parseFloat(target.offsetTop);
	var endW = parseFloat(target.offsetWidth);
	var endH = parseFloat(target.offsetHeight);
	var dX = (endX - startX) / timesLeft;
	var dY = (endY - startY) / timesLeft;
	var dW = (endW - 1) / timesLeft;
	var dH = (endH - 1) / timesLeft;
	var str = "divZoom(" + dX + "," + dY + "," + dW + "," + dH + "," + timesLeft + ");";
	setTimeout(str, 0);
	
}

function divZoomInEnd() {
	zooming = false;
	$('textBg').style.display = '';
	if($('aniDiv') != null) {
		$($('aniDiv').showDiv).style.visibility = 'visible';
		document.body.removeChild($('aniDiv'));
	}
}

function divZoomOutStart(endX, endY, hideDiv, time) {
	
	$('textBg').style.display = 'none';
	if(zooming) {
		setTimeout(divZoomOutStart, 20, endX, endY, hideDiv);
		return;
	}
	zooming = true;
	var timesLeft = 20;
	if($('aniDiv') != null) {
		document.body.removeChild($('aniDiv'));
	}
	var target = $(hideDiv);
	var startX = parseFloat(target.offsetLeft);
	var startY = parseFloat(target.offsetTop);
	var startW = parseFloat(target.offsetWidth);
	var startH = parseFloat(target.offsetHeight);
	var aniDiv = document.createElement('div');
	aniDiv.id = 'aniDiv';
	aniDiv.style.width = startW + 'px';
	aniDiv.style.height = startH + 'px';
	aniDiv.style.left = startX + 'px';
	aniDiv.style.top = startY + 'px';
	aniDiv.showDiv = hideDiv;
	document.body.appendChild(aniDiv);
	var dX = (endX - startX) / timesLeft;
	var dY = (endY - startY) / timesLeft;
	var dW = (1 - startW) / timesLeft;
	var dH = (1 - startH) / timesLeft;
	var str = "divZoom(" + dX + "," + dY + "," + dW + "," + dH + "," + timesLeft + ");";
	$(hideDiv).style.visibility = 'hidden';
	setTimeout(str, 0);
}

function divZoomOutEnd() {
	$('textBg').style.display = '';
	zooming = false;
	if($('aniDiv') != null) {
		document.body.removeChild($('aniDiv'));
	}
}

function divZoom(deltaX, deltaY, deltaW, deltaH, timesLeft) {
	var target = $('aniDiv');
	var curX = parseFloat(target.style.left.substr(0, target.style.left.length - 2));
	var curY = parseFloat(target.style.top.substr(0, target.style.top.length - 2));
	var curW = parseFloat(target.style.width.substr(0, target.style.width.length - 2));
	var curH = parseFloat(target.style.height.substr(0, target.style.height.length - 2));
	target.style.left = curX + deltaX + 'px';
	target.style.top = curY + deltaY + 'px';
	target.style.width = curW + deltaW + 'px';
	target.style.height = curH + deltaH + 'px';
	timesLeft--;
	if(timesLeft > 0) {
		var str = "divZoom(" + deltaX + "," + deltaY + "," + deltaW + "," + deltaH + "," + timesLeft + ");";
		setTimeout(str, 0);
	} else {
		if(deltaH > 0) {
			divZoomInEnd();
		} else {
			divZoomOutEnd();
		}
	}
}

function absLeft(element) {
	var p = element;
	var left = 0;
	while(p.offsetParent != null) {
		left += p.offsetLeft;
		p = p.offsetParent;
	}
	return left;
}

function absTop(element) {
	var p = element;
	var top = 0;
	while(p.offsetParent != null) {
		top += p.offsetTop;
		p = p.offsetParent;
	}
	return top;
}

function startZoomFrom(element, target) {
	var left = absLeft(element);
	var top = absTop(element);
	divZoomInStart(left, top, target);
}

function startZoomTo(element, target) {
	var left = absLeft(element);
	var top = absTop(element);
	divZoomOutStart(left, top, target);
}

function show(id) {
	$(id).style.display = '';
}

function hide(id) {
	$(id).style.display = 'none';
}

function hideAll() {
	imgShowClose();
	for(var i = 1;i < 10;i++) {
		if($('contentDiv' + i) != null) {
			$('contentDiv' + i).style.visibility = 'hidden';
		}
	}
}

function setDefault(id) {
	switch(id) {
		case 'contentDiv1':
			$('aboutus1').style.display = '';
			$('aboutus2').style.display = 'none';
			$('aboutus3').style.display = 'none';
			$('firstLink').className = 'current';
			$('link1').className = '';
			$('link2').className = '';
			$('link3').className = '';
			$('link4').className = '';
			$('link5').className = '';
			break;
		case 'contentDiv2':
			$('firstLink').className = '';
			$('link1').className = 'current';
			$('link2').className = '';
			$('link3').className = '';
			$('link4').className = '';
			$('link5').className = '';
			break;
		case 'contentDiv3':
			$('services1').style.display = '';
			for(var i = 2; i <= 26; i++) {
				$('services' + i).style.display = 'none';
			}
			$('firstLink').className = '';
			$('link1').className = '';
			$('link2').className = 'current';
			$('link3').className = '';
			$('link4').className = '';
			$('link5').className = '';
			break;
		case 'contentDiv4':
			$('quote1').style.display = '';
			$('quote2').style.display = 'none';
			$('firstLink').className = '';
			$('link1').className = '';
			$('link2').className = '';
			$('link3').className = 'current';
			$('link4').className = '';
			$('link5').className = '';
			break;
		case 'contentDiv5':
			$('firstLink').className = '';
			$('link1').className = '';
			$('link2').className = '';
			$('link3').className = '';
			$('link4').className = 'current';
			$('link5').className = '';
			break;
		case 'contentDiv6':
			$('firstLink').className = '';
			$('link1').className = '';
			$('link2').className = '';
			$('link3').className = '';
			$('link4').className = '';
			$('link5').className = 'current';
			hide('buyourworks2');
			show('buyourworks1');
	}
}

function sendQuote() {
	var name = $('quoteName').value;
	var company = $('quoteCompany').value;
	var email = $('quoteEmail').value;
	var lookingFor = $('quoteFor').value;
	var text = $('quoteText').value;
	if(name.length == 0) {
		alert('You must leave your name.');
		$('quoteName').focus();
		return;
	}
	if(email.length == 0) {
		alert('You must leave your email address.');
		$('quoteEmail').focus();
		return;
	}
	if(text.length == 0) {
		alert('You must tell us something about your project.');
		$('quoteText').focus();
		return;
	}
	var x = new myAjax('XML');
	var postStr = 'name=' + encodeURIComponent(name) + '&company=' + encodeURIComponent(company) + '&email=' + encodeURIComponent(email) + '&lookingFor=' + encodeURIComponent(lookingFor) + '&text=' + encodeURIComponent(text);
	x.post('ajax.php?action=sendquote', postStr, updateUI);
}

function updateUI(xml) {
	/*
	var sendquote = xml.getElementsByTagName('sendquote');
	if(sendquote.length > 0) {
		var sendResult = parseInt(sendquote[0].firstChild.data.toString());
		if(sendResult) {
			hide('quote1');
			show('quote2');
		}
	}
	*/
	var imgList = xml.getElementsByTagName('list');
	if(imgList.length > 0) {
		imgList = imgList[0];
		var type = imgList.getAttribute('type');
		var prev = imgList.getAttribute('prev');
		var next = imgList.getAttribute('next');
		var list = $(type + 'Preview');
		while(list.firstChild != null) {
			list.removeChild(list.firstChild);
		}
		var imgs = imgList.getElementsByTagName('img');
		for(var i = 0; i < imgs.length; i++) {
			var width = imgs[i].getAttribute('width');
			var height = imgs[i].getAttribute('height');
			var file = imgs[i].firstChild.data.toString();
			var newP = document.createElement('div');
			newP.className = 'preview';
			newP.innerHTML = '<img src="' + type + '/' + file + '" width="50" height="35" />';
			list.appendChild(newP);
		}
		if(prev == '1') {
			$(type + 'Prev').style.visibility = 'visible';
		} else {
			$(type + 'Prev').style.visibility = 'hidden';
		}
		if(next == '1') {
			$(type + 'Next').style.visibility = 'visible';
		} else {
			$(type + 'Next').style.visibility = 'hidden';
		}
	} else {
		hide('quote1');
		show('quote2');
	}
}

function updateList(type, page) {
	var x = new myAjax('XML');
	x.get('ajax.php?action=updatelist&type=' + type + '&page=' + page, updateUI);
}

function showImage(img) {
	showStatus = new Array();
	for(var i = 1; i <= 10; i++) {
		if($('contentDiv' + i) != null) {
			showStatus.push(new Array('contentDiv' + i, $('contentDiv' + i).style.visibility));
			$('contentDiv' + i).style.visibility = 'hidden';
		}
	}
	$('imgShowDiv').style.visibility = 'visible';
	var x = new myAjax('HTML');
	$('imgShowContent').innerHTML = '';
	x.get('preview.php?file=' + img.src, showImageContent);
}

function showImageContent(s) {
	$('imgShowContent').innerHTML = s;
}

function imgShowClose() {
	for(var i = 0; i < showStatus.length; i++) {
		$(showStatus[i][0]).style.visibility = showStatus[i][1];
	}
	showStatus = new Array();
	$('imgShowDiv').style.visibility = 'hidden';
}

function previewScrollNext(id) {
	if($(id).scrollHeight - 32 - $(id).scrollTop >= 32) {
		$(id).scrollTop += 32;
	}
}

function previewScrollPrev(id) {
	$(id).scrollTop -= 32;
}

function playSwf(swfFile, width, height) {
	$('swfArea').innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="' + width + '" height="' + height + '"><param name="movie" value="flash/' + swfFile + '" /><param name="quality" value="high" /><embed src="flash/' + swfFile + '" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"></embed></object>';
	$('swfDiv').style.left = (document.body.clientWidth - width) / 2 + 'px';
	$('swfDiv').style.top = (window.getHeight() - height - 30) / 2 + 'px';
	$('swfDiv').style.visibility = 'visible';
}

function closeSwf() {
	$('swfArea').innerHTML = '';
	$('swfDiv').style.visibility = 'hidden';
}