var photoId = '';
var colorType = '';
var photoIdRm = '';
var $ = function(id){ return document.getElementById(id); };
var NORMAL = 0;
var ORDER = 1;
var EDIT = 2;
var thumbList = new Array();
var apiUrl = '/index.php?option=com_oypo&';
function focusText(){
	var id = 'searchText';
	if($(id).value == 'Zoek gala:'){
		$(id).value = '';
	}
	else{
		if($(id).value.length == 0){
			$(id).value = "Zoek gala:";
		}
	}
}

function loadPhotoAlbum(){
	$('indicator').style.display = 'block';
	YAHOO.util.Connect.asyncRequest('GET',apiUrl+'task=getfolders&format=xml', {
		success:function(o){
			$('thumbContainer').innerHTML = o.responseText;
			$('indicator').style.display = 'none';
		}
	});	
}

function showPhoto(id, type, status){
	photoId = id;
	colorType = type;
	switch(status){
		case 0:break;
		case 1:
			$('deleteLink').style.visibility='visible';
			photoIdRm = photoId;
			break;
		case 2:break;
	}
	highlightSelected();
	if($('t'+id) != null) { $('t'+id).style.backgroundColor='#ff0000'; }
	$('indicator2').style.display = 'block';
	YAHOO.util.Connect.asyncRequest('GET', apiUrl+'task=showphoto&id='+id+colorType, {
		success:function(o){
			$('mainPhoto').innerHTML = o.responseText;
			$('indicator2').style.display = 'none';
			YAHOO.util.Connect.asyncRequest('GET', apiUrl+'task=showphoto_id&id='+id+colorType, {
				success:function(o2){
					$('imageId').innerHTML = o2.responseText;
				}
			});
			
		}
	});
	return false;
}

function loadPage(id, map, total){
	$('indicator2').style.display = 'block';
	YAHOO.util.Connect.asyncRequest('GET',apiUrl+'task=loadpage&id='+id+'&map='+map+'&total='+total, {
		success:function(o){
			$('photoList').innerHTML = o.responseText;
			$('indicator2').style.display = 'none';
			setNavPhoto();
			YAHOO.util.Connect.asyncRequest('GET',apiUrl+'task=loadpagenav&id='+id+'&map='+map+'&total='+total, {
				success:function(o2){
					$('thumNav').innerHTML = o2.responseText;
					highlightSelected();
				}
			});	
		}
	});
}

function activeLink(id){
	for(var i=1; i < 4; i++) $('editLink'+i).className='editNormal';
	$(id).className = 'editActive';
	if(id == 'editLink1'){
		colorType = '';
	}
	else if(id == 'editLink2'){
		colorType = '|zw';
	}
	else if(id == 'editLink3'){
		colorType = '|sepia';
	}

}

function addOrder(){
	if(photoId.length > 0){
		var img = photoId + colorType;
		var sc = readCookie('shopcart');
		if(sc == null || sc.length==0){
			YAHOO.util.Connect.asyncRequest('GET',apiUrl+'task=getshopcart', {
				success:function(o){
					var sc = o.responseText;
					createCookie('shopcart',sc,'');
					YAHOO.util.Connect.asyncRequest('GET',apiUrl+'task=addorder&photoid='+img, {
						success:function(o2){
							$('frameOrder').innerHTML = o2.responseText;
							getOrderTotal();
							highlightSelected();
							var sc = readCookie('shopcart');
							$('pixxerframe0').src = 'http://www.oypo.nl/winkelwagen/?cart='+sc;
//							$('pixxerframe0').src = urlOypo+'winkelwagen/?cart='+sc;
							activeLink('editLink1');
						} 
					});
				} 
			});	
		}
		else{
			YAHOO.util.Connect.asyncRequest('GET',apiUrl+'task=addorder&photoid='+img+'&shopcart='+sc, {
				success:function(o2){
					$('frameOrder').innerHTML = o2.responseText;
					getOrderTotal();
					highlightSelected();
					$('pixxerframe0').src = 'http://www.oypo.nl/winkelwagen/?cart='+sc;
//					$('pixxerframe0').src = urlOypo+'winkelwagen/?cart='+sc;
					activeLink('editLink1');
				} 
			});
		}
	}
	$('frameOrder').style.display='block';
	return false;
}

function deleteOrder(){
	if(photoIdRm.length > 0){
		var img = photoIdRm;
		YAHOO.util.Connect.asyncRequest('GET',apiUrl+'task=deleteorder&photoid='+img, {
			success:function(o2){
				$('frameOrder').innerHTML = o2.responseText;
				$('deleteLink').style.visibility='hidden';
				photoIdRm = '';
				getOrderTotal();
				highlightSelected();
			} 
		});
	}
	return false;
}

function getOrderTotal(){
	var elements = YAHOO.util.Dom.getElementsByClassName('thumbOrder', 'div'); 
	if(elements.length == 1){
		$('totalSelect').innerHTML = elements.length + ' foto geselecteerd.';
	}
	else{
		$('totalSelect').innerHTML = elements.length + ' foto\'s geselecteerd.';
	}
}

function highlightSelected(){
	var thumbs = YAHOO.util.Dom.getElementsByClassName('listThumb', 'div'); 
	for(var i=0; i < thumbs.length; i++){
		if($(thumbs[i].id) != null) { $(thumbs[i].id).style.backgroundColor=''; }
	}
	var elements = YAHOO.util.Dom.getElementsByClassName('thumbOrder', 'div'); 
	for(var i=0; i < elements.length; i++){
		var t = elements[i].id.replace('tmb_', '');
		var t2 = t.replace('|zw', '');
		var t3 = t2.replace('|sepia', '');
		if($('t'+t3) != null) { $('t'+t3).style.backgroundColor='#ff0000'; }
	}
}

function setNavPhoto(){
	thumbList = new Array();
	var elements = YAHOO.util.Dom.getElementsByClassName('lThumb', 'img');
	for(var i=0; i < elements.length; i++){
		thumbList.push(elements[i].id);
	}
}
function getPhotoPrev(){
	var iF = 0;
	for(var i=0; i < thumbList.length; i++){
		if(thumbList[i] == photoId){
			iF = i-1;
			if(iF >= 0) showPhoto(thumbList[iF], '', NORMAL);
			return true;
		}
	}
	return false;
}
function getPhotoNext(){
	var iF = 0;
	for(var i=0; i < thumbList.length; i++){
		if(thumbList[i] == photoId){
			iF = i+1;
			if(iF < thumbList.length) showPhoto(thumbList[iF], '', NORMAL);
			return true;
		}
	}
	return false;
}

function saveOypoUser(){
	var oypoId = document.getElementById('oypoUsr').value;
	var schoolId = document.getElementById('schoolId')[document.getElementById('schoolId').selectedIndex].value;
	var study = document.getElementById('studyDesc').value;
	var year = document.getElementById('yearGrad')[document.getElementById('yearGrad').selectedIndex].value;
		YAHOO.util.Connect.asyncRequest('GET','/index.php?option=com_oypo&task=newsletter_user&oypo_user='+oypoId+'&schoolid='+schoolId+'&study='+study+'&graduate_year='+year+'&receives=1', {
			success:function(o){
			}
		});	
}
function deleteOypoUser(){
	var oypoId = document.getElementById('oypoUsr').value;
		YAHOO.util.Connect.asyncRequest('GET','/index.php?option=com_oypo&task=newsletter_user_del&oypo_user='+oypoId, {
			success:function(o){
			} 
		});	
}