/* Author: 

*/

(function ($) {
    $.navetierra = {
        rotate: function (options) {
            var defaults = {
                selector: '.rotate',
                pause: 5000,
                randomize: false
            };
            if (typeof options == 'string') defaults.selector = options;
            var options = $.extend(defaults, options);
            return $(options.selector).each(function () {
                var obj = $(this);
                var length = $(obj).children().length;
                var temp = 0;

                function getRan() {
                    var ran = Math.floor(Math.random() * length) + 1;
                    return ran
                };

                function show() {
                    if (options.randomize) {
                        var ran = getRan();
                        while (ran == temp) {
                            ran = getRan()
                        };
                        temp = ran
                    } else {
                        temp = (temp == length) ? 1 : temp + 1
                    };
                    $(obj).children().hide();
                    $(':nth-child(' + temp + ')', obj).fadeIn('slow')
                };
                show();
                setInterval(show, options.pause)
            })
        },
		external: function (options) {
            var defaults = {
                selector: 'a'
            };
            if (typeof options == 'string') defaults.selector = options;
            var options = $.extend(defaults, options);
            var hostname = window.location.hostname;
            hostname = hostname.replace('www.', '').toLowerCase();
            return $(options.selector).each(function () {
				if($(this).attr('href')) {
				  var href = $(this).attr('href').toLowerCase();
				  if (href.indexOf('http://') != -1 && href.indexOf(hostname) == -1) {
					  $(this).attr('target', '_blank');
					  $(this).addClass('external')
				  }
				}
            })
        },
        forms: function (options) {
            var defaults = {
                selector: 'form',
                err: 'Requerido',
                errEmail: 'Se requiere un e-mail v&aacute;lido',
                errUrl: 'Se requiere una URL v&aacute;lida',
                errPhone: 'Se requiere un tel&eacute;fono v&aacute;lido',
                notValidClass: 'notvalid'
            };

            function check(obj) {
                if ($(obj).val() == '' || checkLabel(obj)) {
                    var errormsg = (typeof $(obj).attr('title') != 'undefined') && ($(obj).attr('title') != '') ? $(obj).attr('title') : options.err;
                    error(obj, errormsg)
                }
            };

            function checkRegEx(obj, type) {
                var regEx, err;
                switch (type) {
                case 'url':
                    regEx = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
                    err = options.errUrl;
                    break;
                case 'phone':
                    var regEx = /[\d\s_-]/;
                    err = options.errPhone;
                    break;
                default:
                    regEx = /^[^@]+@[^@]+.[a-z]{2,}$/;
                    err = options.errEmail
                };
                var val = $(obj).val();
                if (val.search(regEx) == -1 || checkLabel(obj)) {
                    var errormsg = (typeof $(obj).attr('title') != 'undefined') && ($(obj).attr('title') != '') ? $(obj).attr('title') : err;
                    error(obj, errormsg)
                }
            };

            function checkLabel(obj) {
                var text = $('label[for=' + $(obj).attr('id') + ']').text();
                return (text == $(obj).val())
            };

            function error(obj, errormsg) {
                var parent = $(obj).parent();
				parent.append('<span class="error">' + errormsg + '</span>');
                $('span.error', parent).hide().fadeIn('fast');
                $(obj).addClass(options.notValidClass);
                valid = false
            };
            $('input.label,textarea.label').each(function () {
                var text = $('label[for=' + $(this).attr('id') + ']').text();
                $('label[for=' + $(this).attr('id') + ']').css('display', 'none');
                $(this).val(text);
                $(this).focus(function () {
                    if ($(this).val() == text) $(this).val('')
                });
                $(this).blur(function () {
                    if ($(this).val() == '') $(this).val(text)
                })
            });
            if (typeof options == 'string') defaults.selector = options;
            var options = $.extend(defaults, options);
            return $(options.selector).each(function () {
                $(this).submit(function () {
                    $('.error', this).remove();
                    $('.' + options.notValidClass, this).removeClass(options.notValidClass);
                    valid = true;
                    $(':text.required', this).each(function () {
                        if ($(this).hasClass('email')) {
                            checkRegEx(this, 'email')
                        } else if ($(this).hasClass('url')) {
                            checkRegEx(this, 'url')
                        } else if ($(this).hasClass('phone')) {
                            checkRegEx(this, 'phone')
                        } else {
                            check(this)
                        }
                    });
                    $(':password.required', this).each(function () {
                        check(this)
                    });
                    $('textarea.required', this).each(function () {
                        check(this)
                    });
                    $(':checkbox.required', this).each(function () {
                        if (!$(this).attr('checked')) {
                            var errormsg = ($(this).attr('title') != '') ? $(this).attr('title') : options.err;
                            error(this, errormsg)
                        }
                    });
                    $('select.required', this).each(function () {
                        check(this)
                    });
                    return valid
                })
            })
		}
    }
})(jQuery);

$(document).ready(function(){
	$.navetierra.rotate();
	$.navetierra.external();
	$.navetierra.forms();
	$("a[href$='gif']").fancybox();
	$("a[href$='GIF']").fancybox();
	$("a[href$='jpg']").fancybox();
	$("a[href$='JPG']").fancybox();
	$("a[href$='jpeg']").fancybox();
	$("a[href$='JPEG']").fancybox();
	$("a[href$='png']").fancybox();
	$("a[href$='PNG']").fancybox();
	$(".popup").fancybox();
	var flashvars = {};
	var params = { 'wmode' : 'transparent' };
	swfobject.embedSWF('/swf/logo.swf', 'header-logo', '274px', '69px', '9.0.0', false, flashvars, params);
	swfobject.embedSWF('/swf/asesor.swf', 'suasesor', '165px', '67px', '9.0.0', false, flashvars, params);
	
	$('#frmFormatoValores input').click(function(){ $(this).closest("form").submit(); });

});
