// JavaScript Document
// AssetBasket for image gallery

function assetInit(){
	
	if (document.getElementById('galleryframe') || document.getElementById('galleryframebig')){
		// replace all the nodes with className assetadd
		var addLinks = getElementsByClassName('assetadd');
		for(var n=0;n<addLinks.length;n++){
			new basketAdd(addLinks[n]);
		}
		
		var delLinks = getElementsByClassName('assetdelete');
		for(var n=0;n<delLinks.length;n++){
			new basketRemove(delLinks[n]);
		}
		
	}
	
}

function basketAdd(a){
	this.link = a;
	this.link.onclick = function(){ new doAddAnimation(this);doAJaxLink(this.href);return false;};
}

function doAddAnimation(a){
	this.link = a;
	this.img  = this.link.parentNode.parentNode.getElementsByTagName('img')[0];
	// clone the image node
	this.clone = this.img.cloneNode(true);
	document.body.appendChild(this.clone);
	this.clone.style.position = 'absolute';
	var pos = getPos(this.img);
	this.clone.style.top = pos[1] + 'px';
	this.clone.style.left = pos[0] + 'px';
	// create new fader AND fx object
	var posto = findPos(document.getElementById('assetbasket'));
	var difx = posto[1] - pos[1];
	var dify = posto[0] - pos[0];
		
	this.anim = new joinkFX(this.clone, {time:30,animation:'outQuintic',style:'top,left,width,height',start:pos[1] + ',' + pos[0] + ',99,99',distance:difx + ',' + dify + ',-88,-88'});
	this.anim.setFunction(bind(this, this.removeAddAnimation));
	this.anim.move();
	
}

doAddAnimation.prototype = {
	removeAddAnimation : function(){
		document.body.removeChild(this.clone);
	}
}



 function getPos(o) {
	var curleft = curtop = 0;
	if (o.offsetParent) {
		curleft = o.offsetLeft
		curtop = o.offsetTop
		while (o = o.offsetParent) {
			curleft += o.offsetLeft
			curtop += o.offsetTop
		}
	}
	return [curleft,curtop];
 } 

function basketRemove(a){
	this.link = a;
	this.link.onclick = function(){
							var rem = window.confirm('Delete this image?');
							if (rem){
								 doAJaxRemove(this.href);
								 var li = this.parentNode.parentNode.parentNode;
								 var ul = li.parentNode;
								 if (ul && li){
									ul.removeChild(li);
								 }
								 // somehow trigger the onmouseout function
								 var out = this.onmouseout
								 if (out){
								 	out();
								 }
							}
							 return false;
						};
}

var ajax = null;

function doAJaxLink(href){
	ajax = new joinkJAH();
	ajax.setFunction(__callback_add2basket);
	ajax.doRequest(href + '&ajax=true', 'GET');
}

function doAJaxRemove(href){
	ajax = new joinkJAH();
	ajax.setFunction(__callback_removeFromBasket);
	ajax.doRequest(href + '&ajax=true', 'GET');
}

function __callback_add2basket(){
	
	//window.alert('Response from page:\n---------\n' + ajax.response); 
	
}

function __callback_removeFromBasket(){
	var cnt = ajax.response;
	if (cnt == 0){
		// change innerHTML from 
		document.getElementById('galleryframebig').parentNode.innerHTML = '<div id="galleryframe"><div class="asseterror">nothing in your basket...</div></div>';
	}
}

function getElementsByClassName(c, p){
 	var f = [];
 	var e = [];
 	var r = new RegExp(c);
 	if (p){
	 	if (p.tagName){
	 		e = document.getElementsByTagName('a')
	 	}else if (p.parent){
	 		_$$($(p.parent), e);
	 	}else{
	 		_$$(document, e);
	 	}
 	}else{
 		_$$(document, e);
 	}
 	for (var n=0;n<e.length;n++){
 		if (r.test(e[n].className)){
 			f.push(e[n]);
 		}
 	}
 	if (f.length > 0){
 		return f;
 	}else{
 		return false;
 	}
 }
 
 // getChildNodes from o and push in array a
 function _$$(o, a){
 	if (typeof o == 'object'){
	 	var x = o.childNodes;
		for (var n=0;n<x.length;n++){
			a.push(x[n]);
			_$$(x[n], a);
	 	}
 	}
 }
 
 function checkUserName(){
 	ajax = new joinkJAH();
 	var username = document.getElementById('username').value;
 	ajax.setFunction(__callback_checkUserName);
 	ajax.doRequest('/imagegallery/checklogin.php?loginid=' + username + '&ajax=true', 'GET');
 }
 
 function __callback_checkUserName(){
 	var an = ajax.response;
 	var inp= document.getElementById('username');
	var sbm= document.getElementById('submit');
	var err= document.getElementById('erruser');
 	if (an == 'true'){
 		inp.className = 'error';
		sbm.disabled = true;
		err.innerHTML = 'login is prohibited, please use a different login';
 	}else{
 		inp.className = '';
		sbm.disabled = false;
		err.innerHTML = '&nbsp;';
 	}
 }
  
 function assetAlterHeight(id){
 	// get the init width / height:
 	var init_width = parseInt(document.getElementById('defwidth_' + id).value);
 	var init_height = parseInt(document.getElementById('defheight_' + id).value);
 	// get the new width:
	var new_width = parseInt(document.getElementById('width_' + id).value);
 	// get the height input
 	var height_input = document.getElementById('height_' + id);
 	if (new_width < init_width && new_width != "0"){
 		if (height_input){
 			height_input.value =  Math.floor((parseInt(init_height) / parseInt(init_width)) * new_width);
 		}
 	}else {
 		document.getElementById('width_' + id).value = init_width;
 		//height_input.value = Math.floor((parseInt(init_height) / parseInt(init_width)) * init_width);
		height_input.value = init_height;
 	}
 	
 }
 
 function assetAlterWidth(id){
 	// get the init width / height:
 	var init_width = parseInt(document.getElementById('defwidth_' + id).value);
 	var init_height = parseInt(document.getElementById('defheight_' + id).value);
 	// get the new width:
	var new_height = parseInt(document.getElementById('height_' + id).value);
 	// get the height input
 	var width_input = document.getElementById('width_' + id);
 	if (new_height < init_height && new_height != "0"){
 		if (width_input){
 			width_input.value =  Math.floor((parseInt(init_width) / parseInt(init_height)) * new_height);
 		}
 	}else {
 		document.getElementById('height_' + id).value = init_height;
 		//width_input.value = (parseInt(init_width) / parseInt(init_height)) * init_height;
		width_input.value = init_width;
 	}
 }
 
 function toggleAdvancedSearch(obj,id){
	 var $link = obj;
	 var $advs = parseInt(document.getElementById(id).value);
	 if($advs == 1){ 
	 	$link.innerHTML = 'advanced search';
	 	document.getElementById(id).value = 0;
		document.getElementById('adv_1').style.display = 'none';
		document.getElementById('adv_2').style.display = 'none';
		document.getElementById('adv_3').style.display = 'none';
		document.getElementById('simple_1').style.display = 'block';
		document.getElementById('simple_2').style.visibility = 'visible';
	 }
	 else{ 
	 	$link.innerHTML = 'simple search';
	 	document.getElementById(id).value = 1;
		document.getElementById('adv_1').style.display = 'block';
		document.getElementById('adv_2').style.display = 'block';
		document.getElementById('adv_3').style.display = 'block';
		/*
		document.getElementById('simple_1').style.display = 'none';
		document.getElementById('simple_2').style.visibility = 'hidden';
		*/
	 }
 }
