$( 'document' ).ready( function() {    
    $( ".datepicker" ).datepicker( {
        dateFormat: 'dd/mm/yy'
    });

    $( ".btn, \n\
        .zend_form input[type=submit], \n\
        .zend_form input[type=reset], \n\
        div.form input[type=submit], \n\
        div.form input[type=reset], \n\
        #admin-panel input[type=submit],\n\
        #login input[type=submit]" ).button();

    $( ".plain .grid > li > div > a > img" ).hover( function(e) {
        $('.imgInfo').hide();
        $(this).parent().next( '.imgInfo' ).show();
    }, function() {
        $('.imgInfo').hide();
    });

    $( ".plain .grid > li > div > a > img" ).mousemove( function(e) {
        var x = e.pageX;
        var y = e.pageY - 200;
        $(this).parent().next( '.imgInfo' ).css({left:x,top:y});
    });

    $('.imgInfo').hover( function(){$(this).show();}, function(){$(this).hide()} );
    $('.plain .imgInfo > p > b').click( function(){$(this).parents('.imgInfo').hide()} );
});

// checkbox i div(show/hide)
function checkbox_show_div( id_checkbox, id_div )
{  
    element = document.getElementById( id_checkbox );
    if( element.checked )
    {
        $( 'div#'+id_div ).show();
    }
    else
    {
        $( 'div#'+id_div ).hide();
    }
}

// radio i div(show/hide)
function radio_show_div( id_div, all_div )
{
    for( i=0; i<all_div.length; i++ )
        $( 'div#'+all_div[i] ).hide();
    
    $( 'div#'+id_div ).show();
}

// button i div(show/hide)
function button_show_div( id_div, id_button )
{
    $( 'div#'+id_div ).show();
    document.getElementById(id_button).style.visibility = 'hidden';
}

// pokaz diva
function show_div( id_div ) {
    var element = document.getElementById( id_div );
    if( element.style.display == 'none' || element.style.display == '' ) {
        $( 'div#'+id_div ).show( 'slow' );
    }
    else {
        $( 'div#'+id_div ).hide( 'slow' );
    }
}

// pokaz diva
function showifnot_div( id_div, id_element, condition ) {
    element = document.getElementById( id_element );
  
    if( element.options[element.selectedIndex].value == condition )
        $( 'div#'+id_div ).hide( 'slow' );
    else
        $( 'div#'+id_div ).show( 'slow' );
}

// tworzenie cookie
function create_cookie( name, value, expire ) {
    document.cookie = name + "=" + escape(value) + ((expire==null)?"" : ("; expires=" + expire.toGMTString()));
}

// zmiana obrazka
function change_img( img_id, img1, img2 )
{
    image = document.getElementById( img_id );
  
    index = image.src.lastIndexOf( '/' );
    href  = image.src.substring( 0,index );
    img   = image.src.substring( index+1 );
  
    if ( img == img1 )
        image.src = href + '/' + img2;
    else if ( img == img2 )
        image.src = href + '/' + img1;
}

//
function show_ifnot( id_show, id_element, condition )
{
    element = document.getElementById( id_element );
  
    if( element.options[element.selectedIndex].value == condition )
        document.getElementById( id_show ).style.visibility = 'hidden';
    else
        document.getElementById( id_show ).style.visibility = 'visible';
}

//
function pxToInt( size ) {
    index = size.indexOf( 'p' );
    return size.substr(0,index);
}

function setWndMsg( id, msg, width, height ) {
    YOFFSET = pxToInt( height ) + pxToInt( document.getElementById(id).style.padding );
  
    document.getElementById(id).style.width  = width;
    document.getElementById(id).style.height = height;
    document.getElementById(id).innerHTML = '<table style="width: 100%; height: 100%;" ><tr><td style="vertical-align: middle; text-align: center;"><p>' + msg.replace('&quot','"') + '</p></td></tr></table>';
}

function submit_if_notempty( id_submit, id, msg, width, height ) {
    if( $( '#'+id ).val().length==0 ) {
        setWndMsg( id_submit, msg, width, height );
        $( 'div#popupMessage' ).show();
        return false;
    }
    return true;
}

/*********************** Efekty *************************/

function rho_scroll_images( _id_div, _step, _interval, _temp ) {
    $( '#'+_id_div ).animate(
    {
        scrollLeft : $( '#'+_id_div ).attr( 'scrollLeft' )+_step
    }, _interval );
}

function rho_close_window( _id_div ) {
    $( '#'+_id_div ).animate( {
        opacity:0
    }, 340 );
    $( '#'+_id_div ).hide( 340 );
}

/*********************** AJAX ***************************/

function rho_change_status( _path, _id, _img ) {
    $( 'img#'+_img ).css( 'cursor', 'progress' );
  
    $.post( _path, {
        id : _id
    },
    function( data ) {
        var json = eval( "("+data+")" );
    
        var old_src = $( 'img#'+_img ).attr( "src" );
        var index   = old_src.lastIndexOf( '/' );
        var new_src = old_src.substring( 0,index )+"/"+json.img;
      
        $( "img#"+_img ).attr( "src", new_src );
        $( 'img#'+_img ).css( 'cursor', 'pointer' );
    });
}

function rho_show_artwork_image( _path, _id_div, _id ) {
    var display = $( 'div#'+_id_div ).css( 'display' );

    if( display == 'none' ) {
      
        $( 'div#'+_id_div ).show();
        $.post( _path, {
            id : _id
        },
        function( data ) {
            var json = eval( "("+data+")" );
            var img  = "<img src=\""+json.src+"\" alt=\"brak obrazka\" />"
        
            $( 'div#'+_id_div ).html( img );
        });
    }
}

function rho_show_window( _path, _link ) {
    $( '#'+_link ).css( 'cursor', 'progress' );

    $.get( _path, {
        ajax : 1
    },
    function( data ) {
        var json = eval( "("+data+")" );
        $( '#'+_link ).css( 'cursor', 'pointer' );
        $( 'div#wndContent' ).html( json.html );
        $( 'div#wnd' ).css( 'opacity', '0' );
        $( 'div#wnd' ).show();
        $( 'div#wnd' ).animate( {
            opacity: 1
        }, 340 );
    });
}

function rho_change_view( _path, _id_div, _link ) {
    $( '#'+_link ).css( 'cursor', 'progress' );

    $.get( _path, {
        ajax : 1
    },
    function( data ) {
        var json = eval( "("+data+")" );
        $( 'div#'+_id_div ).html( json.html );
        $( '#'+_link ).css( 'cursor', 'pointer' );
    });
}

function rho_add_comment( _path, _id_artwork, _id_content, _id_div, _link ) {
    $( '#'+_link ).css( 'cursor', 'progress' );
    $.post( _path, {
        id_artwork : _id_artwork,
        content : $( '#'+_id_content ).val()
    },
    function( data ) {
        var json = eval( "("+data+")" );
        $( 'div#'+_id_div ).html( json.html );
        $( '#'+_link ).css( 'cursor', 'pointer' );
    });
}

function confirm(title,content,ok_func) {
    $('<div id="dialog" title="'+title+'">'+content+'</div>').dialog({
        modal: true,
        buttons: {
            'Ok' : function() {
                ok_func();

                $(this).dialog('close');
            },
            'Cancel' : function() {
                $(this).dialog('close');
            }
        }
    });
}
