// JavaScript Document
$(document).ready(function () {
	$("a.confirmar_borrar").click(function(){
		if(confirm("Desea borrar?")){
			return true;	
		}
		return false;
	});
	$("a.img_modificar_fila").click(function(){
		var obj = this;
		$("input.old_id[name='id']").val($(obj).attr('data_id'));
		$("input[name='name_file']").val($(obj).attr('data_old_name'));
		$("input[name='old_src_file']").val($(obj).attr('data_old_src'));
		return false;
	});
	$("a.img_eliminar_fila").click(function(){
		if(confirm($(this).attr('advise'))){
			return true;	
		}
		return false;
	});
	
});
tinyMCE.init({
	mode : "textareas",
	language : "es",
	theme : "advanced",
	plugins : "inlinepopups,safari",
	theme_advanced_toolbar_location : "top",
	theme_advanced_layout_manager : "SimpleLayout",
	theme_advanced_buttons1 : "forecolor,separator,bold,italic,underline,separator,sub,sup,separator,undo,redo,separator,cleanup,removeformat,separator,hr,separator,link,unlink,separator,code",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : ""
});

/**
 * jQuery.timers - Timer abstractions for jQuery
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2009/02/08
 *
 * @author Blair Mitchelmore
 * @version 1.1.2
 *
 **/

jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.event.special

jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
				return;

			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			
			times = times || 0;
			belay = belay || false;
			
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			
			if (!timers[label])
				timers[label] = {};
			
			fn.timerID = fn.timerID || this.guid++;
			
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			
			handler.timerID = fn.timerID;
			
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			
			this.global.push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});

jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});
$(function () {
  $('.bubbleInfo').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 250;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          top: +10,
          left: +40,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).click(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          top: +10,
          left: +40,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
});
function check_value(text,funcion,error_div,success_div){
	$.post(funcion,{data:text},
		function(data){
			if(data.avaible){
				$(error_div).hide();
				$(success_div).show();
			}
			else{
				$(error_div).show();
				$(success_div).hide();
			}
		}
	,'json');
}
var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	var input = document.createElement('input');
	input.className = 'form_subir_doc_input';
	fakeFileUpload.appendChild(input);
	var buttondiv = document.createElement('div');
	buttondiv.className = 'align_right';
	fakeFileUpload.appendChild(buttondiv);
	var button = document.createElement('input');
	button.type='button';
	button.value='ADJUNTAR';
	button.className = 'form_subir_doc_adjuntar';
	buttondiv.appendChild(button);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}

$(function () {
	$('.email_client').blur(function(){
		var text = $(this).val();
		$('.username_client').each(function(){
			if(!$(this).val()){
				$(this).val(text);
				check_value(text,'check_username','#username_client_error','#username_client_success');
			}
		});
	});
	$('.username_client').blur(function(){
		var text = $(this).val();
		if(text){
			check_value(text,'check_username','#username_client_error','#username_client_success');
		}
		else{
			$('#username_client_error').hide();
			$('#username_client_success').hide();
		}

	});
	$('.client_dropdown').change(function(){
		$(this).parent().submit();
	});
	
	if (!$.browser.msie) {
		$('.language_dropdown').change(function(){
			$(this).parent().submit();
		});
	}

	/*
	initFileUploads();
	$('.fakefile input').click(function(){
		$('#realField').trigger('click');
	});
	*/
	$('.fileStyled:file').si_files({
		
		label_class: 'form_subir_doc_input',
		button_class: 'form_subir_doc_adjuntar'
	});
	var form_subir_doc_input_txt = $('.form_subir_doc_input').val();
	$('.form_subir_doc_input').focusin(function(){
		if($(this).val() == form_subir_doc_input_txt){
			$(this).val('');
		}
	});
	$('.form_subir_doc_input').focusout(function(){
		if($(this).val() == ''){
			$(this).val(form_subir_doc_input_txt);
		}
	});
	
});

