﻿$(document).ready( function() {
    var maxchars = 1000;

    $('textarea').focus( function() {
        var name = $(this).attr('name');
        if (! $('#'+ name +'-charleft').length > 0 ) {
            $(this).after(
                '<br \/><span id="'+ name +'-charleft" style="float: right; margin: 5px 35px 5px 0">'+ maxchars +' merkkiä jäljellä<\/span>'
            );
        }
    });

    $('textarea').keyup( function() {
        var name  = $(this).attr('name');
        var chars = maxchars - $(this).val().length;

        if($(this).val().length > maxchars) {
            $(this).val($(this).val().substr(0, maxchars));
        }
        if ( $('#'+ name +'-charleft').length > 0 ) {
            $('#'+ name +'-charleft').text(chars +' merkkiä jäljellä');
        }
    });

    var l = $('#yhteydenotto-form-left').height();
    var r = $('#yhteydenotto-form-right').height();

    if (l < 200) {
        $('#yhteydenotto-form-left').css('height', '200px');
        l = $('#yhteydenotto-form-left').height();
    }

    if (r < l) {
        $('#yhteydenotto-form-right').css('height', l +'px');
    }

    var showModal = function() {
        $('#ta').hide();
        $('.formids').hide();
        $('#yhteydenotto-modal').show();
    }
    var hideModal = function() {
        $('#ta').show();
        $('.formids').show();
        $('.jqmWindowOverlay').hide();
        $('#yhteydenotto-modal').hide();
    }

    

	/*
		get from prod!!!
	*/

    /*var query  = location.search;
    var id     = /id\=([0-9]+)/i.exec(query);
    var dialog = query.search(/skipdialog/);

    if (id && id[1]) {
        $('#yhteydenotto-form-left').css('height', 'auto');
        l = $('#yhteydenotto-form-left').height();
        if (r < l) {
            $('#yhteydenotto-form-right').css('height', l +'px');
        }

        $('#formid').change( function() {
            if ($('#formid option:selected').val() != '') {
                $('#yhteydenotto-form-elems-left').html('');
                $('#yhteydenotto-form-elems-right').html('');

                if ($('#formid option:selected').val().match(/http:/)) {
                    window.location = $('#formid option:selected').val();
                } else {
                    $('#yhteydenotto-modal').jqm({
                        ajax: 'dialog.inc.php?version=live&form='+ $('#formid option:selected').val(),
                        overlay: 50,
                        overlayClass: 'jqmWindowOverlay',
                        onShow: showModal,
                        onHide: hideModal
                    });
                    $('#yhteydenotto-modal').jqmShow();
                }
            }
        });
    }

    if (id && id[1] && dialog == -1) {
        $('#yhteydenotto-modal').jqm({
            ajax: '/dialog/dialog.inc.php?version=live&form='+ id[1],
            overlay: 50,
            overlayClass: 'jqmWindowOverlay',
            onShow: showModal,
            onHide: hideModal
        });
        $('#yhteydenotto-modal').jqmShow();
    }*/
});
