function ImageX() {
    var angle = 0;
}

ImageX.prototype.publish = function( imageId ) 
{
    this.request( '/images/publish/', imageId );
}

ImageX.prototype.unpublish = function( imageId ) 
{
    this.request( '/images/unpublish/', imageId );
}

ImageX.prototype.showLoading = function(imageid)
{
    $( '#image_actions_'+ imageid +' a' ).hide();
    $( '#image_actions_'+ imageid ).append( '<img id="image_action_loading_'+ imageid +'" src="/media/images/cms/loading_spinner.gif" alt="loading" title="" />' );
}

ImageX.prototype.hideLoading = function(imageid)
{
    $( '#image_action_loading_'+ imageid ).remove();
    $( '#image_actions_'+ imageid +' a' ).show();
}

ImageX.prototype.addToAlbum = function( albumId, imageId )
{
    var url = '/images/addimagetoalbum/';

    $.fn.colorbox.close();

    $.ajax( {
        type: 'POST',
        data: { album_id:albumId,
                image_id:imageId },
        url: url,
        success: function ( msg ) {
           tb_remove(); 
        }
    } );
}

ImageX.prototype.addToSet = function( setId, imageId )
{
//	$.fn.colorbox.close();

    var url = '/imagesets/addimage/';
    $.ajax( {
        type: 'POST',
        data: { action:'save',
                set_id:setId,
                image_id:imageId },
        dataType:'json',
        url: url,
        success: function(msg) {
            if ( msg.message == 'success' ) {
                $( '#actions_' + setId ).html( 'add <a href="#" onclick="Img.removeFromSet('+ setId +', '+ imageId +'); return false">remove</a>' );
            }
        }
    } );
}

ImageX.prototype.removeFromSet = function( setId, imageId )
{
    var url = '/imagesets/removeimage/';
    $.ajax( {
        type: 'POST',
        data: { set_id:setId,
                image_id:imageId },
        dataType:'json',
        url: url,
        success: function( msg ) {
            if ( msg.message == 'success' ) {
                $( '#actions_' + setId ).html( '<a href="#" onclick="Img.addToSet('+ setId +', '+ imageId +'); return false">add</a> remove' );
            }
        }
    } );
}

ImageX.prototype.deleteSet = function( setId )
{
    this.showLoading( setId );
    var obj = this;
    $.ajax( {
        type: 'POST',
        data: { set_id:setId },
        dataType:'json',
        url: '/imagesets/delete/',
        success: function( msg ) {
            if ( msg == null || msg.message != 'success' ) {
                obj.hideLoading( setId );
            }
            else {
                $( '#set_row_' + setId ).fadeOut();   
            }
        }
    } );
}

ImageX.prototype.request = function( url, imageId ) 
{   
    this.showLoading(imageId);

    url += imageId;

    $.ajax( {
        type: 'POST',
        url: url,
        success: function( msg ) {
            if ( msg == 'success' ) {
                $( '#img' + imageId ).fadeOut();
            }
        }
    } );
}

ImageX.prototype.success = function( data ) 
{
    alert( data );
}

ImageX.prototype.update = function( imageId ) 
{
    var title = $('#title').val();
    var description = $('#description').val();
    var category = $('#category').val();
    var tags = $('#tags').val();
    
    var extraParams = '&title=' + title + 
                      '&description=' + description + 
                      '&category=' + category + 
                      '&tags=' + tags;
    
    this.request( 'models/images/images.update.php', imageId, 'update', extraParams );
    tb_remove();
    
    // TODO: update edit image info
}

ImageX.prototype.rotateLeft = function( imageId, container, fullId )
{
    if ( fullId != null ) {
        container = fullId;
    }
    else if ( container != null ) {
        container = container + imageId;
    }
    else {
        container = 'image' + imageId;
    }

    this.angle = $('#' + container ).rotateLeft();

    if ( imageId != null ) {
        this.editSave( imageId );
    }
}

