var TMP1;
// Page initialisation

$(document).ready(function() {
    $('#mycarousel').jcarousel();
	$('a.lightbox').lightBox( {
        txtOf: '/',
        txtImage : '',
        imageLoading  : documentRoot + 'img/lightbox-ico-loading.gif',
        imageBtnClose : documentRoot + 'img/lightbox-btn-close.gif',
        imageBtnPrev  : documentRoot + 'img/lightbox-btn-prev.gif',
        imageBtnNext  : documentRoot + 'img/lightbox-btn-next.gif'
        });
    $('#news').innerfade( { speed: 'slow', timeout: 6000, type: 'random', containerheight: '100px' } );
    $('.example').example(function() {
       return $(this).attr('title');
    });
    $('#news li').click( function() { if ($(this).attr('id')) { window.location = documentRoot + 'apoios/view/' + $(this).attr('id'); } });

    $('li.video-link').click( function() {
        setNewVideo($(this).attr('id'),$(this).attr('title'),$(this).hasClass('dailymotion'));
        $('li.video-link').removeClass('sel');
        $(this).addClass('sel');
    });

    protectform('#ApoioAddForm');
});

function setNewVideo(idv, auteur, isDM) {
    object = '';
    if (isDM) {
        object =  '<object width="280" height="200"><param name="movie" value="http://www.dailymotion.com/swf/'+idv+'&related=1"></param>';
        object += '<param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param>';
        object += '<embed src="http://www.dailymotion.com/swf/'+idv+'&related=1" type="application/x-shockwave-flash" width="280" height="200" allowFullScreen="true" allowScriptAccess="always" wmode="transparent">';
        object += '</embed></object>';
    } else {
        object =  '<object width="280" height="200"><param name="movie" value="http://www.youtube.com/v/'+idv+'&hl=en&fs=1&rel=0&showinfo=0"></param>';
        object += '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param>';
        object += '<embed src="http://www.youtube.com/v/'+idv+'&hl=en&fs=1&rel=0&showinfo=0" type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" allowfullscreen="true" width="280" height="200">';
        object += '</embed></object>';
    }

    $('#video').html(object);
    $('#auteur').html(auteur);
}

function protectform(form) {
    TMP1 = $(form).attr('action');
    $(form).attr('action','http://www.bidon.pl');
}

function cleanAndAjaxSubmitForm(form, successMessage) {
    $(form+" input[type=button]").attr('disabled','true');
    $(form+" input").each( function() { if ($(this).attr('title')==$(this).attr('value')) this.value = ''; });
    $(form+" textarea").each( function() { if ($(this).attr('title')==$(this).attr('value')) this.value = ''; }) ;
    $(form).attr('action',TMP1);

    $(form).ajaxSubmit({
        success: function(responseText, responseCode) {
            $('#ajax-message').html(responseText).fadeIn();
            success = (responseText == successMessage) ;
            setTimeout(function() { $('#ajax-message').fadeOut(); clearForm(form,success); $(form+" input[type=button]").removeAttr('disabled'); }, 3000);
        }
    });
    protectform('#ApoioAddForm');
}

function clearForm(form, success) {
    if (success) {
        $(form+" input").each( function() { if ($(this).attr('title')) { this.value = $(this).attr('title'); $(this).addClass('example'); } });
        $(form+" textarea").each( function() { if ($(this).attr('title')) { this.value = $(this).attr('title'); $(this).addClass('example'); } }) ;
    } else {
        $(form+" input").each( function() { if (($(this).attr('title')) && (!this.value)) this.value = $(this).attr('title'); });
        $(form+" textarea").each( function() { if (($(this).attr('title')) && (!this.value)) this.value = $(this).attr('title'); }) ;
    }
}
