$(document).ready(function () {
	$( "#tietoa div:not(.kuva)" ).css( "display", "none" );
	$( "#tiesitko div" ).css( "display", "none" );
	$( "#kurssit div" ).css( "display", "none" );
	
	$( "#tietoa li > span" ).click( showHideSlide );
	$( "#tiesitko li > span" ).click( showHideSlide );
	$( "#kurssit li > span" ).click( showHide );
	
	$( "img.thumb" ).click( zoom );
	
	$( ".delete" ).click( deleteArticle );
    $( "#upload_form" ).submit( startUpload );

    if( document.getElementById( "login_form" ) )
        document.getElementById( "login_form" ).user.focus();
    else if( document.getElementById( "add_game_form" ) )
        document.getElementById( "add_game_form" ).name.focus();
    else if( document.getElementById( "feedback_form" ) )
        document.getElementById( "feedback_form" ).name.focus();
    else if( document.getElementById( "add_article_form" ) )
        document.getElementById( "add_article_form" ).header.focus();
});

function deleteArticle( event ) {
    $.post( "script_/delete_article.php", { index: $( this ).parent().parent().attr( "id" ), category: $( this ).parent().parent().attr( "class" ) }, function( response ) {    
        var id = rand( 1, 100000 );
        if( response == "fail" ){
            var msg = "<div id='" + id + "' class='error box popup' onclick='$( this ).fadeOut( 600, function() { $( this ).remove(); } );'>" +
                      "Jutun poistaminen epäonnistui. Yritä uudelleen.<br />\nJos ongelma jatkuu, ota yhteyttä ylläpitoon.</div>";
            $( document.body ).append( msg );
            $( "#" + id ).fixedBox();
            $( "#" + id ).fadeIn( 300 );
        }
        else {
            $( "#" + response ).fadeOut( 600, function() { $( "#" + response ).remove(); } );
        } 
    } );
    return false;
};

function displaySyntaxHelp() {
    $( "#syntax_help" ).fixedBox();
    $( "#syntax_help" ).ppdrag();
    $( "#syntax_help" ).fadeIn( 300 );
    $( "#syntax_help" ).click( function() { $( "#syntax_help" ).fadeOut( 500 ) } );
    return false;
};

function showHideSlide() {
    node = $(this).next( "div" );
    $( node ).slideToggle(500);
};

function showHide() {
    node = $(this).next( "div" );
    $( node ).toggle( "normal" );
};

function zoom( event ) {
    var id =            $( this ).attr( "id" );
    var bigId =         "big_" + id;
    var fullSizeName =  $( this ).attr( "src" ).substring( ( $( this ).attr( "src" ).lastIndexOf( "/" ) ), 
                        $( this ).attr( "src" ).length );
    var src =           "http://pv.onmky.fi/images_/full_size" + fullSizeName;
    var div =           "<div id='" + bigId + "' class='zoom loading'></div>";
    var caption =       "<p>" + $( this ).attr( "title" ) + "</p>";
    var header =        "<div style='margin:0;padding:0;text-align:right;'><img src='images_/close.gif' alt='X' /></div>";

    $( document.body ).append( div );
    $( "#" + bigId ).ppdrag().fadeIn( 300 ).fixedBox();

    var img = new Image();
    $( img ).load( function() {
        $( "#" + bigId  ).removeClass('loading').append( header ).find( "img" ).click( function() { 
            $( this ).parent().parent().fadeOut( 300, function() { 
                $( this ).remove();
                $( "#" + id ).css( "visibility", "visible" ); 
            } ); 
        } );
        $( "#" + bigId ).append( caption );
        $( "#" + bigId + " > p" ).before( this ).parent().fixedBox();
    }).error( function() {
        msg = '<div id="result" class="error box">Kuvan lataamisessa tapahtui virhe!</div>';
        $( document.body ).append( msg );
        $( "#result" ).fixedBox();
        t = setTimeout( '$( "#result" ).fadeOut( 600, function() { $( "#result" ).remove() } )', 3500 );     
    }).attr( 'src', src );
    $( this ).css( "visibility", "hidden" );
};

function getCurrentTime() {
        var timestamp = new Date();
        var day = timestamp.getDate();
        var month = ( timestamp.getMonth() + 1 );
        var year = timestamp.getFullYear();
        var hours = timestamp.getHours();
        var minutes = timestamp.getMinutes();
        
        if( day < 10 )
            day = "0" + day;
        if( month < 10 )
            month = "0" + month;
            
        var yearString = new String( year );
        year = yearString.slice( 2, yearString.length );
        
        if( hours < 10 )
            hours = "0" + hours;
        if( minutes < 10 )
            minutes = "0" + minutes;
            
        var time = day + "." + month + "." + year + " " + hours + ":" + minutes;
        
        return time;
};

function startUpload() {
    $( "#upload_process" ).slideDown( 500 );
    return true;
};

function stopUpload( result ) {

    var results = { "success" : 1, "wrongFileType" : 2, "fileSizeTooBig" : 3, "fileExists" : 4 };
    var msg = "";
    
    $( "#file" ).val( "" ); 
    
    if ( result == results.success ){
        msg = '<div id="result" class="notification box">Lähetys onnistui!</div>';
        $( document.body ).append( msg );
        $( "#result" ).fixedBox();
    }
    else if ( result == results.wrongFileType ) {
        msg = '<div id="result" class="error box">Tiedostotyyppi on väärä!</div>';
        $( document.body ).append( msg );
        $( "#result" ).fixedBox();
    }
    else if ( result == results.fileSizeTooBig ) {
        msg = '<div id="result" class="error box">Tiedosto on liian suuri!</div>';
        $( document.body ).append( msg );
        $( "#result" ).fixedBox();
    }
    else if ( result == results.fileExists ) {
        msg = '<div id="result" class="error box">Tiedosto on jo olemassa! Anna toinen nimi.</div>';
        $( document.body ).append( msg );
        $( "#result" ).fixedBox();
    }        

    $( "#upload_process" ).fadeOut( 300 );
    $( "#result" ).fadeIn( 1000 );
    t = setTimeout( '$( "#result" ).fadeOut( 600, function() { $( "#result" ).remove() } )', 3500 );
    
    return true;
};

function rand( min, max ) {
    return max ? min + rand( max - min ) : Math.random() * ++min << .5;
};

function pulsate( element ) {
    var pulsateTime = 200
    $( element ).animate({opacity:"0"}, pulsateTime).animate({opacity:"100"}, pulsateTime).animate({opacity:"0"}, pulsateTime).animate({opacity:"100"}, pulsateTime);
};

