var bncProductinfoToevoegenPanel;
var bncProductDialog;

function addbncProductPanels(){
    bncProductinfoToevoegenPanel = new YAHOO.widget.Panel("bncProductinfoToevoegenPanel", {
        width: "400px",
        visible: false,
        draggable: true,
        close: true,
        fixedcenter: true
    });
    bncProductinfoToevoegenPanel.render(document.body);
	
    bncProductDialog = new YAHOO.widget.Panel("bncProductDialog", {
        width: "400px",
        visible: false,
        draggable: true,
        close: true,
        fixedcenter: true
    });
    bncProductDialog.render(document.body);
	
}

YAHOO.util.Event.onDOMReady(addbncProductPanels);

function bncProductinfoToevoegen(parentUrl){
    document.getElementById('bncProductinfoParentUrl').value = parentUrl;
    bncProductinfoToevoegenPanel.show();
}

function bncProductinfoRedirect(){
    bncProductinfoToevoegenPanel.hide();
    var geselecteerdeKeuze = document.getElementById('bncProductinfoToevoegenBox').options[document.getElementById('bncProductinfoToevoegenBox').selectedIndex].value;
    if (geselecteerdeKeuze == null) {
        alert('Je hebt nog geen keuze gemaakt met wat je wil toevoegen.');
    }
    if (geselecteerdeKeuze == "0") {
        document.location.href = document.getElementById('bncProductinfoParentUrl').value + "?func=add;class=WebGUI::Asset::File;prototype=0rQbIWYiE8Bnj-YYkHJ_OA;proceed=viewParent";
    }
    if (geselecteerdeKeuze == "1") {
        document.location.href = document.getElementById('bncProductinfoParentUrl').value + "?func=add;class=WebGUI::Asset::Wobject::Layout";
    }
    if (geselecteerdeKeuze == "2") {
        document.location.href = document.getElementById('bncProductinfoParentUrl').value + "?func=add;class=WebGUI::Asset::Redirect;proceed=viewParent";
    }
    if (geselecteerdeKeuze == "3") {
        document.location.href = document.getElementById('bncProductinfoParentUrl').value + "?func=add;class=WebGUI::Asset::Wobject::Collaboration;prototype=AZnJ7MAwA7C09T1icqJFoQ";
    }	
}

function bncProductToevoegen(productnaam){
	if (productnaam == "") {
		alert('Productnaam moet worden ingevuld.');
	}
	else {
		bncProductDialog.hide();
		momentDialog.show();
//		var callback = {
//			success: bncProductGetLatestWikiPage,
//			failure: bncProductOnFailure,
//			argument: {
//				productnaam: productnaam
//			}
//		};
//		var now = new Date();
//		YAHOO.util.Connect.asyncRequest("GET", "/bedrijfsoplossingen?func=deployPackage;assetId=VyQ559ebloTp9rpb-JXrZQ&noCache=" + now.getTime(), callback, null);
		bncAjaxConnection({
			url:"/bedrijfsoplossingen?func=deployPackage;assetId=VyQ559ebloTp9rpb-JXrZQ",
			method:'GET',
			success: bncProductGetLatestWikiPage,
			failure: bncProductOnFailure,
			argument: {productnaam: productnaam}
		});
	}
}

function bncProductGetLatestWikiPage(o) {
//		var callback = {
//			success: bncProductChangeName,
//			failure: bncProductOnFailure,
//			argument: {
//				productnaam: o.argument.productnaam
//			}
//		};
//		var now = new Date();
//		YAHOO.util.Connect.asyncRequest("GET", "/framework/assets/producten/getlatestwikipage?noCache=" + now.getTime(), callback, null);
	bncAjaxConnection({
		url:"/framework/assets/producten/getlatestwikipage",
		method:'GET',
		success: bncProductChangeName,
		failure: bncProductOnFailure,
		argument: {
			productnaam: o.argument.productnaam
		}
	});
}

function bncProductChangeName(o){
	var response = eval("("+o.responseText+")");
//    var callback = {
//        success: bncProductFlushCache,
//        failure: bncProductOnFailure,
//        argument: {
//            productnaam: o.argument.productnaam,
//			producturl: response.url
//        }
//    };
//    var now = new Date();
//    YAHOO.util.Connect.asyncRequest("GET", "/framework/assets/producten/changelatestwikipage?naam="+o.argument.productnaam+"&assetId="+response.assetId+"&noCache=" + now.getTime(), callback, null);
	  bncAjaxConnection({
			url:"/framework/assets/producten/changelatestwikipage?naam="+o.argument.productnaam+"&assetId="+response.assetId,
			method:'GET',
			success: bncProductFlushCache,
	        failure: bncProductOnFailure,
	        argument: {
	            productnaam: o.argument.productnaam,
				producturl: response.url
	        }
		});
}

function bncProductFlushCache(o) {
//    var now = new Date();
//    var callback = {
//        success: bncProductChangeUrls,
//        failure: bncProductOnFailure,
//		argument: {
//            productnaam: o.argument.productnaam,
//			producturl: o.argument.producturl		
//		}
//    };
//    YAHOO.util.Connect.asyncRequest("GET", "?op=flushCache&noCache=" + now.getTime(), callback, null);
	  bncAjaxConnection({
			url:"?op=flushCache",
			method:'GET',
			 success: bncProductChangeUrls,
		        failure: bncProductOnFailure,
				argument: {
		            productnaam: o.argument.productnaam,
					producturl: o.argument.producturl		
				}
		});
}

function bncProductChangeUrls(o){
//    var now = new Date();
//    var callback = {
//        success: bncProductSuccess,
//        failure: bncProductOnFailure,
//		argument: {
//			producturl: o.argument.producturl
//		}
//    };
//    var formObject = document.getElementById('bncProductChangeUrlsForm');
//    YAHOO.util.Connect.setForm(formObject);
//    YAHOO.util.Connect.asyncRequest('POST', '/'+o.argument.producturl + '?noCache=' + now.getTime(), callback);
	bncAjaxConnection({
		url:"/"+o.argument.producturl,
		formid:'bncProductChangeUrlsForm',
		method:'POST',
		 success: bncProductSuccess,
	        failure: bncProductOnFailure,
			argument: {
				producturl: o.argument.producturl
			}
	});
}

function bncProductSuccess(o) {
	momentDialog.hide();
	document.location.href="/"+o.argument.producturl;
}

function bncProductOnFailure(o) {
	alert('Er is iets misgegaan: Foutcode:'+o.statusText);
}

