// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function open_dialog_window(param,action){
    Dialog.confirm("Are you sure?", {
        className:"alphacube",
        okLabel: "Yes",
        width:200,
        cancelLabel:"No",
        cancel:function(){
            return false;
        },
        ok:function() {
            new Ajax.Request(action, {
                method:'get',
                parameters:{
                    id:param
                }
            });
            return true;
        }
    });
}

var notif_fade_over = true;

function display_notification(msg){
    if(notif_fade_over == true){
        notif_fade_over = false;
        insert_notification(msg);
        timeout_fade_notification();
    }
}
function insert_notification(msg){
    if($('notification') != null){
        return;
    }
    var notifDiv = new Element('div',{
        id:'notification'
    });

    notifDiv.update(msg);
    $('menu_bar').insert ({
        'after'  : notifDiv
    } );
}



function fade_notification(){
    try {
        Effect.Fade($('notification'),{
            afterFinish:function(){
                $('notification').remove();
                notif_fade_over = true;
            }
        });
    }catch(e){}
}

function timeout_fade_notification(){
    setTimeout('fade_notification()', 1500);
}




/*This function inserts the animated gif and a custom message
 **/
function display_loader(formName,msg,elt,pos_code){
    var d = new Element('div', {
        id: 'loader',
        style:'text-align:center;margin-top:30px;'
    });
    var a = new Element('img', {
        alt: 'loader',
        src: "/images/style/ajax-loader.gif"
    });
    var s = new Element('p', {});
    s.update(msg);
    d.update(a);
    d.insert({
        'top':s
    });
    
    if(pos_code == 1){
        $(''+ elt.toString()).insert({
            'before':d
        } );
    }else if(pos_code == 2){
        $(''+ elt.toString()).insert({
            'after':d
        } );
    }
    setTimeout('document.'+formName+'.submit()', 500);
}

    
function validateForm(formName, fieldName){
    var d = $(fieldName);
    var f =$(formName);
    if(d.value.length == 0){
        if(!$('errmsg').visible()) {
            Modalbox.resizeToInclude('errmsg', {
                afterResize: function(){
                    new Effect.Appear('errmsg');
                }
            })
        }
    }else{
        Modalbox.hide()
        f.submit();
    }
}

function hideErrorMsg(){
    
    if($('errmsg').visible()) {
        $('errmsg').hide();
        Modalbox.resizeToContent();
    }
}

Event.observe(window, 'load', init);

function init(){
    makeItCount('user_profile_description', 350, true);
    makeItCount('user_profile_fav_movies', 145, true);
    makeItCount('user_profile_fav_artists', 145, true);
    makeItCount('user_profile_fav_books', 145, true);
    makeItCount('user_profile_fav_songs', 145, true);
    makeItCount('user_profile_fav_dishes', 145, true);
    makeItCount('reply_message', 800, true);
    makeItCount('message_message_text', 800, true);
    makeItCount('preference_description', 350, true);
    
}
	
function charCounter(id, maxlimit, limited){
    if (!$('counter-'+id)){
        $(id).insert({
            after: '<div id="counter-'+id+'"></div>'
        });
    }
    if($F(id).length >= maxlimit){
        if(limited){	
            $(id).value = $F(id).substring(0, maxlimit);
        }
        $('counter-'+id).addClassName('charcount-limit');
        $('counter-'+id).removeClassName('charcount-safe');
    } else {	
        $('counter-'+id).removeClassName('charcount-limit');
        $('counter-'+id).addClassName('charcount-safe');
    }
    $('counter-'+id).update( $F(id).length + '/' + maxlimit );	
			
}
	
function makeItCount(id, maxsize, limited){
    if(limited == null) limited = true;
    if ($(id)){
        Event.observe($(id), 'keyup', function(){
            charCounter(id, maxsize, limited);
        }, false);
        Event.observe($(id), 'keydown', function(){
            charCounter(id, maxsize, limited);
        }, false);
        charCounter(id,maxsize,limited);
    }
}
