// pickers

function photoPicker(handler, retstring) {
	// retstring gets passed back to handler function
	// format: handlerfunction(photoid, filename, width, height, ret1, ret2 etc)
	if(retstring) {
		var nwstr = "&"+retstring;
	} else {
		var nwstr = "";
	}
	var script = OWN_ADDRESS+"/"+SYS_DIR+"/modules/photo/ajax/ajax.photopicker.php?pid="+PID+"&lang="+LANG+"&handler="+handler+nwstr;
	var fhandler = showPhotoPicker;
	var args = new Array();
	ajaxSendGet(script, fhandler, args);
}

function showPhotoPicker(args) {
	var pickPop = standardPop("phpick", "popbase_r", "popbar_r", "Kies een foto:", true);
	pickPop.fill(args["response"]);
	pickPop.setWidth(800);
	pickPop.center();
	pickPop.show();
	pickPop.center();
}

function albumPicker(handler, retstring, mult, selected) {
	// retstring gets passed back to handler function
	// format: handlerfunction(albumid, ret1, ret2 etc)
	var nwstr = "";
	
	if(retstring) {
		nwstr += "&"+retstring;
	}
	if(mult) {
		nwstr += "&multi=true";
	}
	if (selected) {
		nwstr += "&selected="+selected;
	}

	var script = OWN_ADDRESS+"/"+SYS_DIR+"/modules/photo/ajax/ajax.albumpicker.php?pid="+PID+"&lang="+LANG+"&handler="+handler+nwstr;
	var fhandler = showAlbumPicker;
	var args = new Array();
	ajaxSendGet(script, fhandler, args);
}

function selectMultiAlbumsFromPikcer(availalbums) {
	
	var albums = availalbums.split(",");
	var selalbums = "";
	for (var i=0; i<albums.length; i++) {
		
		if (document.getElementById("selectmultialbums"+albums[i]).checked == true) {
			if (selalbums.length > 0)
				selalbums += ",";
			selalbums += albums[i];
		}
	}
	
	return selalbums;
}

function showAlbumPicker(args) {
	var pickPop = standardPop("alpick", "popbase_r", "popbar_r", "Kies een album:", true);
	pickPop.fill(args["response"]);
	pickPop.setWidth(400);
	pickPop.center();
	pickPop.show();
	pickPop.center();
}

function ph_scrollAlbum(but, divid, side) {
	var ddiv = document.getElementById(divid);
	//but.src = 'images/remote/btn_up_down<? echo $ext; ?>';
	but.onmouseup = function() {
		clearInterval(but.intv);
		// but.src = 'images/remote/btn_up_up<? echo $ext; ?>';
	}
	but.onmouseout = function() {
		clearInterval(but.intv);
		// but.src = 'images/remote/btn_up_up<? echo $ext; ?>';
	}
	if(side == 'left') {
		but.intv = setInterval("document.getElementById('"+divid+"').scrollLeft-=10", 100);
	} else {
		but.intv = setInterval("document.getElementById('"+divid+"').scrollLeft+=10", 100);
	}
}

// album view
function ph_albumView(album, pic, event, info, dmap) {

	var kpop;
	if(kpop = document.getElementById("ph_alview_"+album)) {
		kpop.destroy();
	}
	/*
	var wmleft = false;
	var wmtop = false;
	if(event) {
		var wmleft = getMouseX(event);
		var wmtop = getMouseY(event);
	}
	*/
	var ds = (dmap)?dmap:0;
	var binfo = (info)?'1':'0';
	var gets = "pid="+PID+"&lang="+LANG+"&album="+album+"&pic="+pic+"&info="+binfo+"&dmaps="+ds;
	var script = OWN_ADDRESS+"/"+SYS_DIR+"/modules/photo/ajax/ajax.albumview.php?"+gets;
	var handler = ph_showAlbumView;
	var args = new Array();
	args["album"] = album;
	/*
	args["wmtop"] = wmtop;
	args["wmleft"] = wmleft;
	*/
	ajaxSendGet(script, handler, args);			
}

