$(document).ready(function(){
	
$('#open_plugins').bind('mousedown',function(){
	if($('#box_plugins').css('display') == 'none'){
		$('#box_plugins').slideDown(200,function(){
			$('#open_plugins').addClass('activo');
			$('#open_plugins').css('color','#000');
			$('#open_plugins').text(ocultaPlugins);
		});
	}else {
		$('#box_plugins').slideUp(200,function(){
			$('#open_plugins').removeClass('activo');
			$('#open_plugins').css('color','#3BB3EF');
			$('#open_plugins').text(verPlugins);
		});
	}
});
						   
<!-- Box Sign in --> 

$('#open_signin').bind('mousedown',function(){
	if($('#box_signin').css('display') == 'none'){
		$('#open_signin').addClass('active');
		$('#box_signin').animate({ opacity:1 }); 
		$('#box_signin').css('display', 'block');
	}else {
		$('#open_signin').removeClass('active');
		$('#box_signin').css('display', 'none');
	}
});

<!-- TABS  -->

$(".tab_content").hide();
$("ul.tabs li:last").addClass("active").show();
$(".tab_content:first").show();


$("ul.tabs li").click(function(){
							   
	$("ul.tabs li").removeClass("active");
	$(this).addClass("active");
	$(".tab_content").hide();

	var activeTab = $(this).find("a").attr("href");
	$(activeTab).fadeIn();
	
	return false;
	
});

$(".btab2").click(function(){
							
	var idTab = $(this).attr("rel");
	   
	$("ul.tabs li").removeClass("active");
	$("ul.tabs li#m"+idTab+"").addClass("active");
	$(".tab_content").hide();

	$("#"+idTab).fadeIn();
	
	return false;
	
});

$(".popup").hide();

thingToCharge();

var alto_ventana = $(document).height();
var ancho_ventana = $(document).height();

	$(".popup").css('height', alto_ventana+'px');
	$(".popup").css('width', ancho_ventana+'px');

	$(window).scroll(function(){
            var cuanto = 0;
            var pantalla = $(window).height();
            var capa = $(".tab_container");
            cuanto =$(window).scrollTop()-155;
            if (cuanto < capa.position().top-165 )cuanto = capa.position().top-165;
            if (cuanto > capa.outerHeight()-703 )cuanto = capa.outerHeight()-703;
            $('#mobiletemplate').animate({top:cuanto},{ queue: false, duration: 1000});

        });
	
	
});

function esquinas(){
	

$('.popup').corner('10px');
$('#box_signin').corner('5px');
$('.tabs a').corner('top 5px');
$('.tab_container').corner('left 5px');
$('.tabspage a').corner('top 5px');
$('.button3 izq').corner('left 5px');
$('.button3').corner('5px');
$('#share').corner('left 5px');
$('#bfoto').corner('5px');
$('.corner').corner('5px');

}

