function mytheme(theme) {
    if (theme == 'default') {
        this.primary_text = '333333';
        this.secondary_text = '999999';
        this.link_text = '006699';
        this.primary_background = 'ffffff';
        // this.text_field_background='ffffff';
        this.highlight_background = 'f2f2f2';
        this.borders = 'cccccc';
    }
    if (theme == 'earth') {
        this.primary_text = 'ffffff';
        this.secondary_text = 'cccccc';
        this.link_text = 'ddaa11';
        this.primary_background = '332c22';
        // this.text_field_background='555555';
        this.highlight_background = '28231b';
        this.borders = '997755';
    }
    if (theme == 'ocean') {
        this.primary_text = 'ffffff';
        this.secondary_text = 'cccccc';
        this.link_text = '8bc9cc';
        this.primary_background = '336666';
        // this.text_field_background='ffffff';
        this.highlight_background = '408686';
        this.borders = '285151';
    }
}
$(document).ready(function() {
    if ($('#bg-page').get(0)) {
        initValue();
        initLiveType();
    }
    if ($('#main div.embed').get(0)) {
        initTheme('default');
        initUpdate();
        initCustome();
        initValue();
        initLiveType();
    }
    if ($('#register').get(0)) initValidate();
})
 function initValue() {
    $('#main div.widget input#your_name').DefaultValue("Travis Vocino");
    $('#main div.widget input#your_phone').DefaultValue("561-827-8704");
    $('#main div.widget span.your_name').html("Travis Vocino");
    $('#main div.widget span.your_phone').html("561-827-8704");
}

function initLiveType() {
    $('#main div.widget input#your_name').keyup(function() {
        var html = $('#main div.widget input#your_name').val();
        $('#main div.widget span.your_name').html(html);
    });
    $('#main div.widget input#your_phone').keyup(function() {
        var html = $('#main div.widget input#your_phone').val();
        $('#main div.widget span.your_phone').html(html);
    });
}
function initTheme(theme_name) {
    var theme = new Object();
    if (theme_name == 'default') theme = new mytheme('default');
    if (theme_name == 'earth') theme = new mytheme('earth');
    if (theme_name == 'ocean') theme = new mytheme('ocean');
    $('#main div.dashboard input.text').each(function(i, input) {
        var name = $(input).attr('name');
        //alert(theme.primary_text);
        switch (name) {
        case 'primary-text':
            $(input).val('#' + theme.primary_text);
            break;
        case 'secondary-text':
            $(input).val('#' + theme.secondary_text);
            break;
        case 'link-text':
            $(input).val('#' + theme.link_text);
            break;
        case 'primary-background':
            $(input).val('#' + theme.primary_background);
            break;
            // case 'text-field-background' :  $(input).val('#'+theme.text_field_background);break;
        case 'highlight-background':
            $(input).val('#' + theme.highlight_background);
            break;
        case 'border':
            $(input).val('#' + theme.borders);
            break;
        }
    });
    initColor();
}