function ph_showAlbumView(args) {
	
	unablePage("ph_alview_"+args["album"]);
	
	var viewer = dPopup("ph_alview_"+args["album"], "document_");
	viewer.innerHTML = "<div style='background-color: white; width: 500px;'>"+args["response"]+"</div>";
	viewer.style.zindex = 8;
	viewer.center();
	viewer.style.display = "block";
	viewer.center();

	/*
	var cw = (getWindowWidth() / 2);
	var bw = (viewer.offsetWidth / 2);
	viewer.style.left = (cw - bw) + "px";

	var Btop = 200;
	
	if(Btop < 0) {
		viewer.style.top = "0px";
	} else {
		viewer.style.top = Btop + "px";
	}
	*/

	$("ph_albumviewer_slideshow").ready( function() {
		ph_albumViewer_move();
		//viewer.center();
		document.getElementById("ph_albumviewer_close").style.display = "block";
		document.getElementById("ph_albumviewer_photo_name").style.color = "#555555";
			
		document.getElementById("ph_albumviewer_close").onclick = function() {
			enablePage("ph_alview_"+args["album"]);			
			viewer.destroy();
		}
	});
}

function ph_albumViewer_moveLeft() {
	var curPos = document.getElementById("p_albumviewer_curpos").value -0;
	var maxPos = document.getElementById("p_albumviewer_maxpos").value -0;
	if (curPos-1 >= 0)
		curPos--;
	else
		curPos = maxPos-1;
	document.getElementById("p_albumviewer_curpos").value = curPos;
	ph_albumViewer_move();
}

function ph_albumViewer_moveRight() {	
	var curPos = document.getElementById("p_albumviewer_curpos").value -0;
	var maxPos = document.getElementById("p_albumviewer_maxpos").value -0;
	if (curPos+1 < maxPos)
		curPos++;
	else
		curPos = 0;
	document.getElementById("p_albumviewer_curpos").value = curPos;
	ph_albumViewer_move();
}


function ph_showPicturePopup(url, width, height) {

	unablePage("ph_picview");

	if (width > 800)
		width = 800;

	var viewwidth = (width-1) + 5;
	
	var viewer = dPopup("ph_picview", "document_");
	viewer.setWidth(viewwidth);
	viewer.innerHTML = "<div id='ph_picview_close' style='background-color: white; padding: 2px;'><img src='"+ url+"' style='width: " + width + "px;'><div style='text-align: right; padding: 6px;'><span style='cursor: pointer; color: #555555; font-weight: bold;'>Close</span></div></div>";
	viewer.style.zindex = 8;
	viewer.style.display = "block";

	var cw = (getWindowWidth() / 2);
	var bw = (viewer.offsetWidth / 2);
	viewer.style.left = (cw - bw) + "px";

	var Btop = 100;
	
	if (Btop < 0) {
		viewer.style.top = "0px";
	} else {
		viewer.style.top = Btop + "px";
	}

	document.getElementById("ph_picview_close").onclick = function() {
		enablePage("ph_picview");			
		viewer.destroy();
	}
}


function ph_albumViewer_move() {
	document.getElementById("ph_albumviewer_slideshow").style.display = "block";
	var curPos = document.getElementById("p_albumviewer_curpos").value-0;
	var maxPos = document.getElementById("p_albumviewer_maxpos").value-0;
	var slideWidth = document.getElementById("p_albumviewer_swidth").value;
	var thumbWidth = document.getElementById("p_albumviewer_twidth").value;
	for (var i=0; i<maxPos; i++) {
		document.getElementById("p_album_thumb_pos_"+i).style.borderColor = "#AAAAAA";
		document.getElementById("p_album_thumb_pos_"+i).bgColor = "#AAAAAA";
	}
	document.getElementById("p_album_thumb_pos_"+curPos).style.borderColor = SYS_COLORS[0]["value"];
	document.getElementById("p_album_thumb_pos_"+curPos).bgColor = SYS_COLORS[0]["value"];
	document.getElementById("ph_albumviewer_photo_name").innerHTML = document.getElementById("ph_albumviewer_photo_name_"+curPos).value;
	$('#ph_albumviewer_slideshow').animate({'marginLeft' : (slideWidth*curPos)*-1});
	try {
		$('#ph_albumviewer_slideshow_thumbs').animate({'marginLeft' : (thumbWidth*(curPos-2))*-1});
	} catch(e) {}
}

function ph_aMouseOver(elem) {
	var ow = elem.width;
	var oh = elem.height;
	elem.style.borderWidth = '0';
	elem.style.width = (ow + 8) + 'px';
	elem.style.height = (oh + 8) + 'px';
}

function ph_aMouseOut(elem) {
	if(!elem.freshselect) {
		elem.style.borderWidth = '4px';
		elem.style.width = (elem.width - 8) + 'px';
		elem.style.height = (elem.height - 8) + 'px';
	} else {
		elem.freshselect = false;
	}
}