function thingToCharge(){
   if($("#osettings")) {
	$("#osettings dd").hide();
	$("#osettings dd.noticias").show(); // 
	$("#osettings dt b").click(function() {
		if(this.className.indexOf("clicked") != -1) {
			$(this).parent().next().slideUp(200);
			$(this).removeClass("clicked");
		}
		else {
			$("#osettings dt b").removeClass();
			$(this).addClass("clicked");
			$("#osettings dd:visible").slideUp(200);
			$(this).parent().next().slideDown(200);
		}
		return false;
	});
}

<!-- TABS PAGE  -->

$(".tabpage_content").hide();
$("ul.tabspage li:first").addClass("active").show();
$(".tabpage_content:first").show();


$("ul.tabspage li").click(function(){

	$("ul.tabspage li").removeClass("active");
	$(this).addClass("active");
	$(".tabpage_content").hide();

	var activeTab = $(this).find("a").attr("href");
	$(activeTab).fadeIn();

	return false;

});

$(document).ready(function(){
	
// Boton siguiente - Atras tabs
	
$(".btab").click(function(){

	var actualTab = $(this).attr("id");
	var siguienteTab = parseInt(actualTab) + 1;
	var anteriorTab = parseInt(actualTab);
	var tipo = $(this).attr("rel");
	
	$("ul.tabs li").removeClass("active");
	$("div:contains('John')").css("text-decoration", "underline");
	if(tipo == "next"){
		//alert('next');
		$("ul.tabs li:contains('"+siguienteTab+"')").addClass("active");
		$(".tab_content").hide();
		$("#tab"+actualTab).hide();
		$("#tab"+siguienteTab).fadeIn();
	}else{
		//alert('back');
		$("ul.tabs li:contains('"+anteriorTab+"')").addClass("active");
		$(".tab_content").hide();
		$("#tab"+actualTab).hide();
		$("#tab"+anteriorTab).fadeIn();
	}
	
	return false;
	
});

var mi_fecha=new Date();
var archivo = ""
	new AjaxUpload('#bfoto', {
		
		action: 'uploadfoto.php', // I disabled uploads in this example for security reaaons
		name: 'filefoto',
            data: {nombrefoto: archivo = mi_fecha.getTime()},
	        onSubmit : function(file , ext){
			
			if (ext && /^(jpg|png|jpeg|gif)$/.test(ext)){
				$('#foto .loading').css('display','block');
				$('#foto .loading').html('<img src="http://www.onbile.com/images/loading.gif" />');	
			} else {
				// extension is not allowed
				alert('Error: only images are allowed');
				// cancel upload
				return false;				
			}
		},
		onComplete : function(file, response){

			if(response == "error peso"){
				alert('<p>'+fotoTamano+'</p>');
			}else if (response == "error tamano"){
				alert('<p>'+fotoDimension+'</p>');
			}else{
				// muestro la foto subida
				var file = response;
                                
                $("#frameTheme").contents().find(".image").attr('src',urldomain+'/manager/userimages/'+file);
				$('#image').val(urldomain+'/manager/userimages/'+file);
				$('#foto .loading').css('display','none');
		    }
		}

	});

});

<!-- Opciones adicionales -->

$("#background").change(function(){
    changeBackground($(this).val());
});

$("#subtitular").change(function(){
    changeSubtitular($(this).val());
});

$("#buttons").change(function(){
    changePages($(this).val());
});

$("#titular").change(function(){
    changeTitular($(this).val());
});

$("#tab3 input").each(function(){

     $(this).keyup(function()
     {
          $("#frameTheme").contents().find("#"+$(this).attr('id')).text($(this).val()) ;

     });
});

tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	plugins : "preview",
	theme_advanced_buttons1 : "bold,italic,underline,separator,forecolor,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,redo,link,unlink,removeformat,image",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : false,
	theme_advanced_resizing :false,
	remove_script_host:false,
        setup : function(ed) {
          ed.onKeyUp.add(function(ed, e) {
             $("#frameTheme").contents().find('#'+ed.id).html((ed.getContent({format : 'HTML'})));
          });
        },
	width : "430",
	convert_urls:false,
	content_css : "http://www.onbile.com/inc/css/textareas.css",
	extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
});

var navegador = navigator.appName 
if (navegador != "Microsoft Internet Explorer") {

esquinas();

}


}



/* --------------------------------------------------------------- MY SCRIPTS --------------------------------------------------------------------------------*/




var slideGalleryWidth = 394;
var urldomain = 'http://www.onbile.com/';
//var urldomain = 'http://localhost:8888/onbile/';
var usercheck = false;
var emailcheck = true;
var defaultsDatas = false;
var selectTheme = false;

