var selectedPhotoId;

function selectPhotoActions(){
	if (selectedPhotoActions){
		for(var i=0; i < selectedPhotoActions.length; i++){
			var obj = document.getElementById(selectedPhotoActions[i]);
			if (obj){
				obj.className = 'action';
			}
		}
	}
}

function selectTreeItem(id, newStyle, oldStyle) {
    if (selectedId!=null){
		document.getElementById(selectedId).className = oldStyle;
	}
	selectedId = id;
	if (!document.getElementById(selectedId)) return;

	document.getElementById(selectedId).className = newStyle;

    selectActions();
}

function selectimg(idim){
	if (selectedPhotoId != null) {
		if (document.getElementById("id_" + selectedPhotoId)) document.getElementById("id_" + selectedPhotoId).className = "imglink_href";
		if (document.getElementById(selectedPhotoId)) document.getElementById(selectedPhotoId).className = "imglink";
	}
	selectedPhotoId = idim;
	if (document.getElementById("id_" + selectedPhotoId)) document.getElementById("id_" + selectedPhotoId).className = "imgselectlink_href";
	if (document.getElementById(selectedPhotoId)) document.getElementById(selectedPhotoId).className = "imgselectlink";

	selectPhotoActions();
}

function goToCategory(categoryId) {
    document.location.href="/servlet/viewcatalog?id=" + parseInt(categoryId);
}

function showImage(photoId) {
    showDialog("/servlet/viewcatalogphoto?photoId="+parseInt(photoId) + "&show_add_to_cart_button=true",640,480);
}

function showImage1(photoId) {
    showDialog("/servlet/viewcatalogphoto?photoId="+parseInt(photoId), 640, 480);
}


function showOrderImage(photoId) {
	var height = 480;
	var width = 640;
	var link = "/servlet/vieworderphoto?photoId="+parseInt(photoId);
	var photo = window.open(link,"photo","height="+height+",width="+width+",status=no,toolbar=no,menubar=no,location=no, scrollbars=yes, resizable=yes");
	if (photo) photo.focus();
}


function view_photo_album(){
	alert('View Photo Album');
}
function view_category(){
	alert('View Category');
}
function view_photo(){
	alert('View Photo');
}
function add_category(){
	if (selectedId == null) {
		alert('Please, select a category.');
		return;
	}
	if (parseInt(selectedId) < 0){
		alert('You can\'t add Category!');
		return;
	}
    showDialog("/servlet/addcatalogcategory?parentId="+parseInt(selectedId),590,500);
}
function edit_category(){
	if (selectedId == null) {
		alert('Please, select a category.');
		return;
	}
    showDialog("/servlet/editcatalogcategory?id="+parseInt(selectedId),590,500);
}
function delete_category(){
	if (selectedId == null) {
		alert('Please, select a category.');
		return;
	}
	if (parseInt(selectedId) < 1){
		alert('You can\'t delete this category!');
		return;
	}
	if (confirm("This will permanently delete this category from the Catalog. If the category has any subcategories, they will be deleted as well. There will not be an undo function. Are you absolutely sure you want to delete the category?")) {
        document.location.href="/servlet/deletecatalogcategory?id="+parseInt(selectedId);
    }
}
function upload_photo(){
	if (selectedId == null) {
		alert('Please, select a category.');
		return;
	}
	if (parseInt(selectedId) < 0){
		alert('You can\'t create Product!');
		return;
	}
	showDialog("/servlet/uploadcatalogphoto?categoryId="+parseInt(selectedId),600,450);
}
function replace_photo(){
	if (selectedPhotoId == null) {
		alert('Please, select a product.');
		return;
	}
	showDialog("/servlet/replacecatalogphoto?photoId=" + parseInt(selectedPhotoId),600,700);
}

function edit_attributes(){
	if (selectedPhotoId == null) {
		alert('Please, select a product.');
		return;
	}
	showDialog("/servlet/editproductattributes?photoId=" + parseInt(selectedPhotoId),550,430);
}
function edit_photo_description(){
	if (selectedPhotoId == null) {
		alert('Please, select a product.');
		return;
	}
	showDialog("/servlet/editcatalogphotodescription?photoId=" + parseInt(selectedPhotoId), 950, 670);
}
function delete_photo(){
    if (selectedPhotoId == null) {
		alert('Please, select a product.');
		return;
	}
	if (confirm("This will permanently delete this product from the Catalog. There will not be an undo function. Are you absolutely sure you want to delete the product?")){
		document.location.href="/servlet/deletecatalogphoto?id=" + parseInt(selectedId) + "&photoId=" + parseInt(selectedPhotoId);
	}
}

function viewDescription(photoId) {
    showDialog("/servlet/viewcatalogdescription?id=" + parseInt(photoId),500,300);
}

var addtocart = new Image();
addtocart.src = "/images/modules/productcatalog/addtocart_over.gif";
function cartover(img, i){
	if (document.images) {
		img.src = addtocart.src;
	}
}
function cartout(img, i){
	if (document.images) {
		img.src = "/images/modules/productcatalog/addtocart.gif";
	}
}

function changeProductsImageSize(){
	if (document.images){
		for (var i=0; i<document.images.length; i++){
			var img = document.images[i];
			if ( (img.src.indexOf('/images/productcatalog/') != -1) || (img.src.indexOf('/images/categoryproductcatalog/') != -1) ){
                if ((img.width > 0) && (img.height > 0)){
                	if (img.height > img.width) img.height = 120;
                	else img.width = 120;
                }
			}
		}
	}
}

function addToCart(productId){
	var f = document.forms["form_" + productId];
	if (f) f.submit();
}