function initCustome() {
    $('#main div.dashboard input.text').blur(function() {
        ininUpdateWidget();
        initColor();
    });

    $('#main div.embed a.customize').click(function() {
        $('#main div.embed').fadeOut(500,
        function() {
            $('#main div.dashboard').fadeIn(500);
        });
        /*$('#main div.embed').css('display','none');
		$('#main div.dashboard').css('display','block');*/
        return false;
    });

    function loadPage(name) {
        if (name) {
            var newPage = $('#main div.' + name);
            if (!newPage.hasClass('current')) {
                var current = $('#main div.current');
                var newPageLink = $('.top-panel .account a.link-' + name)[0];
                var useDelay = jQuery.data(newPageLink, 'delay');
                jQuery.data(newPageLink, 'delay', false);
                if (useDelay) {
                    current.removeClass('current').fadeOut(250,
                    function() {
                        newPage.addClass('current').fadeIn(250);
                    });
                } else {
                    current.removeClass('current').hide();
                    newPage.addClass('current').show();
                }
            }
        }
    }

    $('.top-panel .account a.dynamic-nav').click(function(e) {
        e.preventDefault();
        jQuery.data(this, 'delay', true);
        return false;
    });

    $('#main ul.top-buttons a').click(function() {

        var class_name = $(this).attr('class');
        if (class_name == 'default') initTheme('default');
        if (class_name == 'earth') initTheme('earth');
        if (class_name == 'ocean') initTheme('ocean');
        ininUpdateWidget();
        return false;
    });

    function callback(hash)
    {
        if (hash == '')
        hash = 'widgetsettings';

        loadPage(hash);
    }

    $(document).ready(function() {
        $.history.init(callback);
        $("a[@rel='history']").click(function() {
            $.history.load(this.href.replace(/^.*#/, ''));
            return false;
        });
    });

}
function initColor() {

    $('#main div.dashboard div.color').each(function(i, div) {
        var color_val = $(div).prev().val();
        $(div).css('backgroundColor', color_val);
        $(div).ColorPicker({
            color: color_val,
            onShow: function(colpkr) {
                $(colpkr).fadeIn(500);
                //$(colpkr).css('display','block');
                return false;
            },
            onHide: function(colpkr) {
                $(colpkr).fadeOut(500);
                //$(colpkr).css('display','none');
                return false;
            },
            onSubmit: function(hsb, hex, rgb, el) {
                $(el).prev().val('#' + hex);
                $(el).css('backgroundColor', '#' + hex);
                $(el).ColorPickerHide();
                ininUpdateWidget();
            },
            onChange: function(hsb, hex, rgb) {
                $(div).css('backgroundColor', '#' + hex);
            }
        });
    });
}
function ininUpdateWidget() {
    var widget = '#main div.widget';
    $('#main div.dashboard input.text').each(function(i, input) {
        var name = $(input).attr('name');
        var val = $(input).val();
        switch (name) {
        case 'primary-text':
            $(widget).css('color', val);
            $(widget).find('input.text').css('color', val);
            break;
        case 'secondary-text':
            $(widget).find('span').css('color', val);
            break;
        case 'link-text':
            $(widget).find('a').css('color', val);
            $(widget).find('input.btn-send').css('backgroundColor', val);
            break;
        case 'primary-background':
            $(widget).css('backgroundColor', val);
            $(widget).find('input.text').css('backgroundColor', val);
            break;
            // case 'text-field-background' : $(widget).find('input.text').css('backgroundColor',val); break;
        case 'highlight-background':
            $(widget).find('div.contact-data').css('backgroundColor', val);
            $(widget).find('div.close').css('backgroundColor', val);
            break;
        case 'border':
            $(widget).css('borderColor', val);
            $(widget).find('div.chat-form').css('borderColor', val);
            $(widget).find('div.close').css('borderColor', val);
            $(widget).find('input.text').css('borderColor', val);
            $(widget).find('div.info img').css('borderColor', val);
            $(widget).find('div.contact-data').css('borderColor', val);
            break;
        }
    });
}
function initUpdate() {
    $('#main .button a.update').click(function() {
        $('#main div.dashboard').fadeOut(500,
        function() {
            $('#main div.embed').fadeIn(500);
        });
        initColor();
        return false;
    });
    $('#main .button a.cancel').click(function() {
        $('#main div.dashboard').fadeOut(500,
        function() {
            $('#main div.embed').fadeIn(500);
        });
        initTheme('default');
        ininUpdateWidget();
        return false;
    });
}
// Password strength meter
function passwordStrength(password, username) {
    var shortPass = 1,
    badPass = 2,
    goodPass = 3,
    strongPass = 4,
    symbolSize = 0,
    natLog,
    score;

    //password < 4
    if (password.length < 4) {
        return shortPass
    };

    //password == username
    if (password.toLowerCase() == username.toLowerCase()) return badPass;

    if (password.match(/[0-9]/)) symbolSize += 10;
    if (password.match(/[a-z]/)) symbolSize += 26;
    if (password.match(/[A-Z]/)) symbolSize += 26;
    if (password.match(/[^a-zA-Z0-9]/)) symbolSize += 31;

    natLog = Math.log(Math.pow(symbolSize, password.length));
    score = natLog / Math.LN2;
    if (score < 40) return badPass
    if (score < 56) return goodPass
    return strongPass;
}
function check_pass_strength() {
    var pass = $('#user_password').val(),
    user = $('#user_login').val(),
    strength;
    $('#pass-strength-result').removeClass('short bad good strong');
    if (!pass) {
        $('#pass-strength-result').html('');
        return;
    }
    strength = passwordStrength(pass, user);
    switch (strength) {
    case 2:
        $('#pass-strength-result').addClass('bad').html('Weak');
        break;
    case 3:
        $('#pass-strength-result').addClass('good').html('Medium');
        break;
    case 4:
        $('#pass-strength-result').addClass('strong').html('Strong');
        break;
    default:
        $('#pass-strength-result').addClass('short').html('Very weak');
    }
}
function check_pass_confirm() {
    var pass = $('#user_password').val();
    var pass_confirm = $('#user_password_confirmation').val();
    var html = '';
    if (pass_confirm != pass) {
        html = '<span class="match-bad">Passwords do not match.</span>';
    } else {
        html = '<span class="match-ok">OK. Passwords match!</span>';
    }
    $('#match').html(html);
}
function add_http_if_not_present() {
    if ($('#website_url').val().substr(0, 7) != 'http://') {
        $('#website_url').val('http://' + $('#website_url').val())
    }
}

function initValidate() {
    $('#website_url').keyup(add_http_if_not_present);

    $('#user_password').val('').keyup(check_pass_strength);
    $('#user_password_confirmation').val('').keyup(check_pass_confirm);
    $('#register').validate({
        submitHandler: function() {
            // alert("validating... ");
            var val_1 = $('#user_password').val();
            var val_2 = $('#user_password_confirmation').val();
            if (val_1 != val_2) return false;
            // alert("pass ok... ");

            //  /^[a-z0-9\_\-\.]{3,24}$/i
        },
        highlight: function(element, errorClass, validClass) {
            $(element).addClass(errorClass).removeClass(validClass);
            $(element).prev().addClass('error-mes').removeClass('valid-mes');
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).removeClass(errorClass).addClass(validClass);
            $(element).prev().removeClass('error-mes').addClass('valid-mes');
        },
        errorLabelContainer: $("#error"),
        //errorLabelContainer: $("#error"),
        rules: {
            user_first_name: "required",
            user_last_name: "required",
            user_email: {
                required: true,
                email: true
            },
            website_url: {
                required: true,
                url: true
            },
            user_password: "required",
            user_password_confirm: "required",
            user_login: {
                required: true,
                alphanumeric: true,
                minlength: 3,
                maxlength: 24
            }
        },
        messages: {
            user_first_name: {
                required: 'First Name'
            },
            user_last_name: {
                required: 'Last Name'
            },
            user_email: {
                required: 'Email Address'
            },
            website_url: {
                required: 'Web Site URL'
            },
            user_password: {
                required: 'Password'
            },
            user_password_confirm: {
                required: 'Password Confirmation'
            }
        }
    });
}

function hideNotices() {
    $("#notice").fadeOut(1000);
    $("#error").fadeOut(1000);
}

$(document).ready(function() {
    setTimeout(hideNotices, 4000);
});
