$(document).ready(function(){
    $('input[type="text"],input[type="password"],select,textarea').focus(function() {$(this).addClass('active');});
    $('input[type="text"],input[type="password"],select,textarea').blur(function() {$(this).removeClass('active');});
    $('input[type="button"],input[type="submit"]').mouseenter(function() {$(this).addClass('active')});
    $('input[type="button"],input[type="submit"]').mouseleave(function() {$(this).removeClass('active')});
    $('input[type="text"],textarea').bind('init',function() {addInputTitleHelp($(this));});
    $('input[type="text"],textarea').trigger('init');
    $('input[type="text"],textarea').blur(function(){addInputTitleHelp($(this));});
    $('input[type="text"],textarea').focus(function(){removeInputTitleHelp($(this));removeErrorMessage($(this));});
    $('input[type="password"]').focus(function(){removeErrorClass($(this));});
    $('form').submit(function(){
        var myfields = $("input,textarea", $(this));
        myfields.each(function(i){
            var myfield = myfields.eq(i);
            removeInputTitleHelp(myfield);
            removeErrorMessage(myfield);
        });
    });
    setTimeout('$("#authDetails").fadeOut();',5000);
    $("#authDetails").mouseleave(function() {
        setTimeout('$("#authDetails").fadeOut();',5000);
    });
    $('#authCurrentUser').click(ShowLastLoginDetails);
    $('.upload input[type="file"]').change(function() {
        $('.upload input[type="text"]').val($(this).val().replace(/.*\\/g, ""));
    });
});

function addInputTitleHelp(element){
    if(element.attr('title')!='' && (element.attr('value')=='' || element.attr('value')==element.attr('title') )){
        element.attr('value',element.attr('title'));
        element.addClass('inputShadow');
    }
}

function removeInputTitleHelp(element){
    if(element.attr('title')!='' && element.attr('value')==element.attr('title')){
        element.attr('value','');
        element.removeClass('inputShadow');
    }
}

function removeErrorMessage(element){
    if(element.hasClass('error')){
        element.val('');
        removeErrorClass(element);
    }
}

function removeErrorClass(element){
    if(element.hasClass('error')){
        element.removeClass('error');
    }
}

function parseJson(data) {
    data = jQuery.parseJSON(data);
    var parsed = [];
    $.each(data, function(key, value){
        parsed.push({
            data:[value, key],
            value:value,
            result:value
        });
    });
    return parsed;
}

function ShowLastLoginDetails(){
    $("#authDetails").show();
}

function SendFeedBack(pData){
    feedBackSec = $.data(document.body, 'feedBackSec');
    for(attrname in feedBackSec){pData[attrname] = feedBackSec[attrname];}
    $.post(feedBackUrl,pData,function(rData){
        rData = jQuery.parseJSON(rData);
        if(rData.html!=''){
            $('#reviewFeedback_'+pData.idComment).replaceWith(rData.html);
            $.data(document.body, 'feedBackSec', rData.newFeedBackSec);
        }
    });
}

function initFB(){
    FB.init("9498b44a4d7e7d61e9105709400ee1a4", "/fb/xd_reciever.htm");
}

function SelectCommentTab(){
    $('#plDetailPanel').tabs('select', '#pan-add');
    $.scrollTo($('#plDetailPanel'), 300);
}
