var imgArray;
var curindex;
var swtch = 1;
var flag = 1;
window.onload = prepLinks;


function prepLinks(){
	if (!document.getElementsByTagName){ 
		return false;
	}
	
	if (document.getElementById("movement")){
		var movement = document.getElementById("movement");
		var plusminus = movement.getElementsByTagName("a");
		for (var x=0; x < plusminus.length; x++){
			plusminus[x].onclick = function(){
				return changeImg(this.href);
			}
		}
	}
	
	var url = document.URL;
	var urlitems = url.split("/");
	if (url.match('www.') != null){
		makeRequest("http://www.apparatchikdesign.com/xml_images.php?cat="+urlitems[4]+"");
	} else {
		makeRequest("http://apparatchikdesign.com/xml_images.php?cat="+urlitems[4]+"");
	}
}

function unLink(){
	if (document.getElementById("movement")){
		var movement = document.getElementById("movement");
		var plusminus = movement.getElementsByTagName("a");
		for (var x=0; x < plusminus.length; x++){
			plusminus[x].onclick = function(){
				return true;
			}
		}
	}
}

function changeImg(url){
	//URL looks like http://rja-design.com/images/portfolio/14/
	try {
	url = url.replace("http://apparatchikdesign.com/images/", "");
	
	var items = url.split("/");
	//we're interested in index 1
	var index = items[1];
	
	var name = imgArray[index].firstChild.data;
	
	var picholder = document.getElementById('picholder');
	var main = document.getElementById('main');
	} catch (err){
		alert(err.description)
	}
	try {
		if (flag == 1){
			flag = 0;
			if (swtch == 1){
				var newimg = new Image();
				newimg.onload = function(){	
					var pic = document.createElement("img");
					pic.setAttribute("id", "main2");
					pic.setAttribute("src", "images/"+name+"");
					pic.setAttribute("alt", "");
					setOpacity(pic, 0);
					picholder.appendChild(pic);
					fadein("main2", 0);
					fadeout("main", 100);
				}
				newimg.src = "images/"+name+"";
				swtch = 2;	
			} else {
				var newimg2 = new Image();
				newimg2.onload = function(){	
					var pic2 = document.createElement("img");
					pic2.setAttribute("id", "main");
					pic2.setAttribute("src", "images/"+name+"");
					pic2.setAttribute("alt", "");
					setOpacity(pic2, 0);
					picholder.appendChild(pic2);
					fadein("main", 0);
					fadeout("main2", 100);
				}
				newimg2.src = "images/"+name+"";
				swtch = 1;	
				
			}
			curindex = index;
			updateNav(curindex);
		}
	} catch (err){
		alert(err.description);
		return false;
	}
	return false;
	
}

function updateNav(number){
	var movement = document.getElementById("movement");
	var plusminus = movement.getElementsByTagName("a");
	var category = "portfolio";
	theindex = number;
	theindex = theindex*1;
	var next = theindex+1;
	var prev = theindex-1;
	
	if (theindex == imgArray.length-1){
		next = 0;
	}  
	if (theindex == 0){
		prev = 	imgArray.length-1
	}
	
	plusminus[0].setAttribute("href", "http://apparatchikdesign.com/images/"+category+"/"+next+""); 
	plusminus[1].setAttribute("href", "http://apparatchikdesign.com/images/"+category+"/"+prev+"");
	
	updateCounter(next);
}

function updateCounter(number){
	var thumbs = document.getElementById("thumbs");
	var counters = thumbs.getElementsByTagName("img");
	
	if (number == 0){
		number = counters.length;
	}
	
	var l=1;
	for (var n=0; n < counters.length; n++){
		
		if (n == number-1){
			counters[n].src = "/images/other/on.jpg";
		}
		
		if (n != number-1 && counters[n].src.match("/images/other/on.jpg") != null ){
			counters[n].src = "/images/other/off.jpg";
		}
		
		
		
		l++;
		if (l == 7){
			l = 1;
		}
	}
}

function makeRequest(url) {
        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            unLink();
            return false;
        }
        http_request.onreadystatechange = function() { alertContents(http_request); };
        http_request.open('GET', url, true);
        http_request.send(null);

    }
    
function alertContents(http_request) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
               	var xmldoc = http_request.responseXML;
				imgArray = xmldoc.getElementsByTagName('image');
				/*alert(imgArray[0].firstChild.data);*/
            } else {
                alert('There was a problem with the request.');
            }
        }

    }
    
function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function fadein(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		
		if (opacity == 0){
			obj.style.visibility = "visible";
		}
		
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadein('"+objId+"',"+opacity+")", 100);
		} else if (opacity > 100){
			flag = 1;
		}
	}
}

function fadeout(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity == 0){
			obj.style.visibility = "hidden";
			var picholder = document.getElementById('picholder');	
			var trash = picholder.getElementsByTagName("img");
			for (var d=0; d<trash.length; d++){
				if (trash[d].getAttribute('id') == objId){
					picholder.removeChild(trash[d]);
				}
			}			
		}
		
		if (opacity >= 0) {
			setOpacity(obj, opacity);
			opacity = opacity - 10;
			window.setTimeout("fadeout('"+objId+"',"+opacity+")", 100);
		}
	}
}