function ph_aBrowseOne(album, side, info) {
	var pcinp = document.getElementById("ph_sel_pic_"+album);
	var ord = document.getElementById("ph_idstring_"+album).value.split("_");
	var i, current;
	for(i = 0; i < ord.length; i++) {
		current = i;
		if(ord[i] == pcinp.value) {
			break;
		}
	}
	var nwsel;
	if(side == 'left') {
		if(ord[i - 1]) {
			nwsel = ord[i - 1];
		} else {
			nwsel = ord[ord.length - 1];
		}
	} else {
		if(ord[i + 1]) {
			nwsel = ord[i + 1];
		} else {
			nwsel = ord[0];
		}
	}
	var pdat = document.getElementById("ph_albthumb_file_"+nwsel).value.split("%");
	var mdiv = document.getElementById("ph_aview_main_"+album);
	var pname = document.getElementById("ph_albthumb_name_"+nwsel).value;
	var nimg = '<img src="'+OWN_ADDRESS+'/images/photo/previews/'+pdat[4]+'"';
	nimg += ' class="photo_albumview_main"';
	nimg += ' onclick="javascript: popMyImage(\'ph_av_'+album+'\', \''+pname+'\', \''+OWN_ADDRESS+'/images/photo/'+pdat[4]+'\');"';
	nimg += ' style="width: '+pdat[0]+'px; height: '+pdat[1]+'px;" />';

	mdiv.innerHTML = nimg;
	document.getElementById("ph_albthumb_"+nwsel).className = "photo_albumview_thumb_sel";
	document.getElementById("ph_albthumb_"+pcinp.value).className = "photo_albumview_thumb";
	pcinp.value = nwsel;

	if (side == 'left') {
		if ((current-3) || (current-3 === 0))
			var newanch = current-3;
		else if ((current-2) || (current-2 === 0))
			var newanch = current-2;
		else
			var newanch = current-1;
	} else {
		if (current+3)
			var newanch = current+3;
		else if (current-2)
			var newanch = current+2;
		else
			var newanch = current+1;
	}

	if (newanch < 0)
		ph_anchorPhoto(nwsel);
	else if (newanch > ord.length)
		ph_anchorPhoto(nwsel);
	else
		ph_anchorPhoto(ord[newanch]);

	if(info) {
		var pdesc = document.getElementById("ph_albthumb_desc_"+nwsel).value;
		document.getElementById("ph_pic_name_"+album).innerHTML = pname;
		document.getElementById("ph_pic_desc_"+album).innerHTML = pdesc;
	}
}


function ph_albSelectPic(elem, id, album, info) {
	var pcinp = document.getElementById("ph_sel_pic_"+album);
	if(pcinp.value == id) {
		return false;
	}
	var pdat = document.getElementById("ph_albthumb_file_"+id).value.split("%");
	var mdiv = document.getElementById("ph_aview_main_"+album);
	var pname = document.getElementById("ph_albthumb_name_"+id).value;
	var nimg = '<img src="'+OWN_ADDRESS+'/images/photo/previews/'+pdat[4]+'"';
	nimg += ' class="photo_albumview_main"';
	nimg += ' onclick="javascript: popMyImage(\'ph_av_'+album+'\', \''+pname+'\', \''+OWN_ADDRESS+'/images/photo/'+pdat[4]+'\');"';
	nimg += ' style="width: '+pdat[0]+'px; height: '+pdat[1]+'px;" />';

	elem.className = "photo_albumview_thumb_sel";
	elem.style.borderWidth = '4px';
	elem.style.width = (elem.width - 8) + 'px';
	elem.style.height = (elem.height - 8) + 'px';
	elem.freshselect = true;

	mdiv.innerHTML = nimg;
	document.getElementById("ph_albthumb_"+pcinp.value).className = "photo_albumview_thumb";
	pcinp.value = id;

	if(info) {
		var pdesc = document.getElementById("ph_albthumb_desc_"+id).value;
		document.getElementById("ph_pic_name_"+album).innerHTML = pname;
		document.getElementById("ph_pic_desc_"+album).innerHTML = pdesc;
	}
	
	ph_anchorPhoto(pcinp.value);
}

function ph_anchorPhoto(who) {
	document.location.hash = "#ph_albthumb_"+who;
	document.location.hash = '#anchor_photo_album_viewer';
}