ImageX.prototype.rotateRight = function( imageId, container, fullId )
{
    if ( fullId != null ) {
        container = fullId;
    }
    else if ( container != null ) {
        container = container + imageId;
    }
    else {
        container = 'image' + imageId;
    }
    
    this.angle = $('#' + container ).rotateRight();
    
    if ( imageId != null ) {
        this.editSave( imageId );
    }
}

ImageX.prototype.rotateCustom = function( imageId, container, angle )
{
    $( '#' + container ).rotate( angle );
}

ImageX.prototype.editSave = function( imageId )
{
    if ( this.angle != undefined ) {
        var editLinkId = '#editLink' + imageId;
        var savingMsg  = '#savingMsg' + imageId
        $( editLinkId ).hide();
        $( savingMsg ).show();
        
        var url = '/ajax/imageRotate';
        var params = 'image_id=' + imageId + '&angle=' + this.angle;
        var obj = this;

        $.ajax( {
            type: 'POST',
            url: url,
            data: params,
            dataType: 'json',
            success: function( msg ) {
                $( editLinkId ).show();
                $( savingMsg ).hide();

                if ( msg.message == 'success' ) {
                    $('#image' + imageId).rotateRight( obj.angle );
                }
            }
        } );
    }
}

ImageX.prototype.updatecat = function( imageId, categoryId )
{
    var url = '/images/updatecat';
    var params = 'itemid=' + imageId + '&categoryid=' + categoryId;
    $.ajax( {
        type: 'POST',
        url: url,
        data: params,
        success: function( msg ) {
        }
    } );
}

ImageX.prototype.rotateAll = function()
{
    var rotateArray = $('.rotate');
    var container, imageId, angle;
    for( var i = 0; i < rotateArray.length; i ++ ) {
        container = rotateArray[ i ].id;
        objId = container.split( '_' );
        imageId = objId[1];
        angle = $('#' + container ).attr('rel');

        this.rotateCustom( imageId, container, angle );
    }
}

ImageX.prototype.setCreateToggle = function()
{
    $('#createset').toggle();
    
    if ( $( '#createsetbutton_create' ).text() == 'done' ) {
        $( '#setError' ).text('');
        $( '#createsetbutton_create' ).text( 'create set' );
    } else {
        $('#setName').focus();
        $( '#createsetbutton_create' ).text( 'done' );
    }
}

ImageX.prototype.saveSet = function()
{
    var setname = $('#setName').attr( 'value' );
    $( '#setName' ).attr( 'value', '' );
    $('#imagesetloading').toggle();
    $( '#seterror').hide();

    $.ajax( {
        type: 'POST',
        url: '/imagesets/create',
        data: { 'setname':setname },
        dataType: 'json',
        success: function( results ) {
            $('#setName').focus();
            $('#imagesetloading').toggle();
            if ( results.message == 'success' ) {
                Img.displaySet( results.data );
            }
            else {
                $( '#seterror').show();
                $( '#seterror' ).text( results.message );
                $('#setName').attr( 'value', setname );
            }
        }
    } );
}

ImageX.prototype.displaySet = function( data )
{
    $( '#nosets' ).hide();
    $( '#setscontainer tbody' ).prepend( data );
    this.setUpEditable();
}

ImageX.prototype.initColorbox = function()
{
	$( '.colorbox' ).colorbox( { href: this.href,
	 							 initialWidth: '400px',
  								 initialHeight: '400px',
								 width: '400px',
								 height: '400px',
								} );
}

ImageX.prototype.setUpEditable = function()
{
    $('.image_name .edit').editable('/images/save/');
    $('#setscontainer .edit').editable('/imagesets/edit/', {
        cancel    : 'Cancel',
        submit    : 'Save',
        indicator : '<img src="/media/images/cms/loading_spinner.gif" alt="loading..." />',
        tooltip   : 'Click to edit...'
    } );
}

var Img = new ImageX();

$(document).ready(function() {
    Img.initColorbox();
    Img.setUpEditable();
} );

// After window loads, do rotate on images that are rotated in queue
$(window).load( function() {
    Img.rotateAll(); 
} );