﻿debug = {}
debug.active = true;
debug.log = function( string )
{
    if ( debug.active ){
        $.writeln( string );
        writeLn( string );
    }
    return;
}
function getItem( item_name, item_type ){
    var result = null;
    var items = app.project.items;
    var names = [];
    
    for ( i = 1 ; i <= items.length ; i ++ )
    {
        var item = items[i];
        var type = item.typeName;
        if ( ( type == item_type ) && ( item.name == item_name ) )
        {
            result = item;
            //$.writeln( item.name );
        }
    }
    return result;
}
function getFolder( given_name ){
    
    result = getItem( given_name, "Folder" );
    
    return result;
}
function getComp( given_name ){
    
    result = getItem( given_name, "Composition" );
    
    return result;
}
function getSelectedProjectItems (){
    var items = [];
    var p = app.project;
    for ( var i = 1 ; i <= p.numItems ; i ++ ){
        var item = p.item(i);
        if ( item.selected ){
            items.push(item);
        }
    }
    return items;
}
function duplicateSuffix( items , suffix ){
    for ( i = 0 ; i < items.length ; i ++ )
    {
        var item = items[i];
        var new_item = item.duplicate();
        new_item.name = item.name+"_"+suffix
    }
    return true;
}
function duplicateReplace( items , string , newString ){
    for ( i = 0 ; i < items.length ; i ++ )
    {
        var item = items[i];
        var new_item = item.duplicate();
        new_item.name = item.name.replace( string , newString );
    }
    return true;
}
function suffix( items , suffix ){
    for ( i = 0 ; i < items.length ; i ++ )
    {
        var item = items[i];
        item.name+= "_"+suffix
    }
    return true;
}
function replace( items , string , newString ){
    for ( i = 0 ; i < items.length ; i ++ )
    {
        var item = items[i];
        item.name = item.name.replace( string , newString );
    }
    return true;
}
function checkDuration( items , duration ){
    for ( i = 0 ; i < items.length ; i ++ )
    {
        var item = items[i];
        debug.log(item.duration);
    }
}
function enforceDuration( items , duration ){
    for ( i = 0 ; i < items.length ; i ++ )
    {
        var item = items[i];
        item.duration = 5;
    }
}
/*
var items =  getSelectedProjectItems ();
var comp = getComp ( "720@60_10s" );
var targetFolder = getFolder( "Render" );

for ( i = 0 ; i < items.length ; i ++ ){
    var item = items[i];
    var newComp =comp.duplicate();
    newComp.parentFolder = targetFolder;
    newComp.name = item.name;
    newComp.layer(1).replaceSource( item , true );
}
*/

//duplicateSuffix(getSelectedProjectItems (),"txtlss");
suffix(getSelectedProjectItems (),"5s");
//replace(getSelectedProjectItems (),"overFootage","KEY");
//duplicateReplace(getSelectedProjectItems (),"next\_", "");
//enforceDuration(getSelectedProjectItems (),5);
//checkDuration(getSelectedProjectItems (),5)
