var j       = jQuery.noConflict();

/*
var sdsize  = (window.innerHeight-120);
var wsize   = window.innerWidth;

function modulesWidth() {
    if (navigator.appName.indexOf("Microsoft")!=-1) {
        winW = document.body.offsetWidth;
        winH = document.body.offsetHeight;
    } else {
        winW = window.innerWidth;
        winH = window.innerHeight;
    };
    var wmodules = j('#topmenu').width();
	if (j('#top_logo'))
		wmodules -= j('#top_logo').width();
	if (j('#top_logout'))
		wmodules -= j('#top_logout').width();
	if (j('#top_domains'))
		wmodules -= j('#top_domains').width();
	j('#top_modules').width(wmodules);
	scr = j('#top_modules #scrollable').width(wmodules -= 50);
	
	j("#scrollable").scrollable({ 
		mousewheel: true }
	);
	modulesNextPages = Math.floor((j('#top_modules #scrollable').width()/72));
	modulesPrevPages = ('-'+modulesNextPages)*1
	hideLoading()	
}

function showUpload(idgal, url) {
    if (url)
        var urlup = url
    else
        var urlup = urlsrv;

    getID('wid_upload').innerHTML = '<input type="file" name="uploadify" id="uploadify" /><div id="fileQueue"></div>';
	j("#uploadify").uploadify({
		'uploader'       : urlup+'media/lib/uploadify.swf',
		'script'         : urlup+'media/lib/uploadify.php',
		'cancelImg'      : 'images/cancel.png',
		'folder'         : idgal,
		'queueID'        : 'fileQueue',
		'auto'           : true,
		'multi'          : true,
        'scriptAccess'   : 'Always'
	});
};

function showOver(url, win, act) {
    getID('overwin').style.display = 'block';
    getID('overwin2').style.display = 'block';
    if (url) {
        var contenido = j("#overwin2");
        var data = new Object();
        contenido.load(url,data,function(){
        	if (act) {
				var llamada = j("#"+act).attr('class');
				eval(llamada);
        	}
        });
    };
};

function closeOver() {
    document.getElementById('overwin').style.display = 'none';
    document.getElementById('overwin2').style.display = 'none';
    document.getElementById('overwin2').innerHTML = '';
};

function notShow(var1) {
    var contenido = j("#div_notshow");
    var data = new Object();
    contenido.load('scripts/dialog.php?show='+var1,data,function(){ });
};
function dynamicShow(showno) {
    if (showno == 1)
        document.getElementById('dynamicdata').style.display = 'block';
    else
        document.getElementById('dynamicdata').style.display = 'none';


}

function delTag(id_service, id_data, dtype, id_tag) {
    var contenido = j("#tag"+id_data+"_"+id_tag);
    var data = new Object();
    contenido.load('scripts/deltag.php?sname='+sname+'&s='+id_service+'&d='+id_data+'&dt='+dtype+'&t='+id_tag,data,function(){ });
    getID("tag"+id_data+"_"+id_tag).style.display = 'none';
};

function addTag(id_service, id_data, tag, tagname) {
  if (tagname) {
    j.get('scripts/addtag.php?sname='+sname+'&s='+id_service+'&d='+id_data+'&tag='+tagname, function(html) {
        j("#tags_show").append(html);
        getID('newtag_text').value              = '';
        getID('newtag_select').value            = '';
        getID('newtag_text').style.display      = 'none';
        getID('newtag_addbut').style.display    = 'none';
        getID('newtag_select').style.display    = 'block';
    });
  } else {
    if (tag == '-') {
        getID('newtag_select').style.display    = 'none';
        getID('newtag_text').style.display      = 'block';
        getID('newtag_addbut').style.display    = 'block';
    } else {
        getID('newtag_select').style.display    = 'block';
        getID('newtag_text').style.display      = 'none';
        getID('newtag_addbut').style.display    = 'none';
        j.get('scripts/addtag.php?sname='+sname+'&s='+id_service+'&d='+id_data+'&id='+tag, function(html) {
             j("#tags_show").append(html);
        });
    };
  };
};

function resizegrid(var1) {
    var he;
    j("#slider").slider({
			value:10,
			min: 120,
			max: 240,
			slide: function(event, ui) {
                he = ((ui.value*3)/4);
                changecss('.also','width',ui.value)
                changecss('.also','height',he)
                j(".also").find('.mediaimg').stop(false,true).animate({'width':ui.value, 'height':he, 'top':'0', 'left':'0'});
			},
            change: function(event, ui) {
                he = ((ui.value*3)/4);
                var gridsave = j("#gridsave");
                var data = new Object();
                gridsave.load('services/media/savedata.php?t=grid&w='+ui.value+'&h='+he,data,function(){ });
            }
	});
    j('#slider').slider('option', 'value', var1);
}

function getID(var1) {
    return document.getElementById(var1);
};

function showLoading() {
    j('#loading').slideDown();
};

function hideLoading() {
    j('#loading').slideUp();
};

function butSize() {
    if (navigator.appName.indexOf("Microsoft")!=-1) {
        winW = document.body.offsetWidth;
        winH = document.body.offsetHeight;
    } else {
        winW = window.innerWidth;
        winH = window.innerHeight;
    };

    var butsize = null;
    butsize = winW-500;
    if (getID('labelbar')) {
        if (getID('labelbar').style.display == 'none') {
            hsizedata  = (winH-170);
            getID('databar').style.top = '171px';
        } else {
            hsizedata  = (winH-207);
            getID('databar').style.top = '207px';
        };
        if (getID('statusbar').style.display == 'block')
            hsizedata  = (hsizedata-25);
        getID('databar').style.height = hsizedata+'px';
    };
	if (butsize <= (numserv*76)) {
		getID('aleft').style.display = 'inline';
		getID('aright').style.display = 'inline';
	} else {
		getID('aleft').style.display = 'none';
		getID('aright').style.display = 'none';
    }
    if (getID('scrolbuttons'))
        getID('scrolbuttons').style.width = butsize;
    if (getID('scrolbuttons2'))
        getID('scrolbuttons2').style.width = (winW-40);
};

function floatResize() {
    if (navigator.appName.indexOf("Microsoft")!=-1) {
        winW = document.body.offsetWidth;
        winH = document.body.offsetHeight;
    } else {
        winW = window.innerWidth;
        winH = window.innerHeight;
    };
    if (getID('labelbar')) {
        if (getID('labelbar').style.display == 'none') {
            hsizedata  = (winH-176);
            getID('data_list').style.top = '171px';
            getID('data_edit').style.top = '171px';
        } else {
            hsizedata  = (winH-196);
            getID('data_list').style.top = '207px';
            getID('data_edit').style.top = '207px';
        };
        if (getID('statusbar').style.display == 'block')
            hsizedata  = (hsizedata-35);
        getID('data_list').style.height = hsizedata+'px';
        getID('data_edit').style.height = hsizedata+'px';
    };
};


function showStatusBar() {
    j('#statusbar').slideDown();
    butSize();
};

function hideStatusBar() {
    j('#statusbar').slideUp();
    getID('statusbar').innerHTML = '';
    butSize();
};
function adjustIframe() {
    document.getElementById('iload').style.height = (hsize+115)+'px';
};

defaultStep=5
step=defaultStep

function scrollDivLeft(id){
    document.getElementById(id).scrollLeft-=step
    timerLeft=setTimeout("scrollDivLeft('"+id+"')",10)
}

function scrollDivRight(id){
    document.getElementById(id).scrollLeft+=step
    timerRight=setTimeout("scrollDivRight('"+id+"')",10)
}
function scrollDivLeft2(id){
    document.getElementById(id).scrollLeft-=step
    timerLeft2=setTimeout("scrollDivLeft2('"+id+"')",10)
}

function scrollDivRight2(id){
    document.getElementById(id).scrollLeft+=step
    timerRight2=setTimeout("scrollDivRight2('"+id+"')",10)
}

function sdatasize() {
    butSize()
    if (getID('div_float')) {
    	floatResize();
    } else if (getID('users_search_results')) {
    	usersResize();
    } else
        getID('databar').style.overflowY = 'auto';
};

function usersResize() {
    var winH = getID('databar').style.height;
    winH = (winH.replace('px','')*1)-29;
    getID('users_fulllist').style.height = winH+'px';
};

function selectService(newserv) {
    showLoading();
    if (getID('scrolbuttons2'))
        getID('scrolbuttons2').innerHTML = '';
    if (getID('actionsbar'))
        getID('actionsbar').innerHTML = '';
    if (getID('databar'))
        getID('databar').innerHTML = '';
	sdatasize();
	var oldserv = document.getElementById('actserv').value;
	if (document.getElementById('serv'+oldserv+'_off')) {
		document.getElementById('serv'+oldserv+'_off').style.display = 'inline';
		document.getElementById('serv'+oldserv+'_on').style.display = 'none';
	};
	document.getElementById('serv'+newserv+'_off').style.display = 'none';
	document.getElementById('serv'+newserv+'_on').style.display = 'inline';
	document.getElementById('actserv').value = newserv;
	loadService(newserv);
};

function loadService(serviceid) {
    var contenido = j("#scrolbuttons2");
    var data = new Object();
    contenido.load('scripts/loadService.php?sname='+sname+'&s='+serviceid,data,function(){
         var llamada = j("#menubar #loadservx").attr('class');
         eval(llamada);
    });
};

function TabSelect(sname, servicedir, link, ex1, ex2, ex3) {
    hideLabel();
    hideStatusBar();
    showLoading();
    getID('labelbar').innerHTML = '';
    getID('databar').innerHTML = '';
    getID('actionsbar').innerHTML = '';
    if (!ex1)
        ex1 = '';
    if (!ex2)
        ex2 = '';
    if (!ex3)
        ex3 = '';
	menuSelected(link);
    var contenido = j("#actionsbar");
    var data = new Object();
    contenido.load('services/'+servicedir+'/actions.php?sname='+sname+'&s='+link+'&ex1='+ex1+'&ex2='+ex2+'&ex3='+ex3,data,function(){
		if (getID('act_load') != null) {
			var llamada = j("#act_load").attr('class');
			eval(llamada);    		
		};
    });

};

function menuSelected(link, old, old2){
    if (old) {
        var oldlink = old;
        if (old2) {
            var oldlink2 = old2;
        };
    } else {
        if (document.getElementById('contselected').value)
		    var oldlink = document.getElementById('contselected').value;
    };
    if (oldlink) {
		document.getElementById('subl'+oldlink).className		= 'subleftnot';
		document.getElementById('submt'+oldlink).className		= 'submidtopnot';
		document.getElementById('submb'+oldlink).className		= 'submidbottomnot';
		document.getElementById('sublink'+oldlink).className	= 'contnotselected';
		document.getElementById('subr'+oldlink).className		= 'subrightnot';
	}
    if (oldlink2) {
		document.getElementById('subl'+oldlink2).className		= 'subleftnot';
		document.getElementById('submt'+oldlink2).className		= 'submidtopnot';
		document.getElementById('submb'+oldlink2).className		= 'submidbottomnot';
		document.getElementById('sublink'+oldlink2).className	= 'contnotselected';
		document.getElementById('subr'+oldlink2).className		= 'subrightnot';
	}
	if (document.getElementById('subl'+link).className == 'subleftnot') {
		document.getElementById('subl'+link).className		= 'subleft';
		document.getElementById('submt'+link).className		= 'submidtop';
		document.getElementById('submb'+link).className		= 'submidbottom';
		document.getElementById('sublink'+link).className	= 'contselected';
		document.getElementById('subr'+link).className		= 'subright';
		document.getElementById('contselected').value 		= link;
	}
}

function togleadvsearch(showhide) {
    if (showhide) {
        if (showhide == 1) {
            getID('actshow1').style.display = 'none';
            getID('actshow2').style.display = 'inline';
        } else if (showhide == 2) {
            getID('actshow2').style.display = 'none';
            getID('actshow1').style.display = 'inline';
        };
    } else {
        if (getID('actshow2').style.display == 'none') {
            getID('actshow1').style.display = 'none';
            getID('actshow2').style.display = 'inline';
        } else {
            getID('actshow2').style.display = 'none';
            getID('actshow1').style.display = 'inline';
        };
    };
};

function goLogin() {
    getID('iactions').src = htdir+'/scripts/redir.php?go=login';
};

function hideLabel() {
    getID('labelbar').style.display = 'none';
    getID('labelbar').innerHTML     = '';
    butSize();
};

function showLabel() {
    getID('labelbar').style.display = 'inline';
    butSize();
};

function overShowEnable() {
    j("a[rel='mdia']").colorbox({transition:"elastic", height:"85%"});
};

function captionEnable() {
    var move = -15;
    //zoom percentage, 1.2 =120%
    var zoom = 1.2;
    j(".also").hover(function() {
            width = j('.also').width();
            height = j('.also').height()-30;
            j(this).find('.mediaimg').stop(false,true).animate({'width':width, 'height':height, 'top':move, 'left':move}, {duration:200});
            j(this).find('div.captions').stop(false,true).slideDown(200);
        },
        function() {
            j(this).find('.mediaimg').stop(false,true).animate({'width':j('.also').width(), 'height':j('.also').height(), 'top':'0', 'left':'0'}, {duration:200});
            j(this).find('div.captions').stop(false,true).slideUp(200);
        }
    );
};

function sorter(sortfile, otable) {
    if (!otable)
        var otable = 'sortable_list';
    j("#"+otable).sortable({

        update: function() {
            var order = j('#'+otable).sortable('serialize');
            var sortsave = j("#sortable_save");
            var data = new Object();
            sortsave.load(sortfile,{ 'ord': order });
        }
	});

    j("#"+otable).disableSelection();

};

function changecss(theClass,element,value) {
	//Last Updated on June 23, 2009
	//documentation for this script at
	//http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
	 var cssRules;

	 var added = false;
	 for (var S = 0; S < document.styleSheets.length; S++){

    if (document.styleSheets[S]['rules']) {
	  cssRules = 'rules';
	 } else if (document.styleSheets[S]['cssRules']) {
	  cssRules = 'cssRules';
	 } else {
	  //no rules found... browser unknown
	 }

	  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	    if(document.styleSheets[S][cssRules][R].style[element]){
	    document.styleSheets[S][cssRules][R].style[element] = value;
	    added=true;
		break;
	    }
	   }
	  }
	  if(!added){
	  if(document.styleSheets[S].insertRule){
			  document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
			} else if (document.styleSheets[S].addRule) {
				document.styleSheets[S].addRule(theClass,element+': '+value+';');
			}
	  }
	 }
}

function URLEnc(var1)
{
	var nocodificar = "0123456789"+"ABCDEFGHIJKLMNOPQRSTUVWXYZ"+"abcdefghijklmnopqrstuvwxyz" +"-_.!~*'()";
	var HEX = "0123456789ABCDEF";
	var textoAcodificar = var1;
	var codificado = "";
	for (var i = 0; i < textoAcodificar.length; i++ ) {
		var ch = textoAcodificar.charAt(i);
	    if (ch == " ") {
		    codificado += "+";
		} else if (nocodificar.indexOf(ch) != -1) {
		    codificado += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Caracter Unicode '"+ch+"' no puede ser codificado utilizando la codificaci�n URL estandar.\n" +
				          "(s�lo soporta caracteres de 8-bit.)\n" +
						  "Ser� sustituido por un s�mbolo de suma (+)." );
				codificado += "+";
			} else {
				codificado += "%";
				codificado += HEX.charAt((charCode >> 4) & 0xF);
				codificado += HEX.charAt(charCode & 0xF);
			}
		}
	}
	return codificado;
};

function showInDiv(url, div) {
    var contenido = j("#"+div);
    var data = new Object();
    contenido.load(url,data,function(){ });
};

function addremove(tchk, var1) {
	var checks = getID('checks').value;
	var data = getID(tchk+var1).value;
	checks = checks.replace("|"+data+"|,","");
	getID('checks').value = checks;
	if (getID(tchk+var1).checked == true)
	{
		checks = checks + "|"+data+"|,";
		getID('checks').value = checks;
	}
};


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function loadTiny () {
  tinyMCE.init({

  		mode : "textareas",
  		editor_selector : "tinyclass",
  		theme : "advanced",
        skin : "o2k7",
        skin_variant : "silver",
  		plugins : "spellchecker,safari,style,save,advhr,advimage,advlink,media,paste,fullscreen,searchreplace",
  		theme_advanced_buttons1 : "bold,italic,underline,separator,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,redo,|,link,unlink,image,media,search,replace,code,anchor",
  		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,fontselect,fontsizeselect,forecolor,backcolor,spellchecker,fullscreen",
  		theme_advanced_buttons3 : "",
  		theme_advanced_buttons4 : "",
  		convert_fonts_to_spans : false,
  		theme_advanced_toolbar_location : "top",
  		theme_advanced_toolbar_align : "left",
  		theme_advanced_statusbar_location : "bottom",
        entity_encoding : "raw",
        spellchecker_rpc_url : 'scripts/tinynew/plugins/spellchecker/rpc.php'
  });
};

function loadTinysimple () {
  tinyMCE.init({

  		mode : "textareas",
  		editor_selector : "tinyclass",
  		theme : "advanced",
        skin : "o2k7",
        skin_variant : "silver",
  		plugins : "style",
  		theme_advanced_buttons1 : "bold,italic,underline,",
  		theme_advanced_buttons2 : "",
  		theme_advanced_buttons3 : "",
  		theme_advanced_buttons4 : "",
  		convert_fonts_to_spans : false,
  		theme_advanced_toolbar_location : "top",
  		theme_advanced_toolbar_align : "left",
        entity_encoding : "raw",
  		theme_advanced_statusbar_location : "",
  });
};

function sortmenuwidth(menuw) {
    getID('serv_sort').style.width = menuw +'px'
    getID('scrolbuttons2').style.width = wsize;

    if (getID('serv_sort').style.width > getID('scrolbuttons2').style.width) {
        getID('scrolbuttons2').style.width = (wsize-40)+'px';
        getID('scrolbuttons2').style.left = '20px';
        getID('aright2').style.display = 'inline';
        getID('aright2').style.left = wsize-20;
        getID('aleft2').style.display = 'inline';
    } else {
        getID('scrolbuttons2').style.width = wsize+'px';
        getID('aright2').style.display = 'none';
        getID('aleft2').style.display = 'none';
    };
};

function replaceAll( text, busca, reemplaza ){
	while (text.toString().indexOf(busca) != -1)
	    text = text.toString().replace(busca,reemplaza);
	return text;
}

function widUpload_show() {
    getID('wid_upload').innerHTML = '<input type="file" name="uploadify" id="uploadify" /><div id="fileQueue"></div>';
	j("#uploadify").uploadify({
		'uploader'       : urlsrv+'media/lib/uploadify.swf',
		'script'         : urlsrv+'media/lib/uploadify.php',
		'cancelImg'      : 'images/cancel.png',
		'folder'         : '',
		'queueID'        : 'fileQueue',
		'auto'           : true,
		'multi'          : true,
        'scriptAccess'   : 'Always',
        'onComplete'     : function(event, queueID, fileObj, response, data) {
            widUpload_Uploaded(response);
        },
        'onAllComplete'  : function(event, queueID, fileObj, response, data) {
            widUpload_addUploaded();
        }

	});
}

function widUpload_Uploaded(msg) {
    if (msg.substr(0,2) == 'OK') {
        var img = 'm'+msg.substr(3);
        getID('wid_upload_ids').value = getID('wid_upload_ids').value+img+',';
    };
};

function widUpload_addUploaded() {
    var allupl = getID('wid_upload_ids').value;
    getID('wid_upload_ids').value = '';
    if (getID('wid_upload_action').value) {
        var act = getID('wid_upload_action').value;
        act = act.replace('*arr*',allupl);
        eval(act);
    }
};

function delSubscription(id_subscription, id_user) {
    if (id_user)
        j.get('scripts/delSubscription.php?sname='+sname+'&u='+id_user+'&id='+id_subscription, function(html) { });
    else {
        var newids = getID('subscription_newids').value;
        getID('subscription_newids').value = newids.replace(id_subscription+',','');
    };
    var d = document.getElementById('subscription_show');
    var olddiv = document.getElementById('subscription_'+id_subscription);
    d.removeChild(olddiv);

};

function addSubscription(id_user, id_subscription) {
    if (!id_user) {
        var id_user = '';
        var newids = getID('subscription_newids').value;
        var newids2 = newids.replace(id_subscription+',','');
        if (newids == newids2)
            getID('subscription_newids').value = newids+id_subscription+',';
        else
            var noshow = 1;
    }
    if (!noshow) {
        var d = document.getElementById('subscription_show');
        var olddiv = document.getElementById('subscription_'+id_subscription);
        if (!olddiv)
            j.get('scripts/addSubscription.php?sname='+sname+'&u='+id_user+'&id='+id_subscription, function(html) {
                 j("#subscription_show").append(html);
            });

    };
};

function tags_allShow(chk) {
    if (chk) {
        changecss('#tgnotverify','display','none')
        var chk = 1;
    } else {
        changecss('#tgnotverify','display','block')
        var chk = '';
    }
    j.get('scripts/addshowTags.php?sname='+sname+'&a='+chk, function(html) {

    });
}
*/

