/**Quepasa Corporation**/
/**AppsGlobal.js**/
/**version 1.0 by valdemar**/
/*
 * Los elementos con Ids de la estructura qp.appPreview.id.* y con clases
 * qp.appPreview.class.* interactuan con jQuery y pueden o no tener display:none
 */
if(window.qp===undefined){window.qp={};}
$.extend(true,window.qp,{
	'appPreview':{
    'id':{
       'previewArea':'app_preview',
       'infoArea':'app_info',
       'showPreviewBtn':'app_preview_btn',
       'hidePreviewBtn':'app_nopreview_btn',
       'previewFrame':'preview_iframe'
    },
    'clazz':{
    },
    'frameTempSrc':'t_src',
    'frameTempElem':'t_frame'
  },
'appAdd':{
	    'id':{
           'ajaxArea':'app_message',
           'appAddedMsg':'app_message_0',
           'notLoggedMsg':'app_message_1',
           'userNotFoundMsg':'app_message_2',
           'appNotFoundMsg':'app_message_3',
           'appDuplicatedMsg':'app_message_4'
        },
	    'clazz':{
        	'message':'app-message',
        	'appAddBtn':'app-add-btn'
        },
        'actionUrl':'/apps/actions/appadd.php'
      },
'appDel':{
    'id':{
       'ajaxArea':'app_message',
       'appDeletedMsg':'app_message_0',
       'notLoggedMsg':'app_message_1',
       'userNotFoundMsg':'app_message_2',
       'appNotFoundMsg':'app_message_3',
       'deleteAppWarn':'delete_warning'
    },
    'clazz':{
    	'message':'app-message',
    	'deletedApp':'app-del',
    	'rmvAppBtn':'remove-app-btn',
    	'deletedLabel':'deleted-label'
    },
    'actionUrl':'/apps/actions/appremove.php'
  }
});

function togglePreviewApplication() {
	$('#'+qp.appPreview.id.showPreviewBtn).toggle();
	$('#'+qp.appPreview.id.hidePreviewBtn).toggle();
	$('#'+qp.appPreview.id.infoArea).toggle();
	$('#'+qp.appPreview.id.previewArea).toggle();
	buildPreviewFrame();
}
function buildPreviewFrame() {
	tempFrame=$(qp.appPreview.frameTempElem);
	if(tempFrame) {
		iframe=document.createElement('iframe');
		$(iframe).attr('id',tempFrame.attr('id'))
		.attr('width',tempFrame.attr('width'))
		.attr('height',tempFrame.attr('height'))
		.attr('frameborder',tempFrame.attr('frameborder'))
		.attr('src',tempFrame.attr(qp.appPreview.frameTempSrc));
		tempFrame.parent().append(iframe);
		tempFrame.remove(); 
	}
}
function addApplicationFromDirectory(numId,elemId) {
	$('#'+elemId).addClass(qp.appAdd.clazz.appAddBtn);
	addApplication(numId);
}
function addApplication(numId) {
	$.ajax({
		type:'GET',
		url: qp.appAdd.actionUrl,
		data: 'id='+numId,
		success:function(data, textStatus){addApplicationSuccess(data, textStatus, numId)},
		dataType: 'json',
		error:addApplicationError,
		complete:addApplicationComplete
	});

}
function addApplicationComplete(XMLHttpRequest, textStatus) {
}
function addApplicationError(XMLHttpRequest, textStatus, errorThrown) {
	$('#'+qp.appAdd.id.appNotFoundMsg).show();
	$('.'+qp.appAdd.clazz.appAddBtn).hide();
}
function pulsateMsg(strClass){
	$('.'+strClass+':visible').effect("pulsate",{times:1},600);
}
function addApplicationSuccess(data, textStatus, numId) {
	$('.'+qp.appAdd.clazz.message).hide();
        var redirectUrl= '/apps/appview.php?id='+numId+'&own='+userId;
	switch(data.status) {
	case 0: window.location.href=redirectUrl;break;
	case -1: $('#'+qp.appAdd.id.notLoggedMsg).show();break;
	case -2: $('#'+qp.appAdd.id.appNotFoundMsg).show();break;
	case -3: $('#'+qp.appAdd.id.userNotFoundMsg).show();break;
	case -4: window.location.href=redirectUrl;break;
	default: $('#'+qp.appAdd.id.appNotFoundMsg).show();break;
	}
	pulsateMsg(qp.appAdd.clazz.message);
	$('.'+qp.appAdd.clazz.appAddBtn).hide();
}
function delApplicationFromDirectory(numId,elemId) {
	$('#'+elemId).addClass(qp.appDel.clazz.deletedApp);
	delApplication(numId);
}
function delApplication(numId) {
	strWarn = $('#'+qp.appDel.id.deleteAppWarn).html();
	if(confirm(strWarn))
	$.ajax({
		type:'GET',
		url: qp.appDel.actionUrl,
		data: 'id='+numId,
		success:delApplicationSuccess,
		dataType: 'json',
		error:delApplicationError,
		complete:delApplicationComplete
	});
}
function delApplicationComplete(XMLHttpRequest, textStatus) {
	$('.'+qp.appDel.clazz.deletedApp).removeClass(qp.appDel.clazz.deletedApp);
}
function delApplicationError(XMLHttpRequest, textStatus, errorThrown) {
	$('#'+qp.appDel.id.appNotFoundMsg).show();
}
function delApplicationSuccess(data, textStatus) {
	$('.'+qp.appDel.clazz.message).hide();
	switch(data.status) {
	case 0: $('#'+qp.appDel.id.appDeletedMsg).show();break;
	case -1: $('#'+qp.appDel.id.notLoggedMsg).show();break;
	case -2: $('#'+qp.appDel.id.appNotFoundMsg).show();break;
	case -3: $('#'+qp.appDel.id.userNotFoundMsg).show();break;
	default: $('#'+qp.appDel.id.appNotFoundMsg).show();break;
	}
	pulsateMsg(qp.appDel.clazz.message);
	deletedApp = $('.'+qp.appDel.clazz.deletedApp);
	$('.'+qp.appDel.clazz.deletedLabel,deletedApp).show();
	$('.'+qp.appDel.clazz.rmvAppBtn,deletedApp).hide();
	deletedApp.fadeTo('fast',0.33);
	$('a',deletedApp).click(function(){return false;});	
	$('.'+qp.appDel.clazz.rmvAppBtn,deletedApp).attr('onclick','').click(function(){return false;});
}