function lightbox(win, type){
	var links=$('a[rel^=lightbox_'+win+']');
	var overlay=$(jQuery('<div id="overlay" style="display: none"></div>'));
	var container=$(jQuery('<div id="lightbox" style="display: none"></div>'));
	var close=$(jQuery('<a href="#close" class="close">&times; Close</a>'));
	var target=$(jQuery('<div class="target"></div>'));
	if(!type){
		var prev=$(jQuery('<a href="#prev" class="prev"><img style="border:0px;" src="'+OWN_ADDRESS+'/'+SYS_DIR+'/modules/photo/css/images/lb_left.png"></a>'));
		var next=$(jQuery('<a href="#next" class="next"><img style="border:0px;" src="'+OWN_ADDRESS+'/'+SYS_DIR+'/modules/photo/css/images/lb_right.png"></a>'));	
	}	
	$('body').append(overlay).append(container);
	container.append(close).append(target).append(prev).append(next);
	container.show().css({'top':Math.round((($(window).height()>window.innerHeight?window.innerHeight:$(window).height())- container.outerHeight())/2)+'px','left':Math.round(($(window).width()- container.outerWidth())/2)+'px','margin-top':0,'margin-left':0}).hide();
	close.click( function( c ) {
		c.preventDefault();
		overlay.add( container ).fadeOut( 'normal' );
	});
	if(!type){
		prev.add( next ).click( function( c ) {
			c.preventDefault();
			var current = parseInt( links.filter( '.selected' ).attr( 'lb-position' ),10 );
			var to = $( this ).is( '.prev' ) ? links.eq( current - 1 ) : links.eq( current + 1 );
			if( !to.size() ) {
				to = $( this ).is( '.prev' ) ? links.eq( links.size() - 1 ) : links.eq( 0 );
			}
			if(to.size()){
				to.click();
			}
		});
	}
	links.each( function( index ) {
		var link = $( this );
		link.click( function( c ) {
			c.preventDefault();
			if(link.attr( 'href' ).search('\\youtube')>=0){
				open( link.attr( 'href' ), 'yt' );
			}else{
				open( link.attr( 'href' ) );
			}
			links.filter( '.selected' ).removeClass( 'selected' );
			link.addClass( 'selected' );
		} );
		link.attr( {'lb-position': index} );
	} );
	var open = function( url, content_type ) {
		if( container.is( ':visible' ) ) {
			target.children().fadeOut( 'normal', function() {
				target.children().remove();
				if(content_type){
					target.html(url);
				}else{
					loadImage( url );
				}
			} );
		} else {
			target.children().remove();
			overlay.add( container ).fadeIn( 'normal',function(){
				if(content_type=="mixed"){
					target.html(url);
				}
				else if(content_type=="yt"){
					//content ='<iframe width="560" height="315" src="'+url+'" frameborder="0" allowfullscreen></iframe>';
					var content = '</br><object style="vertical-align:text-bottom;height: 360px; width: 600px"><param name="movie" value="'+url+'"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="'+url+'" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="600" height="360"></object>';
					target.html(content);
				}else{
					loadImage( url );
				}
			} );
		}
	}
	var loadImage=function(url){
		if(container.is('.loading')){return;}
		container.addClass('loading');
		var img=new Image();
		img.onload=function(){img.style.display='none';
		var maxWidth=($(window).width()- parseInt(container.css('padding-left'),10)- parseInt(container.css('padding-right'),10))- 100;
		var maxHeight=(($(window).height()>window.innerHeight?window.innerHeight:$(window).height())- parseInt(container.css('padding-top'),10)- parseInt(container.css('padding-bottom'),10))- 100;
		if(img.width>maxWidth||img.height>maxHeight){
			var ratio=img.width/img.height;
			if(img.height>=maxHeight){
				img.height=maxHeight;
				img.width=maxHeight*ratio;
			}else{
				img.width=maxWidth;img.height=maxWidth*ratio;
			}
		}
		container.animate({'width':img.width,'height':img.height,'top':Math.round((($(window).height()>window.innerHeight?window.innerHeight:$(window).height())- img.height- parseInt(container.css('padding-top'),10)- parseInt(container.css('padding-bottom'),10))/2)+'px','left':Math.round(($(window).width()- img.width- parseInt(container.css('padding-left'),10)- parseInt(container.css('padding-right'),10))/2)+'px'},'normal',function(){target.append(img);$(img).fadeIn('normal',function(){container.removeClass('loading');});})}
		img.src=url;
	}
}