function checkUsername() {
  $('#usernameLab').removeClass('error');
  $.ajax({
    type: "POST",
    url: urldomain+'includes/validator.php',
    timeout: 10000,
    data: {user:$('#username').val(), acc: "userexist"},
    success: function(res){

      switch(res) {
        case 'available':
          $('#usernameLab').addClass('ok');
          $('#user-check').html('<ins>OK</ins>');
          usercheck = true;
          break;
        case 'taken':
          $('#usernameLab').addClass('error');
          $('.purl').html("<p style='color:red'>"+userExist+"</p>");
          usercheck = false
          break;
        case 'notvalid':
          $('#usernameLab').addClass('error');
          $('.purl').html("<p style='color:red'>"+checkColor+"</p>");
          usercheck = false
          break;
        case 'blank':
          $('#usernameLab').addClass('error');
          $('.purl').html("<p style='color:red'>"+chooseUsername+"</p>");
          usercheck = false
          break;
      }
    },
    error: function(){
      $('#user-check').html('');
    }
  });
}
function checkPassword() {

  $('#oldPassLab').removeClass('error');
  $.ajax({
    type: "POST",
    url: urldomain+'includes/validator.php',
    timeout: 10000,
    data: {pass:$('#Oldpassword').val(), acc: "checkpass"},
    success: function(res){

      switch(res) {
        case 'correct':
          $('#oldPassLab').addClass('ok');
          $('.userlb').html("<p style='color:green'>Type new password.</p>");
          $("#Newpassword").removeAttr('disabled');
          $("#Repassword").removeAttr('disabled');
          break;
        case 'wrong':
          $('#oldPassLab').addClass('error');
          $('.userlb').html("<p style='color:red'>Your old password was entered incorrectly. Please enter it again.</p>");
          $("#Newpassword").attr("disabled", true)
          $("#Repassword").attr("disabled", true)
          break;
      }
    },
    error: function(){
      $('#user-check').html('');
    }
  });
}
function checkEmailAjax() {
  
  $('#emailLab').removeClass('error');
  $.ajax({
    type: "POST",
    url: urldomain+'includes/validator.php',
    timeout: 10000,
    data: {email:$('#email').val(), acc: "checkemail"},
    success: function(res){
      switch(res) {
        case 'correct':
          $('#emailLab').addClass('ok');
          $('.maillb').html("");
          emailcheck = true;
          break;
        case 'wrong':
          $('#emailLab').addClass('error');
          $('.maillb').html("<p style='color:red'>"+emailExist+"</p>");
          emailcheck = false;
          break;
      }
    },
    error: function(){
      $('#user-check').html('');
    }
  });
}

function checkEmail() {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test($('#email').val()) ){
      $('#emailLab').removeClass('error');
      checkEmailAjax()
  } else {
   $('#emailLab').addClass('error');
    emailcheck = false;
  }
}

function updateUrl() {
  $('.purl').html(''+perfilPublico+':  http://onbile.com/'+$('#username').val());
}

function changeTheme(themeFold,theme){

    $.ajax({
    type: "POST",
    url:  'http://www.onbile.com/includes/validator.php',
    dataType: "json",
    cache: false,
    data: {"theme": themeFold, "acc": "changeTheme", "themeID":theme,"lang":language},
    beforeSend: function(x){
        $(".popup").fadeIn('slow');
    },						
    success: function(res){

       $('textarea').each(function(){ tinyMCE.execCommand('mceRemoveControl', false, $(this).attr('id')); });
 selectTheme = true;
        $("#inputsContents").html(res.tabs[0]);

        thingToCharge();
        //change selection template
        $('#themeSelected').val(themeFold);
		
        $('#themeSelectedID').val(theme);
        changeIframe(theme);
        //changeTypes();
        $("#showicons").html(res.defaults[0]);
        addfunctionsicons(res.colors[0]);
        
        
    },
    error: function(xhr, status, error) {
     alert(xhr.status);
	 alert(error);
    }
  });
  
}

function makeChanges(){
	
$("#tab3 input").each(function(){

         $(this).trigger('keyup');

});

changeBackground($("#background").val());
changeSubtitular($("#subtitular").val());
changePages($("#buttons").val());
changeTitular($("#titular").val());
var idtext = "";
$('textarea').each(function(){
    idtext = $(this).attr('id');
    
    $("#frameTheme").contents().find('#'+idtext).html(  tinyMCE.get(idtext).getContent({format : 'HTML'}));
});

$("#frameTheme").contents().find(".icon").attr('src',function() {return $("#selectedicons").val()+"/"+$(this).attr("rel")});
}

