// JavaScript Document
function get_items(){
	var boxes = album_images.elements["image_id[]"]
	
	if(boxes.length > 1 || boxes.length == 0){
		// Do Nothing
	}else{
		boxes[0] = boxes
	}
	
	return boxes
}

function checked_items(){
	var boxes = new Array();
	items = get_items()
	
	for(i=0;i<items.length;i++){
		if(items[i].checked == true){
			boxes[boxes.length] = items[i]
		}
	}
	
	return boxes
}

function select_items(sel){
	items = get_items()
	for(i=0;i<items.length;i++){
		items[i].checked = sel
	}
}

function checked_ids(){
	var ids = "";
	items = checked_items()
	
	for(i=0;i<items.length;i++){
		ids += ids==""? items[i].value : ","+items[i].value
	}
	
	return ids;
}

function do_images(obj,func){
	var args = do_images.arguments
	
	var custom = (args.length > 2)? '&custom='+args[2] : '';
	
	var chk_items = checked_items()
	if(chk_items.length == 0){
		alert("Nothing is selected.")
	}else{
		obj.innerHTML = '<font color="red"><b>Loading..</b></font>'
		VitaminXP.Ajax.RequestUrl(obj,"pages/publicalbum/ajax_admin.php","func="+func+"&ids="+checked_ids()+custom,"POST")
	}
}

function OpenPhoto(url,w,h){
    h+=40;
	var le=(screen.availWidth/2)-(w/2)
	var to=(screen.availHeight/2)-(h/2)
	setup='toolbar=no,location=no,directories=no,status=no,left='+le+',top='+to+',menubar=no,width='+w+',height='+h
	setup += 'scrollbars=no,resizable=no'
	n = w+'x'+h
	window.open(url,n,setup)
}

function preview_photo(obj){
	file_name = obj.value
	extention = file_name.substr(file_name.length-4,4).toLowerCase()
	
	img = document.getElementById("preview_img")
	img.style.display = 'none'
	
	if(extention == ".jpg"){
		upload_form.upload_submit.disabled = false
		img2 = document.getElementById("temp_img")
		loading_img = document.getElementById("loading_img")
		loading_img.style.display = ''
		img2.src = "file:///" + file_name
		img.src = "file:///" + file_name
		setTimeout('adjust_img_size(img,img2)',1000);
	}else{
		obj.value = ""
		upload_form.upload_submit.disabled = true
		alert("Invalid file type ''"+extention+"''. Must be .JPG (JPEG format).\nSee [?] for uploading tips.")
	}
	if(!isIE){
		alert('Can\'t preview image because you are using FireFox browser which doesn\'t allow this. But you can still upload image. Just enter a name of your picture and press Submit.')
	}
}

function adjust_img_size(img,img2){
	max_size = 300
	
	width = parseInt(img2.width)
	height = parseInt(img2.height)
	
	if(width > max_size){
		new_width = max_size
		height = (max_size * height) / width
		width = new_width
	}
	if(height > max_size){
		new_height = max_size
		width = (max_size * width) / height
		height = new_height
	}
	
	img.style.width = width
	img.style.height = height
	
	loading_img.style.display = 'none'
	img.style.display = ''
}

var temp_obj = null;
function select_image(obj){
	if(temp_obj != null){
		temp_obj.className = 'plain_picture_text';
	}
	obj.className='selected_picture_text'
	temp_obj = obj
}

// Photo Album
function do_photos(obj,func){
	var args = do_photos.arguments;
	var custom = (args.length > 2)? '&custom='+args[2] : '';
	
	var chk_items = checked_items()
	if(chk_items.length == 0 && func != 'create_album'){
		alert("Nothing is selected.")
	}else{
		obj.innerHTML = '<font color="red"><b>Loading..</b></font>'
		VitaminXP.Ajax.RequestUrl(obj,"pages/photo/ajax_admin.php","func="+func+"&ids="+checked_ids()+custom,"POST")
	}
}