function changeIframe(location){

 var url = "../includes/validator.php?lang="+language+"&acc=showdefault&theme="+location;
  $('#frameTheme').attr('src', url);
  defaultsDatas = true;
 
  
}

function putDefaults(){
	
 var defaults = $("#defaults").html();
 if (defaults != null){
       defaults = defaults.split(";");
        $("#background").val(defaults[0]);	
        $("#titular").val(defaults[1]);	
        $("#subtitular").val(defaults[2]);	
        $("#buttons").val(defaults[6]);         
        changeBackground(defaults[0]);
        changeSubtitular(defaults[2]);
        changePages(defaults[6]);
        changeTitular(defaults[1]);
        $("#background").trigger('focus');
        $("#background").trigger('blur');
        $("#background").trigger('click');
        $("#titular").trigger('focus')
        $("#titular").trigger('blur');
        $("#titular").trigger('click');
        $("#subtitular").trigger('focus')
        $("#subtitular").trigger('blur');
        $("#subtitular").trigger('click');
        $("#buttons").trigger('focus')
        $("#buttons").trigger('blur');
        $("#buttons").trigger('click');
        $("#frameTheme").contents().find(".icobk").css("color","#"+defaults[7]);
        $("#selectedicons").val(urldomain+"themes/"+ $("#themeSelected").val()+"/iconset/"+defaults[5]);
        $("#colortextbuttons").val(defaults[7]);

 }

}

function extractDatas(){
	
   var datas = new Array();
   $("input").each(function(){
        datas[$(this).attr("id")]=$(this).val();
   });
   return datas;
   
}
function addfunctionsicons(colors){
        $(".selecticonscolor").html(colors);
}
function changeColorsLis(cual){
   $(".licolors").css("display","none");
   $("#color_"+cual).css("display","block")
}
function changeColorIcon(seleccionado){

var elemento = seleccionado.getAttribute("rel")
$(".colorselector").removeClass("active");
$("#"+seleccionado.id).addClass("active");
$("#frameTheme").contents().find(".icon").attr('src',function() {return elemento+"/"+$(this).attr("rel")});

$("#frameTheme").contents().find(".icobk").css("color","#"+$("#"+seleccionado.id).html());
$("#colortextbuttons").val($("#"+seleccionado.id).html());
$("#selectedicons").val(elemento);
//$("#frameTheme").contents().find(".button").css()
$("#frameTheme").contents().find("#socials").css('background-image', 'url('+elemento+'/arrow_invert.png)');
$("#frameTheme").contents().find(".abierto").css('background-image', 'url('+elemento+'/arrow_invert1.png)');
$("#frameTheme").contents().find("#menu .button a").css('background-image', 'url('+elemento+'/bullet.png)');

}

function changeIcons(elemento){

var ruta = "http://www.onbile.com/icons/"+elemento.value;
$("#selectedicons").val(ruta);
$("#frameTheme").contents().find(".icon").attr('src',function() {return ruta+"/"+$(this).attr("rel")});

}
function addfunctionColors(){
	
$(".colorselector").click(function(){
	var idtipo = $(".selecticons input:checked").val();

        var ruta = $(this).attr("rel").replace("..",  urldomain);
	$("#"+idtipo+"").attr('src', $(this).attr("rel")+'/iconset.png');
	$(".colorselector").removeClass("active");
	$(this).addClass("active");
        $("#frameTheme").contents().find(".icon").attr('src',function() {return ruta+"/"+$(this).attr("rel")});
        $("#frameTheme").contents().find(".icobk").css("color","#"+$(this).html());
        $("#colortextbuttons").val($(this).html());
        $("#selectedicons").val(ruta);
        //$("#frameTheme").contents().find(".button").css()
        $("#frameTheme").contents().find("#socials").css('background-image', 'url('+ruta+'/arrow_invert.png)');
        $("#frameTheme").contents().find(".abierto").css('background-image', 'url('+ruta+'/arrow_invert1.png)');
        $("#frameTheme").contents().find("#menu .button a").css('background-image', 'url('+ruta+'/bullet.png)');
});

}

function changeSubtitular(subtitular){
    $("#frameTheme").contents().find(".txtcolor").css("color","#"+subtitular);
}

function changePages(pages){
	
    $("#frameTheme").contents().find(".button").css("background-color","#"+pages);
    $("#frameTheme").contents().find(".headlinecolor").css("color","#"+pages);
    $("#frameTheme").contents().find("#socials").css("background-color","#"+pages);
    $("#frameTheme").contents().find("#sliderContent").css("background-color","#"+pages+"!important;");
    $("#frameTheme").contents().find(".mainbk").css("background-color","#"+pages);
    $("#frameTheme").contents().find("#socials").addClass('abierto');
    $("#frameTheme").contents().find(".abierto").css("background-color","#"+pages+"!important;");

}
function changeTitular(titular){
    $("#frameTheme").contents().find(".titlecolor").css("color","#"+titular);
}

function changeBackground(background){
    $("#frameTheme").contents().find("body").css("background-color","#"+background);
    $("#frameTheme").contents().find("#corn").css("background-color","#"+background);
    $("#frameTheme").contents().find(".cmainbk").css("color","#"+background);
    $("#frameTheme").contents().find(".jquery-corner div").css("border-color","#"+background);
}

function sendActivation(){

$.ajax({
    type: "POST",
    url:  urldomain+'includes/validator.php',
    timeout: 10000,
    data: {acc: "resendactivate", dato: $("#chivato").html()},
    success: function(res){
        $(".respactivation").html(respActivation);

    },
    error: function(){
      $('.respactivation').html('There was an error while sending the activation');
    }
});


}

function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

function createUser(){
	
    var allok = true;
    var error = '';

    if($("#accept").attr("checked") != true){
        allok = false;
        error += "<p style = 'color:red'>"+checkLegal+"</p>"
    }
    if (!isUrl($("#urluser").val())){
        allok = false;
        error += "<p style = 'color:red'>"+checkUrl+"</p>"
    }
    checkEmail($("#email").val());
    if (emailcheck == false){
        allok = false;
        error += "<p style = 'color:red'>"+checkEmails+"</p>"
    }
    if (usercheck == false){
        allok = false;
        error += "<p style = 'color:red'>"+checkUsernames+"</p>"
    }
    if (selectTheme == false){
        allok = false;
        error += "<p style = 'color:red'>"+checkTheme+"</p>"
    }
    if (allok){
        $("#datasok").val("ok");
        $('#formpublish').submit();
    }else{
        $("#datasok").val("wrong");
        $("#errors").html(error);
    }
	
}

function update(){
	
    var allok = true;
    var error = '';
    if ($("#Newpassword").val() != $("#Repassword").val()){
        allok = false;
        error += "<p style = 'color:red'>"+updatePass+"</p>"
    }
    checkEmail($("#email").val());
    if (emailcheck == false){
        allok = false;
        error += "<p style = 'color:red'>"+updateEmail+"</p>"
    }
    if (!isUrl($("#urluser").val())){
        allok = false;
        error += "<p style = 'color:red'>"+updateUrl+"</p>"
    }
    if (allok){
        $("#datasok").val("ok");
        $('#formpublish').submit();
    }else{
        $("#datasok").val("wrong");
        $("#errors").html(error);
    }
	
}

$(document).ready( function() {

  if ($('#formpublish').length > 0) {

    $('#username').blur(function () { checkUsername(); updateUrl(); });
    $('#Oldpassword').blur(function () { checkPassword();});
    $('#email').blur(function () { checkEmail(); });
    if ($('#username').val() != '') { checkUsername(); }
    $('#username').keypress(function () { if ($(this).hasClass('error')) $(this).removeClass('error'); });
    $('#username').keyup(updateUrl);

  }

    $('#frameTheme').load(function() {
       $(".popup").hide();
       if (defaultsDatas){
           putDefaults();
           defaultsDatas = false;
           
           $(".selicons:first").trigger('click');
          
       }
          
          makeChanges();
    });
	
});
